public void SettingsFile_Remove_LastValueInSection_RemovesSection() { // Arrange var nugetConfigPath = "NuGet.Config"; var config = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""DeleteMe"" value=""value2"" /> </SectionName> </configuration>"; using (var mockBaseDirectory = TestDirectory.Create()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var settingsFile = new SettingsFile(Path.Combine(mockBaseDirectory)); // Act & Assert var section = settingsFile.GetSection("SectionName"); section.Should().NotBeNull(); var item = section.GetFirstItemWithAttribute <AddItem>("key", "DeleteMe"); item.Should().NotBeNull(); item.Value.Should().Be("value2"); settingsFile.Remove("SectionName", item); settingsFile.SaveToDisk(); SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, nugetConfigPath))).Should().Be(SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?><configuration></configuration>")); section = settingsFile.GetSection("SectionName"); section.Should().BeNull(); } }
public void SettingsFile_AddOrUpdate_SectionThatExist_WillAddToSection() { // Arrange var configFile = "NuGet.Config"; using (var mockBaseDirectory = TestDirectory.Create()) { var config = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""key"" value=""value"" /> </SectionName> </configuration>"; SettingsTestUtils.CreateConfigurationFile(configFile, mockBaseDirectory, config); var settingsFile = new SettingsFile(mockBaseDirectory); // Act settingsFile.AddOrUpdate("SectionName", new AddItem("keyTwo", "valueTwo")); settingsFile.SaveToDisk(); // Assert var result = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""key"" value=""value"" /> <add key=""keyTwo"" value=""valueTwo"" /> </SectionName> </configuration>"); SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, configFile))).Should().Be(result); } }
public void SettingsFile_AddOrUpdate_PreserveUnknownItems() { // Arrange var nugetConfigPath = "NuGet.Config"; var config = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""key1"" value=""value"" /> </SectionName> <SectionName2> <add key=""key"" value=""value"" /> </SectionName2> <UnknownSection> <UnknownItem meta1=""data1"" /> <OtherUnknownItem> </OtherUnknownItem> </UnknownSection> </configuration>"; using (var mockBaseDirectory = TestDirectory.Create()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var settingsFile = new SettingsFile(mockBaseDirectory); // Act & Assert settingsFile.AddOrUpdate("SectionName", new AddItem("newKey", "value")); settingsFile.SaveToDisk(); var result = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""key1"" value=""value"" /> <add key=""newKey"" value=""value"" /> </SectionName> <SectionName2> <add key=""key"" value=""value"" /> </SectionName2> <UnknownSection> <UnknownItem meta1=""data1"" /> <OtherUnknownItem> </OtherUnknownItem> </UnknownSection> </configuration>"); SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, nugetConfigPath))).Should().Be(result); var section = settingsFile.GetSection("SectionName"); section.Should().NotBeNull(); var item = section.GetFirstItemWithAttribute <AddItem>("key", "newKey"); item.Should().NotBeNull(); item.Value.Should().Be("value"); } }
public void SettingsFile_Remove_PreserveComments() { // Arrange var nugetConfigPath = "NuGet.Config"; var config = @"<?xml version=""1.0"" encoding=""utf-8""?> <!-- Comment in nuget configuration --> <configuration> <!-- This section has the item to delete --> <SectionName> <add key=""DeleteMe"" value=""value"" /> <add key=""keyNotToDelete"" value=""value"" /> </SectionName> <!-- This section doesn't have the item to delete --> <SectionName2> <add key=""key"" value=""value"" /> </SectionName2> </configuration>"; using (var mockBaseDirectory = TestDirectory.Create()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var settingsFile = new SettingsFile(mockBaseDirectory); // Act & Assert var section = settingsFile.GetSection("SectionName"); section.Should().NotBeNull(); var item = section.GetFirstItemWithAttribute <SettingItem>("key", "DeleteMe"); settingsFile.Remove("SectionName", item); settingsFile.SaveToDisk(); var result = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?> <!-- Comment in nuget configuration --> <configuration> <!-- This section has the item to delete --> <SectionName> <add key=""keyNotToDelete"" value=""value"" /> </SectionName> <!-- This section doesn't have the item to delete --> <SectionName2> <add key=""key"" value=""value"" /> </SectionName2> </configuration>"); SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, nugetConfigPath))).Should().Be(result); section = settingsFile.GetSection("SectionName"); section.Should().NotBeNull(); item = section.GetFirstItemWithAttribute <AddItem>("key", "DeleteMe"); item.Should().BeNull(); } }
public void SettingsFile_Constructor_CreateDefaultConfigFileIfNoConfig() { using (var mockBaseDirectory = TestDirectory.Create()) { // Act var settingsFile = new SettingsFile(mockBaseDirectory); // Assert var text = SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, "NuGet.Config"))); var expectedResult = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> <add key=""nuget.org"" value=""https://api.nuget.org/v3/index.json"" protocolVersion=""3"" /> </packageSources> </configuration>"); text.Should().Be(expectedResult); } }
public void SettingsFile_AddOrUpdate_WhenItemExistsInSection_OverrideItem() { // Arrange var configFile = "NuGet.Config"; using (var mockBaseDirectory = TestDirectory.Create()) { var config = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""key"" value=""value"" /> </SectionName> </configuration>"; SettingsTestUtils.CreateConfigurationFile(configFile, mockBaseDirectory, config); var settingsFile = new SettingsFile(mockBaseDirectory); // Act settingsFile.AddOrUpdate("SectionName", new AddItem("key", "NewValue")); settingsFile.SaveToDisk(); // Assert var result = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""key"" value=""NewValue"" /> </SectionName> </configuration>"); SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, configFile))).Should().Be(result); var section = settingsFile.GetSection("SectionName"); section.Should().NotBeNull(); var item = section.GetFirstItemWithAttribute <AddItem>("key", "key"); item.Should().NotBeNull(); item.Value.Should().Be("NewValue"); } }
public void UnknownItem_Remove_ExistingChild_PreservesComments() { // Arrange var nugetConfigPath = "NuGet.Config"; var config = @" <configuration> <!-- This is a section --> <Section> <!-- Unknown Item --> <Unknown meta=""data""> <!-- Text child --> Text for test <!-- Item child --> <add key=""key"" value=""val"" /> </Unknown> </Section> </configuration>"; var expectedSetting = new UnknownItem("Unknown", attributes: new Dictionary <string, string>() { { "meta", "data" } }, children: new List <SettingBase>() { new AddItem("key", "val") }); using (var mockBaseDirectory = TestDirectory.Create()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var settingsFile = new SettingsFile(mockBaseDirectory); // Act var section = settingsFile.GetSection("Section"); section.Should().NotBeNull(); var element = section.Items.FirstOrDefault() as UnknownItem; element.Should().NotBeNull(); element.Remove(element.Children.First()); settingsFile.AddOrUpdate("Section", element); settingsFile.SaveToDisk(); var expectedConfig = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <!-- This is a section --> <Section> <!-- Unknown Item --> <Unknown meta=""data""> <!-- Text child --> <!-- Item child --> <add key=""key"" value=""val"" /> </Unknown> </Section> </configuration>"); SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, nugetConfigPath))).Should().Be(expectedConfig); // Assert SettingsTestUtils.DeepEquals(element, expectedSetting).Should().BeTrue(); } }