diff --git a/src/NuGet.Clients/NuGet.CommandLine/PackageSourceBuilder.cs b/src/NuGet.Clients/NuGet.CommandLine/PackageSourceBuilder.cs index 703c0cd2a31..bd9abe0c8e2 100644 --- a/src/NuGet.Clients/NuGet.CommandLine/PackageSourceBuilder.cs +++ b/src/NuGet.Clients/NuGet.CommandLine/PackageSourceBuilder.cs @@ -7,13 +7,7 @@ internal static class PackageSourceBuilder { internal static Configuration.PackageSourceProvider CreateSourceProvider(Configuration.ISettings settings) { - var defaultPackageSource = new Configuration.PackageSource( - NuGetConstants.V3FeedUrl, NuGetConstants.FeedName); - - var packageSourceProvider = new Configuration.PackageSourceProvider( - settings, - new[] { defaultPackageSource }); - return packageSourceProvider; + return new Configuration.PackageSourceProvider(settings); } } } diff --git a/src/NuGet.Core/NuGet.Configuration/PackageSource/NuGetConstants.cs b/src/NuGet.Core/NuGet.Configuration/PackageSource/NuGetConstants.cs index 940ae50c6f3..c63fba7bb64 100644 --- a/src/NuGet.Core/NuGet.Configuration/PackageSource/NuGetConstants.cs +++ b/src/NuGet.Core/NuGet.Configuration/PackageSource/NuGetConstants.cs @@ -19,5 +19,12 @@ public static class NuGetConstants public static readonly string FeedName = "nuget.org"; + + public static readonly string DefaultConfigContent = @" + + + + +"; } } diff --git a/src/NuGet.Core/NuGet.Configuration/PackageSource/PackageSourceProvider.cs b/src/NuGet.Core/NuGet.Configuration/PackageSource/PackageSourceProvider.cs index 67c111e6936..87de3c583b9 100644 --- a/src/NuGet.Core/NuGet.Configuration/PackageSource/PackageSourceProvider.cs +++ b/src/NuGet.Core/NuGet.Configuration/PackageSource/PackageSourceProvider.cs @@ -15,45 +15,18 @@ public class PackageSourceProvider : IPackageSourceProvider private const int MaxSupportedProtocolVersion = 3; private ISettings Settings { get; set; } - private readonly IEnumerable _providerDefaultPrimarySources; - private readonly IEnumerable _providerDefaultSecondarySources; private readonly IDictionary _migratePackageSources; private readonly IEnumerable _configurationDefaultSources; public PackageSourceProvider(ISettings settings) - : this(settings, providerDefaultPrimarySources: null, providerDefaultSecondarySources: null) - { - } - - /// - /// Creates a new PackageSourceProvider instance. - /// - /// Specifies the settings file to use to read package sources. - /// The primary default sources you would like to use - public PackageSourceProvider(ISettings settings, IEnumerable providerDefaultPrimarySources) - : this(settings, providerDefaultPrimarySources, providerDefaultSecondarySources: null, migratePackageSources: null) - { - } - - /// - /// Creates a new PackageSourceProvider instance. - /// - /// Specifies the settings file to use to read package sources. - /// The primary default sources you would like to use - /// The secondary default sources you would like to use - public PackageSourceProvider(ISettings settings, IEnumerable providerDefaultPrimarySources, IEnumerable providerDefaultSecondarySources) - : this(settings, providerDefaultPrimarySources, providerDefaultSecondarySources, migratePackageSources: null) + : this(settings, migratePackageSources: null) { } public PackageSourceProvider( ISettings settings, - IEnumerable providerDefaultPrimarySources, - IEnumerable providerDefaultSecondarySources, IDictionary migratePackageSources) : this(settings, - providerDefaultPrimarySources, - providerDefaultSecondarySources, migratePackageSources, ConfigurationDefaults.Instance.DefaultPackageSources) { @@ -61,8 +34,6 @@ public PackageSourceProvider( public PackageSourceProvider( ISettings settings, - IEnumerable providerDefaultPrimarySources, - IEnumerable providerDefaultSecondarySources, IDictionary migratePackageSources, IEnumerable configurationDefaultSources ) @@ -73,8 +44,6 @@ IEnumerable configurationDefaultSources } Settings = settings; Settings.SettingsChanged += (_, __) => { OnPackageSourcesChanged(); }; - _providerDefaultPrimarySources = providerDefaultPrimarySources ?? Enumerable.Empty(); - _providerDefaultSecondarySources = providerDefaultSecondarySources ?? Enumerable.Empty(); _migratePackageSources = migratePackageSources; _configurationDefaultSources = LoadConfigurationDefaultSources(configurationDefaultSources); } @@ -160,13 +129,6 @@ public IEnumerable LoadPackageSources() MigrateSources(loadedPackageSources); } - SetDefaultPackageSources(loadedPackageSources); - - foreach (var source in loadedPackageSources) - { - source.Description = GetDescription(source); - } - return loadedPackageSources; } @@ -231,20 +193,6 @@ private static int AddOrUpdateIndexedSource( return packageIndex; } - // Gets the description of the source if it matches a default source. - // Returns null if it does not match a default source - private string GetDescription(PackageSource source) - { - var matchingSource = _providerDefaultPrimarySources.FirstOrDefault( - s => StringComparer.OrdinalIgnoreCase.Equals(s.Source, source.Source)); - if (matchingSource != null) - { - return matchingSource.Description; - } - - return null; - } - private PackageSourceCredential ReadCredential(string sourceName) { var environmentCredentials = ReadCredentialFromEnvironment(sourceName); @@ -332,113 +280,6 @@ private void MigrateSources(List loadedPackageSources) } } - private void SetDefaultPackageSources(List loadedPackageSources) - { - var defaultPackageSourcesToBeAdded = new List(); - - if (_configurationDefaultSources == null - || !_configurationDefaultSources.Any()) - { - // Update provider default sources and use provider default sources since _configurationDefaultSources is empty - UpdateProviderDefaultSources(loadedPackageSources); - defaultPackageSourcesToBeAdded = GetPackageSourcesToBeAdded( - loadedPackageSources, - Enumerable.Concat(_providerDefaultPrimarySources, _providerDefaultSecondarySources)); - } - else - { - defaultPackageSourcesToBeAdded = GetPackageSourcesToBeAdded(loadedPackageSources, _configurationDefaultSources); - } - - var defaultSourcesInsertIndex = loadedPackageSources.FindIndex(source => source.IsMachineWide); - if (defaultSourcesInsertIndex == -1) - { - defaultSourcesInsertIndex = loadedPackageSources.Count; - } - - // Default package sources go ahead of machine wide sources - loadedPackageSources.InsertRange(defaultSourcesInsertIndex, defaultPackageSourcesToBeAdded); - } - - private List GetPackageSourcesToBeAdded(List loadedPackageSources, IEnumerable allDefaultPackageSources) - { - // There are 4 different cases to consider for primary/ secondary package sources - // Case 1. primary/ secondary Package Source is already present matching both feed source and the feed name. Set IsOfficial to true - // Case 2. primary/ secondary Package Source is already present matching feed source but with a different feed name. DO NOTHING - // Case 3. primary/ secondary Package Source is not present, but there is another feed source with the same feed name. Override that feed entirely - // Case 4. primary/ secondary Package Source is not present, simply, add it. In addition, if Primary is getting added - // for the first time, promote Primary to Enabled and demote secondary to disabled, if it is already enabled - - var defaultPackageSourcesToBeAdded = new List(); - foreach (var packageSource in allDefaultPackageSources) - { - var existingIndex = defaultPackageSourcesToBeAdded.FindIndex( - source => string.Equals(source.Name, packageSource.Name, StringComparison.OrdinalIgnoreCase)); - - // Ignore sources with the same name but lower protocol versions that are already added. - if (existingIndex != -1) - { - var existingSource = defaultPackageSourcesToBeAdded[existingIndex]; - if (existingSource.ProtocolVersion < packageSource.ProtocolVersion) - { - defaultPackageSourcesToBeAdded.RemoveAt(existingIndex); - defaultPackageSourcesToBeAdded.Insert(existingIndex, packageSource); - } - continue; - } - - var sourceMatchingIndex = loadedPackageSources.FindIndex(p => p.Source.Equals(packageSource.Source, StringComparison.OrdinalIgnoreCase)); - if (sourceMatchingIndex != -1) - { - if (loadedPackageSources[sourceMatchingIndex].Name.Equals(packageSource.Name, StringComparison.CurrentCultureIgnoreCase)) - { - // Case 1: Both the feed name and source matches. DO NOTHING except set IsOfficial to true - loadedPackageSources[sourceMatchingIndex].IsOfficial = true; - } - else - { - // Case 2: Only feed source matches but name is different. DO NOTHING - } - } - else - { - var nameMatchingIndex = loadedPackageSources.FindIndex(p => p.Name.Equals(packageSource.Name, StringComparison.CurrentCultureIgnoreCase)); - if (nameMatchingIndex != -1) - { - // Case 3: Only feed name matches but source is different. Override it entirely - //DO NOTHING - } - else - { - // Case 4: Default package source is not present. Add it to the temp list. Later, the temp listed is inserted above the machine wide sources - defaultPackageSourcesToBeAdded.Add(packageSource); - packageSource.IsOfficial = true; - } - } - } - return defaultPackageSourcesToBeAdded; - } - - private void UpdateProviderDefaultSources(List loadedSources) - { - // If there are NO other non-machine wide sources, providerDefaultPrimarySource should be enabled - var areProviderDefaultSourcesEnabled = loadedSources.Count == 0 || loadedSources.Where(p => !p.IsMachineWide).Count() == 0 - || loadedSources.Where(p => p.IsEnabled).Count() == 0; - - foreach (var packageSource in _providerDefaultPrimarySources) - { - packageSource.IsEnabled = areProviderDefaultSourcesEnabled; - packageSource.IsOfficial = true; - } - - //Mark secondary sources as official but not enable them - foreach (var secondaryPackageSource in _providerDefaultSecondarySources) - { - secondaryPackageSource.IsEnabled = areProviderDefaultSourcesEnabled; - secondaryPackageSource.IsOfficial = true; - } - } - public void SavePackageSources(IEnumerable sources) { // clear the old values diff --git a/src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs b/src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs index ec3109474b3..1f22ce8462b 100644 --- a/src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs +++ b/src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs @@ -75,7 +75,7 @@ public Settings(string root, string fileName, bool isMachineWideSettings) Root = root; FileName = fileName; XDocument config = null; - ExecuteSynchronized(() => config = XmlUtility.GetOrCreateDocument("configuration", ConfigFilePath)); + ExecuteSynchronized(() => config = XmlUtility.GetOrCreateDocument(CreateDefaultConfig(), ConfigFilePath)); ConfigXDocument = config; IsMachineWideSettings = isMachineWideSettings; } @@ -155,7 +155,8 @@ public static ISettings LoadDefaultSettings( root, configFileName, machineWideSettings, - loadAppDataSettings: true); + loadAppDataSettings: true, + useTestingGlobalPath : false); } /// @@ -165,7 +166,8 @@ public static ISettings LoadDefaultSettings( string root, string configFileName, IMachineWideSettings machineWideSettings, - bool loadAppDataSettings) + bool loadAppDataSettings, + bool useTestingGlobalPath) { { // Walk up the tree to find a config file; also look in .nuget subdirectories @@ -183,7 +185,7 @@ public static ISettings LoadDefaultSettings( if (loadAppDataSettings) { - LoadUserSpecificSettings(validSettingFiles, root, configFileName, machineWideSettings); + LoadUserSpecificSettings(validSettingFiles, root, configFileName, machineWideSettings, useTestingGlobalPath); } if (machineWideSettings != null) @@ -224,7 +226,8 @@ private static void LoadUserSpecificSettings( List validSettingFiles, string root, string configFileName, - IMachineWideSettings machineWideSettings + IMachineWideSettings machineWideSettings, + bool useTestingGlobalPath ) { if (root == null) @@ -238,8 +241,16 @@ IMachineWideSettings machineWideSettings Settings appDataSettings = null; if (configFileName == null) { - var userSettingsDir = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); - var defaultSettingsFilePath = Path.Combine(userSettingsDir, DefaultSettingsFileName); + var defaultSettingsFilePath = String.Empty; + if (useTestingGlobalPath) + { + defaultSettingsFilePath = Path.Combine(root, "TestingGlobalPath"); + } + else + { + var userSettingsDir = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); + defaultSettingsFilePath = Path.Combine(userSettingsDir, DefaultSettingsFileName); + } if (!File.Exists(defaultSettingsFilePath) && machineWideSettings != null) { @@ -1083,5 +1094,15 @@ private static bool ConfigPathComparer(string path1, string path2) return path1.Equals(path2); } } + + private static XDocument CreateDefaultConfig() + { + return new XDocument(new XElement("configuration", + new XElement(ConfigurationConstants.PackageSources, + new XElement("add", + new XAttribute(ConfigurationConstants.KeyAttribute, NuGetConstants.FeedName), + new XAttribute(ConfigurationConstants.ValueAttribute, NuGetConstants.V3FeedUrl), + new XAttribute(ConfigurationConstants.ProtocolVersionAttribute, "3"))))); + } } } diff --git a/src/NuGet.Core/NuGet.Configuration/Utility/XmlUtility.cs b/src/NuGet.Core/NuGet.Configuration/Utility/XmlUtility.cs index 2912967af33..79d0e2fae53 100644 --- a/src/NuGet.Core/NuGet.Configuration/Utility/XmlUtility.cs +++ b/src/NuGet.Core/NuGet.Configuration/Utility/XmlUtility.cs @@ -11,7 +11,7 @@ namespace NuGet.Configuration { internal static class XmlUtility { - internal static XDocument GetOrCreateDocument(XName rootName, string fullPath) + internal static XDocument GetOrCreateDocument(XDocument content, string fullPath) { if (File.Exists(fullPath)) { @@ -21,18 +21,17 @@ internal static XDocument GetOrCreateDocument(XName rootName, string fullPath) } catch (FileNotFoundException) { - return CreateDocument(rootName, fullPath); + return CreateDocument(content, fullPath); } } - return CreateDocument(rootName, fullPath); + return CreateDocument(content, fullPath); } - private static XDocument CreateDocument(XName rootName, string fullPath) + private static XDocument CreateDocument(XDocument content, string fullPath) { - var document = new XDocument(new XElement(rootName)); // Add it to the file system - FileSystemUtility.AddFile(fullPath, document.Save); - return document; + FileSystemUtility.AddFile(fullPath, content.Save); + return content; } private static XDocument GetDocument(string fullPath) diff --git a/src/NuGet.Core/NuGet.Protocol.Core.Types/Repository.cs b/src/NuGet.Core/NuGet.Protocol.Core.Types/Repository.cs index c78939338a5..9a8a781eac9 100644 --- a/src/NuGet.Core/NuGet.Protocol.Core.Types/Repository.cs +++ b/src/NuGet.Core/NuGet.Protocol.Core.Types/Repository.cs @@ -47,34 +47,6 @@ public static ISourceRepositoryProvider CreateProvider(IEnumerable - /// Create a source provider for the given sources - /// - public static ISourceRepositoryProvider CreateProvider(IEnumerable resourceProviders, IEnumerable sources) - { - return CreateProvider(resourceProviders, sources.Select(s => new PackageSource(s))); - } - - /// - /// Create a source provider for the given sources and with the extra providers. - /// - public static ISourceRepositoryProvider CreateProvider(IEnumerable resourceProviders, IEnumerable sources) - { - if (sources == null) - { - throw new ArgumentNullException("sources"); - } - - if (resourceProviders == null) - { - throw new ArgumentNullException("resourceProviders"); - } - - var sourceProvider = new PackageSourceProvider(NullSettings.Instance, sources, Enumerable.Empty()); - - return new SourceRepositoryProvider(sourceProvider, CreateLazy(resourceProviders)); - } - /// /// Create a SourceRepository /// diff --git a/src/NuGet.Core/NuGet.Protocol.VisualStudio/VSSourceRepositoryProvider.cs b/src/NuGet.Core/NuGet.Protocol.VisualStudio/VSSourceRepositoryProvider.cs index f1c700c7eb4..f724ad2857f 100644 --- a/src/NuGet.Core/NuGet.Protocol.VisualStudio/VSSourceRepositoryProvider.cs +++ b/src/NuGet.Core/NuGet.Protocol.VisualStudio/VSSourceRepositoryProvider.cs @@ -16,24 +16,7 @@ namespace NuGet.Protocol.VisualStudio [Export(typeof(ISourceRepositoryProvider))] public sealed class ExtensibleSourceRepositoryProvider : ISourceRepositoryProvider { - private static Configuration.PackageSource[] DefaultPrimarySources = new[] - { - new Configuration.PackageSource(NuGetConstants.V3FeedUrl, NuGetConstants.FeedName, isEnabled: true, isOfficial: true) - { - Description = Strings.v3sourceDescription, - ProtocolVersion = 3 - } - }; - - private static Configuration.PackageSource[] DefaultSecondarySources = new[] - { - new Configuration.PackageSource(NuGetConstants.V2FeedUrl, NuGetConstants.FeedName, isEnabled: true, isOfficial: true) - { - Description = Strings.v2sourceDescription, - ProtocolVersion = 2 - } - }; - + // TODO: add support for reloading sources when changes occur private readonly Configuration.IPackageSourceProvider _packageSourceProvider; private IEnumerable> _resourceProviders; @@ -51,7 +34,7 @@ public ExtensibleSourceRepositoryProvider() /// [ImportingConstructor] public ExtensibleSourceRepositoryProvider([ImportMany] IEnumerable> resourceProviders, [Import] Configuration.ISettings settings) - : this(new Configuration.PackageSourceProvider(settings, DefaultPrimarySources, DefaultSecondarySources, migratePackageSources: null), resourceProviders) + : this(new Configuration.PackageSourceProvider(settings, migratePackageSources: null), resourceProviders) { } diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs index 80db4188237..de3acb2be4f 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs @@ -1156,6 +1156,7 @@ public void TestInstallWhenNoFeedAvailable() { // Create an empty config file and pass it as -ConfigFile switch. // This imitates the scenario where there is a machine without a default nuget.config under %APPDATA% + // In this case, nuget will not create default nuget.config for user. var config = string.Format( @" @@ -1183,7 +1184,7 @@ public void TestInstallWhenNoFeedAvailable() "Newtonsoft.Json.7.0.1", "Newtonsoft.Json.7.0.1.nupkg"); - Assert.True(File.Exists(expectedPath), "nuget.exe did not install Newtonsoft.Json.7.0.1"); + Assert.False(File.Exists(expectedPath), "nuget.exe installed Newtonsoft.Json.7.0.1"); } } } diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs index 18f7bab157f..12168aea275 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs @@ -1492,6 +1492,7 @@ public void RestoreCommand_NoFeedAvailable() { // Create an empty config file and pass it as -ConfigFile switch. // This imitates the scenario where there is a machine without a default nuget.config under %APPDATA% + // In this case, nuget will not create default nuget.config for user. var config = string.Format( @" @@ -1527,13 +1528,12 @@ string[] args Environment.SetEnvironmentVariable("PATH", path); // Assert - Assert.Equal(0, r.Item1); var expectedPath = Path.Combine( randomTestFolder, "Newtonsoft.Json.7.0.1", "Newtonsoft.Json.7.0.1.nupkg"); - Assert.True(File.Exists(expectedPath)); + Assert.False(File.Exists(expectedPath)); } } diff --git a/test/NuGet.Core.Tests/NuGet.Configuration.Test/PackageSourceProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Configuration.Test/PackageSourceProviderTests.cs index 06c997320b7..cbdc2778831 100644 --- a/test/NuGet.Core.Tests/NuGet.Configuration.Test/PackageSourceProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Configuration.Test/PackageSourceProviderTests.cs @@ -16,292 +16,6 @@ namespace NuGet.Configuration.Test { public class PackageSourceProviderTests { - [Fact] - public void PrimarySourceIsAddedWhenNotPresent() - { - // Act - NullSettings settings = new NullSettings(); - List primary = new List(); - PackageSource item = new PackageSource(NuGetConstants.V3FeedUrl, NuGetConstants.FeedName); - primary.Add(item); - - List secondary = new List(); - PackageSource item2 = new PackageSource(NuGetConstants.V2FeedUrl, NuGetConstants.FeedName, false); - secondary.Add(item2); - - PackageSourceProvider psp = new PackageSourceProvider(settings, primary, secondary); - - // Act - var sources = psp.LoadPackageSources(); - - // Assert - //Primary IsEnabled = true, IsOfficial = true (Added for the first time) - var actual = Assert.Single(sources); - Assert.Equal(item.Name, actual.Name); - Assert.Equal(item.Source, actual.Source); - Assert.True(actual.IsEnabled); - Assert.True(actual.IsOfficial); - } - - [Fact] - public void SecondarySourceIsPreferredOverPrimaryIfItHasAHigherProtocolVersion() - { - // Act - var settings = new NullSettings(); - var primarySource = new PackageSource(NuGetConstants.V3FeedUrl, NuGetConstants.FeedName); - - var secondarySource = new PackageSource(NuGetConstants.V2FeedUrl, NuGetConstants.FeedName); - secondarySource.ProtocolVersion = 3; - - var packageSourceProvider = new PackageSourceProvider(settings, new[] { primarySource }, new[] { secondarySource }); - - // Act - var sources = packageSourceProvider.LoadPackageSources(); - - // Assert - //Primary IsEnabled = true, IsOfficial = true (Added for the first time) - var actual = Assert.Single(sources); - Assert.Equal(secondarySource.Name, actual.Name); - Assert.Equal(secondarySource.Source, actual.Source); - Assert.True(actual.IsEnabled); - Assert.True(actual.IsOfficial); - } - - [Fact] - public void WhenPrimarySourcesAreRepeatedWithTheSameProtocolVersion_FIrstSourceIsPicked() - { - // Act - var settings = new NullSettings(); - var primarySource1 = new PackageSource("Source1", "FeedName"); - var primarySource2 = new PackageSource("Source2", "FeedName"); - var primarySource3 = new PackageSource("Source3", "FeedName") - { - ProtocolVersion = 3 - }; - - var primarySource4 = new PackageSource("Source4", "FeedName") - { - ProtocolVersion = 3 - }; - var primarySources = new[] - { - primarySource1, primarySource2, primarySource3, primarySource4 - }; - - var secondarySource = new PackageSource(NuGetConstants.V2FeedUrl, "FeedName"); - secondarySource.ProtocolVersion = 3; - - var packageSourceProvider = new PackageSourceProvider(settings, primarySources, new[] { secondarySource }); - - // Act - var sources = packageSourceProvider.LoadPackageSources(); - - // Assert - //Primary IsEnabled = true, IsOfficial = true (Added for the first time) - var actual = Assert.Single(sources); - Assert.Equal(primarySource3.Name, actual.Name); - Assert.Equal(primarySource3.Source, actual.Source); - Assert.True(actual.IsOfficial); - } - - public void PrimaryURLIsForcedWhenPrimaryNameHasAnotherFeed() - { - // Act - //Create nuget.config that has Primary name used for a different feed - using (var nugetConfigFileFolder = TestFileSystemUtility.CreateRandomTestFolder()) - { - var nugetConfigFilePath = Path.Combine(nugetConfigFileFolder, "nuget.config"); - - var randomURL = "https://www.somerandomURL.com/"; - var enabledReplacement = @""; - var disabledReplacement = string.Empty; - File.WriteAllText(nugetConfigFilePath, CreateNuGetConfigContent(enabledReplacement, disabledReplacement)); - - Settings settings = new Settings(nugetConfigFileFolder, "nuget.config"); - PackageSourceProvider before = new PackageSourceProvider(settings); - VerifyPackageSource(before, 1, new string[] { NuGetConstants.FeedName }, - new string[] { randomURL }, - new bool[] { true }, new bool[] { false }); - - List primary = new List(); - PackageSource item = new PackageSource(NuGetConstants.V3FeedUrl, NuGetConstants.FeedName); - primary.Add(item); - - PackageSourceProvider after = new PackageSourceProvider(settings, primary, null); - - // Assert - //Primary Name already exists in nuget.config but with different URL - //It gets overwritten by primary package source (which is enabled above while creating it) - //IsEnabled = true, IsOfficial = true - VerifyPackageSource(after, 1, new string[] { NuGetConstants.FeedName }, - new string[] { NuGetConstants.V3FeedUrl }, - new bool[] { false }, new bool[] { true }); - - } - } - - public void SecondaryURLIsForcedWhenSecondaryNameHasAnotherFeed() - { - // Act - //Create nuget.config that has Secondary name used for a different feed - using (var nugetConfigFileFolder = TestFileSystemUtility.CreateRandomTestFolder()) - { - var nugetConfigFilePath = Path.Combine(nugetConfigFileFolder, "nuget.config"); - - var randomURL = "https://www.somerandomURL.com/"; - var enabledReplacement = @""; - enabledReplacement = enabledReplacement + @""; - var disabledReplacement = string.Empty; - File.WriteAllText(nugetConfigFilePath, CreateNuGetConfigContent(enabledReplacement, disabledReplacement)); - - Settings settings = new Settings(nugetConfigFileFolder, "nuget.config"); - PackageSourceProvider before = new PackageSourceProvider(settings); - VerifyPackageSource(before, 2, new string[] { NuGetConstants.FeedName, NuGetConstants.FeedName }, - new string[] { NuGetConstants.V3FeedUrl, randomURL }, - new bool[] { true, true }, new bool[] { false, false }); - - List primary = new List(); - PackageSource item = new PackageSource(NuGetConstants.V3FeedUrl, NuGetConstants.FeedName); - primary.Add(item); - - List secondary = new List(); - PackageSource item2 = new PackageSource(NuGetConstants.V2FeedUrl, NuGetConstants.FeedName, false); - secondary.Add(item2); - - PackageSourceProvider after = new PackageSourceProvider(settings, primary, secondary); - // Assert - //Seconday name already exists in nuget.config but with different URL - //It gets overwritten by secondary scource which is disabled (while getting created above) - //IsOfficial is set to true - VerifyPackageSource(after, 2, new string[] { NuGetConstants.FeedName, NuGetConstants.FeedName }, - new string[] { NuGetConstants.V3FeedUrl, NuGetConstants.V2FeedUrl }, - new bool[] { true, false }, new bool[] { true, true }); - - } - } - - [Fact] - public void PrimaryNameNotChangedWhenTheFeedHasAnotherName() - { - // Act - //Create nuget.config that has Primary defined (Feed Name is different) and Secondary defined - using (var nugetConfigFileFolder = TestFileSystemUtility.CreateRandomTestFolder()) - { - var nugetConfigFilePath = Path.Combine(nugetConfigFileFolder, "nuget.config"); - - var enabledReplacement = @""; - enabledReplacement = enabledReplacement + @""; - var disabledReplacement = string.Empty; - File.WriteAllText(nugetConfigFilePath, CreateNuGetConfigContent(enabledReplacement, disabledReplacement)); - - Settings settings = new Settings(nugetConfigFileFolder, "nuget.config"); - PackageSourceProvider before = new PackageSourceProvider(settings); - VerifyPackageSource(before, 2, new string[] { "anotherName", NuGetConstants.FeedName }, - new string[] { NuGetConstants.V3FeedUrl, NuGetConstants.V2FeedUrl }, - new bool[] { true, true }, new bool[] { false, false }); - - List primary = new List(); - PackageSource item = new PackageSource(NuGetConstants.V3FeedUrl, NuGetConstants.FeedName); - primary.Add(item); - - List secondary = new List(); - PackageSource item2 = new PackageSource(NuGetConstants.V2FeedUrl, NuGetConstants.FeedName, false); - secondary.Add(item2); - - PackageSourceProvider after = new PackageSourceProvider(settings, primary, secondary); - - // Assert - //Primary feed is present in nuget.config but with a different name - //In this case, we don't set IsOfficial = true - //Secondary matches both name and URL so secondary is set to true - //Since this is not the first time primary is getting added, we aren't aggressive in demoting secondary from enabled to disabled - VerifyPackageSource(after, 2, new string[] { "anotherName", NuGetConstants.FeedName }, - new string[] { NuGetConstants.V3FeedUrl, NuGetConstants.V2FeedUrl }, - new bool[] { true, true }, new bool[] { false, true }); - - } - } - - [Fact] - public void SecondaryNameNotChangedWhenTheFeedHasAnotherName() - { - // Act - //Create nuget.config that has Primary defined and Secondary missing - using (var nugetConfigFileFolder = TestFileSystemUtility.CreateRandomTestFolder()) - { - var nugetConfigFilePath = Path.Combine(nugetConfigFileFolder, "nuget.config"); - - var enabledReplacement = @""; - enabledReplacement = enabledReplacement + @""; - var disabledReplacement = string.Empty; - File.WriteAllText(nugetConfigFilePath, CreateNuGetConfigContent(enabledReplacement, disabledReplacement)); - - Settings settings = new Settings(nugetConfigFileFolder, "nuget.config"); - PackageSourceProvider before = new PackageSourceProvider(settings); - VerifyPackageSource(before, 2, new string[] { NuGetConstants.FeedName, "anotherName" }, - new string[] { NuGetConstants.V3FeedUrl, NuGetConstants.V2FeedUrl }, - new bool[] { true, true }, new bool[] { false, false }); - - List primary = new List(); - PackageSource item = new PackageSource(NuGetConstants.V3FeedUrl, NuGetConstants.FeedName); - primary.Add(item); - - List secondary = new List(); - PackageSource item2 = new PackageSource(NuGetConstants.V2FeedUrl, NuGetConstants.FeedName, false); - secondary.Add(item2); - - PackageSourceProvider after = new PackageSourceProvider(settings, primary, secondary); - - // Assert - //Secondary feed is present in nuget.config but with a different name - //In this case, we don't set IsOfficial = true - //Primary matches both name and URL so primary's IsOfficial is set to true - //Since this is not the first time primary is getting added, we aren't aggressive in demoting secondary from enabled to disabled - VerifyPackageSource(after, 2, new string[] { NuGetConstants.FeedName, "anotherName" }, - new string[] { NuGetConstants.V3FeedUrl, NuGetConstants.V2FeedUrl }, - new bool[] { true, true }, new bool[] { true, false }); - - } - } - - public void PrimaryIsEnabledAndSecondaryIsDisabledWhenPrimaryIsAddedForTheFirstTimeAndSecondaryAlreadyExists() - { - // Act - //Create nuget.config that has Secondary defined - using (var nugetConfigFileFolder = TestFileSystemUtility.CreateRandomTestFolder()) - { - var nugetConfigFilePath = Path.Combine(nugetConfigFileFolder, "nuget.Config"); - - var enabledReplacement = @""; - var disabledReplacement = string.Empty; - File.WriteAllText(nugetConfigFilePath, CreateNuGetConfigContent(enabledReplacement, disabledReplacement)); - - Settings settings = new Settings(nugetConfigFileFolder, "nuget.config"); - PackageSourceProvider before = new PackageSourceProvider(settings); - VerifyPackageSource(before, 1, new string[] { NuGetConstants.FeedName }, - new string[] { NuGetConstants.V2FeedUrl }, - new bool[] { true }, new bool[] { false }); - - List primary = new List(); - PackageSource item = new PackageSource(NuGetConstants.V3FeedUrl, NuGetConstants.FeedName); - primary.Add(item); - - List secondary = new List(); - PackageSource item2 = new PackageSource(NuGetConstants.V2FeedUrl, NuGetConstants.FeedName, false); - secondary.Add(item2); - - PackageSourceProvider after = new PackageSourceProvider(settings, primary, secondary); - - // Assert - //First time Primary is getting added so it is set to Enabled - //Secondary is demoted to disabled even though it is already enabled through nuget.config - VerifyPackageSource(after, 2, new string[] { NuGetConstants.FeedName, NuGetConstants.FeedName }, - new string[] { NuGetConstants.V2FeedUrl, NuGetConstants.V3FeedUrl }, - new bool[] { false, true }, new bool[] { true, true }); - - } - } - [Fact] public void ActivePackageSourceCanBeReadAndWrittenInNuGetConfig() { @@ -465,25 +179,11 @@ public void TestNoPackageSourcesAreReturnedIfUserSettingsIsEmpty() } [Fact] - public void LoadPackageSourcesReturnsEmptySequenceIfDefaultPrimaryPackageSourceIsNull() - { - // Arrange - var settings = new Mock(); - var provider = CreatePackageSourceProvider(settings.Object, providerDefaultPrimarySources: null); - - // Act - var values = provider.LoadPackageSources(); - - // Assert - Assert.False(values.Any()); - } - - [Fact] - public void LoadPackageSourcesReturnsEmptySequenceIfDefaultPackageSourceIsEmpty() + public void LoadPackageSourcesReturnsEmptySequence() { // Arrange var settings = new Mock(); - var provider = CreatePackageSourceProvider(settings.Object, providerDefaultPrimarySources: new PackageSource[] { }); + var provider = CreatePackageSourceProvider(settings.Object); // Act var values = provider.LoadPackageSources(); @@ -492,61 +192,6 @@ public void LoadPackageSourcesReturnsEmptySequenceIfDefaultPackageSourceIsEmpty( Assert.False(values.Any()); } - [Fact] - public void LoadPackageSourcesReturnsDefaultSourcesIfSpecified() - { - // Arrange - var settings = new Mock().Object; - var provider = CreatePackageSourceProvider(settings, providerDefaultPrimarySources: new[] { new PackageSource("A"), new PackageSource("B") }); - - // Act - var values = provider.LoadPackageSources().ToList(); - - // Assert - Assert.Equal(2, values.Count); - Assert.Equal("A", values.First().Source); - Assert.Equal("B", values.Last().Source); - } - - [Fact] - public void LoadPackageSourcesWhereAMigratedSourceIsAlsoADefaultSource() - { - // Arrange - var settings = new Mock(); - settings.Setup(s => s.GetSettingValues("packageSources", true)) - .Returns(new[] { new SettingValue("AOld", "urlA", false), new SettingValue("userDefinedSource", "userDefinedSourceUrl", false) }); - settings.Setup(s => s.GetSettingValues("disabledPackageSources", false)).Returns(new SettingValue[0]); - settings.Setup(s => s.GetNestedValues("packageSourceCredentials", It.IsAny())).Returns(new KeyValuePair[0]); - - var defaultPackageSourceA = new PackageSource("urlA", "ANew"); - var defaultPackageSourceB = new PackageSource("urlB", "B"); - - var provider = CreatePackageSourceProvider(settings.Object, providerDefaultPrimarySources: new[] { defaultPackageSourceA, defaultPackageSourceB }, - migratePackageSources: new Dictionary - { - { new PackageSource("urlA", "AOld"), defaultPackageSourceA }, - }); - - // Act - var values = provider.LoadPackageSources().ToList(); - - // Assert - // Package Source AOld will be migrated to ANew. B will simply get added - // Since default source B got added when there are other package sources it will be disabled - // However, package source ANew must stay enabled - // PackageSource userDefinedSource is a user package source and is untouched - Assert.Equal(3, values.Count); - Assert.Equal("urlA", values[0].Source); - Assert.Equal("ANew", values[0].Name); - Assert.True(values[0].IsEnabled); - Assert.Equal("userDefinedSourceUrl", values[1].Source); - Assert.Equal("userDefinedSource", values[1].Name); - Assert.True(values[1].IsEnabled); - Assert.Equal("urlB", values[2].Source); - Assert.Equal("B", values[2].Name); - Assert.False(values[2].IsEnabled); - } - [Fact] public void LoadPackageSourcesPerformMigrationIfSpecified() { @@ -571,8 +216,6 @@ public void LoadPackageSourcesPerformMigrationIfSpecified() .Verifiable(); var provider = CreatePackageSourceProvider(settings.Object, - null, - null, new Dictionary { { new PackageSource("onesource", "one"), new PackageSource("goodsource", "good") }, @@ -674,7 +317,8 @@ public void SavePackageSourcesWithRelativePath() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); var packageSourceList = packageSourceProvider.LoadPackageSources().ToList(); @@ -717,7 +361,8 @@ public void SavePackageSourcesWithRelativePathAndAddNewSource() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); var packageSourceList = packageSourceProvider.LoadPackageSources().ToList(); @@ -765,7 +410,8 @@ public void SavePackageSourcesWithOneClear() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); var packageSourceList = packageSourceProvider.LoadPackageSources().ToList(); @@ -817,7 +463,8 @@ public void SavePackageSourcesWithMoreCLear() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); var packageSourceList = packageSourceProvider.LoadPackageSources().ToList(); @@ -868,7 +515,8 @@ public void SavePackageSourcesWithOnlyClear() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); var packageSourceList = packageSourceProvider.LoadPackageSources().ToList(); @@ -922,7 +570,8 @@ public void SavePackageSourcesWithHierarchyClear() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); var packageSourceList = packageSourceProvider.LoadPackageSources().ToList(); @@ -982,7 +631,8 @@ public void SavePackageSources_RetainUnavailableDisabledSources() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var expectedDisabledSources = settings.GetSettingValues("disabledPackageSources")?.ToList(); @@ -995,7 +645,8 @@ public void SavePackageSources_RetainUnavailableDisabledSources() var newSettings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var actualDisabledSources = newSettings.GetSettingValues("disabledPackageSources").ToList(); @@ -1033,7 +684,8 @@ public void SavePackageSources_EnablesDisabledSources() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var disabledSources = settings.GetSettingValues("disabledPackageSources")?.ToList(); @@ -1058,7 +710,8 @@ public void SavePackageSources_EnablesDisabledSources() var newSettings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); // Main Assert disabledSources = newSettings.GetSettingValues("disabledPackageSources")?.ToList(); @@ -1233,8 +886,7 @@ public void LoadPackageSourcesDoesNotDuplicateFeedsOnMigration() settings.Setup(s => s.GetNestedValues("packageSourceCredentials", It.IsAny())).Returns(new KeyValuePair[0]); settings.Setup(s => s.GetSettingValues("disabledPackageSources", false)).Returns(new SettingValue[0]); - var provider = CreatePackageSourceProvider(settings.Object, providerDefaultPrimarySources: null, - providerDefaultSecondarySources: null, + var provider = CreatePackageSourceProvider(settings.Object, migratePackageSources: new Dictionary { { new PackageSource("https://nuget.org/api/v2", "NuGet official package source"), new PackageSource("https://www.nuget.org/api/v2", "nuget.org") } @@ -1285,8 +937,6 @@ public void LoadPackageSources_ReadsSourcesWithProtocolVersionFromPackageSourceS }); var provider = CreatePackageSourceProvider(settings.Object, - providerDefaultPrimarySources: null, - providerDefaultSecondarySources: null, migratePackageSources: null); // Act @@ -1388,8 +1038,6 @@ public void LoadPackageSources_MigratesSourcesToNewerProtocol_ButKeepsExistingSo }; var provider = CreatePackageSourceProvider(settings.Object, - providerDefaultPrimarySources: null, - providerDefaultSecondarySources: null, migratePackageSources: migratePackageSources); // Act @@ -1446,8 +1094,7 @@ public void LoadPackageSourcesDoesNotDuplicateFeedsOnMigrationAndSavesIt() .Callback((string section, IReadOnlyList settingValues) => { Assert.Empty(settingValues); }) .Verifiable(); - var provider = CreatePackageSourceProvider(settings.Object, providerDefaultPrimarySources: null, - providerDefaultSecondarySources: null, + var provider = CreatePackageSourceProvider(settings.Object, migratePackageSources: new Dictionary { { new PackageSource("https://nuget.org/api/v2", "NuGet official package source"), new PackageSource("https://www.nuget.org/api/v2", "nuget.org") } @@ -1912,7 +1559,8 @@ public void HighPrioritySourceNotDisabled() Path.Combine(mockBaseDirectory, @"a\b\c"), configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var provider = CreatePackageSourceProvider(settings); // Act @@ -1951,7 +1599,8 @@ public void LowPrioritySourceDisabled() Path.Combine(mockBaseDirectory, "a", "b", "c"), configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var provider = CreatePackageSourceProvider(settings); @@ -1983,7 +1632,8 @@ public void V2NotDisabled() mockBaseDirectory, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var provider = CreatePackageSourceProvider(settings); @@ -2015,7 +1665,8 @@ public void AddPackageSourcesWithConfigFile() var settings = Settings.LoadDefaultSettings(mockBaseDirectory.Path, configFileName: "NuGet.config", machineWideSettings: null, - loadAppDataSettings: true); + loadAppDataSettings: true, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); // Act @@ -2062,7 +1713,8 @@ public void SavePackageSources_AddDisabledSourceToTheConfigContainingSource() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); // Act - 1 @@ -2124,7 +1776,8 @@ public void SavePackageSources_WritesToTheSettingsFileWithTheNearestPriority() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); // Act - 1 @@ -2198,7 +1851,8 @@ public void SavePackageSources_UpdatesSourceInAllConfigs() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); // Act - 1 @@ -2274,7 +1928,8 @@ public void SavePackageSources_AddsNewSourcesToTheSettingWithLowestPriority() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); // Act - 1 @@ -2348,7 +2003,8 @@ public void SavePackageSources_AddsOrderingForCollapsedFeeds() var settings = Settings.LoadDefaultSettings(rootPath, configFileName: null, machineWideSettings: null, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); // Act - 1 @@ -2428,7 +2084,8 @@ public void SavePackageSources_DisabledMachineWideSource() var settings = Settings.LoadDefaultSettings(mockBaseDirectory.Path, configFileName: null, machineWideSettings: m.Object, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); var sources = packageSourceProvider.LoadPackageSources().ToList(); @@ -2478,7 +2135,8 @@ public void SavePackageSources_DisabledOneMachineWideSource() var settings = Settings.LoadDefaultSettings(mockBaseDirectory.Path, configFileName: null, machineWideSettings: m.Object, - loadAppDataSettings: false); + loadAppDataSettings: false, + useTestingGlobalPath: false); var packageSourceProvider = new PackageSourceProvider(settings); var sources = packageSourceProvider.LoadPackageSources().ToList(); @@ -2496,7 +2154,7 @@ public void SavePackageSources_DisabledOneMachineWideSource() } } - [Fact(Skip = "Test currently failing")] + [Fact] public void DisabledMachineWideSourceByDefault() { using (var mockBaseDirectory = TestFileSystemUtility.CreateRandomTestFolder()) @@ -2522,9 +2180,10 @@ public void DisabledMachineWideSourceByDefault() var settings = Settings.LoadDefaultSettings(mockBaseDirectory.Path, configFileName: null, machineWideSettings: m.Object, - loadAppDataSettings: true); + loadAppDataSettings: true, + useTestingGlobalPath: true); var packageSourceProvider = new PackageSourceProvider(settings); - var sources = packageSourceProvider.LoadPackageSources().ToList(); + var sources = packageSourceProvider.LoadPackageSources().Where(p => p.IsMachineWide).ToList(); // Assert Assert.False(sources[0].IsEnabled); @@ -2532,7 +2191,7 @@ public void DisabledMachineWideSourceByDefault() } } - [Fact(Skip = "Test currently failing")] + [Fact] public void DisabledMachineWideSourceByDefaultWithNull() { using (var mockBaseDirectory = TestFileSystemUtility.CreateRandomTestFolder()) @@ -2541,14 +2200,78 @@ public void DisabledMachineWideSourceByDefaultWithNull() var settings = Settings.LoadDefaultSettings(mockBaseDirectory.Path, configFileName: null, machineWideSettings: null, - loadAppDataSettings: true); + loadAppDataSettings: true, + useTestingGlobalPath: true); + var packageSourceProvider = new PackageSourceProvider(settings); + + // Act + var sources = packageSourceProvider.LoadPackageSources().ToList(); + + // Assert + Assert.Equal(1, sources.Count); + } + } + + [Fact] + public void LoadPackageSourceEmptyConfigFileOnUserMachine() + { + using (var mockBaseDirectory = TestFileSystemUtility.CreateRandomTestFolder()) + { + // Arrange + var configContents = + @" + + + + + +"; + File.WriteAllText(Path.Combine(mockBaseDirectory.Path, "nuget.config"), configContents); + var settings = Settings.LoadDefaultSettings(mockBaseDirectory.Path, + configFileName: null, + machineWideSettings: null, + loadAppDataSettings: true, + useTestingGlobalPath: true); + var packageSourceProvider = new PackageSourceProvider(settings); + + // Act + var sources = packageSourceProvider.LoadPackageSources().ToList(); + + // Assert + Assert.Equal(0, sources.Count); + } + } + + [Fact] + public void LoadPackageSourceLocalConfigFileOnUserMachine() + { + using (var mockBaseDirectory = TestFileSystemUtility.CreateRandomTestFolder()) + { + // Arrange + var configContents = + @" + + + + + + +"; + File.WriteAllText(Path.Combine(mockBaseDirectory.Path, "nuget.config"), configContents); + var settings = Settings.LoadDefaultSettings(mockBaseDirectory.Path, + configFileName: null, + machineWideSettings: null, + loadAppDataSettings: true, + useTestingGlobalPath: true); var packageSourceProvider = new PackageSourceProvider(settings); // Act var sources = packageSourceProvider.LoadPackageSources().ToList(); // Assert - Assert.Equal(2, sources.Count); + Assert.Equal(1, sources.Count); + Assert.Equal(@"https://nuget/test", sources[0].Source); + Assert.Equal("test", sources[0].Name); } } @@ -2598,13 +2321,11 @@ private void VerifyPackageSource(PackageSourceProvider psp, int count, string[] private IPackageSourceProvider CreatePackageSourceProvider( ISettings settings = null, - IEnumerable providerDefaultPrimarySources = null, - IEnumerable providerDefaultSecondarySources = null, IDictionary migratePackageSources = null ) { settings = settings ?? new Mock().Object; - return new PackageSourceProvider(settings, providerDefaultPrimarySources, providerDefaultSecondarySources, migratePackageSources); + return new PackageSourceProvider(settings, migratePackageSources); } private void AssertPackageSource(PackageSource ps, string name, string source, bool isEnabled, bool isMachineWide = false, bool isOfficial = false) diff --git a/test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs b/test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs index d6af02b5635..dff63089f18 100644 --- a/test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Configuration.Test/SettingsTests.cs @@ -2153,6 +2153,26 @@ public void GetGlobalPackagesFolder_Default() Assert.Equal(expectedPath, globalPackagesFolderPath); } + [Fact] + public void CreateNewConfigFileIfNoConfig() + { + using (var mockBaseDirectory = TestFileSystemUtility.CreateRandomTestFolder()) + { + // Act + Settings settings = new Settings(mockBaseDirectory); + + // Assert + var text = File.ReadAllText(Path.Combine(mockBaseDirectory, "NuGet.Config")).Replace("\r\n", "\n"); + var result = @" + + + + +".Replace("\r\n","\n"); + Assert.Equal(result, text); + } + } + private void AssertEqualCollections(IList actual, string[] expected) { Assert.Equal(actual.Count, expected.Length / 2);