Exemplo n.º 1
0
        public void UnknownItem_Add_Item_WorksSuccessfully()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <Unknown />
    </Section>
</configuration>";

            var expectedSetting = new UnknownItem("Unknown", attributes: null,
                                                  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.Add(new AddItem("key", "val")).Should().BeTrue();

                // Assert
                SettingsTestUtils.DeepEquals(element, expectedSetting).Should().BeTrue();
            }
        }
Exemplo n.º 2
0
        public void AddItem_Parsing_ElementWithChildren_Throws()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <add key='key1' value='value1'>
            <add key='key2' value='value2' />
        </add>
    </Section>
</configuration>";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.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("Error parsing NuGet.Config. Element '{0}' cannot have descendant elements. Path: '{1}'.", "add", Path.Combine(mockBaseDirectory, nugetConfigPath)));
            }
        }
Exemplo n.º 3
0
        public void AddItem_SingleTag_WithOnlyKeyAndValue_ParsedSuccessfully()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <add key='key1' value='value1' />
    </Section>
</configuration>";

            var expectedSetting = new AddItem("key1", "value1");

            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 AddItem;
                element.Should().NotBeNull();

                // Assert
                SettingsTestUtils.DeepEquals(element, expectedSetting).Should().BeTrue();
            }
        }
Exemplo n.º 4
0
        public void CertificateItem_WithChildren_Throws()
        {
            // Arrange
            var config          = @"
<configuration>
    <SectionName>
        <certificate fingerprint=""abcdefg"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""true"">
            <add key=""key"" value=""val"" />
        </certificate>
    </SectionName>
</configuration>";
            var nugetConfigPath = "NuGet.Config";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.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("Error parsing NuGet.Config. Element 'certificate' cannot have descendant elements. Path: '{0}'.", Path.Combine(mockBaseDirectory, nugetConfigPath)));
            }
        }
Exemplo n.º 5
0
        public void CertificateItem_Clone_CopiesTheSameItem()
        {
            // Arrange
            var config          = @"
<configuration>
    <SectionName>
        <certificate fingerprint=""abcdefg"" hashAlgorithm=""Sha256"" allowUntrustedRoot=""true"" />
    </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.TryGetSection("SectionName", out var section).Should().BeTrue();
                section.Should().NotBeNull();

                section.Items.Count.Should().Be(1);
                var item = section.Items.First();
                item.IsCopy().Should().BeFalse();
                item.Origin.Should().NotBeNull();

                var clone = item.Clone();
                clone.IsCopy().Should().BeTrue();
                clone.Origin.Should().NotBeNull();
                SettingsTestUtils.DeepEquals(clone, item).Should().BeTrue();
            }
        }
Exemplo n.º 6
0
        public void SettingSection_GetValues_UnexistantChild_ReturnsNull()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <SectionName>
        <add key='key1' value='value1' />
        <add key='key2' value='value2' />
    </SectionName>
</configuration>";

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

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

                var key3Element = section.GetFirstItemWithAttribute <AddItem>("key", "key3");

                // Assert
                key3Element.Should().BeNull();
            }
        }
Exemplo n.º 7
0
        public void SettingSection_Remove_UnexistantChild_DoesNotRemoveAnything()
        {
            // 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.Remove(new AddItem("key7", "value7"));

                var updatedFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().BeEquivalentTo(configFileHash);

                section.Items.Count.Should().Be(2);
            }
        }
Exemplo n.º 8
0
        public void GetGlobalPackagesFolder_FromNuGetConfig_RelativePath()
        {
            // Arrange
            var config = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <config>
        <add key=""globalPackagesFolder"" value=""..\..\NuGetPackages"" />
    </config>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

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

                var expected = Path.GetFullPath(Path.Combine(mockBaseDirectory, @"..\..\NuGetPackages"));

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

                // Assert
                globalPackagesFolderPath.Should().Be(expected);
            }
        }
Exemplo n.º 9
0
        public void CredentialsItem_Parsing_WithMultipleValidAuthenticationTypes_TakesFirstAndIgnoresRest()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <packageSourceCredentials>
        <NuGet.Org meta1='data1'>
            <add key='Username' value='username' />
            <add key='Password' value='password' />
            <add key='ValidAuthenticationTypes' value='one,two,three' />
            <add key='ValidAuthenticationTypes' value='four,five,six' />
        </NuGet.Org>
    </packageSourceCredentials>
</configuration>";

            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("packageSourceCredentials");
                section.Should().NotBeNull();
                section.Items.Count.Should().Be(1);

                var item = section.Items.First() as CredentialsItem;
                item.Should().NotBeNull();

                item.ValidAuthenticationTypes.Should().Be("one,two,three");
            }
        }
