Esempio n. 1
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())
            {
                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();
            }
        }
Esempio n. 2
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]));
                }
        }
Esempio n. 3
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");
                }
            }
        }