public void UnknownItem_Update_RemovesMissingChildText() { // Arrange var nugetConfigPath = "NuGet.Config"; var config = @" <configuration> <Section> <Unknown old=""attr""> test <add key=""key"" value=""val"" /> </Unknown> </Section> </configuration>"; var updateSetting = new UnknownItem("Unknown", attributes: new Dictionary <string, string>() { { "old", "attr" } }, 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.Update(updateSetting); // Assert element.Children.Count.Should().Be(1); element.Children.First().Should().BeOfType <AddItem>(); } }
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()) { 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()); // Assert SettingsTestUtils.DeepEquals(element, expectedSetting).Should().BeTrue(); } }
public void CertificateItem_Update_UpdatesAllowUntrustedRootCorrectly() { // 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() as CertificateItem; var updatedItem = item.Clone() as CertificateItem; updatedItem.AllowUntrustedRoot = false; item.Update(updatedItem); SettingsTestUtils.DeepEquals(item, updatedItem).Should().BeTrue(); settingsFile.SaveToDisk(); // Assert var result = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <certificate fingerprint=""abcdefg"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </SectionName> </configuration>"; Assert.Equal(result.Replace("\r\n", "\n"), File.ReadAllText(Path.Combine(mockBaseDirectory, nugetConfigPath)).Replace("\r\n", "\n")); } }
public void SettingsFile_Constructor_InvalidXml_Throws() { // Arrange var config = @"boo>"; var nugetConfigPath = "NuGet.Config"; using (var mockBaseDirectory = TestDirectory.Create()) { SettingsTestUtils.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))); } }
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()) { SettingsTestUtils.CreateConfigurationFile(configFile, mockBaseDirectory, config); var settingsFile = new SettingsFile(mockBaseDirectory); // Act & Assert settingsFile.IsEmpty().Should().BeFalse(); } }
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()) { SettingsTestUtils.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++) { SettingsTestUtils.DeepEquals(children[i], expectedValues[i]).Should().BeTrue(); } } }
public void FileClientCertItem_WithClearTextPassword_ParsedCorrectly() { // Arrange var config = @" <configuration> <SectionName> <fileCert packageSource=""Foo"" path="".\certificate.pfx"" clearTextPassword=""..."" /> </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(); var items = section.Items.ToList(); items.Count.Should().Be(1); var fileClientCertItem = (FileClientCertItem)items[0]; fileClientCertItem.ElementName.Should().Be("fileCert"); fileClientCertItem.PackageSource.Should().Be("Foo"); fileClientCertItem.FilePath.Should().Be(@".\certificate.pfx"); fileClientCertItem.Password.Should().Be(@"..."); fileClientCertItem.IsPasswordIsClearText.Should().Be(true); var expectedFileClientCertItem = new FileClientCertItem("Foo", @".\certificate.pfx", "...", true, string.Empty); SettingsTestUtils.DeepEquals(fileClientCertItem, expectedFileClientCertItem).Should().BeTrue(); } }
public void Remove_WithOneConfig_RemovesElementOfInterest() { // Arrange using var mockBaseDirectory = TestDirectory.Create(); var configPath1 = Path.Combine(mockBaseDirectory, "NuGet.Config"); SettingsTestUtils.CreateConfigurationFile(configPath1, @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSourceMapping> <packageSource key=""nuget.org""> <package pattern=""stuff"" /> </packageSource> <packageSource key=""contoso""> <package pattern=""stuff2"" /> </packageSource> </packageSourceMapping> </configuration>"); var settings = Settings.LoadSettingsGivenConfigPaths(new string[] { configPath1 }); var sourceMappingProvider = new PackageSourceMappingProvider(settings); IReadOnlyList <PackageSourceMappingSourceItem> packageSourceMappingItems = sourceMappingProvider.GetPackageSourceMappingItems(); packageSourceMappingItems.Should().HaveCount(2); var contosoSourceItem = packageSourceMappingItems.First(e => e.Key.Equals("contoso")); // Act & Assert sourceMappingProvider.Remove(new PackageSourceMappingSourceItem[] { contosoSourceItem }); var result = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSourceMapping> <packageSource key=""nuget.org""> <package pattern=""stuff"" /> </packageSource> </packageSourceMapping> </configuration>"; result.Replace("\r\n", "\n") .Should().BeEquivalentTo( File.ReadAllText(configPath1).Replace("\r\n", "\n")); }
public void RepositoryItem_Clone_CopiesTheSameItem() { // Arrange var config = @" <configuration> <SectionName> <repository name=""repositoryName"" serviceIndex=""https://api.test/index/""> <certificate fingerprint=""abcdefg"" hashAlgorithm=""Sha256"" allowUntrustedRoot=""true"" /> <owners>test;text</owners> </repository> </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 RepositoryItem; clone.IsCopy().Should().BeTrue(); clone.Origin.Should().NotBeNull(); SettingsTestUtils.DeepEquals(clone, item).Should().BeTrue(); foreach (var cert in clone.Certificates) { cert.IsCopy().Should().BeTrue(); cert.Origin.Should().NotBeNull(); } } }
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()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var configFileHash = SettingsTestUtils.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 = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath)); updatedFileHash.Should().BeEquivalentTo(configFileHash); } }
public void TrustedSigner_WithAuthorsAndRepositories_ParsedCorrectly() { // Arrange var config = @" <configuration> <SectionName> <repository name=""repositoryName"" serviceIndex=""https://api.test/index/""> <certificate fingerprint=""abcdefg"" hashAlgorithm=""Sha256"" allowUntrustedRoot=""true"" /> <owners>test;text</owners> </repository> <author name=""authorName""> <certificate fingerprint=""abcdefg"" hashAlgorithm=""Sha256"" allowUntrustedRoot=""true"" /> </author> </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(); var items = section.Items.ToList(); items.Count.Should().Be(2); var repositoryitem = items[0] as RepositoryItem; var expectedRepositoryItem = new RepositoryItem("repositoryName", "https://api.test/index/", "test;text", new CertificateItem("abcdefg", Common.HashAlgorithmName.SHA256, allowUntrustedRoot: true)); SettingsTestUtils.DeepEquals(repositoryitem, expectedRepositoryItem).Should().BeTrue(); var authorItem = items[1] as AuthorItem; var expectedAuthorItem = new AuthorItem("authorName", new CertificateItem("abcdefg", Common.HashAlgorithmName.SHA256, allowUntrustedRoot: true)); SettingsTestUtils.DeepEquals(authorItem, expectedAuthorItem).Should().BeTrue(); } }
public void Remove_WithMultipleConfigs_ChangesOriginConfig() { // Arrange using var mockBaseDirectory = TestDirectory.Create(); var configPath1 = Path.Combine(mockBaseDirectory, "NuGet.Config"); SettingsTestUtils.CreateConfigurationFile(configPath1, @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSourceMapping> <packageSource key=""nuget.org""> <package pattern=""stuff"" /> </packageSource> </packageSourceMapping> </configuration>"); var configPath2 = Path.Combine(mockBaseDirectory, "NuGet.Config.2"); SettingsTestUtils.CreateConfigurationFile(configPath2, @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSourceMapping> <packageSource key=""contoso""> <package pattern=""stuff2"" /> </packageSource> </packageSourceMapping> </configuration>"); var settings = Settings.LoadSettingsGivenConfigPaths(new string[] { configPath1, configPath2 }); // Act & Assert var sourceMappingProvider = new PackageSourceMappingProvider(settings); IReadOnlyList <PackageSourceMappingSourceItem> packageSourceMappingItems = sourceMappingProvider.GetPackageSourceMappingItems(); sourceMappingProvider.Remove(new PackageSourceMappingSourceItem[] { packageSourceMappingItems.Last() }); var result = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> </configuration>"; result.Replace("\r\n", "\n") .Should().BeEquivalentTo( File.ReadAllText(configPath1).Replace("\r\n", "\n")); }
public void GetPackageSourceMappingItems_WithMultipleConfigs_RespectsClearTag() { // Arrange using var mockBaseDirectory = TestDirectory.Create(); var configPath1 = Path.Combine(mockBaseDirectory, "NuGet.Config"); SettingsTestUtils.CreateConfigurationFile(configPath1, @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSourceMapping> <clear /> <packageSource key=""nuget.org""> <package pattern=""stuff"" /> </packageSource> </packageSourceMapping> </configuration>"); var configPath2 = Path.Combine(mockBaseDirectory, "NuGet.Config.2"); SettingsTestUtils.CreateConfigurationFile(configPath2, @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSourceMapping> <packageSource key=""contoso""> <package pattern=""stuff2"" /> </packageSource> </packageSourceMapping> </configuration>"); var settings = Settings.LoadSettingsGivenConfigPaths(new string[] { configPath1, configPath2 }); // Act & Assert var sourceMappingProvider = new PackageSourceMappingProvider(settings); IReadOnlyList <PackageSourceMappingSourceItem> packageSourceMappingItems = sourceMappingProvider.GetPackageSourceMappingItems(); packageSourceMappingItems.Should().HaveCount(1); var contosoSourceItem = packageSourceMappingItems.First(); contosoSourceItem.Key.Should().Be("nuget.org"); contosoSourceItem.Patterns.Should().HaveCount(1); contosoSourceItem.Patterns.First().Pattern.Should().Be("stuff"); }
public void PackageSourceMappingSourceItemParse_WithoutPattern_Throws() { // Arrange var config = @" <configuration> <packageSourceMapping> <packageSource key=""nuget.org""> </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("Package source '{0}' must have at least one package pattern. Path: '{1}'", "nuget.org", Path.Combine(mockBaseDirectory, nugetConfigPath))); }
public void SettingsFile_Constructor_MachineWideConfigsAreReadOnly(bool isMachineWide, bool isReadOnlyInput, bool expected) { // Arrange var nugetConfigPath = "NuGet.Config"; var config = @" <configuration> <Section> <add key='key0' value='value0' /> </Section> </configuration>"; using (var mockBaseDirectory = TestDirectory.Create()) { // Set-up and Act SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var settingsFile = new SettingsFile(mockBaseDirectory, nugetConfigPath, isMachineWide: isMachineWide, isReadOnly: isReadOnlyInput); // Assert settingsFile.IsReadOnly.Should().Be(expected); settingsFile.IsMachineWide.Should().Be(isMachineWide); } }
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()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var configFileHash = SettingsTestUtils.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 = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath)); updatedFileHash.Should().BeEquivalentTo(configFileHash); } }
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>"; SettingsTestUtils.CreateConfigurationFile(configFile, mockBaseDirectory, config); var settingsFile = new SettingsFile(mockBaseDirectory); // Act settingsFile.AddOrUpdate("NewSectionName", new AddItem("key", "value")); settingsFile.SaveToDisk(); // Assert var result = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""key"" value=""value"" /> </SectionName> <NewSectionName> <add key=""key"" value=""value"" /> </NewSectionName> </configuration>"); SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, configFile))).Should().Be(result); var section = settingsFile.GetSection("NewSectionName"); section.Should().NotBeNull(); section.Items.Count.Should().Be(1); } }
public void SettingsFile_AddOrUpdate_WhenItemExistsInSection_OverrideItem() { // Arrange var configFile = "NuGet.Config"; using (var mockBaseDirectory = TestDirectory.Create()) { var config = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""key"" value=""value"" /> </SectionName> </configuration>"; SettingsTestUtils.CreateConfigurationFile(configFile, mockBaseDirectory, config); var settingsFile = new SettingsFile(mockBaseDirectory); // Act settingsFile.AddOrUpdate("SectionName", new AddItem("key", "NewValue")); settingsFile.SaveToDisk(); // Assert var result = SettingsTestUtils.RemoveWhitespace(@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <SectionName> <add key=""key"" value=""NewValue"" /> </SectionName> </configuration>"); SettingsTestUtils.RemoveWhitespace(File.ReadAllText(Path.Combine(mockBaseDirectory, configFile))).Should().Be(result); var section = settingsFile.GetSection("SectionName"); section.Should().NotBeNull(); var item = section.GetFirstItemWithAttribute <AddItem>("key", "key"); item.Should().NotBeNull(); item.Value.Should().Be("NewValue"); } }
public void 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 = TestDirectory.Create()) { var testFolder = Path.Combine(mockBaseDirectory, "test"); var srcFolder = Path.Combine(mockBaseDirectory, "src"); var globalFolder = Path.Combine(mockBaseDirectory, "global"); SettingsTestUtils.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); } }
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()) { 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.Items.Count.Should().Be(1); var deletedChild = section.GetFirstItemWithAttribute <AddItem>("key", "key0"); deletedChild.Should().BeNull(); } }
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()) { 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: Missing required attribute 'key' in element 'add'. Path: '{0}'.", Path.Combine(mockBaseDirectory, nugetConfigPath))); } }
public void CertificateItem_WithoutAllowUntrustedRoot_Throws() { // Arrange var config = @" <configuration> <SectionName> <certificate fingerprint=""abcdefg"" hashAlgorithm=""SHA256"" /> </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("Unable to parse config file because: Missing required attribute 'allowUntrustedRoot' in element 'certificate'. Path: '{0}'.", Path.Combine(mockBaseDirectory, nugetConfigPath))); } }
public void AuthorItem_WithoutCertificates_Throws() { // Arrange var config = @" <configuration> <SectionName> <author name=""authorname"" /> </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("Unable to parse config file because: A trusted signer entry must have at least one certificate entry. Path: '{0}'.", Path.Combine(mockBaseDirectory, nugetConfigPath))); } }
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()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var settings = new Settings(mockBaseDirectory); // Act var result = SettingsUtility.GetDecryptedValueForAddItem(settings, "SectionName", "NoKeyByThatName"); // Assert result.Should().BeNull(); } }
public void PackageSourcePatternItemParse_WithoutKey_Throws() { // Arrange var config = @" <configuration> <packageSourceMapping> <packageSource id=""nuget.org""> <package pattern=""sadas"" /> </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("Unable to parse config file because: Missing required attribute 'key' in element 'packageSource'. Path: '{0}'.", Path.Combine(mockBaseDirectory, nugetConfigPath))); }
public void SettingsFile_PriorityIsPreservedInSettings() { // Arrange var configFile = "NuGet.Config"; using (var mockBaseDirectory = TestDirectory.Create()) using (var mockSubDirectory = TestDirectory.Create(mockBaseDirectory)) using (var mockSubSubDirectory = TestDirectory.Create(mockSubDirectory)) { SettingsTestUtils.CreateConfigurationFile(configFile, mockBaseDirectory, @"<configuration></configuration>"); SettingsTestUtils.CreateConfigurationFile(configFile, mockSubDirectory, @"<configuration></configuration>"); SettingsTestUtils.CreateConfigurationFile(configFile, mockSubSubDirectory, @"<configuration></configuration>"); var baseSettingsFile = new SettingsFile(mockBaseDirectory); var subSettingsFile = new SettingsFile(mockSubDirectory); var subSubSettingsFile = new SettingsFile(mockSubSubDirectory); // Act & Assert baseSettingsFile.Should().NotBeNull(); subSettingsFile.Should().NotBeNull(); subSubSettingsFile.Should().NotBeNull(); var settings = new Settings(new List <SettingsFile>() { subSubSettingsFile, subSettingsFile, baseSettingsFile }); var priority = settings.Priority.GetEnumerator(); Assert.True(priority.MoveNext()); priority.Current.Should().BeSameAs(subSubSettingsFile); Assert.True(priority.MoveNext()); priority.Current.Should().BeSameAs(subSettingsFile); Assert.True(priority.MoveNext()); priority.Current.Should().BeSameAs(baseSettingsFile); Assert.False(priority.MoveNext()); } }
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()) { SettingsTestUtils.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>(); SettingsTestUtils.DeepEquals(children[1], expectedItem).Should().BeTrue(); } }
public void GetPackageSourceNamespaces_WithMultipleConfigs_ReturnsClosestNamespaces() { // 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> </packageNamespaces> </configuration>"); var configPath2 = Path.Combine(mockBaseDirectory, "NuGet.Config.2"); SettingsTestUtils.CreateConfigurationFile(configPath2, @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageNamespaces> <packageSource key=""nuget.org""> <namespace id=""stuff2"" /> </packageSource> </packageNamespaces> </configuration>"); var settings = Settings.LoadSettingsGivenConfigPaths(new string[] { configPath1, configPath2 }); // Act & Assert var namespaceProvider = new PackageNamespacesProvider(settings); IReadOnlyList <PackageNamespacesSourceItem> packageSourceNamespaces = namespaceProvider.GetPackageSourceNamespaces(); packageSourceNamespaces.Should().HaveCount(1); var packageSourceNamespace = packageSourceNamespaces.First(); packageSourceNamespace.Key.Should().Be("nuget.org"); packageSourceNamespace.Namespaces.Should().HaveCount(1); packageSourceNamespace.Namespaces.First().Id.Should().Be("stuff"); }
public void SettingSection_Remove_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(); var child = section.GetFirstItemWithAttribute <AddItem>("key", "key0"); child.Should().NotBeNull(); var ex = Record.Exception(() => settingsFile.Remove("Section", child)); ex.Should().NotBeNull(); ex.Should().BeOfType <InvalidOperationException>(); ex.Message.Should().Be(Resources.CannotUpdateReadOnlyConfig); settingsFile.SaveToDisk(); var updatedFileHash = SettingsTestUtils.GetFileHash(Path.Combine(mockBaseDirectory, nugetConfigPath)); updatedFileHash.Should().BeEquivalentTo(configFileHash); } }
public void Update_WithoutAnyPackagePattern_Throws() { // Arrange var config = @" <configuration> <packageSourceMapping> <packageSource key=""nuget.org""> <package pattern=""first"" /> </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); settingsFile.TryGetSection("packageSourceMapping", out var section).Should().BeTrue(); section.Should().NotBeNull(); section.Items.Count.Should().Be(1); var packageSourceMappingSourceItem = section.Items.First() as PackageSourceMappingSourceItem; packageSourceMappingSourceItem.Patterns.Should().HaveCount(1); var clone = packageSourceMappingSourceItem.Clone() as PackageSourceMappingSourceItem; clone.Patterns.Clear(); var ex = Record.Exception(() => packageSourceMappingSourceItem.Update(clone)); ex.Should().NotBeNull(); ex.Should().BeOfType <InvalidOperationException>(); ex.Message.Should().Be(string.Format("Package source 'nuget.org' must have at least one package pattern.")); }