Exemplo n.º 10
0
        public void UnknownItem_Merge_AddsNewAttributes()
        {
            var originalSetting = new UnknownItem("Unknown",
                                                  attributes: new Dictionary <string, string>()
            {
                { "old", "attr" }
            },
                                                  children: null);

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

            originalSetting.Merge(newSetting);

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

            SettingsTestUtils.DeepEquals(originalSetting, expectedSetting).Should().BeTrue();
        }
Exemplo n.º 11
0
        public void DeleteConfigValue_WithValidSettings_DeletesKey()
        {
            // Arrange
            var keyName         = "dependencyVersion";
            var nugetConfigPath = "NuGet.Config";
            var config          = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
  <config>
    <add key=""" + keyName + @""" value=""Highest"" />
  </config>
</configuration>";

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

                // Act
                SettingsUtility.DeleteConfigValue(settings, keyName);

                // Assert
                var content = File.ReadAllText(Path.Combine(mockBaseDirectory, nugetConfigPath));
                content.Should().NotContain(keyName);
            }
        }
Exemplo n.º 12
0
        public void UnknownItem_WithChildren_OnlyText_ParsedCorrectly()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <Unknown>Text for test</Unknown>
    </Section>
</configuration>";

            var expectedSetting = new UnknownItem("Unknown", attributes: null, children: new List <SettingBase>()
            {
                new SettingText("Text for test")
            });

            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();
                element.Should().NotBeNull();

                // Assert
                SettingsTestUtils.DeepEquals(element, expectedSetting).Should().BeTrue();
            }
        }
Exemplo n.º 13
0
        public void UnknownItem_Update_RemovesMissingAttributes()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <Unknown old=""attr"" />
    </Section>
</configuration>";

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

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

                // Assert
                element.Attributes.TryGetValue("old", out var _).Should().BeFalse();
            }
        }
Exemplo n.º 14
0
        public void UnknownItem_Remove_ToReadOnly_Throws()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <Unknown>
            <Unknown2 />
        </Unknown>
    </Section>
</configuration>";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                var settingsFile = new SettingsFile(mockBaseDirectory, nugetConfigPath, isMachineWide: false, isReadOnly: true);

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

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

                // Assert
                var ex = Record.Exception(() => element.Remove(element.Children.First()));
                ex.Should().NotBeNull();
                ex.Should().BeOfType <InvalidOperationException>();
            }
        }
        public void PackageSourceNamespacesItemParse_WithValidData_ParsesCorrectly()
        {
            // Arrange
            var config          = @"
<configuration>
    <packageNamespaces>
        <packageSource key=""nuget.org"">
            <namespace id=""sadas"" />
        </packageSource>
    </packageNamespaces>
</configuration>";
            var nugetConfigPath = "NuGet.Config";

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

            // Act and Assert
            var settingsFile = new SettingsFile(mockBaseDirectory);
            var section      = settingsFile.GetSection("packageNamespaces");

            section.Should().NotBeNull();

            section.Items.Count.Should().Be(1);
            var packageSourceNamespaceItem = section.Items.First() as PackageNamespacesSourceItem;
            var item         = packageSourceNamespaceItem.Namespaces.First();
            var expectedItem = new NamespaceItem("sadas");

            SettingsTestUtils.DeepEquals(item, expectedItem).Should().BeTrue();
        }
Exemplo n.º 16
0
        public void CredentialsItem_Parsing_WithUsernamePasswordAndClearTextPassword_TakesFirstAndIgnoresRest()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <packageSourceCredentials>
        <NuGet.Org meta1='data1'>
            <add key='Username' value='username' />
            <add key='Password' value='password' />
            <add key='ClearTextPassword' value='clearTextPassword' />
        </NuGet.Org>
    </packageSourceCredentials>
</configuration>";

            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("packageSourceCredentials");
                section.Should().NotBeNull();
                section.Items.Count.Should().Be(1);

                var item = section.Items.First() as CredentialsItem;
                item.Should().NotBeNull();

                item.Password.Should().Be("password");
                item.IsPasswordClearText.Should().BeFalse();
            }
        }
        public void PackageSourceMappingSourceItemParse_WithUnrecognizedItems_UnknownItemsAreIgnored()
        {
            // Arrange
            // Arrange
            var config          = @"
<configuration>
    <packageSourceMapping>
        <packageSource key=""nuget.org"">
            <package pattern=""sadas"" />
            <notANamespace id=""sadas"" />
        </packageSource>
    </packageSourceMapping>
</configuration>";
            var nugetConfigPath = "NuGet.Config";

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

            // Act and Assert
            var settingsFile = new SettingsFile(mockBaseDirectory);
            var section      = settingsFile.GetSection("packageSourceMapping");

            section.Should().NotBeNull();

            section.Items.Count.Should().Be(1);
            var packageSourceMappingSourceItem = section.Items.First() as PackageSourceMappingSourceItem;
            var item         = packageSourceMappingSourceItem.Patterns.First();
            var expectedItem = new PackagePatternItem("sadas");

            SettingsTestUtils.DeepEquals(item, expectedItem).Should().BeTrue();
        }
