public void RemovePackageDeleteProjectConfigFileIfNoPackageLeft()
        {
            // 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"" />
  <package id=""B"" version=""3.0"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: "cool", sourceRepository: repository.Object);

            var packageB = PackageUtility.CreatePackage("B", "3.0");

            // Act
            referenceRepository.RemovePackage(packageA);
            referenceRepository.RemovePackage(packageB);

            // Assert
            Assert.False(fileSystem.FileExists("packages.config"));
            Assert.False(fileSystem.FileExists("packages.cool.config"));
        }
        public void RemovingAndAddingPackageReferenceWithSameIdPreservesConstraint()
        {
            // Arrange
            var sharedRepository = new Mock <ISharedPackageRepository>();
            var fileSystem       = new MockFileSystem();

            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" allowedVersions=""[1.0, 5.0)"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object);
            var A10 = PackageUtility.CreatePackage("A");
            var A20 = PackageUtility.CreatePackage("A", "2.0");

            // Act
            referenceRepository.RemovePackage(A10);
            referenceRepository.AddPackage(A20);

            // Assert
            Assert.True(fileSystem.FileExists("packages.config"));
            AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""2.0"" allowedVersions=""[1.0, 5.0)"" />
</packages>", fileSystem.ReadAllText("packages.config"));
        }
        public void RemovePackageRemovesEntryFromPackagesConfigDeletesFileAndUnregistersRepositoryIfLastEntry()
        {
            // Arrange
            var    sharedRepository = new Mock <ISharedPackageRepository>();
            string path             = null;

            sharedRepository.Setup(m => m.UnregisterRepository(It.IsAny <string>()))
            .Callback <string>(p => path = p);
            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: sharedRepository.Object);
            var package             = PackageUtility.CreatePackage("A");

            // Act
            referenceRepository.RemovePackage(package);

            // Assert
            Assert.False(fileSystem.FileExists("packages.config"));
            Assert.NotNull(path);
            Assert.Equal(@"C:\MockFileSystem\packages.config", path);
        }
        public void RemovingAndAddingPackageReferenceWithSameIdPreservesDevelopmentFlag()
        {
            // Arrange
            var sharedRepository = new Mock <ISharedPackageRepository>();
            var fileSystem       = new MockFileSystem();

            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" developmentDependency=""false"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: sharedRepository.Object);
            var A10 = PackageUtility.CreatePackage("A");
            var A20 = PackageUtility.CreatePackage("A", "2.0");

            // Act
            referenceRepository.RemovePackage(A10);
            referenceRepository.AddPackage(A20);

            // Assert
            Assert.True(fileSystem.FileExists("packages.config"));
            AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""2.0"" developmentDependency=""false"" />
</packages>", fileSystem.ReadAllText("packages.config"));
        }
        public void RemovePackageRemovesEntryFromPackagesConfig()
        {
            // Arrange
            var sharedRepository = new Mock <ISharedPackageRepository>();
            var fileSystem       = new MockFileSystem();

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

            // Act
            referenceRepository.RemovePackage(package);

            // Assert
            Assert.True(fileSystem.FileExists("packages.config"));
            AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""B"" version=""1.0"" />
</packages>", fileSystem.ReadAllText("packages.config"));
        }
        public void RemovePackageRemovesEntryFromPackagesConfigDeletesFileAndUnregistersRepositoryIfLastEntry()
        {
            // Arrange
            var sharedRepository = new Mock<ISharedPackageRepository>();
            string path = null;
            sharedRepository.Setup(m => m.UnregisterRepository(It.IsAny<string>()))
                            .Callback<string>(p => path = p);
            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, sharedRepository.Object);
            var package = PackageUtility.CreatePackage("A");

            // Act
            referenceRepository.RemovePackage(package);

            // Assert
            Assert.False(fileSystem.FileExists("packages.config"));
            Assert.NotNull(path);
            Assert.Equal(@"C:\MockFileSystem\packages.config", path);
        }
        public void RemovePackageRemovesEntryFromPackagesConfig()
        {
            // Arrange
            var sharedRepository = new Mock<ISharedPackageRepository>();
            var fileSystem = new MockFileSystem();
            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" />
  <package id=""B"" version=""1.0"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object);
            var package = PackageUtility.CreatePackage("A");

            // Act
            referenceRepository.RemovePackage(package);

            // Assert
            Assert.True(fileSystem.FileExists("packages.config"));
            Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""B"" version=""1.0"" />
</packages>", fileSystem.ReadAllText("packages.config"));
        }
        public void RemovingAndAddingPackageReferenceWithSameIdPreservesConstraint()
        {
            // Arrange
            var sharedRepository = new Mock<ISharedPackageRepository>();
            var fileSystem = new MockFileSystem();
            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" allowedVersions=""[1.0, 5.0)"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object);
            var A10 = PackageUtility.CreatePackage("A");
            var A20 = PackageUtility.CreatePackage("A", "2.0");

            // Act
            referenceRepository.RemovePackage(A10);
            referenceRepository.AddPackage(A20);

            // Assert
            Assert.True(fileSystem.FileExists("packages.config"));
            Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""2.0"" allowedVersions=""[1.0, 5.0)"" />
</packages>", fileSystem.ReadAllText("packages.config"));
        }
        public void RemovePackagePreservesProjectConfigFile()
        {
            // 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"" />
  <package id=""B"" version=""3.0"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: "cool", sourceRepository: repository.Object);

            var packageB = PackageUtility.CreatePackage("B", "3.0");

            // Act
            referenceRepository.RemovePackage(packageB);

            // Assert
            Assert.False(fileSystem.FileExists("packages.config"));
            Assert.True(fileSystem.FileExists("packages.cool.config"));

            string content = fileSystem.ReadAllText("packages.cool.config");

            AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" />
</packages>", content);
        }
        public void RemovingAndAddingPackageReferenceWithSameIdPreservesDevelopmentFlag()
        {
            // Arrange
            var sharedRepository = new Mock<ISharedPackageRepository>();
            var fileSystem = new MockFileSystem();
            fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""1.0"" developmentDependency=""false"" />
</packages>");
            var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: sharedRepository.Object);
            var A10 = PackageUtility.CreatePackage("A");
            var A20 = PackageUtility.CreatePackage("A", "2.0");

            // Act
            referenceRepository.RemovePackage(A10);
            referenceRepository.AddPackage(A20);

            // Assert
            Assert.True(fileSystem.FileExists("packages.config"));
            AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""A"" version=""2.0"" developmentDependency=""false"" />
</packages>", fileSystem.ReadAllText("packages.config"));
        }