コード例 #1
0
        public void SettingsFile_MergeSectionsInto_WithSectionsInCommon_AndClear_ClearsSection()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section1>
        <add key='key1' value='value1' />
    </Section1>
    <Section2>
        <clear />
    </Section2>
</configuration>";

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

                // Act
                var settingsDict = new Dictionary <string, VirtualSettingSection>()
                {
                    { "Section2", new VirtualSettingSection("Section2", new AddItem("keyX", "valueX")) },
                    { "Section3", new VirtualSettingSection("Section3", new AddItem("key3", "valueY")) },
                };

                settingsFile.MergeSectionsInto(settingsDict);

                var expectedSettingsDict = new Dictionary <string, VirtualSettingSection>()
                {
                    { "Section2", new VirtualSettingSection("Section2", new ClearItem()) },
                    { "Section3", new VirtualSettingSection("Section3", new AddItem("key3", "valueY")) },
                    { "Section1", new VirtualSettingSection("Section1", new AddItem("key1", "value1")) },
                };

                foreach (var pair in settingsDict)
                {
                    expectedSettingsDict.TryGetValue(pair.Key, out var expectedSection).Should().BeTrue();
                    pair.Value.DeepEquals(expectedSection).Should().BeTrue();
                    expectedSettingsDict.Remove(pair.Key);
                }
                expectedSettingsDict.Should().BeEmpty();
            }
        }
コード例 #2
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())
            {
                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("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 = ConfigurationFileTestUtility.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().NotBeEquivalentTo(configFileHash);
            }
        }
コード例 #3
0
        public void UnknownItem_Update_UpdatesExistingChildTexts()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <Unknown old=""attr"">
            test
        </Unknown>
    </Section>
</configuration>";

            var updateSetting = new UnknownItem("Unknown",
                                                attributes: new Dictionary <string, string>()
            {
                { "old", "attr" }
            },
                                                children: new List <SettingBase>()
            {
                new SettingText("New test")
            });

            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.Update(updateSetting);

                // Assert
                element.Children.Count.Should().Be(1);
                element.Children.First().Should().BeOfType <SettingText>();
                (element.Children.First() as SettingText).Value.Should().Be("New test");
            }
        }
コード例 #4
0
        public void GetDecryptedValueForAddItem_ReturnsDecryptedValue()
        {
            // Arrange
            var configFile = "NuGet.Config";

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

                SettingsUtility.SetEncryptedValueForAddItem(settings, "SectionName", "key", "value");

                // Act
                var result = SettingsUtility.GetDecryptedValueForAddItem(settings, "SectionName", "key");

                // Assert
                result.Should().Be("value");
            }
        }
コード例 #5
0
        public void UnknownItem_Remove_ExistingChild_Succeeds()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <Unknown meta=""data"">
            Text for test
            <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());

                // Assert
                element.DeepEquals(expectedSetting).Should().BeTrue();
            }
        }
コード例 #6
0
        public void SettingsFile_Constructor_InvalidXml_Throws()
        {
            // Arrange
            var config = @"boo>";

            var nugetConfigPath = "NuGet.Config";

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

                // Act
                var ex = Record.Exception(() => new SettingsFile(mockBaseDirectory));

                // Assert
                ex.Should().NotBeNull();
                ex.Should().BeOfType <NuGetConfigurationException>();
                ex.Message.Should().Be(string.Format("NuGet.Config is not valid XML. Path: '{0}'.", Path.Combine(mockBaseDirectory, nugetConfigPath)));
            }
        }
コード例 #7
0
        public void SettingsFile_Constructor_CreateDefaultConfigFileIfNoConfig()
        {
            using (var mockBaseDirectory = TestDirectory.Create())
            {
                // Act
                var settingsFile = new SettingsFile(mockBaseDirectory);

                // Assert
                var text = ConfigurationFileTestUtility.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, "NuGet.Config")));

                var expectedResult = ConfigurationFileTestUtility.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);
            }
        }
