public void GetPackagesLoadsPlainPackagesConfigIfProjectPackagesConfigDoesNotExist()
        {
            // Arrange
            var repository = new Mock <MockPackageRepository>()
            {
                CallBase = true
            }.As <ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");

            repository.Object.AddPackage(packageA);
            var fileSystem = new MockFileSystem();

            fileSystem.AddFile("packages.cool.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" />
</packages>");

            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""B"" version=""1.0"" />
  <package id=""C"" version=""1.0"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: "cool", sourceRepository: repository.Object);


            // Act
            var packages = referenceRepository.GetPackages().ToList();

            // Assert
            Assert.Equal(1, packages.Count);
            Assert.Same(packageA, packages[0]);

            Assert.True(fileSystem.FileExists("packages.cool.config"));
        }
        public void GetPackagesReturnsPackagesFromSourceRepositoryListedInPackagesConfig()
        {
            // Arrange
            var repository = new Mock <MockPackageRepository>()
            {
                CallBase = true
            }.As <ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");

            repository.Object.AddPackage(packageA);
            var fileSystem = new MockFileSystem();

            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: repository.Object);


            // Act
            var packages = referenceRepository.GetPackages().ToList();

            // Assert
            Assert.Equal(1, packages.Count);
            Assert.Same(packageA, packages[0]);
        }
        public void GetPackagesNoPackagesConfigReturnsEmptyList()
        {
            // Arrange
            var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");
            repository.Object.AddPackage(packageA);
            var fileSystem = new MockFileSystem();
            var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object);

            // Act
            var packages = referenceRepository.GetPackages().ToList();

            // Assert
            Assert.AreEqual(0, packages.Count);
        }
        public void GetPackagesReturnsOnlyValidPackagesFromSourceRepositoryListedInPackagesConfig()
        {
            // Arrange
            var repository = new Mock <MockPackageRepository>()
            {
                CallBase = true
            }.As <ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");

            repository.Object.AddPackage(packageA);
            var packageC = PackageUtility.CreatePackage("C", "2.0");

            repository.Object.AddPackage(packageC);
            var fileSystem = new MockFileSystem();

            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""C"" version=""2.0"" />
  <package id=""B"" version=""1.0"" />
  <package id=""A"" version=""1.0"" />
  <package id="""" version=""1.0"" />
  <package id=""G"" version="""" />
  <package />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object);


            // Act
            var packages = referenceRepository.GetPackages().ToList();

            // Assert
            Assert.Equal(2, packages.Count);
            Assert.Same(packageC, packages[0]);
            Assert.Same(packageA, packages[1]);
            Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""C"" version=""2.0"" />
  <package id=""B"" version=""1.0"" />
  <package id=""A"" version=""1.0"" />
  <package id="""" version=""1.0"" />
  <package id=""G"" version="""" />
  <package />
</packages>", fileSystem.ReadAllText("packages.config"));
        }
        public void GetPackagesNoPackagesConfigReturnsEmptyList()
        {
            // Arrange
            var repository = new Mock <MockPackageRepository>()
            {
                CallBase = true
            }.As <ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");

            repository.Object.AddPackage(packageA);
            var fileSystem          = new MockFileSystem();
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: repository.Object);

            // Act
            var packages = referenceRepository.GetPackages().ToList();

            // Assert
            Assert.Equal(0, packages.Count);
        }
        public void GetPackagesWithMalformedPackagesConfigThrows()
        {
            // Arrange
            var repository = new Mock <MockPackageRepository>()
            {
                CallBase = true
            }.As <ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");

            repository.Object.AddPackage(packageA);
            var fileSystem = new MockFileSystem();

            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package ");
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: repository.Object);


            // Act
            ExceptionAssert.Throws <InvalidOperationException>(() => referenceRepository.GetPackages().ToList(), @"Error reading 'C:\MockFileSystem\packages.config'.");
        }
        public void GetPackagesWithMalformedPackagesConfigThrows()
        {
            // Arrange
            var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");
            repository.Object.AddPackage(packageA);
            var fileSystem = new MockFileSystem();
            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package ");
            var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object);


            // Act
            ExceptionAssert.Throws<InvalidOperationException>(() => referenceRepository.GetPackages().ToList(), @"Error reading 'C:\MockFileSystem\packages.config'.");
        }
        public void GetPackagesReturnsOnlyValidPackagesFromSourceRepositoryListedInPackagesConfig()
        {
            // Arrange
            var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");
            repository.Object.AddPackage(packageA);
            var packageC = PackageUtility.CreatePackage("C", "2.0");
            repository.Object.AddPackage(packageC);
            var fileSystem = new MockFileSystem();
            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""C"" version=""2.0"" />
  <package id=""B"" version=""1.0"" />
  <package id=""A"" version=""1.0"" />
  <package id="""" version=""1.0"" />
  <package id=""G"" version="""" />
  <package />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object);


            // Act
            var packages = referenceRepository.GetPackages().ToList();

            // Assert
            Assert.Equal(2, packages.Count);
            Assert.Same(packageC, packages[0]);
            Assert.Same(packageA, packages[1]);
            Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""C"" version=""2.0"" />
  <package id=""B"" version=""1.0"" />
  <package id=""A"" version=""1.0"" />
  <package id="""" version=""1.0"" />
  <package id=""G"" version="""" />
  <package />
</packages>", fileSystem.ReadAllText("packages.config"));
        }
        public void GetPackagesLoadsPlainPackagesConfigIfProjectPackagesConfigDoesNotExist()
        {
            // Arrange
            var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");
            repository.Object.AddPackage(packageA);
            var fileSystem = new MockFileSystem();
            fileSystem.AddFile("packages.cool.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" />
</packages>");

            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""B"" version=""1.0"" />
  <package id=""C"" version=""1.0"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: "cool", sourceRepository: repository.Object);


            // Act
            var packages = referenceRepository.GetPackages().ToList();

            // Assert
            Assert.Equal(1, packages.Count);
            Assert.Same(packageA, packages[0]);

            Assert.True(fileSystem.FileExists("packages.cool.config"));
        }
        public void GetPackagesReturnsPackagesFromSourceRepositoryListedInPackagesConfig()
        {
            // Arrange
            var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>();
            var packageA = PackageUtility.CreatePackage("A");
            repository.Object.AddPackage(packageA);
            var fileSystem = new MockFileSystem();
            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: repository.Object);


            // Act
            var packages = referenceRepository.GetPackages().ToList();

            // Assert
            Assert.Equal(1, packages.Count);
            Assert.Same(packageA, packages[0]);
        }