From cc3d62f481517a9db70b4f529ae9829801673b69 Mon Sep 17 00:00:00 2001 From: erdem Date: Sat, 17 Jul 2021 00:24:35 -0500 Subject: [PATCH] Ensure the namespaces filtering is respected in packages.config package installation/updates in PMC (#4150) * Add PMC Apex, End2End install/update tests. --- .../NuGet.Tests.Apex/Apex/ApexTestContext.cs | 4 +- .../NuGetConsoleTestCase.cs | 221 ++++++++++++++++++ .../NuGetEndToEndTests/NuGetUITestCase.cs | 1 - .../NuGet.Tests.Apex/Utility/CommonUtility.cs | 20 ++ 4 files changed, 243 insertions(+), 3 deletions(-) diff --git a/test/NuGet.Tests.Apex/NuGet.Tests.Apex/Apex/ApexTestContext.cs b/test/NuGet.Tests.Apex/NuGet.Tests.Apex/Apex/ApexTestContext.cs index 35557d0f353..ebca0648b3b 100644 --- a/test/NuGet.Tests.Apex/NuGet.Tests.Apex/Apex/ApexTestContext.cs +++ b/test/NuGet.Tests.Apex/NuGet.Tests.Apex/Apex/ApexTestContext.cs @@ -22,10 +22,10 @@ internal class ApexTestContext : IDisposable public NuGetApexTestService NuGetApexTestService { get; } - public ApexTestContext(VisualStudioHost visualStudio, ProjectTemplate projectTemplate, ILogger logger, bool noAutoRestore = false, bool addNetStandardFeeds = false) + public ApexTestContext(VisualStudioHost visualStudio, ProjectTemplate projectTemplate, ILogger logger, bool noAutoRestore = false, bool addNetStandardFeeds = false, SimpleTestPathContext simpleTestPathContext = null) { logger.LogInformation("Creating test context"); - _pathContext = new SimpleTestPathContext(); + _pathContext = simpleTestPathContext ?? new SimpleTestPathContext(); if (noAutoRestore) { diff --git a/test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NuGetConsoleTestCase.cs b/test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NuGetConsoleTestCase.cs index b23240a1f9a..72816cfe81d 100644 --- a/test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NuGetConsoleTestCase.cs +++ b/test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NuGetConsoleTestCase.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.Test.Apex.VisualStudio.Solution; using NuGet.StaFact; +using NuGet.Test.Utility; using Xunit; using Xunit.Abstractions; @@ -291,6 +292,226 @@ public async Task InstallAndUpdatePackageWithSourceParameterWarnsAsync(ProjectTe } } + [NuGetWpfTheory] + [MemberData(nameof(GetPackagesConfigTemplates))] + public async Task InstallPackageForPC_PackageNamespace_WithSingleFeed(ProjectTemplate projectTemplate) + { + // Arrange + EnsureVisualStudioHost(); + + using var simpleTestPathContext = new SimpleTestPathContext(); + string solutionDirectory = simpleTestPathContext.SolutionRoot; + var privateRepositoryPath = Path.Combine(solutionDirectory, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + var packageName = "Contoso.A"; + var packageVersion = "1.0.0"; + + await CommonUtility.CreatePackageInSourceAsync(privateRepositoryPath, packageName, packageVersion); + + //Create nuget.config with Package namespace filtering rules. + CommonUtility.CreateConfigurationFile(Path.Combine(solutionDirectory, "NuGet.config"), $@" + + + + + + + + + + + + +//"); + + using (var testContext = new ApexTestContext(VisualStudio, projectTemplate, XunitLogger, noAutoRestore: false, addNetStandardFeeds: false, simpleTestPathContext: simpleTestPathContext)) + { + var nugetConsole = GetConsole(testContext.Project); + + // Act + nugetConsole.InstallPackageFromPMC(packageName, packageVersion); + + // Assert + CommonUtility.AssertPackageInPackagesConfig(VisualStudio, testContext.Project, packageName, packageVersion, XunitLogger); + } + } + + [NuGetWpfTheory] + [MemberData(nameof(GetPackagesConfigTemplates))] + public async Task UpdatePackageForPC_PackageNamespace_WithSingleFeed(ProjectTemplate projectTemplate) + { + // Arrange + EnsureVisualStudioHost(); + + using var simpleTestPathContext = new SimpleTestPathContext(); + string solutionDirectory = simpleTestPathContext.SolutionRoot; + var privateRepositoryPath = Path.Combine(solutionDirectory, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + var packageName = "Contoso.A"; + var packageVersion1 = "1.0.0"; + var packageVersion2 = "2.0.0"; + + await CommonUtility.CreatePackageInSourceAsync(privateRepositoryPath, packageName, packageVersion1); + await CommonUtility.CreatePackageInSourceAsync(privateRepositoryPath, packageName, packageVersion2); + + //Create nuget.config with Package namespace filtering rules. + CommonUtility.CreateConfigurationFile(Path.Combine(solutionDirectory, "NuGet.config"), $@" + + + + + + + + + + + + +//"); + + using (var testContext = new ApexTestContext(VisualStudio, projectTemplate, XunitLogger, noAutoRestore: false, addNetStandardFeeds: false, simpleTestPathContext: simpleTestPathContext)) + { + var nugetConsole = GetConsole(testContext.Project); + + // Act + nugetConsole.InstallPackageFromPMC(packageName, packageVersion1); + nugetConsole.UpdatePackageFromPMC(packageName, packageVersion2); + + // Assert + CommonUtility.AssertPackageInPackagesConfig(VisualStudio, testContext.Project, packageName, packageVersion2, XunitLogger); + } + } + + [NuGetWpfTheory] + [MemberData(nameof(GetPackagesConfigTemplates))] + public async Task InstallPackageForPC_PackageNamespace_WithMultipleFeedsWithIdenticalPackages_InstallsCorrectPackage(ProjectTemplate projectTemplate) + { + // Arrange + EnsureVisualStudioHost(); + + using var simpleTestPathContext = new SimpleTestPathContext(); + string solutionDirectory = simpleTestPathContext.SolutionRoot; + var packageName = "Contoso.A"; + var packageVersion1 = "1.0.0"; + var packageVersion2 = "2.0.0"; + + var opensourceRepositoryPath = Path.Combine(solutionDirectory, "OpensourceRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + + await CommonUtility.CreateNetCorePackageInSourceAsync(opensourceRepositoryPath, packageName, packageVersion1, "Thisisfromopensourcerepo1.txt"); + await CommonUtility.CreateNetCorePackageInSourceAsync(opensourceRepositoryPath, packageName, packageVersion2, "Thisisfromopensourcerepo2.txt"); + + var privateRepositoryPath = Path.Combine(solutionDirectory, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + await CommonUtility.CreateNetCorePackageInSourceAsync(privateRepositoryPath, packageName, packageVersion1, "Thisisfromprivaterepo1.txt"); + await CommonUtility.CreateNetCorePackageInSourceAsync(privateRepositoryPath, packageName, packageVersion2, "Thisisfromprivaterepo2.txt"); + + //Create nuget.config with Package namespace filtering rules. + CommonUtility.CreateConfigurationFile(Path.Combine(solutionDirectory, "NuGet.config"), $@" + + + + + + + + + + + + + + + + + +//"); + + using (var testContext = new ApexTestContext(VisualStudio, projectTemplate, XunitLogger, noAutoRestore: false, addNetStandardFeeds: false, simpleTestPathContext: simpleTestPathContext)) + { + var nugetConsole = GetConsole(testContext.Project); + + // Act + nugetConsole.InstallPackageFromPMC(packageName, packageVersion1); + + // Assert + CommonUtility.AssertPackageInPackagesConfig(VisualStudio, testContext.Project, packageName, packageVersion1, XunitLogger); + + var packagesDirectory = Path.Combine(solutionDirectory, "packages"); + var uniqueContentFile = Path.Combine(packagesDirectory, packageName + '.' + packageVersion1, "lib", "net5.0", "Thisisfromprivaterepo1.txt"); + // Make sure name squatting package not restored from opensource repository. + Assert.True(File.Exists(uniqueContentFile)); + } + } + + [NuGetWpfTheory] + [MemberData(nameof(GetPackagesConfigTemplates))] + public async Task UpdatePackageForPC_PackageNamespace_WithMultipleFeedsWithIdenticalPackages_UpdatesCorrectPackage(ProjectTemplate projectTemplate) + { + // Arrange + EnsureVisualStudioHost(); + + using var simpleTestPathContext = new SimpleTestPathContext(); + string solutionDirectory = simpleTestPathContext.SolutionRoot; + var packageName = "Contoso.A"; + var packageVersion1 = "1.0.0"; + var packageVersion2 = "2.0.0"; + + var opensourceRepositoryPath = Path.Combine(solutionDirectory, "OpensourceRepository"); + Directory.CreateDirectory(opensourceRepositoryPath); + + await CommonUtility.CreateNetCorePackageInSourceAsync(opensourceRepositoryPath, packageName, packageVersion1, "Thisisfromopensourcerepo1.txt"); + await CommonUtility.CreateNetCorePackageInSourceAsync(opensourceRepositoryPath, packageName, packageVersion2, "Thisisfromopensourcerepo2.txt"); + + var privateRepositoryPath = Path.Combine(solutionDirectory, "PrivateRepository"); + Directory.CreateDirectory(privateRepositoryPath); + + await CommonUtility.CreateNetCorePackageInSourceAsync(privateRepositoryPath, packageName, packageVersion1, "Thisisfromprivaterepo1.txt"); + await CommonUtility.CreateNetCorePackageInSourceAsync(privateRepositoryPath, packageName, packageVersion2, "Thisisfromprivaterepo2.txt"); + + //Create nuget.config with Package namespace filtering rules. + CommonUtility.CreateConfigurationFile(Path.Combine(solutionDirectory, "NuGet.config"), $@" + + + + + + + + + + + + + + + + + +//"); + + using (var testContext = new ApexTestContext(VisualStudio, projectTemplate, XunitLogger, noAutoRestore: false, addNetStandardFeeds: false, simpleTestPathContext: simpleTestPathContext)) + { + var nugetConsole = GetConsole(testContext.Project); + + // Act + nugetConsole.InstallPackageFromPMC(packageName, packageVersion1); + nugetConsole.UpdatePackageFromPMC(packageName, packageVersion2); + + // Assert + CommonUtility.AssertPackageInPackagesConfig(VisualStudio, testContext.Project, packageName, packageVersion2, XunitLogger); + + var packagesDirectory = Path.Combine(solutionDirectory, "packages"); + var uniqueContentFile = Path.Combine(packagesDirectory, packageName + '.' + packageVersion2, "lib", "net5.0", "Thisisfromprivaterepo2.txt"); + // Make sure name squatting package not restored from opensource repository. + Assert.True(File.Exists(uniqueContentFile)); + } + } + // There is a bug with VS or Apex where NetCoreConsoleApp creates a netcore 2.1 project that is not supported by the sdk // Commenting out any NetCoreConsoleApp template and swapping it for NetStandardClassLib as both are package ref. public static IEnumerable GetNetCoreTemplates() diff --git a/test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NuGetUITestCase.cs b/test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NuGetUITestCase.cs index 7a4a72bcbeb..8cfdb98aec9 100644 --- a/test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NuGetUITestCase.cs +++ b/test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NuGetUITestCase.cs @@ -317,7 +317,6 @@ public async Task InstallPackageFromUI_PackageNamespace_WithMultiFeed_Fails() [StaFact] public async Task UpdatePackageFromUI_PackageNamespace_WithSingleFeed_Succeeds() { - // Arrange // Arrange EnsureVisualStudioHost(); var solutionService = VisualStudio.Get(); diff --git a/test/NuGet.Tests.Apex/NuGet.Tests.Apex/Utility/CommonUtility.cs b/test/NuGet.Tests.Apex/NuGet.Tests.Apex/Utility/CommonUtility.cs index 34a3b09c81d..1714ff64729 100644 --- a/test/NuGet.Tests.Apex/NuGet.Tests.Apex/Utility/CommonUtility.cs +++ b/test/NuGet.Tests.Apex/NuGet.Tests.Apex/Utility/CommonUtility.cs @@ -79,6 +79,12 @@ public static SimpleTestPackageContext CreateRepositoryCountersignedPackage( return RepositoryCountersignPackage(authorSignedPackage, repoCertificate, v3ServiceIndexUrl, packageOwners, timestampProviderUrl); } + public static async Task CreateNetCorePackageInSourceAsync(string packageSource, string packageName, string packageVersion, string requestAdditionalContent = null) + { + var package = CreateNetCorePackage(packageName, packageVersion, requestAdditionalContent); + await SimpleTestPackageUtility.CreatePackagesAsync(packageSource, package); + } + public static SimpleTestPackageContext AuthorSignPackage( SimpleTestPackageContext package, X509Certificate2 authorCertificate, @@ -149,6 +155,20 @@ public static SimpleTestPackageContext CreatePackage(string packageName, string return package; } + public static SimpleTestPackageContext CreateNetCorePackage(string packageName, string packageVersion, string requestAdditionalContent) + { + var package = new SimpleTestPackageContext(packageName, packageVersion); + package.Files.Clear(); + package.AddFile("lib/net5.0/_._"); + + if(!string.IsNullOrWhiteSpace(requestAdditionalContent)) + { + package.AddFile("lib/net5.0/" + requestAdditionalContent); + } + + return package; + } + public static void AssertPackageReferenceExists(VisualStudioHost visualStudio, ProjectTestExtension project, string packageName, string packageVersion, ILogger logger) { logger.LogInformation($"Checking for PackageReference {packageName} {packageVersion}");