コード例 #8
0
        public void SettingsFile_IsEmpty_WithNonemptyNuGetConfig_ReturnsFalse()
        {
            // Arrange
            var config     = @"
<configuration>
    <SectionName>
        <add key='key1' value='value1' />
    </SectionName>
</configuration>";
            var configFile = "NuGet.Config";

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

                // Act & Assert
                settingsFile.IsEmpty().Should().BeFalse();
            }
        }
コード例 #9
0
        public void SourceItem_ParsedSuccessfully()
        {
            // Arrange
            var config = @"
<configuration>
    <packageSources>
        <add key='nugetorg' value='http://serviceIndexorg.test/api/index.json' />
        <add key='nuget2' value='http://serviceIndex.test/api/index.json' protocolVersion='3' />
    </packageSources>
</configuration>";

            var expectedValues = new List <SourceItem>()
            {
                new SourceItem("nugetorg", "http://serviceIndexorg.test/api/index.json"),
                new SourceItem("nuget2", "http://serviceIndex.test/api/index.json", "3"),
            };

            var nugetConfigPath = "NuGet.Config";

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

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

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

                var children = section.Items.ToList();

                children.Should().NotBeEmpty();
                children.Count.Should().Be(2);

                for (var i = 0; i < children.Count; i++)
                {
                    children[i].DeepEquals(expectedValues[i]).Should().BeTrue();
                }
            }
        }
コード例 #10
0
        public void SettingsFile_Remove_WithMachineWideSettings_Throws()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <add key='key0' value='value0' />
    </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, nugetConfigPath, isMachineWide: true);

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

                var item = section.GetFirstItemWithAttribute <AddItem>("key", "key0");
                item.Should().NotBeNull();
                item.Value.Should().Be("value0");

                var ex = Record.Exception(() => settingsFile.Remove("Section", item));
                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 section1 = settingsFile.GetSection("Section");
                section1.Items.Count.Should().Be(1);

                var updatedFileHash = ConfigurationFileTestUtility.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().BeEquivalentTo(configFileHash);
            }
        }
コード例 #11
0
        public void SettingSection_AddOrUpdate_ToMachineWide_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())
            {
                ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                var configFileHash = ConfigurationFileTestUtility.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));

                var settingsFile = new SettingsFile(mockBaseDirectory, nugetConfigPath, isMachineWide: 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("Unable to update setting since it is in a machine-wide NuGet.Config.");

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

                settingsFile.SaveToDisk();

                var updatedFileHash = ConfigurationFileTestUtility.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().BeEquivalentTo(configFileHash);
            }
        }
コード例 #12
0
        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>";

                ConfigurationFileTestUtility.CreateConfigurationFile(configFile, mockBaseDirectory, config);
                var settingsFile = new SettingsFile(mockBaseDirectory);

                // Act
                settingsFile.AddOrUpdate("SectionName", new AddItem("key", "NewValue"));
                settingsFile.SaveToDisk();

                // Assert
                var result = ConfigurationFileTestUtility.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <SectionName>
        <add key=""key"" value=""NewValue"" />
    </SectionName>
</configuration>");

                ConfigurationFileTestUtility.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");
            }
        }
コード例 #13
0
        public void SettingsFile_AddOrUpdate_SectionThatDoesntExist_WillAddSection()
        {
            // 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>";

                ConfigurationFileTestUtility.CreateConfigurationFile(configFile, mockBaseDirectory, config);
                var settingsFile = new SettingsFile(mockBaseDirectory);

                // Act
                settingsFile.AddOrUpdate("NewSectionName", new AddItem("key", "value"));
                settingsFile.SaveToDisk();

                // Assert
                var result = ConfigurationFileTestUtility.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <SectionName>
        <add key=""key"" value=""value"" />
    </SectionName>
    <NewSectionName>
        <add key=""key"" value=""value"" />
    </NewSectionName>
</configuration>");

                ConfigurationFileTestUtility.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, configFile))).Should().Be(result);
                var section = settingsFile.GetSection("NewSectionName");
                section.Should().NotBeNull();
                section.Items.Count.Should().Be(1);
            }
        }
