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_WithMachineWideSettings_Throws()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <add key='key0' value='value0' />
    </Section>
</configuration>";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                var configFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));

                var settingsFile = new SettingsFile(mockBaseDirectory, nugetConfigPath, isMachineWide: true);

                // Act
                var ex = Record.Exception(() => settingsFile.AddOrUpdate("section", new AddItem("SomeKey", "SomeValue")));
                ex.Should().NotBeNull();
                ex.Should().BeOfType <InvalidOperationException>();
                ex.Message.Should().Be("Unable to update setting since it is in a machine-wide NuGet.Config.");

                settingsFile.SaveToDisk();

                var section = settingsFile.GetSection("Section");
                section.Items.Count.Should().Be(1);

                var updatedFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().BeEquivalentTo(configFileHash);
            }
        }
        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");
            }
        }
Example #4
0
        public void SettingText_UpdatesSuccessfully()
        {
            // Arrange
            var config          = @"
<configuration>
    <SectionName>
        <Item>This is a test</Item>
    </SectionName>
</configuration>";
            var nugetConfigPath = "NuGet.Config";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);

                // Act and Assert
                var settingsFile = new SettingsFile(mockBaseDirectory);
                settingsFile.Should().NotBeNull();

                var section = settingsFile.GetSection("SectionName");
                section.Should().NotBeNull();

                section.Items.Count.Should().Be(1);
                var item = section.Items.First() as UnknownItem;
                item.Should().NotBeNull();

                item.Children.Count.Should().Be(1);
                var text = item.Children.First() as SettingText;
                text.Should().NotBeNull();

                text.Value.Should().Be("This is a test");

                text.Value = "This is another test";

                settingsFile.AddOrUpdate("SectionName", new UnknownItem(item.ElementName, attributes: null, children: new List <SettingBase>()
                {
                    text
                }));

                section = settingsFile.GetSection("SectionName");
                section.Should().NotBeNull();

                section.Items.Count.Should().Be(1);
                item = section.Items.First() as UnknownItem;
                item.Should().NotBeNull();

                item.Children.Count.Should().Be(1);
                text = item.Children.First() as SettingText;
                text.Should().NotBeNull();

                text.Value.Should().Be("This is another test");
            }
        }
Example #5
0
        public void AddItem_UpdatingAttribute_WithAddOrUpdate_SuccessfullyUpdatesConfigFile()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <add key='key1' value='value1' />
    </Section>
</configuration>";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                var configFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));

                // Act and Assert
                var settingsFile = new SettingsFile(mockBaseDirectory);

                // Act
                var section = settingsFile.GetSection("Section");
                section.Should().NotBeNull();

                var element = section.Items.FirstOrDefault() as AddItem;
                element.Should().NotBeNull();

                element.Value = "newValue";
                element.Value.Should().Be("newValue");

                var section2 = settingsFile.GetSection("Section");
                section2.Should().NotBeNull();

                var element2 = section2.Items.FirstOrDefault() as AddItem;
                element2.Should().NotBeNull();
                element2.Value.Should().Be("value1");

                settingsFile.AddOrUpdate("Section", element);
                settingsFile.SaveToDisk();

                var section3 = settingsFile.GetSection("Section");
                section3.Should().NotBeNull();

                var element3 = section.Items.FirstOrDefault() as AddItem;
                element3.Should().NotBeNull();
                element3.Value.Should().Be("newValue");

                var updatedFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().NotBeEquivalentTo(configFileHash);
            }
        }
Example #6
0
        public void SettingsFile_AddOrUpdate_PreserveComments()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"<?xml version=""1.0"" encoding=""utf-8""?>
<!-- Comment in nuget configuration -->
<configuration>
    <!-- Will add an item to this section -->
    <SectionName>
        <add key=""key1"" value=""value"" />
    </SectionName>
    <!-- This section wont have a new item -->
    <SectionName2>
        <add key=""key"" value=""value"" />
    </SectionName2>
</configuration>";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                var settingsFile = new SettingsFile(mockBaseDirectory);

                // Act & Assert
                settingsFile.AddOrUpdate("SectionName", new AddItem("newKey", "value"));
                settingsFile.SaveToDisk();

                var result = ConfigurationFileTestUtility.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?>
<!-- Comment in nuget configuration -->
<configuration>
    <!-- Will add an item to this section -->
    <SectionName>
        <add key=""key1"" value=""value"" />
        <add key=""newKey"" value=""value"" />
    </SectionName>
    <!-- This section wont have a new item -->
    <SectionName2>
        <add key=""key"" value=""value"" />
    </SectionName2>
