From 428e6fde49fe6bd9d23564827916fbff28b73e12 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 19 Aug 2021 19:31:41 -0700 Subject: [PATCH 01/27] 1 --- src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs index 292d8bb30aa..da0e525d2c7 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs @@ -127,7 +127,8 @@ public static async Task GetDownloadResourceResultAsync( { if (isPackageNamespaceEnabled) { - if (configuredPackageSources != null && + if (configuredPackageSources == null || + configuredPackageSources.Count == 0 || !configuredPackageSources.Contains(source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)) { // This package's id prefix is not defined in current package source, let's skip. From 78dbeeb6bb5312d8f019e739a6b1f36007a1a0fa Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 19 Aug 2021 21:10:19 -0700 Subject: [PATCH 02/27] Adjust tests. --- .../NuGetRestoreCommandTest.cs | 96 +------------- .../MsbuildRestoreTaskTests.cs | 120 ------------------ 2 files changed, 5 insertions(+), 211 deletions(-) diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs index f007fe19830..02d40f2278d 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs @@ -2431,18 +2431,13 @@ public void RestoreCommand_NameSpaceFilter_Succeed() var publicRepositoryPath = Path.Combine(workingPath, "PublicRepository"); Directory.CreateDirectory(publicRepositoryPath); Util.CreateTestPackage("测试更新包", "1.0.0", publicRepositoryPath); - Util.CreateTestPackage("_", "1.0.0", publicRepositoryPath); - Util.CreateTestPackage("123123123123", "1.0.0", publicRepositoryPath); - Util.CreateTestPackage("jQuery", "2.1.1", publicRepositoryPath); Util.CreateTestPackage("Microsoft.AspNet.Mvc", "5.2.7", publicRepositoryPath); Util.CreateTestPackage("Microsoft.AspNet.WebApi.Cors", "5.2.7", publicRepositoryPath); Util.CreateTestPackage("Microsoft.Extensions.Configuration.Abstractions", "5.0.0", publicRepositoryPath); - Util.CreateTestPackage("Microsoft.VisualStudio.Shell.Interop.9.0", "16.9.31023.347", publicRepositoryPath); Util.CreateTestPackage("Moq", "4.16.1", publicRepositoryPath); Util.CreateTestPackage("Moq.AutoMock", "2.3.0", publicRepositoryPath); Util.CreateTestPackage("NerdBank.Algorithms", "1.0.0", publicRepositoryPath); Util.CreateTestPackage("Nerdbank.GitVersioning", "1.1.64", publicRepositoryPath); - Util.CreateTestPackage("Newtonsoft.Json", "12.0.3", publicRepositoryPath); Util.CreateTestPackage("System.Buffers", "4.5.1", publicRepositoryPath); Util.CreateTestPackage("System.Memory", "4.5.4", publicRepositoryPath); Util.CreateTestPackage("System.Numerics.Vectors", "4.5.0", publicRepositoryPath); @@ -2474,21 +2469,16 @@ public void RestoreCommand_NameSpaceFilter_Succeed() Util.CreateFile(proj1Directory, "packages.config", @" - - - - - @@ -2515,17 +2505,18 @@ public void RestoreCommand_NameSpaceFilter_Succeed() - + - - + + + - + @@ -2697,7 +2688,6 @@ public void RestoreCommand_PackageNamespaceLongerMatches_Succeed() Util.CreateFile(proj1Directory, "packages.config", @" - "); @@ -2747,82 +2737,6 @@ public void RestoreCommand_PackageNamespaceLongerMatches_Succeed() } } - - [Fact] - public void RestoreCommand_PackageNamespace_NoNamespaceMatches() - { - // Arrange - var nugetexe = Util.GetNuGetExePath(); - - using (var workingPath = TestDirectory.Create()) - { - var proj1Directory = Path.Combine(workingPath, "proj1"); - Directory.CreateDirectory(proj1Directory); - - var proj1File = Path.Combine(proj1Directory, "proj1.csproj"); - File.WriteAllText( - proj1File, - @" - - Library - out - v4.0 - - - - -"); - - var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); - Directory.CreateDirectory(sharedRepositoryPath); - Util.CreateTestPackage("My.MVC.ASP", "1.0.0", sharedRepositoryPath); - - Util.CreateFile(proj1Directory, "packages.config", -@" - -"); - - var configPath = Path.Combine(workingPath, "nuget.config"); - SettingsTestUtils.CreateConfigurationFile(configPath, $@" - - - - - - - - - - - -"); - - var packagePath = Path.Combine(workingPath, "packages"); - - string[] args = new string[] - { - "restore", - proj1File, - "-solutionDir", - workingPath, - "-Verbosity", - "d" - }; - - // Act - var r = CommandRunner.Run( - nugetexe, - workingPath, - string.Join(" ", args), - waitForExit: true); - - // Assert - Assert.Equal(_successCode, r.ExitCode); - Assert.Contains("Package namespace match not found for package ID 'My.MVC.ASP'", r.Output); - } - } - [Fact] public void RestoreCommand_PackageNamespaceMatchesMultipleSources_Succeed() { diff --git a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs index d7d93e49d7c..275a3b58208 100644 --- a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs +++ b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs @@ -1223,7 +1223,6 @@ public async Task MsbuildRestore_PackageNamespacePartial_Succeed() { writer.Write( @" - "); @@ -1232,17 +1231,6 @@ public async Task MsbuildRestore_PackageNamespacePartial_Succeed() var opensourceRepositoryPath = pathContext.PackageSource; Directory.CreateDirectory(opensourceRepositoryPath); - var packageOpenSourceInternational = new SimpleTestPackageContext() - { - Id = "测试更新包", - Version = "1.0.0" - }; - packageOpenSourceInternational.AddFile("lib/net461/a.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, - packageOpenSourceInternational); - var packageOpenSourceContosoMvc = new SimpleTestPackageContext() { Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package @@ -1445,7 +1433,6 @@ public async Task MsbuildRestore_PackageNamespaceLongerMatches_Succeed() { writer.Write( @" - "); @@ -1454,17 +1441,6 @@ public async Task MsbuildRestore_PackageNamespaceLongerMatches_Succeed() var opensourceRepositoryPath = pathContext.PackageSource; Directory.CreateDirectory(opensourceRepositoryPath); - var packageOpenSourceInternational = new SimpleTestPackageContext() - { - Id = "测试更新包", - Version = "1.0.0" - }; - packageOpenSourceInternational.AddFile("lib/net461/a.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, - packageOpenSourceInternational); - var packageOpenSourceContosoMvc = new SimpleTestPackageContext() { Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package @@ -1547,102 +1523,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } - [PlatformFact(Platform.Windows)] - public async Task MsbuildRestore_PackageNamespaceLongerMatches_NoNamespaceMatchesLog() - { - // Arrange - using (var pathContext = new SimpleTestPathContext()) - { - - // Set up solution, project, and packages - var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); - - var net461 = NuGetFramework.Parse("net461"); - - var projectA = new SimpleTestProjectContext( - "a", - ProjectStyle.PackagesConfig, - pathContext.SolutionRoot); - projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); - var projectAPackages = Path.Combine(pathContext.SolutionRoot, "packages"); - - solution.Projects.Add(projectA); - solution.Create(pathContext.SolutionRoot); - - using (var writer = new StreamWriter(Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "packages.config"))) - { - writer.Write( -@" - -"); - } - - var opensourceRepositoryPath = pathContext.PackageSource; - Directory.CreateDirectory(opensourceRepositoryPath); - - var packageOpenSourceContosoMvc = new SimpleTestPackageContext() - { - Id = "My.MVC.ASP", // Package Id conflict with internally created package - Version = "1.0.0" - }; - packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, - packageOpenSourceContosoMvc); - - var packageContosoBuffersOpenSource = new SimpleTestPackageContext() - { - Id = "Contoso.Opensource.Buffers", - Version = "1.0.0" - }; - packageContosoBuffersOpenSource.AddFile("lib/net461/openA.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, - packageContosoBuffersOpenSource); - - // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, - // so we go ahead and remove that config before running MSBuild. - var configPath = Path.Combine(Path.GetDirectoryName(pathContext.SolutionRoot), "NuGet.Config"); - var configText = -$@" - - - - - - - - - - - -"; - - using (var writer = new StreamWriter(configPath)) - { - writer.Write(configText); - } - - // Act - var result = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore -v:d {pathContext.SolutionRoot} /p:RestorePackagesConfig=true", ignoreExitCode: true); - - // Assert - Assert.True(result.ExitCode == 0); - var contosoRestorePath = Path.Combine(projectAPackages, packageOpenSourceContosoMvc.ToString(), packageOpenSourceContosoMvc.ToString() + ".nupkg"); - using (var nupkgReader = new PackageArchiveReader(contosoRestorePath)) - { - var allFiles = nupkgReader.GetFiles().ToList(); - // Assert correct package was restored. - Assert.Contains("lib/net461/openA.dll", allFiles); - } - - Assert.True(result.ExitCode == 0); - Assert.Contains("Package namespace match not found for package ID 'My.MVC.ASP'", result.Output); - } - } - [PlatformFact(Platform.Windows)] public async Task MsbuildRestore_PackageNamespaceSameMatchesMultipleSources_Succeed() { From 95b1f3caf40221bd5c2ea3b22b7e95cf22e3ab8d Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 20 Aug 2021 06:22:21 -0700 Subject: [PATCH 03/27] Test --- .../MsbuildRestoreTaskTests.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs index 275a3b58208..0340c0280af 100644 --- a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs +++ b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs @@ -997,7 +997,6 @@ public async Task MsbuildRestore_PackageNamespaceFull_Succeed() { writer.Write( @" - "); @@ -1006,18 +1005,6 @@ public async Task MsbuildRestore_PackageNamespaceFull_Succeed() var opensourceRepositoryPath = pathContext.PackageSource; Directory.CreateDirectory(opensourceRepositoryPath); - var packageOpenSourceInternational = new SimpleTestPackageContext() - { - Id = "测试更新包", - Version = "1.0.0" - }; - packageOpenSourceInternational.Files.Clear(); - packageOpenSourceInternational.AddFile("lib/net461/a.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, - packageOpenSourceInternational); - var packageOpenSourceContosoMvc = new SimpleTestPackageContext() { Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package From 463246c0dc0ffac1eabb58e6abf5c4d3dd78c9bd Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 20 Aug 2021 09:16:39 -0700 Subject: [PATCH 04/27] Add install package scenario. --- .../Resolution/ResolverGather.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/NuGet.Core/NuGet.PackageManagement/Resolution/ResolverGather.cs b/src/NuGet.Core/NuGet.PackageManagement/Resolution/ResolverGather.cs index 3cea0a295ae..d1995bf4f5e 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/Resolution/ResolverGather.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/Resolution/ResolverGather.cs @@ -560,10 +560,15 @@ private void QueueWork(IReadOnlyList sources, PackageIdentity pa { foreach (SourceResource source in sources) { - if (_areNamespacesEnabled && configuredPackageSources != null && !configuredPackageSources.Contains(source.Source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)) + if (_areNamespacesEnabled) { - // This package's id prefix is not defined in current package source, let's skip. - continue; + if (configuredPackageSources == null || + configuredPackageSources.Count == 0 || + !configuredPackageSources.Contains(source.Source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)) + { + // This package's id prefix is not defined in current package source, let's skip. + continue; + } } // Keep track of the order in which these were made From 77d9dc77e4f45aa5c6e89ebe6e7dd82e68270cd5 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 20 Aug 2021 11:54:41 -0700 Subject: [PATCH 05/27] Fix install tests. --- .../NuGetPackageManagerTests.cs | 5 +++-- .../NuGet.PackageManagement.Test/ResolverGatherTests.cs | 8 +++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/test/NuGet.Core.Tests/NuGet.PackageManagement.Test/NuGetPackageManagerTests.cs b/test/NuGet.Core.Tests/NuGet.PackageManagement.Test/NuGetPackageManagerTests.cs index cc05b62bcd7..2f86d256cee 100644 --- a/test/NuGet.Core.Tests/NuGet.PackageManagement.Test/NuGetPackageManagerTests.cs +++ b/test/NuGet.Core.Tests/NuGet.PackageManagement.Test/NuGetPackageManagerTests.cs @@ -7349,11 +7349,12 @@ public async Task TestPacManPreview_InstallForPC_PackageNamespace_WithMultipleFe + - + @@ -7370,7 +7371,7 @@ public async Task TestPacManPreview_InstallForPC_PackageNamespace_WithMultipleFe var ExternalDirectA = new SimpleTestPackageContext() { - Id = directPackageIdentity.Id, // Package Id not filtered by package namespace. + Id = directPackageIdentity.Id, Version = "1.0.0", Dependencies = new List() { ExternalContosoA } // We set Contoso.A as dependent package. }; diff --git a/test/NuGet.Core.Tests/NuGet.PackageManagement.Test/ResolverGatherTests.cs b/test/NuGet.Core.Tests/NuGet.PackageManagement.Test/ResolverGatherTests.cs index 21dfb7c10ac..58440b713fc 100644 --- a/test/NuGet.Core.Tests/NuGet.PackageManagement.Test/ResolverGatherTests.cs +++ b/test/NuGet.Core.Tests/NuGet.PackageManagement.Test/ResolverGatherTests.cs @@ -1571,17 +1571,15 @@ public async Task ResolverGather_PackageNamespace_Fails(string packageNamespaces }; // Act - HashSet results = await ResolverGather.GatherAsync(context, CancellationToken.None); - - List check = results.OrderBy(e => e.Id).ToList(); + var exception = await Assert.ThrowsAsync( + () => ResolverGather.GatherAsync(context, CancellationToken.None)); // Assert Assert.True(namespacesConfiguration.AreNamespacesEnabled); Assert.Null(configuredSources); - Assert.Equal(1, check.Count); // Assert log. - Assert.Equal($"Package namespace match not found for package ID '{packageId}' ", testNuGetProjectContext.Logs.Value[0]); + Assert.Contains($"Package '{packageId} 1.0.0' is not found in the following primary source(s)", exception.Message); } private static SourceRepository CreateTimeoutRepo(string source) From e451729d013b08c8184162e7c1f822777cad61ca Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 20 Aug 2021 13:56:59 -0700 Subject: [PATCH 06/27] Move namespace logic --- .../NuGetPackageManager.cs | 34 +++++++++++++++---- .../PackageDownloader.cs | 11 ------ .../PublicAPI.Unshipped.txt | 1 + 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs index 2185eca27e7..0e0fbd50e49 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs @@ -66,6 +66,8 @@ public class NuGetPackageManager /// public event EventHandler BatchEnd; + public PackageNamespacesConfiguration PackageNamespaces { get; } + /// /// To construct a NuGetPackageManager that does not need a SolutionManager like NuGet.exe /// @@ -93,6 +95,7 @@ public NuGetPackageManager( InstallationCompatibility = PackageManagement.InstallationCompatibility.Instance; InitializePackagesFolderInfo(packagesFolderPath, excludeVersion); + PackageNamespaces = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); } /// @@ -120,6 +123,7 @@ public NuGetPackageManager( InstallationCompatibility = PackageManagement.InstallationCompatibility.Instance; InitializePackagesFolderInfo(PackagesFolderPathUtility.GetPackagesFolderPath(SolutionManager, Settings), excludeVersion); DeleteOnRestartManager = deleteOnRestartManager ?? throw new ArgumentNullException(nameof(deleteOnRestartManager)); + PackageNamespaces = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); } /// @@ -1109,9 +1113,7 @@ private async Task> PreviewUpdatePackagesForClas } } - PackageNamespacesConfiguration packageNamespacesConfiguration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); - - var gatherContext = new GatherContext(packageNamespacesConfiguration) + var gatherContext = new GatherContext(PackageNamespaces) { InstalledPackages = oldListOfInstalledPackages.ToList(), PrimaryTargetIds = primaryTargetIds.ToList(), @@ -1738,9 +1740,8 @@ await PreviewBuildIntegratedProjectActionsAsync(buildIntegratedProject, actions, var primaryPackages = new List { packageIdentity }; HashSet availablePackageDependencyInfoWithSourceSet = null; - PackageNamespacesConfiguration packageNamespacesConfiguration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); - var gatherContext = new GatherContext(packageNamespacesConfiguration) + var gatherContext = new GatherContext(PackageNamespaces) { InstalledPackages = oldListOfInstalledPackages.ToList(), PrimaryTargets = primaryPackages, @@ -3386,11 +3387,11 @@ public async Task RestorePackageAsync( token.ThrowIfCancellationRequested(); nuGetProjectContext.Log(MessageLevel.Info, string.Format(Strings.RestoringPackage, packageIdentity)); - var enabledSources = (sourceRepositories != null && sourceRepositories.Any()) ? sourceRepositories : - SourceRepositoryProvider.GetRepositories().Where(e => e.PackageSource.IsEnabled); token.ThrowIfCancellationRequested(); + IEnumerable enabledSources = GetEnabledSources(sourceRepositories, packageIdentity); + using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( enabledSources, packageIdentity, @@ -3406,6 +3407,25 @@ public async Task RestorePackageAsync( return true; } + private IEnumerable GetEnabledSources(IEnumerable sourceRepositories, PackageIdentity packageIdentity) + { + var enabledSources = (sourceRepositories != null && sourceRepositories.Any()) ? sourceRepositories : + SourceRepositoryProvider.GetRepositories().Where(e => e.PackageSource.IsEnabled).ToList(); + + if (PackageNamespaces?.AreNamespacesEnabled == true) + { + IReadOnlyList sources = PackageNamespaces.GetConfiguredPackageSources(packageIdentity.Id); + + if (sources == null || sources.Count == 0) + { + return Array.Empty(); + } + + return enabledSources.Where(s => sources.Contains(s.PackageSource.Name)).ToList(); + } + return enabledSources; + } + public Task CopySatelliteFilesAsync(PackageIdentity packageIdentity, INuGetProjectContext nuGetProjectContext, CancellationToken token) { return PackagesFolderNuGetProject.CopySatelliteFilesAsync(packageIdentity, nuGetProjectContext, token); diff --git a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs index da0e525d2c7..d1700cc61cb 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs @@ -125,17 +125,6 @@ public static async Task GetDownloadResourceResultAsync( foreach (SourceRepository source in sourceGroup) { - if (isPackageNamespaceEnabled) - { - if (configuredPackageSources == null || - configuredPackageSources.Count == 0 || - !configuredPackageSources.Contains(source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)) - { - // This package's id prefix is not defined in current package source, let's skip. - continue; - } - } - var task = GetDownloadResourceResultAsync( source, packageIdentity, diff --git a/src/NuGet.Core/NuGet.PackageManagement/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.PackageManagement/PublicAPI.Unshipped.txt index e008ad3161c..e4ada60c8d9 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.PackageManagement/PublicAPI.Unshipped.txt @@ -1,4 +1,5 @@ NuGet.PackageManagement.GatherContext.GatherContext(NuGet.Configuration.PackageNamespacesConfiguration packageNamespacesConfiguration) -> void NuGet.PackageManagement.GatherContext.PackageNamespacesConfiguration.get -> NuGet.Configuration.PackageNamespacesConfiguration const NuGet.ProjectManagement.ProjectBuildProperties.AssemblyName = "AssemblyName" -> string +NuGet.PackageManagement.NuGetPackageManager.PackageNamespaces.get -> NuGet.Configuration.PackageNamespacesConfiguration const NuGet.ProjectManagement.ProjectBuildProperties.CLRSupport = "CLRSupport" -> string From 73f820bdd657c0baddf7e79fe74f995e12ed3984 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 20 Aug 2021 14:50:07 -0700 Subject: [PATCH 07/27] Move package namespace logic more ahead than actual restore action. --- .../NuGet.CommandLine/Commands/InstallCommand.cs | 8 ++++++-- .../NuGet.PackageManagement/IDE/PackageRestoreManager.cs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs b/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs index fbd4772da99..e6b98cab898 100644 --- a/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs +++ b/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs @@ -217,7 +217,9 @@ private async Task PerformV2RestoreAsync(string packagesConfigFilePath, string i Console) }; - var downloadContext = new PackageDownloadContext(cacheContext, installPath, DirectDownload) + PackageNamespacesConfiguration packageNamespacesConfiguration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); + + var downloadContext = new PackageDownloadContext(cacheContext, installPath, DirectDownload, packageNamespacesConfiguration) { ClientPolicyContext = clientPolicyContext }; @@ -373,7 +375,9 @@ private async Task InstallPackageAsync( resolutionContext.SourceCacheContext.NoCache = NoCache; resolutionContext.SourceCacheContext.DirectDownload = DirectDownload; - var downloadContext = new PackageDownloadContext(resolutionContext.SourceCacheContext, installPath, DirectDownload) + PackageNamespacesConfiguration packageNamespacesConfiguration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); + + var downloadContext = new PackageDownloadContext(resolutionContext.SourceCacheContext, installPath, DirectDownload, packageNamespacesConfiguration) { ClientPolicyContext = clientPolicyContext }; diff --git a/src/NuGet.Core/NuGet.PackageManagement/IDE/PackageRestoreManager.cs b/src/NuGet.Core/NuGet.PackageManagement/IDE/PackageRestoreManager.cs index 49f7f60032f..16f62054e5b 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/IDE/PackageRestoreManager.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/IDE/PackageRestoreManager.cs @@ -386,7 +386,7 @@ private static async Task> ThrottledPackageRestore { var packageReferencesQueue = new ConcurrentQueue(packageReferences); var tasks = new List>>(); - for (var i = 0; i < Math.Min(packageRestoreContext.MaxNumberOfParallelTasks, packageReferences.Count); i++) + for (var i = 0; i < 1; i++) { tasks.Add(Task.Run(() => PackageRestoreRunnerAsync( packageReferencesQueue, From 0baa1d9a58cd95dc05ca8afd5f481d11f9099db8 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 20 Aug 2021 15:26:03 -0700 Subject: [PATCH 08/27] Add tests. --- test/EndToEnd/tests/PackageNameSpaceTests.ps1 | 290 ++++++++++++++++++ .../Commands/RestoreCommandTests.cs | 205 +++++++++++++ .../NuGetInstallCommandTest.cs | 204 ++++++++++++ .../NuGetRestoreCommandTest.cs | 179 ++++++++++- .../RestoreNETCoreTest.cs | 222 ++++++++++++++ .../DotnetRestoreTests.cs | 154 ++++++++++ .../MsbuildRestoreTaskTests.cs | 216 ++++++++++++- 7 files changed, 1468 insertions(+), 2 deletions(-) diff --git a/test/EndToEnd/tests/PackageNameSpaceTests.ps1 b/test/EndToEnd/tests/PackageNameSpaceTests.ps1 index 13960cf14a7..7a1f0fd4dab 100644 --- a/test/EndToEnd/tests/PackageNameSpaceTests.ps1 +++ b/test/EndToEnd/tests/PackageNameSpaceTests.ps1 @@ -326,6 +326,296 @@ function Test-VsPackageInstallerServices-PackageNamespaceInstall-WithMultipleFee } } +function Test-PackageNamespaceInstall_Succeed +{ + param($context) + + # Arrange + $nugetConfigPath = Join-Path $OutputPath 'nuget.config' + $settingFileContent =@" + + + + + + + + + + + + +"@ + try { + # We have to create config file before creating solution, otherwise it's not effective for new solutions. + $settingFileContent -f $context.RepositoryRoot | Out-File -Encoding "UTF8" $nugetConfigPath + + $p = New-ConsoleApplication + + # Act + $p | Install-Package SolutionLevelPkg -Version 1.0 + + # # Assert + Assert-Package $p SolutionLevelPkg 1.0.0 + $errorlist = Get-Errors + Assert-AreEqual 0 $errorlist.Count + $warninglist = Get-Warnings + Assert-AreEqual 0 $warninglist.Count + } + finally { + Remove-Item $nugetConfigPath + } +} + +function Test-PackageNamespaceInstall_WrongSource_Fails +{ + param($context) + + # Arrange + $repoDirectory = $context.RepositoryRoot + $privateRepo = Join-Path $repoDirectory "privateRepo" + + $nugetConfigPath = Join-Path $OutputPath 'nuget.config' + $settingFileContent =@" + + + + + + + + + + + + + +"@ + try { + # We have to create config file before creating solution, otherwise it's not effective for new solutions. + $settingFileContent -f $context.RepositoryRoot,$privateRepo | Out-File -Encoding "UTF8" $nugetConfigPath + + $p = New-ConsoleApplication + + # Act & Assert + # Even though SolutionLevelPkg package exist in $repoDirectory since package namespace filter set SolutionLevelPkg can be restored only from PrivateRepository repository so it'll fail. + $exceptionMessage = "Package 'SolutionLevelPkg 1.0' is not found in the following primary source(s): '" + $context.RepositoryRoot + "," + $privateRepo + "'. Please verify all your online package sources are available (OR) package id, version are specified correctly." + Assert-Throws { $p | Install-Package SolutionLevelPkg -Version 1.0 } $exceptionMessage + Assert-NoPackage $p SolutionLevelPkg 1.0.0 + } + finally { + Remove-Item $nugetConfigPath + } +} + +function Test-PackageNamespaceInstall_NonExistingSource_Fails +{ + param($context) + + # Arrange + $nugetConfigPath = Join-Path $OutputPath 'nuget.config' + $settingFileContent =@" + + + + + + + + + + + +"@ + try { + # We have to create config file before creating solution, otherwise it's not effective for new solutions. + $settingFileContent -f $context.RepositoryRoot | Out-File -Encoding "UTF8" $nugetConfigPath + + $p = New-ConsoleApplication + + # Act & Assert + # Even though SolutionLevelPkg package exist in $repoDirectory since package namespace filter set SolutionLevelPkg can be restored only from UnavailableRepository repository so it'll fail. + $exceptionMessage = "Argument cannot be null or empty`r`nParameter name: primarySources" + Assert-Throws { $p | Install-Package SolutionLevelPkg -Version 1.0 } $exceptionMessage + Assert-NoPackage $p SolutionLevelPkg 1.0.0 + } + finally { + Remove-Item $nugetConfigPath + } +} + +function Test-PackageNamespaceInstall_Pass_CorrectSourceOption_Succeed +{ + param($context) + + # Arrange + $repoDirectory = Join-Path $OutputPath "CustomPackages" + $opensourceRepo = Join-Path $repoDirectory "opensourceRepo" + $privateRepo = Join-Path $repoDirectory "privateRepo" + $nugetConfigPath = Join-Path $OutputPath 'nuget.config' + + $settingFileContent =@" + + + + + + + + + + + + + +"@ + try { + # We have to create config file before creating solution, otherwise it's not effective for new solutions. + $settingFileContent -f $opensourceRepo,$privateRepo | Out-File -Encoding "UTF8" $nugetConfigPath + + $p = New-ConsoleApplication + $projectDirectoryPath = $p.Properties.Item("FullPath").Value + $packagesConfigPath = Join-Path $projectDirectoryPath 'packages.config' + $projectDirectoryPath = $p.Properties.Item("FullPath").Value + $solutionDirectory = Split-Path -Path $projectDirectoryPath -Parent + + CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $privateRepo "Thisisfromprivaterepo1.txt" + CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $opensourceRepo "Thisisfromopensourcerepo1.txt" + + # Act + $p | Install-Package Contoso.MVC.ASP -Source $privateRepo + + # Assert + Assert-Package $p Contoso.MVC.ASP 1.0.0 + $packagesFolder = Join-Path $solutionDirectory "packages" + $contosoNupkgFolder = Join-Path $packagesFolder "Contoso.MVC.ASP.1.0.0" + Assert-PathExists(Join-Path $contosoNupkgFolder "Contoso.MVC.ASP.1.0.0.nupkg") + # Make sure name squatting package from public repo not restored. + $contentFolder = Join-Path $contosoNupkgFolder "content" + Assert-PathExists(Join-Path $contentFolder "Thisisfromprivaterepo1.txt") + + $errorlist = Get-Errors + Assert-AreEqual 0 $errorlist.Count + } + finally { + Remove-Item -Recurse -Force $repoDirectory + Remove-Item $nugetConfigPath + } +} + +function Test-PackageNamespaceInstall_Pass_WrongSourceOption_Fails +{ + param($context) + + # Arrange + $repoDirectory = Join-Path $OutputPath "CustomPackages" + $opensourceRepo = Join-Path $repoDirectory "opensourceRepo" + $privateRepo = Join-Path $repoDirectory "privateRepo" + $nugetConfigPath = Join-Path $OutputPath 'nuget.config' + + $settingFileContent =@" + + + + + + + + + + + + + +"@ + try { + # We have to create config file before creating solution, otherwise it's not effective for new solutions. + $settingFileContent -f $opensourceRepo,$privateRepo | Out-File -Encoding "UTF8" $nugetConfigPath + + $p = New-ConsoleApplication + $projectDirectoryPath = $p.Properties.Item("FullPath").Value + $packagesConfigPath = Join-Path $projectDirectoryPath 'packages.config' + $projectDirectoryPath = $p.Properties.Item("FullPath").Value + $solutionDirectory = Split-Path -Path $projectDirectoryPath -Parent + + CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $privateRepo "Thisisfromprivaterepo1.txt" + CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $opensourceRepo "Thisisfromopensourcerepo1.txt" + + # Act & Assert + $exceptionMessage = "Package 'Contoso.MVC.ASP 1.0.0' is not found in the following primary source(s): '"+ $opensourceRepo + "'. Please verify all your online package sources are available (OR) package id, version are specified correctly." + Assert-Throws { $p | Install-Package Contoso.MVC.ASP -Source $opensourceRepo } $exceptionMessage + Assert-NoPackage $p SolutionLevelPkg 1.0.0 + } + finally { + Remove-Item -Recurse -Force $repoDirectory + Remove-Item $nugetConfigPath + } +} + +function Test-PackageNamespaceUpdate_Pass_CorrectSourceOption_Succeed +{ + param($context) + + # Arrange + $repoDirectory = Join-Path $OutputPath "CustomPackages" + $opensourceRepo = Join-Path $repoDirectory "opensourceRepo" + $privateRepo = Join-Path $repoDirectory "privateRepo" + $nugetConfigPath = Join-Path $OutputPath 'nuget.config' + + $settingFileContent =@" + + + + + + + + + + + + + +"@ + try { + # We have to create config file before creating solution, otherwise it's not effective for new solutions. + $settingFileContent -f $opensourceRepo,$privateRepo | Out-File -Encoding "UTF8" $nugetConfigPath + + $p = New-ConsoleApplication + $projectDirectoryPath = $p.Properties.Item("FullPath").Value + $packagesConfigPath = Join-Path $projectDirectoryPath 'packages.config' + $projectDirectoryPath = $p.Properties.Item("FullPath").Value + $solutionDirectory = Split-Path -Path $projectDirectoryPath -Parent + + CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $privateRepo "Thisisfromprivaterepo1.txt" + CreateCustomTestPackage "Contoso.MVC.ASP" "2.0.0" $privateRepo "Thisisfromprivaterepo2.txt" + CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $opensourceRepo "Thisisfromopensourcerepo1.txt" + CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $opensourceRepo "Thisisfromopensourcerepo2.txt" + + # Act + $p | Install-Package Contoso.MVC.ASP -Version 1.0 -Source $privateRepo + Assert-Package $p Contoso.MVC.ASP 1.0.0 + + $p | Update-Package Contoso.MVC.ASP -Version 2.0 -Source $privateRepo + Assert-Package $p Contoso.MVC.ASP 2.0.0 + + # Assert + $packagesFolder = Join-Path $solutionDirectory "packages" + $contosoNupkgFolder = Join-Path $packagesFolder "Contoso.MVC.ASP.2.0.0" + Assert-PathExists(Join-Path $contosoNupkgFolder "Contoso.MVC.ASP.2.0.0.nupkg") + # Make sure name squatting package from public repo not restored. + $contentFolder = Join-Path $contosoNupkgFolder "content" + Assert-PathExists(Join-Path $contentFolder "Thisisfromprivaterepo2.txt") + + $errorlist = Get-Errors + Assert-AreEqual 0 $errorlist.Count + } + finally { + Remove-Item -Recurse -Force $repoDirectory + Remove-Item $nugetConfigPath + } +} + # Create a custom test package function CreateCustomTestPackage { param( diff --git a/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs b/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs index 3e06a68a0ad..c94e327add0 100644 --- a/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs +++ b/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs @@ -1168,6 +1168,211 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } + [Fact] + public async Task Restore_PackageNamespace_PassAllNecessarySourceOption_Succeed() + { + // Arrange + using (var pathContext = new SimpleTestPathContext()) + { + // Set up solution, project, and packages + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + + var net461 = NuGetFramework.Parse("net461"); + + var projectA = SimpleTestProjectContext.CreateLegacyPackageReference( + "a", + pathContext.SolutionRoot, + net461); + var projectAPackages = Path.Combine(pathContext.SolutionRoot, "packages"); + + var externalRepositoryPath = Path.Combine(pathContext.SolutionRoot, "ExternalRepository"); + Directory.CreateDirectory(externalRepositoryPath); + + var contosoRepositoryPath = Path.Combine(pathContext.SolutionRoot, "ContosoRepository"); + Directory.CreateDirectory(contosoRepositoryPath); + + var configPath = Path.Combine(pathContext.WorkingDirectory, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" + + + + + + + + + + + + + + + + + +"); + + var ContosoReal = new SimpleTestPackageContext() + { + Id = "Contoso.A", + Version = "1.0.0" + }; + ContosoReal.AddFile("lib/net461/contosoA.dll"); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + contosoRepositoryPath, + PackageSaveMode.Defaultv3, + ContosoReal); + + var ExternalContoso = new SimpleTestPackageContext() + { + Id = "Contoso.A", // This package exist both repo. + Version = "1.0.0" + }; + ExternalContoso.AddFile("lib/net461/externalA.dll"); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + externalRepositoryPath, + PackageSaveMode.Defaultv3, + ExternalContoso); + + var ExternalB = new SimpleTestPackageContext() + { + Id = "External.B", + Version = "2.0.0" + }; + ExternalB.AddFile("lib/net461/externalB.dll"); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + externalRepositoryPath, + PackageSaveMode.Defaultv3, + ExternalB); + + Util.CreateFile(Path.GetDirectoryName(projectA.ProjectPath), "packages.config", +@" + + +"); + + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + // Act + var result = RunRestore(pathContext, _successExitCode, new string[] { "-source", $"{externalRepositoryPath};{contosoRepositoryPath}" }); + + // Assert + var contosoRestorePath = Path.Combine(projectAPackages, ContosoReal.ToString(), ContosoReal.ToString() + ".nupkg"); + using (var nupkgReader = new PackageArchiveReader(contosoRestorePath)) + { + var allFiles = nupkgReader.GetFiles().ToList(); + // Assert correct Contoso package from Contoso repository was restored. + Assert.Contains("lib/net461/contosoA.dll", allFiles); + } + var externalRestorePath = Path.Combine(projectAPackages, ExternalB.ToString(), ExternalB.ToString() + ".nupkg"); + Assert.True(File.Exists(externalRestorePath)); + } + } + + [Fact] + public async Task Restore_PackageNamespace_PassNotEnoughSourceOption_Fails() + { + // Arrange + using (var pathContext = new SimpleTestPathContext()) + { + // Set up solution, project, and packages + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + + var net461 = NuGetFramework.Parse("net461"); + + var projectA = SimpleTestProjectContext.CreateLegacyPackageReference( + "a", + pathContext.SolutionRoot, + net461); + var projectAPackages = Path.Combine(pathContext.SolutionRoot, "packages"); + + var externalRepositoryPath = Path.Combine(pathContext.SolutionRoot, "ExternalRepository"); + Directory.CreateDirectory(externalRepositoryPath); + + var contosoRepositoryPath = Path.Combine(pathContext.SolutionRoot, "ContosoRepository"); + Directory.CreateDirectory(contosoRepositoryPath); + + var configPath = Path.Combine(pathContext.WorkingDirectory, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" + + + + + + + + + + + + + + + + + +"); + + var ContosoReal = new SimpleTestPackageContext() + { + Id = "Contoso.A", + Version = "1.0.0" + }; + ContosoReal.AddFile("lib/net461/contosoA.dll"); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + contosoRepositoryPath, + PackageSaveMode.Defaultv3, + ContosoReal); + + var ExternalContoso = new SimpleTestPackageContext() + { + Id = "Contoso.A", // This package exist both repo. + Version = "1.0.0" + }; + ExternalContoso.AddFile("lib/net461/externalA.dll"); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + externalRepositoryPath, + PackageSaveMode.Defaultv3, + ExternalContoso); + + var ExternalB = new SimpleTestPackageContext() + { + Id = "External.B", + Version = "2.0.0" + }; + ExternalB.AddFile("lib/net461/externalB.dll"); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + externalRepositoryPath, + PackageSaveMode.Defaultv3, + ExternalB); + + Util.CreateFile(Path.GetDirectoryName(projectA.ProjectPath), "packages.config", +@" + + +"); + + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + // Act + // contosoRepositoryPath is not passed in source option so it should fail. + var result = RunRestore(pathContext, _failureExitCode, new string[] { "-source", $"{externalRepositoryPath}" }); + + // Assert + var contosoRestorePath = Path.Combine(projectAPackages, ContosoReal.ToString(), ContosoReal.ToString() + ".nupkg"); + Assert.False(File.Exists(contosoRestorePath)); + Assert.Contains("Unable to find version '1.0.0' of package 'Contoso.A'.", result.AllOutput); + } + } + public static string GetResource(string name) { using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(name))) diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs index 19569a879dd..51753a80af6 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs @@ -11,6 +11,7 @@ using System.Xml.Linq; using FluentAssertions; using NuGet.Common; +using NuGet.Configuration.Test; using NuGet.Packaging; using NuGet.Packaging.Core; using NuGet.Test.Utility; @@ -1864,6 +1865,209 @@ public async Task InstallCommand_SpecifyUnlistedVersion_InstallUnlistedPackagesA File.Exists(a2Nupkg).Should().BeTrue(); } } + + [Fact] + public void InstallCommand_NameSpaceFilter_Succeed() + { + // Arrange + using (var pathContext = new SimpleTestPathContext()) + { + var workingPath = pathContext.WorkingDirectory; + + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" + + + + + + + + + + + + + + + +"); + + // Act + var r1 = RunInstall(pathContext, "Contoso.MVC.ASP", 0, "-Version", "1.0.0", "-OutputDirectory", "outputDir", "-Verbosity", "d"); + var r2 = RunInstall(pathContext, "Contoso.Opensource", 0, "-Version", "1.0.0", "-OutputDirectory", "outputDir", "-Verbosity", "d"); + + // Assert + Assert.Equal(0, r1.Item1); + Assert.Equal(0, r2.Item1); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r1.Output); + var packageFileContosoMVCASP = Path.Combine(workingPath, "outputDir", "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); + var packageFileContosoOpensource = Path.Combine(workingPath, "outputDir", "Contoso.Opensource.1.0.0", "Contoso.Opensource.1.0.0.nupkg"); + Assert.True(File.Exists(packageFileContosoMVCASP)); + Assert.True(File.Exists(packageFileContosoOpensource)); + } + } + + [Fact] + public void InstallCommand_NameSpaceFilter_Fails() + { + // Arrange + using (var pathContext = new SimpleTestPathContext()) + { + var workingPath = pathContext.WorkingDirectory; + + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", opensourceRepositoryPath); //This package supposed to be restored from other repo. + + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" + + + + + + + + + + + + + + + +"); + + // Act + var r = RunInstall(pathContext, "Contoso.MVC.ASP", 1, "-Version", "1.0.0", "-OutputDirectory", "outputDir"); + + // Assert + Assert.Equal(1, r.Item1); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); + r.AllOutput.Should().NotContain("NU1000"); + r.Errors.Should().Contain("Package 'Contoso.MVC.ASP 1.0.0' is not found in the following primary source(s):"); + } + } + + [Fact] + public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_AllSourceOptions_Succeed() + { + // Arrange + using (var pathContext = new SimpleTestPathContext()) + { + var workingPath = pathContext.WorkingDirectory; + + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + + Util.CreateFile(pathContext.SolutionRoot, "packages.config", +@" + + +"); + + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" + + + + + + + + + + + + + + + +"); + + // Act + var r = RunInstall(pathContext, Path.Combine(pathContext.SolutionRoot, "packages.config"), 0, "-OutputDirectory", "outputDir", "-Source", + $"{opensourceRepositoryPath};{sharedRepositoryPath}"); // We pass both repositories. + + // Assert + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); + var packageFileContosoMVCASP = Path.Combine(workingPath, "outputDir", "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); + var packageFileContosoOpensource = Path.Combine(workingPath, "outputDir", "Contoso.Opensource.1.0.0", "Contoso.Opensource.1.0.0.nupkg"); + Assert.True(File.Exists(packageFileContosoMVCASP)); + Assert.True(File.Exists(packageFileContosoOpensource)); + } + } + + [Fact] + public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_NotEnoughSources_Fails() + { + // Arrange + using (var pathContext = new SimpleTestPathContext()) + { + var workingPath = pathContext.WorkingDirectory; + + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + + Util.CreateFile(pathContext.SolutionRoot, "packages.config", +@" + + +"); + + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" + + + + + + + + + + + + + + + +"); + + // Act + var r = RunInstall(pathContext, Path.Combine(pathContext.SolutionRoot, "packages.config"), 1, "-OutputDirectory", "outputDir", "-Source", + opensourceRepositoryPath); // We pass 1 repository. + + // Assert + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); + r.AllOutput.Should().NotContain("NU1000"); + r.Errors.Should().Contain("Unable to find version '1.0.0' of package 'Contoso.MVC.ASP'."); + } + } + public static CommandRunnerResult RunInstall(SimpleTestPathContext pathContext, string input, int expectedExitCode = 0, params string[] additionalArgs) { var nugetexe = Util.GetNuGetExePath(); diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs index 02d40f2278d..69b52ab1091 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs @@ -2707,7 +2707,7 @@ public void RestoreCommand_PackageNamespaceLongerMatches_Succeed() - + "); @@ -2965,6 +2965,183 @@ public void RestoreCommand_NoPackageNamespacesection_NoSourceRelatedLogMessage() } } + [Fact] + public void RestoreCommand_NameSpaceFilter_Cli_Pass_AllSourceOptions_Succeed() + { + // Arrange + var nugetexe = Util.GetNuGetExePath(); + + using (var workingPath = TestDirectory.Create()) + { + var proj1Directory = Path.Combine(workingPath, "proj1"); + Directory.CreateDirectory(proj1Directory); + + var proj1File = Path.Combine(proj1Directory, "proj1.csproj"); + File.WriteAllText( + proj1File, + @" + + Library + out + v4.0 + + + + +"); + + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource.A", "1.0.0", opensourceRepositoryPath); + + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + + Util.CreateFile(proj1Directory, "packages.config", +@" + + +"); + + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" + + + + + + + + + + + + + + + +"); + + var packagePath = Path.Combine(workingPath, "packages"); + + string[] args = new string[] + { + "restore", + proj1File, + "-solutionDir", + workingPath, + "-Verbosity", + "d", + "-source", + sharedRepositoryPath + ';' + opensourceRepositoryPath //We pass both repositories as source + }; + + // Act + var r = CommandRunner.Run( + nugetexe, + workingPath, + string.Join(" ", args), + waitForExit: true); + + // Assert + Assert.Equal(_successCode, r.ExitCode); + // If we pass source then log include actual path to repository instead of repository name. + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: '{opensourceRepositoryPath}'", r.Output); + } + } + + [Fact] + public void RestoreCommand_NameSpaceFilter_Cli_Pass_NotEnoughSources_Fails() + { + // Arrange + var nugetexe = Util.GetNuGetExePath(); + + using (var workingPath = TestDirectory.Create()) + { + var proj1Directory = Path.Combine(workingPath, "proj1"); + Directory.CreateDirectory(proj1Directory); + + var proj1File = Path.Combine(proj1Directory, "proj1.csproj"); + File.WriteAllText( + proj1File, + @" + + Library + out + v4.0 + + + + +"); + + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", opensourceRepositoryPath); + + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + + Util.CreateFile(proj1Directory, "packages.config", +@" + + +"); + + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" + + + + + + + + + + + + + + + +"); + + var packagePath = Path.Combine(workingPath, "packages"); + + string[] args = new string[] + { + "restore", + proj1File, + "-solutionDir", + workingPath, + "-source", + opensourceRepositoryPath, // We only pass 1 source here + "-Verbosity", + "d" + }; + + // Act + var r = CommandRunner.Run( + nugetexe, + workingPath, + string.Join(" ", args), + waitForExit: true); + + // Assert + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); + // Even though there is eligible source SharedRepository exist but only opensourceRepositoryPath passed as option it'll fail to restore. + Assert.Equal(_failureCode, r.ExitCode); + Assert.Contains("WARNING: Unable to find version '1.0.0' of package 'Contoso.MVC.ASP'.", r.Output); + } + } + private static byte[] GetResource(string name) { return ResourceTestUtility.GetResourceBytes( diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs index 1e43b55f021..2292e189a1f 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs @@ -10854,6 +10854,228 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( Assert.Contains($"Installed {packageY} {version} from {pathContext.PackageSource}", result.AllOutput); } + [Fact] + public async Task RestoreCommand_NameSpaceFilter_PR_WithAllRestoreSources_Properies_Succeed() + { + // Arrange + var pathContext = new SimpleTestPathContext(); + // Set up solution, project, and packages + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + var workingPath = pathContext.WorkingDirectory; + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + + var privateRepositoryPath = Path.Combine(workingPath, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + var net461 = NuGetFramework.Parse("net461"); + + var projectA = new SimpleTestProjectContext( + "a", + ProjectStyle.PackageReference, + pathContext.SolutionRoot); + projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); + + // Add both repositories as RestoreSources + projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath};{privateRepositoryPath}"); + + var packageOpenSourceA = new SimpleTestPackageContext() + { + Id = "Contoso.Opensource.A", + Version = "1.0.0" + }; + + packageOpenSourceA.AddFile("lib/net461/openA.dll"); + + var packageOpenSourceContosoMvc = new SimpleTestPackageContext() + { + Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package + Version = "1.0.0" + }; + packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); + + var packageContosoMvcReal = new SimpleTestPackageContext() + { + Id = "Contoso.MVC.ASP", + Version = "1.0.0" + }; + packageContosoMvcReal.AddFile("lib/net461/realA.dll"); + + projectA.AddPackageToAllFrameworks(packageOpenSourceA); + projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); + + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, + // so we go ahead and replace that config before running MSBuild. + var configAPath = Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "NuGet.Config"); + var configText = +$@" + + + + + + + + + + + + + + + +"; + using (var writer = new StreamWriter(configAPath)) + { + writer.Write(configText); + } + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceA); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceContosoMvc); + + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + privateRepositoryPath, + packageContosoMvcReal); + + var packagePath = Path.Combine(pathContext.WorkingDirectory, "packages"); + + string[] args = new string[] + { + "-OutputDirectory", + "packages" + }; + + // Act + var r = Util.Restore(pathContext, projectA.ProjectPath, expectedExitCode: 0, args); + + // Assert + // If we pass source then log include actual path to repository instead of repository name. + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'PrivateRepository'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); + var contosoRestorePath = Path.Combine(packagePath, packageContosoMvcReal.Id.ToString(), packageContosoMvcReal.Version.ToString(), packageContosoMvcReal.ToString() + ".nupkg"); + using (var nupkgReader = new PackageArchiveReader(contosoRestorePath)) + { + var allFiles = nupkgReader.GetFiles().ToList(); + // Assert correct Contoso package was restored. + Assert.True(allFiles.Contains("lib/net461/realA.dll")); + } + } + + [Fact] + public async Task RestoreCommand_NameSpaceFilter_PR_WithNotEnoughRestoreSources_Property_Fails() + { + // Arrange + var pathContext = new SimpleTestPathContext(); + // Set up solution, project, and packages + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + var workingPath = pathContext.WorkingDirectory; + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + + var privateRepositoryPath = Path.Combine(workingPath, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + var net461 = NuGetFramework.Parse("net461"); + + var projectA = new SimpleTestProjectContext( + "a", + ProjectStyle.PackageReference, + pathContext.SolutionRoot); + projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); + + // Add only 1 repository as RestoreSources + projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath}"); + + var packageOpenSourceA = new SimpleTestPackageContext() + { + Id = "Contoso.Opensource.A", + Version = "1.0.0" + }; + + packageOpenSourceA.AddFile("lib/net461/openA.dll"); + + var packageOpenSourceContosoMvc = new SimpleTestPackageContext() + { + Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package + Version = "1.0.0" + }; + packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); + + var packageContosoMvcReal = new SimpleTestPackageContext() + { + Id = "Contoso.MVC.ASP", + Version = "1.0.0" + }; + packageContosoMvcReal.AddFile("lib/net461/realA.dll"); + + projectA.AddPackageToAllFrameworks(packageOpenSourceA); + projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); + + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, + // so we go ahead and replace that config before running MSBuild. + var configAPath = Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "NuGet.Config"); + var configText = +$@" + + + + + + + + + + + + + + + +"; + using (var writer = new StreamWriter(configAPath)) + { + writer.Write(configText); + } + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceA); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceContosoMvc); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + privateRepositoryPath, + packageContosoMvcReal); + + string[] args = new string[] + { + "-OutputDirectory", + "packages" + }; + + // Act + var r = Util.Restore(pathContext, projectA.ProjectPath, expectedExitCode: 1, args); + + // Assert + Assert.Contains("Package namespace match not found for package ID 'Contoso.MVC.ASP'.", r.Output); + // Even though there is eligible source SharedRepository exist but only opensourceRepositoryPath passed as option it'll fail to restore. + Assert.Contains($"Failed to restore {projectA.ProjectPath}", r.Output); + } + private static byte[] GetTestUtilityResource(string name) { return ResourceTestUtility.GetResourceBytes( diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetRestoreTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetRestoreTests.cs index c76650b10dc..2f59c544ea4 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetRestoreTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetRestoreTests.cs @@ -1776,6 +1776,160 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( Assert.Contains($"Installed {packageY} {version} from {pathContext.PackageSource}", result.AllOutput); } + [Fact] + public async Task DotnetRestore_NameSpaceFilter_Pass_AllSourceOptions_Succeed() + { + using var pathContext = _msbuildFixture.CreateSimpleTestPathContext(); + + // Set up solution, and project + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + var projFramework = FrameworkConstants.CommonFrameworks.Net50; + var projectPackageName = "projectA"; + var projectA = SimpleTestProjectContext.CreateNETCore( + projectPackageName, + pathContext.SolutionRoot, + projFramework); + + const string version = "1.0.0"; + const string packageX = "X", packageY = "Y", packageZ = "Z", packageK = "K"; + + var packageX100 = new SimpleTestPackageContext(packageX, version); + var packageY100 = new SimpleTestPackageContext(packageY, version); + var packageZ100 = new SimpleTestPackageContext(packageZ, version); + var packageK100 = new SimpleTestPackageContext(packageK, version); + + packageX100.Dependencies.Add(packageZ100); + + projectA.AddPackageToAllFrameworks(packageX100); + projectA.AddPackageToAllFrameworks(packageY100); + projectA.AddPackageDownloadToAllFrameworks(packageK100); + + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + var packageSource2 = new DirectoryInfo(Path.Combine(pathContext.WorkingDirectory, "source2")); + packageSource2.Create(); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + pathContext.PackageSource, + PackageSaveMode.Defaultv3, + packageX100, + packageY100, + packageZ100, + packageK100); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packageSource2.FullName, + PackageSaveMode.Defaultv3, + packageX100, + packageY100, + packageZ100, + packageK100); + + var configFile = @$" + + + + + + + + + + + + + + + + +"; + File.WriteAllText(Path.Combine(pathContext.SolutionRoot, projectA.ProjectName, "NuGet.Config"), configFile); + + //Act + var result = _msbuildFixture.RunDotnet(pathContext.WorkingDirectory, $"restore {projectA.ProjectPath} --source {packageSource2.FullName};{pathContext.PackageSource} -v d", ignoreExitCode: true); + + result.Success.Should().BeTrue(because: result.AllOutput); + Assert.Contains($"Installed {packageX} {version} from {packageSource2.FullName}", result.AllOutput); + Assert.Contains($"Installed {packageZ} {version} from {pathContext.PackageSource}", result.AllOutput); + Assert.Contains($"Installed {packageY} {version} from {pathContext.PackageSource}", result.AllOutput); + Assert.Contains($"Installed {packageK} {version} from {packageSource2.FullName}", result.AllOutput); + Assert.Contains($"Package namespace matches found for package ID 'Y' are: 'source1'.", result.AllOutput); + Assert.Contains($"Package namespace matches found for package ID 'Z' are: 'source1'.", result.AllOutput); + Assert.Contains($"Package namespace matches found for package ID 'X' are: 'source2'.", result.AllOutput); + Assert.Contains($"Package namespace matches found for package ID 'K' are: 'source2'.", result.AllOutput); + } + + [Fact] + public async Task DotnetRestore_NameSpaceFilter_Pass_NotEnoughSources_Fails() + { + using var pathContext = _msbuildFixture.CreateSimpleTestPathContext(); + + // Set up solution, and project + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + var projFramework = FrameworkConstants.CommonFrameworks.Net50; + var projectPackageName = "projectA"; + var projectA = SimpleTestProjectContext.CreateNETCore( + projectPackageName, + pathContext.SolutionRoot, + projFramework); + + const string version = "1.0.0"; + const string packageX = "X", packageY = "Y"; + + var packageX100 = new SimpleTestPackageContext(packageX, version); + var packageY100 = new SimpleTestPackageContext(packageY, version); + + + projectA.AddPackageToAllFrameworks(packageX100); + projectA.AddPackageToAllFrameworks(packageY100); + + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + var packageSource2 = new DirectoryInfo(Path.Combine(pathContext.WorkingDirectory, "source2")); + packageSource2.Create(); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + pathContext.PackageSource, + PackageSaveMode.Defaultv3, + packageX100, + packageY100); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packageSource2.FullName, + PackageSaveMode.Defaultv3, + packageX100, + packageY100); + + var configFile = @$" + + + + + + + + + + + + + + +"; + File.WriteAllText(Path.Combine(pathContext.SolutionRoot, projectA.ProjectName, "NuGet.Config"), configFile); + + //Act + var result = _msbuildFixture.RunDotnet(pathContext.WorkingDirectory, $"restore {projectA.ProjectPath} --source {packageSource2.FullName} -v d", ignoreExitCode: true); + + result.Success.Should().BeFalse(because: result.AllOutput); + Assert.Contains("Package namespace match not found for package ID 'Y'", result.AllOutput); + Assert.Contains($"NU1100: Unable to resolve '{packageY} (>= {version})'", result.AllOutput); + Assert.Contains($"Package namespace matches found for package ID 'X' are: 'source2'.", result.AllOutput); + Assert.Contains($"Installed {packageX} {version} from {packageSource2.FullName}", result.AllOutput); + } + private static SimpleTestPackageContext CreateNetstandardCompatiblePackage(string id, string version) { var pkgX = new SimpleTestPackageContext(id, version); diff --git a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs index 0340c0280af..17aa72dd6e3 100644 --- a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs +++ b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs @@ -1610,7 +1610,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [PlatformFact(Platform.Windows)] - public async Task MsbuildRestore__NoPackageNamespacesection_NoSourceRelatedLogMessage() + public async Task MsbuildRestore_NoPackageNamespacesection_NoSourceRelatedLogMessage() { // Arrange using (var pathContext = new SimpleTestPathContext()) @@ -1678,5 +1678,219 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( Assert.DoesNotContain("namespace", result.Output); } } + + [PlatformFact(Platform.Windows)] + public async Task MsbuildRestore_PR_PackageNamespace_WithAllRestoreSources_Properies_Succeed() + { + // Arrange + using (var pathContext = new SimpleTestPathContext()) + { + // Set up solution, project, and packages + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + var workingPath = pathContext.WorkingDirectory; + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + + var privateRepositoryPath = Path.Combine(workingPath, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + var net461 = NuGetFramework.Parse("net461"); + + var projectA = new SimpleTestProjectContext( + "a", + ProjectStyle.PackageReference, + pathContext.SolutionRoot); + projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); + + // Add both repositories as RestoreSources + projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath};{privateRepositoryPath}"); + + var packageOpenSourceA = new SimpleTestPackageContext() + { + Id = "Contoso.Opensource.A", + Version = "1.0.0" + }; + + packageOpenSourceA.AddFile("lib/net461/openA.dll"); + + var packageOpenSourceContosoMvc = new SimpleTestPackageContext() + { + Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package + Version = "1.0.0" + }; + packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); + + var packageContosoMvcReal = new SimpleTestPackageContext() + { + Id = "Contoso.MVC.ASP", + Version = "1.0.0" + }; + packageContosoMvcReal.AddFile("lib/net461/realA.dll"); + + projectA.AddPackageToAllFrameworks(packageOpenSourceA); + projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); + + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, + // so we go ahead and replace that config before running MSBuild. + var configAPath = Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "NuGet.Config"); + var configText = +$@" + + + + + + + + + + + + + + + +"; + using (var writer = new StreamWriter(configAPath)) + { + writer.Write(configText); + } + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceA); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceContosoMvc); + + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + privateRepositoryPath, + packageContosoMvcReal); + + // Act + var r = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore -v:d {pathContext.SolutionRoot}", ignoreExitCode: true); + + // Assert + Assert.True(r.ExitCode == 0); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'PrivateRepository'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); + var contosoRestorePath = Path.Combine(pathContext.UserPackagesFolder, packageContosoMvcReal.Id.ToString(), packageContosoMvcReal.Version.ToString(), packageContosoMvcReal.ToString() + ".nupkg"); + using (var nupkgReader = new PackageArchiveReader(contosoRestorePath)) + { + var allFiles = nupkgReader.GetFiles().ToList(); + // Assert correct Contoso package was restored. + Assert.True(allFiles.Contains("lib/net461/realA.dll")); + } + } + } + + [PlatformFact(Platform.Windows)] + public async Task MsbuildRestore_PR_PackageNamespace_WithNotEnoughRestoreSources_Property_Fails() + { + // Arrange + using (var pathContext = new SimpleTestPathContext()) + { + // Set up solution, project, and packages + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + var workingPath = pathContext.WorkingDirectory; + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + + var privateRepositoryPath = Path.Combine(workingPath, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + var net461 = NuGetFramework.Parse("net461"); + + var projectA = new SimpleTestProjectContext( + "a", + ProjectStyle.PackageReference, + pathContext.SolutionRoot); + projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); + + // Add only 1 repository as RestoreSources + projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath}"); + + var packageOpenSourceA = new SimpleTestPackageContext() + { + Id = "Contoso.Opensource.A", + Version = "1.0.0" + }; + + packageOpenSourceA.AddFile("lib/net461/openA.dll"); + + var packageOpenSourceContosoMvc = new SimpleTestPackageContext() + { + Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package + Version = "1.0.0" + }; + packageOpenSourceContosoMvc.Files.Clear(); + packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); + + var packageContosoMvcReal = new SimpleTestPackageContext() + { + Id = "Contoso.MVC.ASP", + Version = "1.0.0" + }; + packageContosoMvcReal.AddFile("lib/net461/realA.dll"); + + projectA.AddPackageToAllFrameworks(packageOpenSourceA); + projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); + + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, + // so we go ahead and replace that config before running MSBuild. + var configAPath = Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "NuGet.Config"); + var configText = +$@" + + + + + + + + + + + + + + + +"; + using (var writer = new StreamWriter(configAPath)) + { + writer.Write(configText); + } + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceA); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceContosoMvc); + + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + privateRepositoryPath, + packageContosoMvcReal); + + // Act + var r = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore -v:d {pathContext.SolutionRoot}", ignoreExitCode: true); + + // Assert + Assert.True(r.ExitCode == 1); + Assert.Contains($"Package namespace match not found for package ID 'Contoso.MVC.ASP'.", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); + } + } } } From 45525fc909b670189c03b324872c5c0f54d277e1 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 20 Aug 2021 15:44:47 -0700 Subject: [PATCH 09/27] Handle case insensetive case. --- .../NuGet.PackageManagement/IDE/PackageRestoreManager.cs | 2 +- src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.PackageManagement/IDE/PackageRestoreManager.cs b/src/NuGet.Core/NuGet.PackageManagement/IDE/PackageRestoreManager.cs index 16f62054e5b..49f7f60032f 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/IDE/PackageRestoreManager.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/IDE/PackageRestoreManager.cs @@ -386,7 +386,7 @@ private static async Task> ThrottledPackageRestore { var packageReferencesQueue = new ConcurrentQueue(packageReferences); var tasks = new List>>(); - for (var i = 0; i < 1; i++) + for (var i = 0; i < Math.Min(packageRestoreContext.MaxNumberOfParallelTasks, packageReferences.Count); i++) { tasks.Add(Task.Run(() => PackageRestoreRunnerAsync( packageReferencesQueue, diff --git a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs index 0e0fbd50e49..7b953460e5b 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs @@ -3421,7 +3421,7 @@ private IEnumerable GetEnabledSources(IEnumerable(); } - return enabledSources.Where(s => sources.Contains(s.PackageSource.Name)).ToList(); + return enabledSources.Where(s => sources.Contains(s.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)).ToList(); } return enabledSources; } From 606934786253f63e4fc1476a526329840396a829 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 20 Aug 2021 19:16:20 -0700 Subject: [PATCH 10/27] Fix tests. --- .../Commands/RestoreCommandTests.cs | 2 +- .../NuGetInstallCommandTest.cs | 12 ++++++------ .../NuGetRestoreCommandTest.cs | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs b/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs index c94e327add0..52073093530 100644 --- a/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs +++ b/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs @@ -1067,7 +1067,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( solution.Create(pathContext.SolutionRoot); // Act - var result = RunRestore(pathContext, _successExitCode); + var result = RunRestore(pathContext, _successExitCode, "--debug"); // Assert var contosoRestorePath = Path.Combine(projectAPackages, ContosoReal.ToString(), ContosoReal.ToString() + ".nupkg"); diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs index 51753a80af6..4b36653df9d 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs @@ -847,7 +847,7 @@ public void InstallCommand_PackageSaveModeNuspecReinstall() "-ForceEnglishOutput", "-OutputDirectory", outputDirectory, "-Source", source, - "-PackageSaveMode", "nuspec" }; + "-PackageSaveMode", "nuspec", "--debug" }; // Act var r = RunInstall(pathContext, "testPackage1", 0, args); @@ -1908,7 +1908,7 @@ public void InstallCommand_NameSpaceFilter_Succeed() // Assert Assert.Equal(0, r1.Item1); Assert.Equal(0, r2.Item1); - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r1.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r1.Output); var packageFileContosoMVCASP = Path.Combine(workingPath, "outputDir", "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); var packageFileContosoOpensource = Path.Combine(workingPath, "outputDir", "Contoso.Opensource.1.0.0", "Contoso.Opensource.1.0.0.nupkg"); Assert.True(File.Exists(packageFileContosoMVCASP)); @@ -1956,7 +1956,7 @@ public void InstallCommand_NameSpaceFilter_Fails() // Assert Assert.Equal(1, r.Item1); - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); r.AllOutput.Should().NotContain("NU1000"); r.Errors.Should().Contain("Package 'Contoso.MVC.ASP 1.0.0' is not found in the following primary source(s):"); } @@ -2008,7 +2008,7 @@ public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_AllSo $"{opensourceRepositoryPath};{sharedRepositoryPath}"); // We pass both repositories. // Assert - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); var packageFileContosoMVCASP = Path.Combine(workingPath, "outputDir", "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); var packageFileContosoOpensource = Path.Combine(workingPath, "outputDir", "Contoso.Opensource.1.0.0", "Contoso.Opensource.1.0.0.nupkg"); Assert.True(File.Exists(packageFileContosoMVCASP)); @@ -2059,10 +2059,10 @@ public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_NotEn // Act var r = RunInstall(pathContext, Path.Combine(pathContext.SolutionRoot, "packages.config"), 1, "-OutputDirectory", "outputDir", "-Source", - opensourceRepositoryPath); // We pass 1 repository. + opensourceRepositoryPath, "--debug"); // We pass 1 repository. // Assert - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); r.AllOutput.Should().NotContain("NU1000"); r.Errors.Should().Contain("Unable to find version '1.0.0' of package 'Contoso.MVC.ASP'."); } diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs index 69b52ab1091..0eeb0f7a84b 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs @@ -3048,8 +3048,8 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_AllSourceOptions_Succeed() // Assert Assert.Equal(_successCode, r.ExitCode); // If we pass source then log include actual path to repository instead of repository name. - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); - Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: '{opensourceRepositoryPath}'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); } } @@ -3135,7 +3135,7 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_NotEnoughSources_Fails() waitForExit: true); // Assert - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: '{sharedRepositoryPath}'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); // Even though there is eligible source SharedRepository exist but only opensourceRepositoryPath passed as option it'll fail to restore. Assert.Equal(_failureCode, r.ExitCode); Assert.Contains("WARNING: Unable to find version '1.0.0' of package 'Contoso.MVC.ASP'.", r.Output); From 6a8d8f2b101e662534902662681819c42c245fbb Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 20 Aug 2021 20:39:12 -0700 Subject: [PATCH 11/27] Remove --debug --- .../Commands/RestoreCommandTests.cs | 2 +- .../NuGet.CommandLine.Test/NuGetInstallCommandTest.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs b/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs index 52073093530..c94e327add0 100644 --- a/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs +++ b/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs @@ -1067,7 +1067,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( solution.Create(pathContext.SolutionRoot); // Act - var result = RunRestore(pathContext, _successExitCode, "--debug"); + var result = RunRestore(pathContext, _successExitCode); // Assert var contosoRestorePath = Path.Combine(projectAPackages, ContosoReal.ToString(), ContosoReal.ToString() + ".nupkg"); diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs index 4b36653df9d..ac9671c0b0c 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs @@ -847,7 +847,7 @@ public void InstallCommand_PackageSaveModeNuspecReinstall() "-ForceEnglishOutput", "-OutputDirectory", outputDirectory, "-Source", source, - "-PackageSaveMode", "nuspec", "--debug" }; + "-PackageSaveMode", "nuspec" }; // Act var r = RunInstall(pathContext, "testPackage1", 0, args); @@ -2059,7 +2059,7 @@ public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_NotEn // Act var r = RunInstall(pathContext, Path.Combine(pathContext.SolutionRoot, "packages.config"), 1, "-OutputDirectory", "outputDir", "-Source", - opensourceRepositoryPath, "--debug"); // We pass 1 repository. + opensourceRepositoryPath); // We pass 1 repository. // Assert Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); From a5e73c97e8f6d9a3160f57dd20436fce0672ea60 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Sat, 21 Aug 2021 11:13:39 -0700 Subject: [PATCH 12/27] Cleanup --- .../NuGet.CommandLine/Commands/InstallCommand.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs b/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs index e6b98cab898..d94f7fe58fa 100644 --- a/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs +++ b/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs @@ -252,11 +252,6 @@ private CommandLineSourceRepositoryProvider GetSourceRepositoryProvider() { return new CommandLineSourceRepositoryProvider(SourceProvider); } - - - - - private async Task InstallPackageAsync( string packageId, NuGetVersion version, From 56d20ccf0823246d4ae1779ab1c3f61889e343d0 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Sat, 21 Aug 2021 11:16:59 -0700 Subject: [PATCH 13/27] 1 --- .../NuGetPackageManager.cs | 34 ++++--------------- .../PackageDownloader.cs | 10 ++++++ 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs index 7b953460e5b..2185eca27e7 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs @@ -66,8 +66,6 @@ public class NuGetPackageManager /// public event EventHandler BatchEnd; - public PackageNamespacesConfiguration PackageNamespaces { get; } - /// /// To construct a NuGetPackageManager that does not need a SolutionManager like NuGet.exe /// @@ -95,7 +93,6 @@ public NuGetPackageManager( InstallationCompatibility = PackageManagement.InstallationCompatibility.Instance; InitializePackagesFolderInfo(packagesFolderPath, excludeVersion); - PackageNamespaces = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); } /// @@ -123,7 +120,6 @@ public NuGetPackageManager( InstallationCompatibility = PackageManagement.InstallationCompatibility.Instance; InitializePackagesFolderInfo(PackagesFolderPathUtility.GetPackagesFolderPath(SolutionManager, Settings), excludeVersion); DeleteOnRestartManager = deleteOnRestartManager ?? throw new ArgumentNullException(nameof(deleteOnRestartManager)); - PackageNamespaces = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); } /// @@ -1113,7 +1109,9 @@ private async Task> PreviewUpdatePackagesForClas } } - var gatherContext = new GatherContext(PackageNamespaces) + PackageNamespacesConfiguration packageNamespacesConfiguration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); + + var gatherContext = new GatherContext(packageNamespacesConfiguration) { InstalledPackages = oldListOfInstalledPackages.ToList(), PrimaryTargetIds = primaryTargetIds.ToList(), @@ -1740,8 +1738,9 @@ await PreviewBuildIntegratedProjectActionsAsync(buildIntegratedProject, actions, var primaryPackages = new List { packageIdentity }; HashSet availablePackageDependencyInfoWithSourceSet = null; + PackageNamespacesConfiguration packageNamespacesConfiguration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings); - var gatherContext = new GatherContext(PackageNamespaces) + var gatherContext = new GatherContext(packageNamespacesConfiguration) { InstalledPackages = oldListOfInstalledPackages.ToList(), PrimaryTargets = primaryPackages, @@ -3387,11 +3386,11 @@ public async Task RestorePackageAsync( token.ThrowIfCancellationRequested(); nuGetProjectContext.Log(MessageLevel.Info, string.Format(Strings.RestoringPackage, packageIdentity)); + var enabledSources = (sourceRepositories != null && sourceRepositories.Any()) ? sourceRepositories : + SourceRepositoryProvider.GetRepositories().Where(e => e.PackageSource.IsEnabled); token.ThrowIfCancellationRequested(); - IEnumerable enabledSources = GetEnabledSources(sourceRepositories, packageIdentity); - using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( enabledSources, packageIdentity, @@ -3407,25 +3406,6 @@ public async Task RestorePackageAsync( return true; } - private IEnumerable GetEnabledSources(IEnumerable sourceRepositories, PackageIdentity packageIdentity) - { - var enabledSources = (sourceRepositories != null && sourceRepositories.Any()) ? sourceRepositories : - SourceRepositoryProvider.GetRepositories().Where(e => e.PackageSource.IsEnabled).ToList(); - - if (PackageNamespaces?.AreNamespacesEnabled == true) - { - IReadOnlyList sources = PackageNamespaces.GetConfiguredPackageSources(packageIdentity.Id); - - if (sources == null || sources.Count == 0) - { - return Array.Empty(); - } - - return enabledSources.Where(s => sources.Contains(s.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)).ToList(); - } - return enabledSources; - } - public Task CopySatelliteFilesAsync(PackageIdentity packageIdentity, INuGetProjectContext nuGetProjectContext, CancellationToken token) { return PackagesFolderNuGetProject.CopySatelliteFilesAsync(packageIdentity, nuGetProjectContext, token); diff --git a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs index d1700cc61cb..292d8bb30aa 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs @@ -125,6 +125,16 @@ public static async Task GetDownloadResourceResultAsync( foreach (SourceRepository source in sourceGroup) { + if (isPackageNamespaceEnabled) + { + if (configuredPackageSources != null && + !configuredPackageSources.Contains(source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)) + { + // This package's id prefix is not defined in current package source, let's skip. + continue; + } + } + var task = GetDownloadResourceResultAsync( source, packageIdentity, From cc662a639892f5155b902665203d2e130a1b9e16 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Sat, 21 Aug 2021 14:28:28 -0700 Subject: [PATCH 14/27] 2 --- src/NuGet.Core/NuGet.PackageManagement/PublicAPI.Unshipped.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.PackageManagement/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.PackageManagement/PublicAPI.Unshipped.txt index e4ada60c8d9..e008ad3161c 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.PackageManagement/PublicAPI.Unshipped.txt @@ -1,5 +1,4 @@ NuGet.PackageManagement.GatherContext.GatherContext(NuGet.Configuration.PackageNamespacesConfiguration packageNamespacesConfiguration) -> void NuGet.PackageManagement.GatherContext.PackageNamespacesConfiguration.get -> NuGet.Configuration.PackageNamespacesConfiguration const NuGet.ProjectManagement.ProjectBuildProperties.AssemblyName = "AssemblyName" -> string -NuGet.PackageManagement.NuGetPackageManager.PackageNamespaces.get -> NuGet.Configuration.PackageNamespacesConfiguration const NuGet.ProjectManagement.ProjectBuildProperties.CLRSupport = "CLRSupport" -> string From a7846dcb37a4f6edd07ff7a217a2ca90a78a944b Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Sat, 21 Aug 2021 15:09:32 -0700 Subject: [PATCH 15/27] 3 --- src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs index 292d8bb30aa..da0e525d2c7 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs @@ -127,7 +127,8 @@ public static async Task GetDownloadResourceResultAsync( { if (isPackageNamespaceEnabled) { - if (configuredPackageSources != null && + if (configuredPackageSources == null || + configuredPackageSources.Count == 0 || !configuredPackageSources.Contains(source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)) { // This package's id prefix is not defined in current package source, let's skip. From cb5cc3f518dcce8964b40d5a9370b66eb30509f1 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Mon, 23 Aug 2021 17:27:42 -0700 Subject: [PATCH 16/27] Address PR comment --- .../NuGet.CommandLine/Commands/InstallCommand.cs | 1 + .../NuGet.CommandLine.Test/RestoreNETCoreTest.cs | 14 ++++++-------- .../MsbuildRestoreTaskTests.cs | 11 ++++------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs b/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs index d94f7fe58fa..7db6da712c0 100644 --- a/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs +++ b/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs @@ -252,6 +252,7 @@ private CommandLineSourceRepositoryProvider GetSourceRepositoryProvider() { return new CommandLineSourceRepositoryProvider(SourceProvider); } + private async Task InstallPackageAsync( string packageId, NuGetVersion version, diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs index 2292e189a1f..81f432c3c92 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs @@ -10858,7 +10858,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( public async Task RestoreCommand_NameSpaceFilter_PR_WithAllRestoreSources_Properies_Succeed() { // Arrange - var pathContext = new SimpleTestPathContext(); + using var pathContext = new SimpleTestPathContext(); // Set up solution, project, and packages var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); var workingPath = pathContext.WorkingDirectory; @@ -10962,19 +10962,17 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'PrivateRepository'", r.Output); Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); var contosoRestorePath = Path.Combine(packagePath, packageContosoMvcReal.Id.ToString(), packageContosoMvcReal.Version.ToString(), packageContosoMvcReal.ToString() + ".nupkg"); - using (var nupkgReader = new PackageArchiveReader(contosoRestorePath)) - { - var allFiles = nupkgReader.GetFiles().ToList(); - // Assert correct Contoso package was restored. - Assert.True(allFiles.Contains("lib/net461/realA.dll")); - } + var localResolver = new VersionFolderPathResolver(packagePath); + var contosoMvcMetadataPath = localResolver.GetNupkgMetadataPath(packageContosoMvcReal.Identity.Id, packageContosoMvcReal.Identity.Version); + NupkgMetadataFile contosoMvcmetadata = NupkgMetadataFileFormat.Read(contosoMvcMetadataPath); + Assert.Equal(privateRepositoryPath, contosoMvcmetadata.Source); } [Fact] public async Task RestoreCommand_NameSpaceFilter_PR_WithNotEnoughRestoreSources_Property_Fails() { // Arrange - var pathContext = new SimpleTestPathContext(); + using var pathContext = new SimpleTestPathContext(); // Set up solution, project, and packages var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); var workingPath = pathContext.WorkingDirectory; diff --git a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs index 17aa72dd6e3..ae219d0c23b 100644 --- a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs +++ b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs @@ -1779,13 +1779,10 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( Assert.True(r.ExitCode == 0); Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'PrivateRepository'", r.Output); Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); - var contosoRestorePath = Path.Combine(pathContext.UserPackagesFolder, packageContosoMvcReal.Id.ToString(), packageContosoMvcReal.Version.ToString(), packageContosoMvcReal.ToString() + ".nupkg"); - using (var nupkgReader = new PackageArchiveReader(contosoRestorePath)) - { - var allFiles = nupkgReader.GetFiles().ToList(); - // Assert correct Contoso package was restored. - Assert.True(allFiles.Contains("lib/net461/realA.dll")); - } + var localResolver = new VersionFolderPathResolver(pathContext.UserPackagesFolder); + var contosoMvcMetadataPath = localResolver.GetNupkgMetadataPath(packageContosoMvcReal.Identity.Id, packageContosoMvcReal.Identity.Version); + NupkgMetadataFile contosoMvcmetadata = NupkgMetadataFileFormat.Read(contosoMvcMetadataPath); + Assert.Equal(privateRepositoryPath, contosoMvcmetadata.Source); } } From cd27e32dab8447754a72fe0f6b72c64488ffe065 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 23 Aug 2021 21:26:00 -0700 Subject: [PATCH 17/27] Add project type in End2end tests. --- test/EndToEnd/tests/PackageNameSpaceTests.ps1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/EndToEnd/tests/PackageNameSpaceTests.ps1 b/test/EndToEnd/tests/PackageNameSpaceTests.ps1 index 7a1f0fd4dab..4b0fe807346 100644 --- a/test/EndToEnd/tests/PackageNameSpaceTests.ps1 +++ b/test/EndToEnd/tests/PackageNameSpaceTests.ps1 @@ -326,7 +326,7 @@ function Test-VsPackageInstallerServices-PackageNamespaceInstall-WithMultipleFee } } -function Test-PackageNamespaceInstall_Succeed +function Test-PC-PackageNamespaceInstall-Succeed { param($context) @@ -367,7 +367,7 @@ function Test-PackageNamespaceInstall_Succeed } } -function Test-PackageNamespaceInstall_WrongSource_Fails +function Test-PC-PackageNamespaceInstall-WrongSource-Fails { param($context) @@ -408,7 +408,7 @@ function Test-PackageNamespaceInstall_WrongSource_Fails } } -function Test-PackageNamespaceInstall_NonExistingSource_Fails +function Test-PC-PackageNamespaceInstall-NonExistingSource-Fails { param($context) @@ -444,7 +444,7 @@ function Test-PackageNamespaceInstall_NonExistingSource_Fails } } -function Test-PackageNamespaceInstall_Pass_CorrectSourceOption_Succeed +function Test-PC-PackageNamespaceInstall-Pass-CorrectSourceOption-Succeed { param($context) @@ -503,7 +503,7 @@ function Test-PackageNamespaceInstall_Pass_CorrectSourceOption_Succeed } } -function Test-PackageNamespaceInstall_Pass_WrongSourceOption_Fails +function Test-PC-PackageNamespaceInstall-Pass-WrongSourceOption-Fails { param($context) @@ -552,7 +552,7 @@ function Test-PackageNamespaceInstall_Pass_WrongSourceOption_Fails } } -function Test-PackageNamespaceUpdate_Pass_CorrectSourceOption_Succeed +function Test-PC-PackageNamespaceUpdate-Pass-CorrectSourceOption-Succeed { param($context) From cd3bc758084f5a894b130b2c91756dede8a4e25b Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 23 Aug 2021 23:17:53 -0700 Subject: [PATCH 18/27] Remove unrelated test --- test/EndToEnd/tests/PackageNameSpaceTests.ps1 | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/test/EndToEnd/tests/PackageNameSpaceTests.ps1 b/test/EndToEnd/tests/PackageNameSpaceTests.ps1 index 4b0fe807346..9903a41cc6b 100644 --- a/test/EndToEnd/tests/PackageNameSpaceTests.ps1 +++ b/test/EndToEnd/tests/PackageNameSpaceTests.ps1 @@ -408,42 +408,6 @@ function Test-PC-PackageNamespaceInstall-WrongSource-Fails } } -function Test-PC-PackageNamespaceInstall-NonExistingSource-Fails -{ - param($context) - - # Arrange - $nugetConfigPath = Join-Path $OutputPath 'nuget.config' - $settingFileContent =@" - - - - - - - - - - - -"@ - try { - # We have to create config file before creating solution, otherwise it's not effective for new solutions. - $settingFileContent -f $context.RepositoryRoot | Out-File -Encoding "UTF8" $nugetConfigPath - - $p = New-ConsoleApplication - - # Act & Assert - # Even though SolutionLevelPkg package exist in $repoDirectory since package namespace filter set SolutionLevelPkg can be restored only from UnavailableRepository repository so it'll fail. - $exceptionMessage = "Argument cannot be null or empty`r`nParameter name: primarySources" - Assert-Throws { $p | Install-Package SolutionLevelPkg -Version 1.0 } $exceptionMessage - Assert-NoPackage $p SolutionLevelPkg 1.0.0 - } - finally { - Remove-Item $nugetConfigPath - } -} - function Test-PC-PackageNamespaceInstall-Pass-CorrectSourceOption-Succeed { param($context) From b2648b75cc56324eec0a4a85312e7b3103cb82af Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Tue, 24 Aug 2021 09:25:43 -0700 Subject: [PATCH 19/27] Revert unneeded tests. --- .../Commands/RestoreCommandTests.cs | 205 ------------------ 1 file changed, 205 deletions(-) diff --git a/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs b/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs index c94e327add0..3e06a68a0ad 100644 --- a/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs +++ b/test/NuGet.Clients.FuncTests/NuGet.CommandLine.FuncTest/Commands/RestoreCommandTests.cs @@ -1168,211 +1168,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } - [Fact] - public async Task Restore_PackageNamespace_PassAllNecessarySourceOption_Succeed() - { - // Arrange - using (var pathContext = new SimpleTestPathContext()) - { - // Set up solution, project, and packages - var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); - - var net461 = NuGetFramework.Parse("net461"); - - var projectA = SimpleTestProjectContext.CreateLegacyPackageReference( - "a", - pathContext.SolutionRoot, - net461); - var projectAPackages = Path.Combine(pathContext.SolutionRoot, "packages"); - - var externalRepositoryPath = Path.Combine(pathContext.SolutionRoot, "ExternalRepository"); - Directory.CreateDirectory(externalRepositoryPath); - - var contosoRepositoryPath = Path.Combine(pathContext.SolutionRoot, "ContosoRepository"); - Directory.CreateDirectory(contosoRepositoryPath); - - var configPath = Path.Combine(pathContext.WorkingDirectory, "nuget.config"); - SettingsTestUtils.CreateConfigurationFile(configPath, $@" - - - - - - - - - - - - - - - - - -"); - - var ContosoReal = new SimpleTestPackageContext() - { - Id = "Contoso.A", - Version = "1.0.0" - }; - ContosoReal.AddFile("lib/net461/contosoA.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - contosoRepositoryPath, - PackageSaveMode.Defaultv3, - ContosoReal); - - var ExternalContoso = new SimpleTestPackageContext() - { - Id = "Contoso.A", // This package exist both repo. - Version = "1.0.0" - }; - ExternalContoso.AddFile("lib/net461/externalA.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - externalRepositoryPath, - PackageSaveMode.Defaultv3, - ExternalContoso); - - var ExternalB = new SimpleTestPackageContext() - { - Id = "External.B", - Version = "2.0.0" - }; - ExternalB.AddFile("lib/net461/externalB.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - externalRepositoryPath, - PackageSaveMode.Defaultv3, - ExternalB); - - Util.CreateFile(Path.GetDirectoryName(projectA.ProjectPath), "packages.config", -@" - - -"); - - solution.Projects.Add(projectA); - solution.Create(pathContext.SolutionRoot); - - // Act - var result = RunRestore(pathContext, _successExitCode, new string[] { "-source", $"{externalRepositoryPath};{contosoRepositoryPath}" }); - - // Assert - var contosoRestorePath = Path.Combine(projectAPackages, ContosoReal.ToString(), ContosoReal.ToString() + ".nupkg"); - using (var nupkgReader = new PackageArchiveReader(contosoRestorePath)) - { - var allFiles = nupkgReader.GetFiles().ToList(); - // Assert correct Contoso package from Contoso repository was restored. - Assert.Contains("lib/net461/contosoA.dll", allFiles); - } - var externalRestorePath = Path.Combine(projectAPackages, ExternalB.ToString(), ExternalB.ToString() + ".nupkg"); - Assert.True(File.Exists(externalRestorePath)); - } - } - - [Fact] - public async Task Restore_PackageNamespace_PassNotEnoughSourceOption_Fails() - { - // Arrange - using (var pathContext = new SimpleTestPathContext()) - { - // Set up solution, project, and packages - var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); - - var net461 = NuGetFramework.Parse("net461"); - - var projectA = SimpleTestProjectContext.CreateLegacyPackageReference( - "a", - pathContext.SolutionRoot, - net461); - var projectAPackages = Path.Combine(pathContext.SolutionRoot, "packages"); - - var externalRepositoryPath = Path.Combine(pathContext.SolutionRoot, "ExternalRepository"); - Directory.CreateDirectory(externalRepositoryPath); - - var contosoRepositoryPath = Path.Combine(pathContext.SolutionRoot, "ContosoRepository"); - Directory.CreateDirectory(contosoRepositoryPath); - - var configPath = Path.Combine(pathContext.WorkingDirectory, "nuget.config"); - SettingsTestUtils.CreateConfigurationFile(configPath, $@" - - - - - - - - - - - - - - - - - -"); - - var ContosoReal = new SimpleTestPackageContext() - { - Id = "Contoso.A", - Version = "1.0.0" - }; - ContosoReal.AddFile("lib/net461/contosoA.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - contosoRepositoryPath, - PackageSaveMode.Defaultv3, - ContosoReal); - - var ExternalContoso = new SimpleTestPackageContext() - { - Id = "Contoso.A", // This package exist both repo. - Version = "1.0.0" - }; - ExternalContoso.AddFile("lib/net461/externalA.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - externalRepositoryPath, - PackageSaveMode.Defaultv3, - ExternalContoso); - - var ExternalB = new SimpleTestPackageContext() - { - Id = "External.B", - Version = "2.0.0" - }; - ExternalB.AddFile("lib/net461/externalB.dll"); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - externalRepositoryPath, - PackageSaveMode.Defaultv3, - ExternalB); - - Util.CreateFile(Path.GetDirectoryName(projectA.ProjectPath), "packages.config", -@" - - -"); - - solution.Projects.Add(projectA); - solution.Create(pathContext.SolutionRoot); - - // Act - // contosoRepositoryPath is not passed in source option so it should fail. - var result = RunRestore(pathContext, _failureExitCode, new string[] { "-source", $"{externalRepositoryPath}" }); - - // Assert - var contosoRestorePath = Path.Combine(projectAPackages, ContosoReal.ToString(), ContosoReal.ToString() + ".nupkg"); - Assert.False(File.Exists(contosoRestorePath)); - Assert.Contains("Unable to find version '1.0.0' of package 'Contoso.A'.", result.AllOutput); - } - } - public static string GetResource(string name) { using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(name))) From 685702456585e6731a6437641e4bcc8c3bad0799 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Tue, 24 Aug 2021 10:19:40 -0700 Subject: [PATCH 20/27] Address PR comments --- .../RestoreNETCoreTest.cs | 51 +++-------------- .../MsbuildRestoreTaskTests.cs | 55 +++---------------- 2 files changed, 16 insertions(+), 90 deletions(-) diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs index 81f432c3c92..79bb4e92811 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs @@ -10869,7 +10869,6 @@ public async Task RestoreCommand_NameSpaceFilter_PR_WithAllRestoreSources_Proper Directory.CreateDirectory(privateRepositoryPath); var net461 = NuGetFramework.Parse("net461"); - var projectA = new SimpleTestProjectContext( "a", ProjectStyle.PackageReference, @@ -10879,26 +10878,13 @@ public async Task RestoreCommand_NameSpaceFilter_PR_WithAllRestoreSources_Proper // Add both repositories as RestoreSources projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath};{privateRepositoryPath}"); - var packageOpenSourceA = new SimpleTestPackageContext() - { - Id = "Contoso.Opensource.A", - Version = "1.0.0" - }; - + var packageOpenSourceA = new SimpleTestPackageContext("Contoso.Opensource.A", "1.0.0"); packageOpenSourceA.AddFile("lib/net461/openA.dll"); - var packageOpenSourceContosoMvc = new SimpleTestPackageContext() - { - Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package - Version = "1.0.0" - }; + var packageOpenSourceContosoMvc = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); // Package Id conflict with internally created package packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); - var packageContosoMvcReal = new SimpleTestPackageContext() - { - Id = "Contoso.MVC.ASP", - Version = "1.0.0" - }; + var packageContosoMvcReal = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); packageContosoMvcReal.AddFile("lib/net461/realA.dll"); projectA.AddPackageToAllFrameworks(packageOpenSourceA); @@ -10935,13 +10921,9 @@ public async Task RestoreCommand_NameSpaceFilter_PR_WithAllRestoreSources_Proper await SimpleTestPackageUtility.CreateFolderFeedV3Async( opensourceRepositoryPath, - packageOpenSourceA); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, + packageOpenSourceA, packageOpenSourceContosoMvc); - await SimpleTestPackageUtility.CreateFolderFeedV3Async( privateRepositoryPath, packageContosoMvcReal); @@ -10983,7 +10965,6 @@ public async Task RestoreCommand_NameSpaceFilter_PR_WithNotEnoughRestoreSources_ Directory.CreateDirectory(privateRepositoryPath); var net461 = NuGetFramework.Parse("net461"); - var projectA = new SimpleTestProjectContext( "a", ProjectStyle.PackageReference, @@ -10993,26 +10974,13 @@ public async Task RestoreCommand_NameSpaceFilter_PR_WithNotEnoughRestoreSources_ // Add only 1 repository as RestoreSources projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath}"); - var packageOpenSourceA = new SimpleTestPackageContext() - { - Id = "Contoso.Opensource.A", - Version = "1.0.0" - }; - + var packageOpenSourceA = new SimpleTestPackageContext("Contoso.Opensource.A", "1.0.0"); packageOpenSourceA.AddFile("lib/net461/openA.dll"); - var packageOpenSourceContosoMvc = new SimpleTestPackageContext() - { - Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package - Version = "1.0.0" - }; + var packageOpenSourceContosoMvc = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); // Package Id conflict with internally created package packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); - var packageContosoMvcReal = new SimpleTestPackageContext() - { - Id = "Contoso.MVC.ASP", - Version = "1.0.0" - }; + var packageContosoMvcReal = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); packageContosoMvcReal.AddFile("lib/net461/realA.dll"); projectA.AddPackageToAllFrameworks(packageOpenSourceA); @@ -11049,10 +11017,7 @@ public async Task RestoreCommand_NameSpaceFilter_PR_WithNotEnoughRestoreSources_ await SimpleTestPackageUtility.CreateFolderFeedV3Async( opensourceRepositoryPath, - packageOpenSourceA); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, + packageOpenSourceA, packageOpenSourceContosoMvc); await SimpleTestPackageUtility.CreateFolderFeedV3Async( diff --git a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs index ae219d0c23b..68c666fdc3d 100644 --- a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs +++ b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs @@ -1695,7 +1695,6 @@ public async Task MsbuildRestore_PR_PackageNamespace_WithAllRestoreSources_Prope Directory.CreateDirectory(privateRepositoryPath); var net461 = NuGetFramework.Parse("net461"); - var projectA = new SimpleTestProjectContext( "a", ProjectStyle.PackageReference, @@ -1705,31 +1704,17 @@ public async Task MsbuildRestore_PR_PackageNamespace_WithAllRestoreSources_Prope // Add both repositories as RestoreSources projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath};{privateRepositoryPath}"); - var packageOpenSourceA = new SimpleTestPackageContext() - { - Id = "Contoso.Opensource.A", - Version = "1.0.0" - }; - + var packageOpenSourceA = new SimpleTestPackageContext("Contoso.Opensource.A", "1.0.0"); packageOpenSourceA.AddFile("lib/net461/openA.dll"); - var packageOpenSourceContosoMvc = new SimpleTestPackageContext() - { - Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package - Version = "1.0.0" - }; + var packageOpenSourceContosoMvc = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); // Package Id conflict with internally created package packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); - var packageContosoMvcReal = new SimpleTestPackageContext() - { - Id = "Contoso.MVC.ASP", - Version = "1.0.0" - }; + var packageContosoMvcReal = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); packageContosoMvcReal.AddFile("lib/net461/realA.dll"); projectA.AddPackageToAllFrameworks(packageOpenSourceA); projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); - solution.Projects.Add(projectA); solution.Create(pathContext.SolutionRoot); @@ -1761,13 +1746,9 @@ public async Task MsbuildRestore_PR_PackageNamespace_WithAllRestoreSources_Prope await SimpleTestPackageUtility.CreateFolderFeedV3Async( opensourceRepositoryPath, - packageOpenSourceA); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, + packageOpenSourceA, packageOpenSourceContosoMvc); - await SimpleTestPackageUtility.CreateFolderFeedV3Async( privateRepositoryPath, packageContosoMvcReal); @@ -1802,7 +1783,6 @@ public async Task MsbuildRestore_PR_PackageNamespace_WithNotEnoughRestoreSources Directory.CreateDirectory(privateRepositoryPath); var net461 = NuGetFramework.Parse("net461"); - var projectA = new SimpleTestProjectContext( "a", ProjectStyle.PackageReference, @@ -1812,32 +1792,17 @@ public async Task MsbuildRestore_PR_PackageNamespace_WithNotEnoughRestoreSources // Add only 1 repository as RestoreSources projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath}"); - var packageOpenSourceA = new SimpleTestPackageContext() - { - Id = "Contoso.Opensource.A", - Version = "1.0.0" - }; - + var packageOpenSourceA = new SimpleTestPackageContext("Contoso.Opensource.A", "1.0.0"); packageOpenSourceA.AddFile("lib/net461/openA.dll"); - var packageOpenSourceContosoMvc = new SimpleTestPackageContext() - { - Id = "Contoso.MVC.ASP", // Package Id conflict with internally created package - Version = "1.0.0" - }; - packageOpenSourceContosoMvc.Files.Clear(); + var packageOpenSourceContosoMvc = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); // Package Id conflict with internally created package packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); - var packageContosoMvcReal = new SimpleTestPackageContext() - { - Id = "Contoso.MVC.ASP", - Version = "1.0.0" - }; + var packageContosoMvcReal = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); packageContosoMvcReal.AddFile("lib/net461/realA.dll"); projectA.AddPackageToAllFrameworks(packageOpenSourceA); projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); - solution.Projects.Add(projectA); solution.Create(pathContext.SolutionRoot); @@ -1869,13 +1834,9 @@ public async Task MsbuildRestore_PR_PackageNamespace_WithNotEnoughRestoreSources await SimpleTestPackageUtility.CreateFolderFeedV3Async( opensourceRepositoryPath, - packageOpenSourceA); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, + packageOpenSourceA, packageOpenSourceContosoMvc); - await SimpleTestPackageUtility.CreateFolderFeedV3Async( privateRepositoryPath, packageContosoMvcReal); From 2f48afd89b07dc44d5d0d6f67c63c17d4759a37e Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Tue, 24 Aug 2021 13:50:38 -0700 Subject: [PATCH 21/27] Clean up. --- .../NuGetInstallCommandTest.cs | 160 ++++++------ .../NuGetRestoreCommandTest.cs | 140 +++++------ .../MsbuildRestoreTaskTests.cs | 238 +++++++++--------- 3 files changed, 260 insertions(+), 278 deletions(-) diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs index ac9671c0b0c..3d45dae8efd 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs @@ -1870,20 +1870,19 @@ public async Task InstallCommand_SpecifyUnlistedVersion_InstallUnlistedPackagesA public void InstallCommand_NameSpaceFilter_Succeed() { // Arrange - using (var pathContext = new SimpleTestPathContext()) - { - var workingPath = pathContext.WorkingDirectory; + using var pathContext = new SimpleTestPathContext(); + var workingPath = pathContext.WorkingDirectory; - var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); - Directory.CreateDirectory(opensourceRepositoryPath); - Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); - var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); - Directory.CreateDirectory(sharedRepositoryPath); - Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); - var configPath = Path.Combine(workingPath, "nuget.config"); - SettingsTestUtils.CreateConfigurationFile(configPath, $@" + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" @@ -1901,39 +1900,37 @@ public void InstallCommand_NameSpaceFilter_Succeed() "); - // Act - var r1 = RunInstall(pathContext, "Contoso.MVC.ASP", 0, "-Version", "1.0.0", "-OutputDirectory", "outputDir", "-Verbosity", "d"); - var r2 = RunInstall(pathContext, "Contoso.Opensource", 0, "-Version", "1.0.0", "-OutputDirectory", "outputDir", "-Verbosity", "d"); + // Act + var r1 = RunInstall(pathContext, "Contoso.MVC.ASP", 0, "-Version", "1.0.0", "-OutputDirectory", "outputDir", "-Verbosity", "d"); + var r2 = RunInstall(pathContext, "Contoso.Opensource", 0, "-Version", "1.0.0", "-OutputDirectory", "outputDir", "-Verbosity", "d"); - // Assert - Assert.Equal(0, r1.Item1); - Assert.Equal(0, r2.Item1); - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r1.Output); - var packageFileContosoMVCASP = Path.Combine(workingPath, "outputDir", "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); - var packageFileContosoOpensource = Path.Combine(workingPath, "outputDir", "Contoso.Opensource.1.0.0", "Contoso.Opensource.1.0.0.nupkg"); - Assert.True(File.Exists(packageFileContosoMVCASP)); - Assert.True(File.Exists(packageFileContosoOpensource)); - } + // Assert + Assert.Equal(0, r1.Item1); + Assert.Equal(0, r2.Item1); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r1.Output); + var packageFileContosoMVCASP = Path.Combine(workingPath, "outputDir", "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); + var packageFileContosoOpensource = Path.Combine(workingPath, "outputDir", "Contoso.Opensource.1.0.0", "Contoso.Opensource.1.0.0.nupkg"); + Assert.True(File.Exists(packageFileContosoMVCASP)); + Assert.True(File.Exists(packageFileContosoOpensource)); } [Fact] public void InstallCommand_NameSpaceFilter_Fails() { // Arrange - using (var pathContext = new SimpleTestPathContext()) - { - var workingPath = pathContext.WorkingDirectory; + using var pathContext = new SimpleTestPathContext(); + var workingPath = pathContext.WorkingDirectory; - var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); - Directory.CreateDirectory(opensourceRepositoryPath); - Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); - Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", opensourceRepositoryPath); //This package supposed to be restored from other repo. + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", opensourceRepositoryPath); //This package supposed to be restored from other repo. - var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); - Directory.CreateDirectory(sharedRepositoryPath); + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); - var configPath = Path.Combine(workingPath, "nuget.config"); - SettingsTestUtils.CreateConfigurationFile(configPath, $@" + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" @@ -1951,41 +1948,39 @@ public void InstallCommand_NameSpaceFilter_Fails() "); - // Act - var r = RunInstall(pathContext, "Contoso.MVC.ASP", 1, "-Version", "1.0.0", "-OutputDirectory", "outputDir"); + // Act + var r = RunInstall(pathContext, "Contoso.MVC.ASP", 1, "-Version", "1.0.0", "-OutputDirectory", "outputDir"); - // Assert - Assert.Equal(1, r.Item1); - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); - r.AllOutput.Should().NotContain("NU1000"); - r.Errors.Should().Contain("Package 'Contoso.MVC.ASP 1.0.0' is not found in the following primary source(s):"); - } + // Assert + Assert.Equal(1, r.Item1); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); + r.AllOutput.Should().NotContain("NU1000"); + r.Errors.Should().Contain("Package 'Contoso.MVC.ASP 1.0.0' is not found in the following primary source(s):"); } [Fact] public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_AllSourceOptions_Succeed() { // Arrange - using (var pathContext = new SimpleTestPathContext()) - { - var workingPath = pathContext.WorkingDirectory; + using var pathContext = new SimpleTestPathContext(); + var workingPath = pathContext.WorkingDirectory; - var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); - Directory.CreateDirectory(opensourceRepositoryPath); - Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); - var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); - Directory.CreateDirectory(sharedRepositoryPath); - Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); - Util.CreateFile(pathContext.SolutionRoot, "packages.config", + Util.CreateFile(pathContext.SolutionRoot, "packages.config", @" "); - var configPath = Path.Combine(workingPath, "nuget.config"); - SettingsTestUtils.CreateConfigurationFile(configPath, $@" + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" @@ -2003,43 +1998,41 @@ public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_AllSo "); - // Act - var r = RunInstall(pathContext, Path.Combine(pathContext.SolutionRoot, "packages.config"), 0, "-OutputDirectory", "outputDir", "-Source", - $"{opensourceRepositoryPath};{sharedRepositoryPath}"); // We pass both repositories. + // Act + var r = RunInstall(pathContext, Path.Combine(pathContext.SolutionRoot, "packages.config"), 0, "-OutputDirectory", "outputDir", "-Source", + $"{opensourceRepositoryPath};{sharedRepositoryPath}"); // We pass both repositories. - // Assert - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); - var packageFileContosoMVCASP = Path.Combine(workingPath, "outputDir", "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); - var packageFileContosoOpensource = Path.Combine(workingPath, "outputDir", "Contoso.Opensource.1.0.0", "Contoso.Opensource.1.0.0.nupkg"); - Assert.True(File.Exists(packageFileContosoMVCASP)); - Assert.True(File.Exists(packageFileContosoOpensource)); - } + // Assert + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); + var packageFileContosoMVCASP = Path.Combine(workingPath, "outputDir", "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); + var packageFileContosoOpensource = Path.Combine(workingPath, "outputDir", "Contoso.Opensource.1.0.0", "Contoso.Opensource.1.0.0.nupkg"); + Assert.True(File.Exists(packageFileContosoMVCASP)); + Assert.True(File.Exists(packageFileContosoOpensource)); } [Fact] public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_NotEnoughSources_Fails() { // Arrange - using (var pathContext = new SimpleTestPathContext()) - { - var workingPath = pathContext.WorkingDirectory; + using var pathContext = new SimpleTestPathContext(); + var workingPath = pathContext.WorkingDirectory; - var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); - Directory.CreateDirectory(opensourceRepositoryPath); - Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); - var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); - Directory.CreateDirectory(sharedRepositoryPath); - Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); - Util.CreateFile(pathContext.SolutionRoot, "packages.config", + Util.CreateFile(pathContext.SolutionRoot, "packages.config", @" "); - var configPath = Path.Combine(workingPath, "nuget.config"); - SettingsTestUtils.CreateConfigurationFile(configPath, $@" + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" @@ -2057,15 +2050,14 @@ public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_NotEn "); - // Act - var r = RunInstall(pathContext, Path.Combine(pathContext.SolutionRoot, "packages.config"), 1, "-OutputDirectory", "outputDir", "-Source", - opensourceRepositoryPath); // We pass 1 repository. + // Act + var r = RunInstall(pathContext, Path.Combine(pathContext.SolutionRoot, "packages.config"), 1, "-OutputDirectory", "outputDir", "-Source", + opensourceRepositoryPath); // We pass 1 repository. - // Assert - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); - r.AllOutput.Should().NotContain("NU1000"); - r.Errors.Should().Contain("Unable to find version '1.0.0' of package 'Contoso.MVC.ASP'."); - } + // Assert + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); + r.AllOutput.Should().NotContain("NU1000"); + r.Errors.Should().Contain("Unable to find version '1.0.0' of package 'Contoso.MVC.ASP'."); } public static CommandRunnerResult RunInstall(SimpleTestPathContext pathContext, string input, int expectedExitCode = 0, params string[] additionalArgs) diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs index 0eeb0f7a84b..57d5fdf1ade 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs @@ -2970,16 +2970,14 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_AllSourceOptions_Succeed() { // Arrange var nugetexe = Util.GetNuGetExePath(); - - using (var workingPath = TestDirectory.Create()) - { - var proj1Directory = Path.Combine(workingPath, "proj1"); - Directory.CreateDirectory(proj1Directory); - - var proj1File = Path.Combine(proj1Directory, "proj1.csproj"); - File.WriteAllText( - proj1File, - @" Library @@ -2991,22 +2989,22 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_AllSourceOptions_Succeed() "); - var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); - Directory.CreateDirectory(opensourceRepositoryPath); - Util.CreateTestPackage("Contoso.Opensource.A", "1.0.0", opensourceRepositoryPath); + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource.A", "1.0.0", opensourceRepositoryPath); - var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); - Directory.CreateDirectory(sharedRepositoryPath); - Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); - Util.CreateFile(proj1Directory, "packages.config", + Util.CreateFile(proj1Directory, "packages.config", @" "); - var configPath = Path.Combine(workingPath, "nuget.config"); - SettingsTestUtils.CreateConfigurationFile(configPath, $@" + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" @@ -3024,10 +3022,10 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_AllSourceOptions_Succeed() "); - var packagePath = Path.Combine(workingPath, "packages"); + var packagePath = Path.Combine(workingPath, "packages"); - string[] args = new string[] - { + string[] args = new string[] + { "restore", proj1File, "-solutionDir", @@ -3036,21 +3034,20 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_AllSourceOptions_Succeed() "d", "-source", sharedRepositoryPath + ';' + opensourceRepositoryPath //We pass both repositories as source - }; - - // Act - var r = CommandRunner.Run( - nugetexe, - workingPath, - string.Join(" ", args), - waitForExit: true); + }; - // Assert - Assert.Equal(_successCode, r.ExitCode); - // If we pass source then log include actual path to repository instead of repository name. - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); - Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); - } + // Act + var r = CommandRunner.Run( + nugetexe, + workingPath, + string.Join(" ", args), + waitForExit: true); + + // Assert + Assert.Equal(_successCode, r.ExitCode); + // If we pass source then log include actual path to repository instead of repository name. + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); } [Fact] @@ -3058,16 +3055,14 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_NotEnoughSources_Fails() { // Arrange var nugetexe = Util.GetNuGetExePath(); - - using (var workingPath = TestDirectory.Create()) - { - var proj1Directory = Path.Combine(workingPath, "proj1"); - Directory.CreateDirectory(proj1Directory); - - var proj1File = Path.Combine(proj1Directory, "proj1.csproj"); - File.WriteAllText( - proj1File, - @" Library @@ -3079,23 +3074,23 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_NotEnoughSources_Fails() "); - var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); - Directory.CreateDirectory(opensourceRepositoryPath); - Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); - Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", opensourceRepositoryPath); + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.Opensource", "1.0.0", opensourceRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", opensourceRepositoryPath); - var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); - Directory.CreateDirectory(sharedRepositoryPath); - Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); + var sharedRepositoryPath = Path.Combine(workingPath, "SharedRepository"); + Directory.CreateDirectory(sharedRepositoryPath); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath); - Util.CreateFile(proj1Directory, "packages.config", + Util.CreateFile(proj1Directory, "packages.config", @" "); - var configPath = Path.Combine(workingPath, "nuget.config"); - SettingsTestUtils.CreateConfigurationFile(configPath, $@" + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" @@ -3113,10 +3108,10 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_NotEnoughSources_Fails() "); - var packagePath = Path.Combine(workingPath, "packages"); + var packagePath = Path.Combine(workingPath, "packages"); - string[] args = new string[] - { + string[] args = new string[] + { "restore", proj1File, "-solutionDir", @@ -3125,21 +3120,20 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_NotEnoughSources_Fails() opensourceRepositoryPath, // We only pass 1 source here "-Verbosity", "d" - }; - - // Act - var r = CommandRunner.Run( - nugetexe, - workingPath, - string.Join(" ", args), - waitForExit: true); + }; - // Assert - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); - // Even though there is eligible source SharedRepository exist but only opensourceRepositoryPath passed as option it'll fail to restore. - Assert.Equal(_failureCode, r.ExitCode); - Assert.Contains("WARNING: Unable to find version '1.0.0' of package 'Contoso.MVC.ASP'.", r.Output); - } + // Act + var r = CommandRunner.Run( + nugetexe, + workingPath, + string.Join(" ", args), + waitForExit: true); + + // Assert + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", r.Output); + // Even though there is eligible source SharedRepository exist but only opensourceRepositoryPath passed as option it'll fail to restore. + Assert.Equal(_failureCode, r.ExitCode); + Assert.Contains("WARNING: Unable to find version '1.0.0' of package 'Contoso.MVC.ASP'.", r.Output); } private static byte[] GetResource(string name) diff --git a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs index 68c666fdc3d..fa2a8b12b33 100644 --- a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs +++ b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs @@ -1683,45 +1683,44 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( public async Task MsbuildRestore_PR_PackageNamespace_WithAllRestoreSources_Properies_Succeed() { // Arrange - using (var pathContext = new SimpleTestPathContext()) - { - // Set up solution, project, and packages - var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); - var workingPath = pathContext.WorkingDirectory; - var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); - Directory.CreateDirectory(opensourceRepositoryPath); - - var privateRepositoryPath = Path.Combine(workingPath, "PrivateRepository"); - Directory.CreateDirectory(privateRepositoryPath); - - var net461 = NuGetFramework.Parse("net461"); - var projectA = new SimpleTestProjectContext( - "a", - ProjectStyle.PackageReference, - pathContext.SolutionRoot); - projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); - - // Add both repositories as RestoreSources - projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath};{privateRepositoryPath}"); - - var packageOpenSourceA = new SimpleTestPackageContext("Contoso.Opensource.A", "1.0.0"); - packageOpenSourceA.AddFile("lib/net461/openA.dll"); - - var packageOpenSourceContosoMvc = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); // Package Id conflict with internally created package - packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); - - var packageContosoMvcReal = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); - packageContosoMvcReal.AddFile("lib/net461/realA.dll"); - - projectA.AddPackageToAllFrameworks(packageOpenSourceA); - projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); - solution.Projects.Add(projectA); - solution.Create(pathContext.SolutionRoot); - - // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, - // so we go ahead and replace that config before running MSBuild. - var configAPath = Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "NuGet.Config"); - var configText = + using var pathContext = new SimpleTestPathContext(); + // Set up solution, project, and packages + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + var workingPath = pathContext.WorkingDirectory; + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + + var privateRepositoryPath = Path.Combine(workingPath, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + var net461 = NuGetFramework.Parse("net461"); + var projectA = new SimpleTestProjectContext( + "a", + ProjectStyle.PackageReference, + pathContext.SolutionRoot); + projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); + + // Add both repositories as RestoreSources + projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath};{privateRepositoryPath}"); + + var packageOpenSourceA = new SimpleTestPackageContext("Contoso.Opensource.A", "1.0.0"); + packageOpenSourceA.AddFile("lib/net461/openA.dll"); + + var packageOpenSourceContosoMvc = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); // Package Id conflict with internally created package + packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); + + var packageContosoMvcReal = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); + packageContosoMvcReal.AddFile("lib/net461/realA.dll"); + + projectA.AddPackageToAllFrameworks(packageOpenSourceA); + projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, + // so we go ahead and replace that config before running MSBuild. + var configAPath = Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "NuGet.Config"); + var configText = $@" @@ -1739,77 +1738,75 @@ public async Task MsbuildRestore_PR_PackageNamespace_WithAllRestoreSources_Prope "; - using (var writer = new StreamWriter(configAPath)) - { - writer.Write(configText); - } - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, - packageOpenSourceA, - packageOpenSourceContosoMvc); - - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - privateRepositoryPath, - packageContosoMvcReal); - - // Act - var r = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore -v:d {pathContext.SolutionRoot}", ignoreExitCode: true); - - // Assert - Assert.True(r.ExitCode == 0); - Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'PrivateRepository'", r.Output); - Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); - var localResolver = new VersionFolderPathResolver(pathContext.UserPackagesFolder); - var contosoMvcMetadataPath = localResolver.GetNupkgMetadataPath(packageContosoMvcReal.Identity.Id, packageContosoMvcReal.Identity.Version); - NupkgMetadataFile contosoMvcmetadata = NupkgMetadataFileFormat.Read(contosoMvcMetadataPath); - Assert.Equal(privateRepositoryPath, contosoMvcmetadata.Source); + using (var writer = new StreamWriter(configAPath)) + { + writer.Write(configText); } + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceA, + packageOpenSourceContosoMvc); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + privateRepositoryPath, + packageContosoMvcReal); + + // Act + var r = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore -v:d {pathContext.SolutionRoot}", ignoreExitCode: true); + + // Assert + Assert.True(r.ExitCode == 0); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'PrivateRepository'", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); + var localResolver = new VersionFolderPathResolver(pathContext.UserPackagesFolder); + var contosoMvcMetadataPath = localResolver.GetNupkgMetadataPath(packageContosoMvcReal.Identity.Id, packageContosoMvcReal.Identity.Version); + NupkgMetadataFile contosoMvcmetadata = NupkgMetadataFileFormat.Read(contosoMvcMetadataPath); + Assert.Equal(privateRepositoryPath, contosoMvcmetadata.Source); } [PlatformFact(Platform.Windows)] public async Task MsbuildRestore_PR_PackageNamespace_WithNotEnoughRestoreSources_Property_Fails() { // Arrange - using (var pathContext = new SimpleTestPathContext()) - { - // Set up solution, project, and packages - var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); - var workingPath = pathContext.WorkingDirectory; - var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); - Directory.CreateDirectory(opensourceRepositoryPath); - - var privateRepositoryPath = Path.Combine(workingPath, "PrivateRepository"); - Directory.CreateDirectory(privateRepositoryPath); - - var net461 = NuGetFramework.Parse("net461"); - var projectA = new SimpleTestProjectContext( - "a", - ProjectStyle.PackageReference, - pathContext.SolutionRoot); - projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); - - // Add only 1 repository as RestoreSources - projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath}"); - - var packageOpenSourceA = new SimpleTestPackageContext("Contoso.Opensource.A", "1.0.0"); - packageOpenSourceA.AddFile("lib/net461/openA.dll"); - - var packageOpenSourceContosoMvc = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); // Package Id conflict with internally created package - packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); - - var packageContosoMvcReal = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); - packageContosoMvcReal.AddFile("lib/net461/realA.dll"); - - projectA.AddPackageToAllFrameworks(packageOpenSourceA); - projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); - solution.Projects.Add(projectA); - solution.Create(pathContext.SolutionRoot); - - // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, - // so we go ahead and replace that config before running MSBuild. - var configAPath = Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "NuGet.Config"); - var configText = + using var pathContext = new SimpleTestPathContext(); + // Set up solution, project, and packages + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + var workingPath = pathContext.WorkingDirectory; + var opensourceRepositoryPath = Path.Combine(workingPath, "PublicRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + + var privateRepositoryPath = Path.Combine(workingPath, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + var net461 = NuGetFramework.Parse("net461"); + var projectA = new SimpleTestProjectContext( + "a", + ProjectStyle.PackageReference, + pathContext.SolutionRoot); + projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); + + // Add only 1 repository as RestoreSources + projectA.Properties.Add("RestoreSources", $"{opensourceRepositoryPath}"); + + var packageOpenSourceA = new SimpleTestPackageContext("Contoso.Opensource.A", "1.0.0"); + packageOpenSourceA.AddFile("lib/net461/openA.dll"); + + var packageOpenSourceContosoMvc = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); // Package Id conflict with internally created package + packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); + + var packageContosoMvcReal = new SimpleTestPackageContext("Contoso.MVC.ASP", "1.0.0"); + packageContosoMvcReal.AddFile("lib/net461/realA.dll"); + + projectA.AddPackageToAllFrameworks(packageOpenSourceA); + projectA.AddPackageToAllFrameworks(packageOpenSourceContosoMvc); + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, + // so we go ahead and replace that config before running MSBuild. + var configAPath = Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "NuGet.Config"); + var configText = $@" @@ -1827,28 +1824,27 @@ public async Task MsbuildRestore_PR_PackageNamespace_WithNotEnoughRestoreSources "; - using (var writer = new StreamWriter(configAPath)) - { - writer.Write(configText); - } + using (var writer = new StreamWriter(configAPath)) + { + writer.Write(configText); + } - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - opensourceRepositoryPath, - packageOpenSourceA, - packageOpenSourceContosoMvc); + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceA, + packageOpenSourceContosoMvc); - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - privateRepositoryPath, - packageContosoMvcReal); + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + privateRepositoryPath, + packageContosoMvcReal); - // Act - var r = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore -v:d {pathContext.SolutionRoot}", ignoreExitCode: true); + // Act + var r = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore -v:d {pathContext.SolutionRoot}", ignoreExitCode: true); - // Assert - Assert.True(r.ExitCode == 1); - Assert.Contains($"Package namespace match not found for package ID 'Contoso.MVC.ASP'.", r.Output); - Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); - } + // Assert + Assert.True(r.ExitCode == 1); + Assert.Contains($"Package namespace match not found for package ID 'Contoso.MVC.ASP'.", r.Output); + Assert.Contains($"Package namespace matches found for package ID 'Contoso.Opensource.A' are: 'PublicRepository'", r.Output); } } } From c9a605bf49b89d4b443fe75002e3357a90e03e39 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 25 Aug 2021 10:56:06 -0700 Subject: [PATCH 22/27] Change comparison for international id. --- src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs | 2 +- .../NuGet.PackageManagement/Resolution/ResolverGather.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs index da0e525d2c7..baf7a8038c5 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs @@ -129,7 +129,7 @@ public static async Task GetDownloadResourceResultAsync( { if (configuredPackageSources == null || configuredPackageSources.Count == 0 || - !configuredPackageSources.Contains(source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)) + !configuredPackageSources.Contains(source.PackageSource.Name, StringComparer.OrdinalIgnoreCase)) { // This package's id prefix is not defined in current package source, let's skip. continue; diff --git a/src/NuGet.Core/NuGet.PackageManagement/Resolution/ResolverGather.cs b/src/NuGet.Core/NuGet.PackageManagement/Resolution/ResolverGather.cs index d1995bf4f5e..043eea085a0 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/Resolution/ResolverGather.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/Resolution/ResolverGather.cs @@ -564,7 +564,7 @@ private void QueueWork(IReadOnlyList sources, PackageIdentity pa { if (configuredPackageSources == null || configuredPackageSources.Count == 0 || - !configuredPackageSources.Contains(source.Source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase)) + !configuredPackageSources.Contains(source.Source.PackageSource.Name, StringComparer.OrdinalIgnoreCase)) { // This package's id prefix is not defined in current package source, let's skip. continue; From 2510b5c47a701062564db4c0a176a6a612e21bc4 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 25 Aug 2021 17:18:54 -0700 Subject: [PATCH 23/27] International source id tests --- .../NuGetRestoreCommandTest.cs | 83 +++++++++++++++++++ .../SearchTreeTest.cs | 16 ++++ 2 files changed, 99 insertions(+) diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs index 57d5fdf1ade..d77c068878a 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs @@ -3136,6 +3136,89 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_NotEnoughSources_Fails() Assert.Contains("WARNING: Unable to find version '1.0.0' of package 'Contoso.MVC.ASP'.", r.Output); } + [Fact] + public void RestoreCommand_PackageNamespace_InternationalSources_SearchMatchWithOneSource() + { + // Arrange + var nugetexe = Util.GetNuGetExePath(); + + using (var workingPath = TestDirectory.Create()) + { + var proj1Directory = Path.Combine(workingPath, "proj1"); + Directory.CreateDirectory(proj1Directory); + + var proj1File = Path.Combine(proj1Directory, "proj1.csproj"); + File.WriteAllText( + proj1File, + @" + + Library + out + v4.0 + + + + +"); + + var sharedRepositoryPath1 = Path.Combine(workingPath, "SharedRepository1"); + Directory.CreateDirectory(sharedRepositoryPath1); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath1); + + var sharedRepositoryPath2 = Path.Combine(workingPath, "SharedRepository2"); + Directory.CreateDirectory(sharedRepositoryPath2); + Util.CreateTestPackage("Contoso.MVC.ASP", "1.0.0", sharedRepositoryPath2); + + Util.CreateFile(proj1Directory, "packages.config", +@" + +"); + + var configPath = Path.Combine(workingPath, "nuget.config"); + SettingsTestUtils.CreateConfigurationFile(configPath, $@" + + + + + + + + + + + + +"); + + var packagePath = Path.Combine(workingPath, "packages"); + + string[] args = new string[] + { + "restore", + proj1File, + "-solutionDir", + workingPath, + "-Verbosity", + "d" + }; + + // Act + var r = CommandRunner.Run( + nugetexe, + workingPath, + string.Join(" ", args), + waitForExit: true); + + // Assert + Assert.Equal(_successCode, r.ExitCode); + var contosoRestorePath = Path.Combine(packagePath, "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); + Assert.True(File.Exists(contosoRestorePath)); + Assert.Contains("Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'encyclopædia'", r.Output); + Assert.Contains($"Added package 'Contoso.MVC.ASP.1.0.0' to folder '{packagePath}' from source '{sharedRepositoryPath2}'", r.Output); + } + } + private static byte[] GetResource(string name) { return ResourceTestUtility.GetResourceBytes( diff --git a/test/NuGet.Core.Tests/NuGet.Configuration.Test/SearchTreeTest.cs b/test/NuGet.Core.Tests/NuGet.Configuration.Test/SearchTreeTest.cs index f96ffdcdea0..5f1e7443d20 100644 --- a/test/NuGet.Core.Tests/NuGet.Configuration.Test/SearchTreeTest.cs +++ b/test/NuGet.Core.Tests/NuGet.Configuration.Test/SearchTreeTest.cs @@ -77,6 +77,22 @@ public void SearchTree_WithMultipleSources_Match(string packageNamespaces, strin Assert.True(configuredSources[0].StartsWith(term.Trim().Substring(0, 5))); } + [Theory] + [InlineData(" Encyclopaedia , encyclopaedia* | encyclopædia , encyclopædia* ", " encyclopaedia ")] + [InlineData(" encyclopaedia , Encyclopaedia* | encyclopædia , encyclopædia* ", " encyclopædia. ")] + [InlineData(" encyclopaedia , encyclopaedia* | encyclopedia , encyclopedia* ", "ENCYCLOPEDIA.")] + public void SearchTree_InternationalSources_MatchesWithOne(string packageNamespaces, string term) + { + // Arrange + PackageNamespacesConfiguration configuration = PackageNamespacesConfigurationUtility.GetPackageNamespacesConfiguration(packageNamespaces); + SearchTree searchTree = new SearchTree(configuration); + + // Act & Assert + IReadOnlyList configuredSources = searchTree.GetConfiguredPackageSources(term); + Assert.Equal(1, configuredSources.Count); + Assert.True(term.Trim().StartsWith(configuredSources[0], StringComparison.OrdinalIgnoreCase)); + } + [Theory] [InlineData("nuget.org,nuget|privateRepository,private*", "nuge")] [InlineData("nuget.org,nuget|privateRepository,private*", "nuget1")] From 3e3a8374b0d6a111c0175cfc8fb3ab25029608a2 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 25 Aug 2021 17:37:13 -0700 Subject: [PATCH 24/27] Rename End2end test names --- test/EndToEnd/tests/PackageNameSpaceTests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/EndToEnd/tests/PackageNameSpaceTests.ps1 b/test/EndToEnd/tests/PackageNameSpaceTests.ps1 index 9903a41cc6b..f13c592bbfe 100644 --- a/test/EndToEnd/tests/PackageNameSpaceTests.ps1 +++ b/test/EndToEnd/tests/PackageNameSpaceTests.ps1 @@ -367,7 +367,7 @@ function Test-PC-PackageNamespaceInstall-Succeed } } -function Test-PC-PackageNamespaceInstall-WrongSource-Fails +function Test-PC-PackageNamespaceInstall-Fails { param($context) @@ -408,7 +408,7 @@ function Test-PC-PackageNamespaceInstall-WrongSource-Fails } } -function Test-PC-PackageNamespaceInstall-Pass-CorrectSourceOption-Succeed +function Test-PC-PackageNamespaceInstall-WithCorrectSourceOption-Succeed { param($context) @@ -467,7 +467,7 @@ function Test-PC-PackageNamespaceInstall-Pass-CorrectSourceOption-Succeed } } -function Test-PC-PackageNamespaceInstall-Pass-WrongSourceOption-Fails +function Test-PC-PackageNamespaceInstall-WithWrongSourceOption-Fails { param($context) @@ -516,7 +516,7 @@ function Test-PC-PackageNamespaceInstall-Pass-WrongSourceOption-Fails } } -function Test-PC-PackageNamespaceUpdate-Pass-CorrectSourceOption-Succeed +function Test-PC-PackageNamespaceUpdate-WithCorrectSourceOption-Succeed { param($context) From 9133e01eebe04d9ff68c0e99a0e49046979cedb6 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 26 Aug 2021 09:00:01 -0700 Subject: [PATCH 25/27] Add back test --- .../MsbuildRestoreTaskTests.cs | 91 ++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs index a96b64e8a65..9fdcaec3d3d 100644 --- a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs +++ b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs @@ -1509,7 +1509,96 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( Assert.Contains("Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'SharedRepository'", result.Output); } } - + + [PlatformFact(Platform.Windows)] + public async Task MsbuildRestore_PackageNamespaceLongerMatches_NoNamespaceMatchesLog() + { + // Arrange + using (var pathContext = new SimpleTestPathContext()) + { + + // Set up solution, project, and packages + var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); + + var net461 = NuGetFramework.Parse("net461"); + + var projectA = new SimpleTestProjectContext( + "a", + ProjectStyle.PackagesConfig, + pathContext.SolutionRoot); + projectA.Frameworks.Add(new SimpleTestProjectFrameworkContext(net461)); + var projectAPackages = Path.Combine(pathContext.SolutionRoot, "packages"); + + solution.Projects.Add(projectA); + solution.Create(pathContext.SolutionRoot); + + using (var writer = new StreamWriter(Path.Combine(Path.GetDirectoryName(projectA.ProjectPath), "packages.config"))) + { + writer.Write( +@" + +"); + } + + var opensourceRepositoryPath = pathContext.PackageSource; + Directory.CreateDirectory(opensourceRepositoryPath); + + var packageOpenSourceContosoMvc = new SimpleTestPackageContext() + { + Id = "My.MVC.ASP", // Package Id conflict with internally created package + Version = "1.0.0" + }; + packageOpenSourceContosoMvc.AddFile("lib/net461/openA.dll"); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageOpenSourceContosoMvc); + + var packageContosoBuffersOpenSource = new SimpleTestPackageContext() + { + Id = "Contoso.Opensource.Buffers", + Version = "1.0.0" + }; + packageContosoBuffersOpenSource.AddFile("lib/net461/openA.dll"); + + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + opensourceRepositoryPath, + packageContosoBuffersOpenSource); + + // SimpleTestPathContext adds a NuGet.Config with a repositoryPath, + // so we go ahead and remove that config before running MSBuild. + var configPath = Path.Combine(Path.GetDirectoryName(pathContext.SolutionRoot), "NuGet.Config"); + var configText = +$@" + + + + + + + + + + + +"; + + using (var writer = new StreamWriter(configPath)) + { + writer.Write(configText); + } + + // Act + var result = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore -v:d {pathContext.SolutionRoot} /p:RestorePackagesConfig=true", ignoreExitCode: true); + + // Assert + Assert.Equal(1, result.ExitCode); + Assert.Contains("Package namespace match not found for package ID 'My.MVC.ASP'", result.Output); + Assert.Contains("warning : Unable to find version '1.0.0' of package 'My.MVC.ASP'.", result.Output); + Assert.Contains("error MSB4181: The \"RestoreTask\" task returned false but did not log an error.", result.Output); + } + } + [PlatformFact(Platform.Windows)] public async Task MsbuildRestore_PackageNamespaceSameMatchesMultipleSources_Succeed() { From beb0169b76848fc77fea75c6d522490e37514897 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 26 Aug 2021 09:57:27 -0700 Subject: [PATCH 26/27] Rename tests --- .../NuGet.CommandLine.Test/NuGetInstallCommandTest.cs | 4 ++-- .../NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs | 5 ++--- .../NuGet.CommandLine.Test/RestoreNETCoreTest.cs | 4 ++-- .../Dotnet.Integration.Test/DotnetRestoreTests.cs | 4 ++-- .../Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs | 4 ++-- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs index 3d45dae8efd..339308c2d18 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs @@ -1959,7 +1959,7 @@ public void InstallCommand_NameSpaceFilter_Fails() } [Fact] - public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_AllSourceOptions_Succeed() + public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_WithCorrectSourceOptions_Succeed() { // Arrange using var pathContext = new SimpleTestPathContext(); @@ -2011,7 +2011,7 @@ public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_AllSo } [Fact] - public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_Pass_NotEnoughSources_Fails() + public void InstallCommand_NameSpaceFilter_Cli_FromPackagesConfigFile_WithNotEnoughSourceOptions_Fails() { // Arrange using var pathContext = new SimpleTestPathContext(); diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs index f1e5f1bec28..b192f295930 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs @@ -2966,7 +2966,7 @@ public void RestoreCommand_NoPackageNamespacesection_NoSourceRelatedLogMessage() } [Fact] - public void RestoreCommand_NameSpaceFilter_Cli_Pass_AllSourceOptions_Succeed() + public void RestoreCommand_NameSpaceFilter_Cli_WithAllSourceOptions_Succeed() { // Arrange var nugetexe = Util.GetNuGetExePath(); @@ -3051,7 +3051,7 @@ public void RestoreCommand_NameSpaceFilter_Cli_Pass_AllSourceOptions_Succeed() } [Fact] - public void RestoreCommand_NameSpaceFilter_Cli_Pass_NotEnoughSources_Fails() + public void RestoreCommand_NameSpaceFilter_Cli_WithNotEnoughSourceOption_Fails() { // Arrange var nugetexe = Util.GetNuGetExePath(); @@ -3214,7 +3214,6 @@ public void RestoreCommand_PackageNamespace_InternationalSources_SearchMatchWith Assert.Equal(_successCode, r.ExitCode); var contosoRestorePath = Path.Combine(packagePath, "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); Assert.True(File.Exists(contosoRestorePath)); - Assert.Contains("Package namespace matches found for package ID 'Contoso.MVC.ASP' are: 'encyclopædia'", r.Output); Assert.Contains($"Added package 'Contoso.MVC.ASP.1.0.0' to folder '{packagePath}' from source '{sharedRepositoryPath2}'", r.Output); } } diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs index 79bb4e92811..dcfa4b55523 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/RestoreNETCoreTest.cs @@ -10855,7 +10855,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task RestoreCommand_NameSpaceFilter_PR_WithAllRestoreSources_Properies_Succeed() + public async Task RestoreCommand_NameSpaceFilter_PR_WithAllRestoreSourceProperies_Succeed() { // Arrange using var pathContext = new SimpleTestPathContext(); @@ -10951,7 +10951,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task RestoreCommand_NameSpaceFilter_PR_WithNotEnoughRestoreSources_Property_Fails() + public async Task RestoreCommand_NameSpaceFilter_PR_WithNotEnoughRestoreSourceProperty_Fails() { // Arrange using var pathContext = new SimpleTestPathContext(); diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetRestoreTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetRestoreTests.cs index 2f59c544ea4..84355fcd7cb 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetRestoreTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetRestoreTests.cs @@ -1777,7 +1777,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task DotnetRestore_NameSpaceFilter_Pass_AllSourceOptions_Succeed() + public async Task DotnetRestore_NameSpaceFilter_WithAllSourceOptions_Succeed() { using var pathContext = _msbuildFixture.CreateSimpleTestPathContext(); @@ -1861,7 +1861,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task DotnetRestore_NameSpaceFilter_Pass_NotEnoughSources_Fails() + public async Task DotnetRestore_NameSpaceFilter_WithNotEnoughSourceOptions_Fails() { using var pathContext = _msbuildFixture.CreateSimpleTestPathContext(); diff --git a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs index 9fdcaec3d3d..aafa9003429 100644 --- a/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs +++ b/test/NuGet.Core.FuncTests/Msbuild.Integration.Test/MsbuildRestoreTaskTests.cs @@ -1769,7 +1769,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [PlatformFact(Platform.Windows)] - public async Task MsbuildRestore_PR_PackageNamespace_WithAllRestoreSources_Properies_Succeed() + public async Task MsbuildRestore_PR_PackageNamespace_WithAllRestoreSourceProperies_Succeed() { // Arrange using var pathContext = new SimpleTestPathContext(); @@ -1855,7 +1855,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [PlatformFact(Platform.Windows)] - public async Task MsbuildRestore_PR_PackageNamespace_WithNotEnoughRestoreSources_Property_Fails() + public async Task MsbuildRestore_PR_PackageNamespace_WithNotEnoughRestoreSourceProperty_Fails() { // Arrange using var pathContext = new SimpleTestPathContext(); From 870dfd517e830837d5ac1df034c9768db192e984 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 26 Aug 2021 12:51:33 -0700 Subject: [PATCH 27/27] Fix test --- .../NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs index b192f295930..945710c30a1 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs @@ -2813,9 +2813,9 @@ public void RestoreCommand_PackageNamespaceMatchesMultipleSources_Succeed() waitForExit: true); // Assert - Assert.Equal(_failureCode, r.ExitCode); - Assert.Contains("Unable to find version '1.0.0' of package 'My.MVC.ASP'.", r.Errors); - Assert.Contains("Package namespace match not found for package ID 'My.MVC.ASP'", r.Output); + Assert.Equal(_successCode, r.ExitCode); + var contosoRestorePath = Path.Combine(packagePath, "Contoso.MVC.ASP.1.0.0", "Contoso.MVC.ASP.1.0.0.nupkg"); + Assert.True(File.Exists(contosoRestorePath)); } }