コード例 #14
0
        public void SettingSection_Remove_Succeeds()
        {
            // 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();

                var child = section.GetFirstItemWithAttribute <AddItem>("key", "key0");
                child.Should().NotBeNull();

                settingsFile.Remove("Section", child);
                settingsFile.SaveToDisk();

                var updatedFileHash = ConfigurationFileTestUtility.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().NotBeEquivalentTo(configFileHash);

                section = settingsFile.GetSection("Section");
                section.Items.Count.Should().Be(1);
                var deletedChild = section.GetFirstItemWithAttribute <AddItem>("key", "key0");
                deletedChild.Should().BeNull();
            }
        }
コード例 #15
0
        public void NuGetPathContext_LoadSettings()
        {
            // Arrange
            var config = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <config>
        <add key=""globalPackagesFolder"" value=""global"" />
    </config>
    <fallbackPackageFolders>
        <add key=""shared"" value=""test"" />
        <add key=""src"" value=""src"" />
    </fallbackPackageFolders>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

            using (var mockBaseDirectory = TestFileSystemUtility.CreateRandomTestFolder())
            {
                var testFolder   = Path.Combine(mockBaseDirectory, "test");
                var srcFolder    = Path.Combine(mockBaseDirectory, "src");
                var globalFolder = Path.Combine(mockBaseDirectory, "global");

                ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                Settings settings = new Settings(mockBaseDirectory);

                var http = SettingsUtility.GetHttpCacheFolder();

                // Act
                var pathContext = NuGetPathContext.Create(settings);

                // Assert
                Assert.Equal(2, pathContext.FallbackPackageFolders.Count);
                Assert.Equal(testFolder, pathContext.FallbackPackageFolders[0]);
                Assert.Equal(srcFolder, pathContext.FallbackPackageFolders[1]);
                Assert.Equal(globalFolder, pathContext.UserPackageFolder);
                Assert.Equal(http, pathContext.HttpCacheFolder);
            }
        }
コード例 #16
0
        public void GetDecryptedValueForAddItem_WithNoKey_ReturnsNull()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <SectionName>
        <add key=""key"" value=""value"" />
    </SectionName>
</configuration>";

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

                // Act
                var result = SettingsUtility.GetDecryptedValueForAddItem(settings, "SectionName", "NoKeyByThatName");

                // Assert
                result.Should().BeNull();
            }
        }
コード例 #17
0
        public void AddItem_WithoutRequiredAttributes_Throws()
        {
            // Arrange
            var config          = @"
<configuration>
    <SectionName>
        <add Key='key2' Value='value2' />,
    </SectionName>
</configuration>";
            var nugetConfigPath = "NuGet.Config";

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

                // Act and Assert
                var ex = Record.Exception(() => new SettingsFile(mockBaseDirectory));

                ex.Should().NotBeNull();
                ex.Should().BeOfType <NuGetConfigurationException>();
                ex.Message.Should().Be(string.Format("Unable to parse config file because: Missing required attribute 'key' in element 'add'. Path: '{0}'.", Path.Combine(mockBaseDirectory, nugetConfigPath)));
            }
        }
コード例 #18
0
        public void SettingSection_WithAClear_ParseClearCorrectly()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <config>
        <add key='key0' value='value0' />
        <add key='key1' value='value1' meta1='data1' meta2='data2'/>
        <clear />
        <add key='key2' value='value2' meta3='data3'/>
    </config>
</configuration>";

            var expectedItem = new AddItem("key2", "value2", new ReadOnlyDictionary <string, string>(
                                               new Dictionary <string, string> {
                { "meta3", "data3" }
            }));

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

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

                var children = section.Items.ToList();

                // Assert
                children.Should().NotBeEmpty();
                children.Count.Should().Be(2);
                children.FirstOrDefault().Should().BeOfType <ClearItem>();
                children[1].DeepEquals(expectedItem).Should().BeTrue();
            }
        }