</configuration>");

                ConfigurationFileTestUtility.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_AddOrUpdate_WithNullItem_Throws()
        {
            // Arrange
            var configFile = "NuGet.Config";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.CreateConfigurationFile(configFile, mockBaseDirectory, @"<configuration></configuration>");
                var settingsFile = new SettingsFile(mockBaseDirectory);

                // Act & Assert
                var ex = Record.Exception(() => settingsFile.AddOrUpdate("SomeKey", item: null));
                ex.Should().NotBeNull();
                ex.Should().BeOfType <ArgumentNullException>();
            }
        }
Example #8
0
        public void SettingsFile_AddOrUpdate_WithEmptySectionName_Throws()
        {
            // Arrange
            var configFile = "NuGet.Config";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                ConfigurationFileTestUtility.CreateConfigurationFile(configFile, mockBaseDirectory, @"<configuration></configuration>");
                var settingsFile = new SettingsFile(mockBaseDirectory);

                // Act & Assert
                var ex = Record.Exception(() => settingsFile.AddOrUpdate("", new AddItem("SomeKey", "SomeValue")));
                ex.Should().NotBeNull();
                ex.Should().BeOfType <ArgumentException>();
            }
        }
Example #9
0
        public void SettingSection_AddOrUpdate_UpdatesAnElementCorrectly()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <add key='key0' value='value0' />
        <add key='key1' value='value1' meta1='data1' meta2='data2'/>
    </Section>
</configuration>";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                var configFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));

                var settingsFile = new SettingsFile(mockBaseDirectory);

                // Act
                var section = settingsFile.GetSection("Section");
                section.Should().NotBeNull();
                section.Items.Count.Should().Be(2);

                settingsFile.AddOrUpdate("Section", new AddItem("key0", "value0", new Dictionary <string, string>()
                {
                    { "meta1", "data1" }
                }));

                section = settingsFile.GetSection("Section");
                section.Should().NotBeNull();
                section.Items.Count.Should().Be(2);

                var item = section.Items.First() as AddItem;
                item.Should().NotBeNull();
                item.AdditionalAttributes.Count.Should().Be(1);
                item.AdditionalAttributes["meta1"].Should().Be("data1");

                settingsFile.SaveToDisk();

                var updatedFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().NotBeEquivalentTo(configFileHash);
            }
        }
        public void SettingSection_AddOrUpdate_ToReadOnly_Throws()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <add key='key0' value='value0' />
        <add key='key1' value='value1' meta1='data1' meta2='data2'/>
    </Section>
</configuration>";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                var configFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));

                var settingsFile = new SettingsFile(mockBaseDirectory, nugetConfigPath, isMachineWide: false, isReadOnly: true);

                // Act
                var section = settingsFile.GetSection("Section");
                section.Should().NotBeNull();
                section.Items.Count.Should().Be(2);

                var ex = Record.Exception(() => settingsFile.AddOrUpdate("Section", new AddItem("key2", "value2")));
                ex.Should().NotBeNull();
                ex.Should().BeOfType <InvalidOperationException>();
                ex.Message.Should().Be(Resources.CannotUpdateReadOnlyConfig);

                section = settingsFile.GetSection("Section");
                section.Should().NotBeNull();
                section.Items.Count.Should().Be(2);

                settingsFile.SaveToDisk();

                var updatedFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().BeEquivalentTo(configFileHash);
            }
        }
        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");
            }
        }
Example #12
0
        public void SettingSection_AddOrUpdate_AddsAnElementCorrectly()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <add key='key0' value='value0' />
        <add key='key1' value='value1' meta1='data1' meta2='data2'/>
    </Section>
</configuration>";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                var configFileHash = ConfigurationFileTestUtility.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));

                var settingsFile = new SettingsFile(mockBaseDirectory);

                // Act
                var section = settingsFile.GetSection("Section");
                section.Should().NotBeNull();
                section.Items.Count.Should().Be(2);

                settingsFile.AddOrUpdate("Section", new AddItem("key2", "value2"));

                section = settingsFile.GetSection("Section");
                section.Should().NotBeNull();
                section.Items.Count.Should().Be(3);

                settingsFile.SaveToDisk();

                var updatedFileHash = ConfigurationFileTestUtility.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().NotBeEquivalentTo(configFileHash);
            }
        }
Example #13
0
        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())
            {
                ConfigurationFileTestUtility.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 = ConfigurationFileTestUtility.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>");

                ConfigurationFileTestUtility.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, nugetConfigPath))).Should().Be(expectedConfig);

                // Assert
                element.DeepEquals(expectedSetting).Should().BeTrue();
            }
        }