Exemplo n.º 18
0
        public void GetPackageNamespacesConfiguration_WithOneSource()
        {
            // Arrange
            using var mockBaseDirectory = TestDirectory.Create();
            var configPath1 = Path.Combine(mockBaseDirectory, "NuGet.Config");

            SettingsTestUtils.CreateConfigurationFile(configPath1, @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <packageNamespaces>
        <clear />
        <packageSource key=""nuget.org"">
            <namespace id=""stuff"" />
        </packageSource>
    </packageNamespaces>
</configuration>");
            var settings = Settings.LoadSettingsGivenConfigPaths(new string[] { configPath1 });

            // Act & Assert
            var configuration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(settings);

            configuration.AreNamespacesEnabled.Should().BeTrue();
            configuration.Namespaces.Should().HaveCount(1);
            KeyValuePair <string, IReadOnlyList <string> > namespaceForSource = configuration.Namespaces.First();

            namespaceForSource.Key.Should().Be("nuget.org");
            namespaceForSource.Value.Should().BeEquivalentTo(new string[] { "stuff" });
        }
Exemplo n.º 19
0
        public void SettingSection_Remove_OnlyOneChild_SucceedsAndRemovesSection()
        {
            // 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);

                // 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 = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath));
                updatedFileHash.Should().NotBeEquivalentTo(configFileHash);

                section = settingsFile.GetSection("Section");
                section.Should().BeNull();
            }
        }
Exemplo n.º 20
0
        public void GetPackageNamespacesConfiguration_WithMultipleSources()
        {
            // Arrange
            using var mockBaseDirectory = TestDirectory.Create();
            var configPath1 = Path.Combine(mockBaseDirectory, "NuGet.Config");

            SettingsTestUtils.CreateConfigurationFile(configPath1, @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <packageNamespaces>
        <packageSource key=""nuget.org"">
            <namespace id=""stuff"" />
        </packageSource>
        <packageSource key=""contoso"">
            <namespace id=""moreStuff"" />
        </packageSource>
    </packageNamespaces>
</configuration>");
            var settings = Settings.LoadSettingsGivenConfigPaths(new string[] { configPath1 });

            // Act & Assert
            var configuration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(settings);

            configuration.AreNamespacesEnabled.Should().BeTrue();
            configuration.Namespaces.Should().HaveCount(2);

            IReadOnlyList <string> nugetNamespaces = configuration.Namespaces["nuget.org"];

            nugetNamespaces.Should().BeEquivalentTo(new string[] { "stuff" });

            IReadOnlyList <string> contosoNamespace = configuration.Namespaces["contoso"];

            contosoNamespace.Should().BeEquivalentTo(new string[] { "moreStuff" });
        }
Exemplo n.º 21
0
        public void SettingSection_GetValues_ReturnsAllChildElements()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <SectionName>
        <add key='key1' value='value1' />
        <add key='key2' value='value2' />
    </SectionName>
</configuration>";

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

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

                var children = section.Items;

                // Assert
                children.Should().NotBeEmpty();
                children.Count.Should().Be(2);
                children.Should().AllBeOfType <AddItem>();
            }
        }
Exemplo n.º 22
0
        public void AddItem_Clone_ReturnsItemClone()
        {
            // Arrange
            var config          = @"
<configuration>
    <SectionName>
        <add key=""key1"" value=""val"" meta=""data"" />
    </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.TryGetSection("SectionName", out var section).Should().BeTrue();
                section.Should().NotBeNull();

                section.Items.Count.Should().Be(1);
                var item = section.Items.First();
                item.IsCopy().Should().BeFalse();
                item.Origin.Should().NotBeNull();

                var clone = item.Clone() as AddItem;
                clone.IsCopy().Should().BeTrue();
                clone.Origin.Should().NotBeNull();
                SettingsTestUtils.DeepEquals(clone, item).Should().BeTrue();
            }
        }