コード例 #19
0
        public void SettingsFile_Constructor_WithInvalidRootElement_Throws()
        {
            // Arrange
            var configFile = "NuGet.Config";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                var config = @"
<notvalid>
    <SectionName>
        <add key='key1' value='value1' />
        <add key='key2' value='value2' />
    </SectionName>
</notvalid>";

                ConfigurationFileTestUtility.CreateConfigurationFile(configFile, mockBaseDirectory, config);

                // Act & Assert
                var ex = Record.Exception(() => new SettingsFile(mockBaseDirectory));
                ex.Should().NotBeNull();
                ex.Should().BeOfType <NuGetConfigurationException>();
                ex.Message.Should().Be(string.Format("NuGet.Config does not contain the expected root element: 'configuration'. Path: '{0}'.", Path.Combine(mockBaseDirectory, configFile)));
            }
        }
コード例 #20
0
        public void GetGlobalPackagesFolder_FromNuGetConfig()
        {
            // Arrange
            var config = @"<?xml version='1.0' encoding='utf-8'?>
<configuration>
    <config>
        <add key='globalPackagesFolder' value='a' />
    </config>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

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

                // Act
                var globalPackagesFolderPath = SettingsUtility.GetGlobalPackagesFolder(settings);

                // Assert
                globalPackagesFolderPath.Should().Be(Path.Combine(mockBaseDirectory, "a"));
            }
        }
コード例 #21
0
        public void GetFallbackPackageFolders_SingleFolderFromNuGetConfig()
        {
            // Arrange
            var config = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""shared"" value=""a"" />
    </fallbackPackageFolders>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

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

                // Act
                var paths = SettingsUtility.GetFallbackPackageFolders(settings);

                // Assert
                Assert.Equal(Path.Combine(mockBaseDirectory, "a"), paths.Single());
            }
        }
コード例 #22
0
        public void UnknownItem_Update_UpdatesExistingAttributes()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <Unknown old=""attr"" />
    </Section>
</configuration>";

            var updateSetting = new UnknownItem("Unknown",
                                                attributes: new Dictionary <string, string>()
            {
                { "old", "newAttr" }
            },
                                                children: null);

            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.Update(updateSetting);

                // Assert
                element.Attributes["old"].Should().Be("newAttr");
            }
        }
コード例 #23
0
        public void SetEncryptedValueForAddItem_UpdatesExistingItemValueWithEncryptedOne()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <SectionName>
        <add key=""key"" value=""value"" />
    </SectionName>
</configuration>";

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

                // Act
                SettingsUtility.SetEncryptedValueForAddItem(settings, "SectionName", "key", "NewValue");

                // Assert
                var content = File.ReadAllText(Path.Combine(mockBaseDirectory, nugetConfigPath));
                content.Should().NotContain("NewValue");
            }
        }
コード例 #24
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();
            }
        }
コード例 #25
0
        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())
            {
                ConfigurationFileTestUtility.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();

                ConfigurationFileTestUtility.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, nugetConfigPath))).Should().Be(ConfigurationFileTestUtility.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?><configuration></configuration>"));

                section = settingsFile.GetSection("SectionName");
                section.Should().BeNull();
            }
        }
コード例 #26
0
        public void GetFallbackPackageFolders_MultipleFoldersFromMultipleNuGetConfigs()
        {
            // Arrange
            var configA = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""a"" value=""C:\Temp\a"" />
        <add key=""b"" value=""C:\Temp\b"" />
    </fallbackPackageFolders>
</configuration>";

            var configB = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""c"" value=""C:\Temp\c"" />
        <add key=""d"" value=""C:\Temp\d"" />
    </fallbackPackageFolders>
</configuration>";

            var configC = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""x"" value=""C:\Temp\x"" />
        <add key=""y"" value=""C:\Temp\y"" />
    </fallbackPackageFolders>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

            using (var machineWide = TestDirectory.Create())
                using (var mockBaseDirectory = TestDirectory.Create())
                {
                    var subFolder = Path.Combine(mockBaseDirectory, "sub");

                    ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, subFolder, configA);
                    ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, configB);
                    ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, machineWide, configC);

                    var machineWiderFolderSettings = new Settings(machineWide);
                    var machineWideSettings        = new TestMachineWideSettings(machineWiderFolderSettings);

                    var settings = Settings.LoadDefaultSettings(
                        subFolder,
                        configFileName: null,
                        machineWideSettings: machineWideSettings);

                    // Act
                    var paths = SettingsUtility.GetFallbackPackageFolders(settings).ToArray();

                    var expected = new HashSet <string>(StringComparer.Ordinal)
                    {
                        "a",
                        "b",
                        "c",
                        "d",
                        "x",
                        "y"
                    };

                    // Ignore any extra folders on the machine
                    var pathsFiltered = paths.Where(s => expected.Contains(GetFileName(s))).ToArray();

                    // Assert
                    Assert.Equal(6, pathsFiltered.Length);
                    Assert.Equal("a", GetFileName(pathsFiltered[0]));
                    Assert.Equal("b", GetFileName(pathsFiltered[1]));
                    Assert.Equal("c", GetFileName(pathsFiltered[2]));
                    Assert.Equal("d", GetFileName(pathsFiltered[3]));
                    Assert.Equal("x", GetFileName(pathsFiltered[4]));
                    Assert.Equal("y", GetFileName(pathsFiltered[5]));
                }
        }
コード例 #27
0
        public void AddItem_GetValueAsPath_ResolvesPathsCorrectly()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <SectionName>
        <!-- values that are relative paths -->
        <add key=""key1"" value=""..\value1"" />
        <add key=""key2"" value=""a\b\c"" />
        <add key=""key3"" value="".\a\b\c"" />

        <!-- values that are not relative paths -->
        <add key=""key4"" value=""c:\value2"" />
        <add key=""key5"" value=""http://value3"" />
        <add key=""key6"" value=""\\a\b\c"" />
        <add key=""key7"" value=""\a\b\c"" />
    </SectionName>
</configuration>";

            if (!RuntimeEnvironmentHelper.IsWindows)
            {
                config = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <SectionName>
        <!-- values that are relative paths -->
        <add key=""key1"" value=""../value1"" />
        <add key=""key2"" value=""a/b/c"" />
        <add key=""key3"" value=""./a/b/c"" />

        <!-- values that are not relative paths -->
        <add key=""key5"" value=""http://value3"" />
        <add key=""key7"" value=""/a/b/c"" />
    </SectionName>
</configuration>";
            }

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

                // Act
                var section = settings.GetSection("SectionName");

                // Assert
                section.Should().NotBeNull();
                section.Items.Should().NotBeEmpty();
                section.Items.Should().AllBeOfType <AddItem>();

                if (RuntimeEnvironmentHelper.IsWindows)
                {
                    section.Items.Count.Should().Be(7);

                    var item = section.GetFirstItemWithAttribute <AddItem>("key", "key1");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(Path.Combine(mockBaseDirectory, @"..\value1"));
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key2");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(Path.Combine(mockBaseDirectory, @"a\b\c"));
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key3");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(Path.Combine(mockBaseDirectory, @".\a\b\c"));
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key4");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(@"c:\value2");
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key5");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(@"http://value3");
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key6");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(@"\\a\b\c");
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key7");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(Path.Combine(Path.GetPathRoot(mockBaseDirectory), @"a\b\c"));
                }
                else
                {
                    section.Items.Count.Should().Be(5);

                    var item = section.GetFirstItemWithAttribute <AddItem>("key", "key1");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(Path.Combine(mockBaseDirectory, @"../value1"));
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key2");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(Path.Combine(mockBaseDirectory, @"a/b/c"));
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key3");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(Path.Combine(mockBaseDirectory, @"./a/b/c"));
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key5");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(@"http://value3");
                    item = section.GetFirstItemWithAttribute <AddItem>("key", "key7");
                    item.Should().NotBeNull();
                    item.GetValueAsPath().Should().Be(@"/a/b/c");
                }
            }
        }