Exemplo n.º 23
0
        public void CertificateItem_ParsedCorrectly()
        {
            // Arrange
            var config          = @"
<configuration>
    <SectionName>
        <certificate fingerprint=""abcdefg"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""true"" />
    </SectionName>
</configuration>";
            var nugetConfigPath = "NuGet.Config";

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

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

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

                var expectedItem = new CertificateItem("abcdefg", Common.HashAlgorithmName.SHA256, allowUntrustedRoot: true);
                SettingsTestUtils.DeepEquals(item, expectedItem).Should().BeTrue();
            }
        }
        public void CredentialsItem_Clone_WithSpaceOnName_ReturnsItemClone()
        {
            // Arrange
            var config          = @"
<configuration>
    <packageSourceCredentials>
        <nuget_x0020_org>
            <add key='Username' value='username' />
            <add key='Password' value='password' />
        </nuget_x0020_org>
    </packageSourceCredentials>
</configuration>";
            var nugetConfigPath = "NuGet.Config";

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

                // Act and Assert
                var settingsFile = new SettingsFile(mockBaseDirectory);
                settingsFile.TryGetSection("packageSourceCredentials", out var section).Should().BeTrue();
                section.Should().NotBeNull();

                section.Items.Count.Should().Be(1);
                var item = section.Items.First();
                item.IsCopy().Should().BeFalse();
                item.Origin.Should().NotBeNull();

                var clone = item.Clone() as CredentialsItem;
                clone.IsCopy().Should().BeTrue();
                clone.Origin.Should().NotBeNull();
                SettingsTestUtils.DeepEquals(clone, item).Should().BeTrue();
            }
        }
Exemplo n.º 25
0
        public void AddItem_WithAdditionalMetada_ParsedSuccessfully()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <Section>
        <add key='key1' value='value1' meta1='data1' meta2='data2'/>
    </Section>
</configuration>";

            var expectedSetting = new AddItem("key1", "value1",
                                              new ReadOnlyDictionary <string, string>(new Dictionary <string, string> {
                { "meta1", "data1" },
                { "meta2", "data2" }
            }));

            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 value = section.Items.FirstOrDefault() as AddItem;
                value.Should().NotBeNull();

                // Assert
                SettingsTestUtils.DeepEquals(value, expectedSetting).Should().BeTrue();
            }
        }
        public void CredentialsItem_Parsing_WithUsernamePasswordAndClearTextPassword_Throws()
        {
            // Arrange
            var nugetConfigPath = "NuGet.Config";
            var config          = @"
<configuration>
    <packageSourceCredentials>
        <NuGet.Org meta1='data1'>
            <add key='Username' value='username' />
            <add key='Password' value='password' />
            <add key='ClearTextPassword' value='clearTextPassword' />
        </NuGet.Org>
    </packageSourceCredentials>
</configuration>";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                SettingsTestUtils.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: {0} Path: '{1}'.", _moreThanOnePassword, Path.Combine(mockBaseDirectory, nugetConfigPath)));
            }
        }
Exemplo n.º 27
0
        public void SourceItem_CaseInsensitive_ParsedSuccessfully()
        {
            // Arrange
            var config = @"
<configuration>
    <section>
        <AdD key='key' value='val' />
    </section>
</configuration>";

            var expectedValue = new AddItem("key", "val");

            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("section");
                section.Should().NotBeNull();

                var children = section.Items.ToList();

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

                SettingsTestUtils.DeepEquals(children[0], expectedValue).Should().BeTrue();
            }
        }
Exemplo n.º 28
0
        public void PackagePatternItemParse_WithChildren_Throws()
        {
            // Arrange
            var config          = @"
<configuration>
    <packageSourceMapping>
        <packageSource key=""nuget.org"">
            <package pattern=""sadas"">
                <add key=""key"" value=""val"" />
            </package>
        </packageSource>
    </packageSourceMapping>
</configuration>";
            var nugetConfigPath = "NuGet.Config";

            using var mockBaseDirectory = TestDirectory.Create();
            SettingsTestUtils.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("Error parsing NuGet.Config. Element 'package' cannot have descendant elements. Path: '{0}'.", Path.Combine(mockBaseDirectory, nugetConfigPath)));
        }
Exemplo n.º 29
0
        public void SettingText_ParsedSuccessfully()
        {
            // 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");
            }
        }
Exemplo n.º 30
0
        public void UnknownItem_Merge_AddshNewChildren()
        {
            var originalSetting = new UnknownItem("Unknown",
                                                  attributes: null,
                                                  children: new List <SettingBase>()
            {
                new AddItem("key", "val")
            });

            var newSetting = new UnknownItem("Unknown",
                                             attributes: null,
                                             children: new List <SettingBase>()
            {
                new SettingText("New test")
            });

            originalSetting.Merge(newSetting);

            var expectedSetting = new UnknownItem("Unknown",
                                                  attributes: null,
                                                  children: new List <SettingBase>()
            {
                new AddItem("key", "val"), new SettingText("New test")
            });

            SettingsTestUtils.DeepEquals(originalSetting, expectedSetting).Should().BeTrue();
        }