From ed9460ce50ea5902b01bc267223b077fe4b1b53b Mon Sep 17 00:00:00 2001 From: Greybird Date: Sun, 9 Jul 2023 16:45:26 +0200 Subject: [PATCH 1/4] Fix typo --- .../NuGet.XPlat.FuncTest/XplatListPackageJsonRendererTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XplatListPackageJsonRendererTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XplatListPackageJsonRendererTests.cs index 27d52400d60..3bcd1701f88 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XplatListPackageJsonRendererTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XplatListPackageJsonRendererTests.cs @@ -68,7 +68,7 @@ public void JsonRenderer_ListPackage_SucceedsAsync() requestedVersion : "2.0.0", resolvedVersion : "2.0.0") }, - // Below transitive packages shouldn't be in json output because this report doesn't have --include-transive option. + // Below transitive packages shouldn't be in json output because this report doesn't have --include-transitive option. TransitivePackages = new List() { new ListReportPackage( @@ -695,7 +695,7 @@ public void JsonRenderer_ListPackage_IncludeTransitives_SucceedsAsync() requestedVersion : "2.0.0", resolvedVersion : "2.0.0") }, - // Below transitive packages should be in json output because this report has --include-transive option. + // Below transitive packages should be in json output because this report has --include-transitive option. TransitivePackages = new List() { new ListReportPackage( From 2734e05b9f6b2608a4f30f34689ccaddbd6d07f9 Mon Sep 17 00:00:00 2001 From: Greybird Date: Sun, 9 Jul 2023 16:45:36 +0200 Subject: [PATCH 2/4] Write unit tests --- .../DotnetListPackageTests.cs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs index 79a7236cbcc..270d642510d 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs @@ -122,6 +122,66 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } + [PlatformFact(Platform.Windows)] + public async Task DotnetListPackage_DoesNotReturnProjects() + { + using (var pathContext = _fixture.CreateSimpleTestPathContext()) + { + string directDependencyProjectName = $"{ProjectName}Dependency"; + string transitiveDependencyProjectName = $"{ProjectName}TransitiveDependency"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, "net46"); + var projectB = XPlatTestUtils.CreateProject(directDependencyProjectName, pathContext, "net46"); + var projectC = XPlatTestUtils.CreateProject(transitiveDependencyProjectName, pathContext, "net46"); + + var packageX = XPlatTestUtils.CreatePackage(packageId: "packageX"); + var packageY = XPlatTestUtils.CreatePackage(packageId: "packageY"); + var packageZ = XPlatTestUtils.CreatePackage(packageId: "packageZ"); + var packageT = XPlatTestUtils.CreatePackage(packageId: "packageT"); + packageX.Dependencies.Add(packageT); + packageY.Dependencies.Add(packageT); + packageZ.Dependencies.Add(packageT); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + pathContext.PackageSource, + PackageSaveMode.Defaultv3, + packageX, + packageY, + packageZ, + packageT); + + _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, + $"add {projectA.ProjectPath} reference {projectB.ProjectPath}"); + + _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, + $"add {projectB.ProjectPath} reference {projectC.ProjectPath}"); + + _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectB.ProjectPath).FullName, + $"add {projectA.ProjectPath} package packageX --no-restore"); + + _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectB.ProjectPath).FullName, + $"add {projectB.ProjectPath} package packageY --no-restore"); + + _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectB.ProjectPath).FullName, + $"add {projectC.ProjectPath} package packageZ --no-restore"); + + _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, + $"restore {projectA.ProjectName}.csproj"); + + CommandRunnerResult listResult = _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, + $"list {projectA.ProjectPath} package"); + + Assert.False(ContainsIgnoringSpaces(listResult.AllOutput, projectB.ProjectName)); + Assert.False(ContainsIgnoringSpaces(listResult.AllOutput, projectC.ProjectName)); + + listResult = _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, + $"list {projectA.ProjectPath} package --include-transitive"); + + Assert.False(ContainsIgnoringSpaces(listResult.AllOutput, projectB.ProjectName)); + Assert.False(ContainsIgnoringSpaces(listResult.AllOutput, projectC.ProjectName)); + } + } + [PlatformTheory(Platform.Windows)] [InlineData("", "net48", null)] [InlineData("", "net46", null)] From 89890606c79b0d10b44c09d44ed64038b31ef2d1 Mon Sep 17 00:00:00 2001 From: Greybird Date: Wed, 26 Jul 2023 15:25:19 +0200 Subject: [PATCH 3/4] Ensure projects are not returned by list command --- .../ListPackage/ListPackageCommandRunner.cs | 2 +- .../NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/ListPackage/ListPackageCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/ListPackage/ListPackageCommandRunner.cs index 2e96cd3c9c6..62eea3e3898 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/ListPackage/ListPackageCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/ListPackage/ListPackageCommandRunner.cs @@ -115,7 +115,7 @@ private async Task GetProjectMetadataAsync( List frameworks; try { - frameworks = msBuild.GetResolvedVersions(project.FullPath, listPackageArgs.Frameworks, assetsFile, listPackageArgs.IncludeTransitive, includeProjects: listPackageArgs.ReportType == ReportType.Default); + frameworks = msBuild.GetResolvedVersions(project.FullPath, listPackageArgs.Frameworks, assetsFile, listPackageArgs.IncludeTransitive); } catch (InvalidOperationException ex) { diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs index 8362b30cf95..5d1fd5bb6e4 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs @@ -652,11 +652,10 @@ internal static bool IsPackageReferenceProject(Project project) /// A list of frameworks /// Assets file for all targets and libraries /// Include transitive packages/projects in the result - /// Include project references in top-level and transitive package lists /// FrameworkPackages collection with top-level and transitive package/project /// references for each framework, or null on error internal List GetResolvedVersions( - string projectPath, IEnumerable userInputFrameworks, LockFile assetsFile, bool transitive, bool includeProjects) + string projectPath, IEnumerable userInputFrameworks, LockFile assetsFile, bool transitive) { if (userInputFrameworks == null) { @@ -773,7 +772,7 @@ internal List GetResolvedVersions( installedPackage.AutoReference = topLevelPackage.AutoReferenced; - if (library.Type != "project" || includeProjects) + if (library.Type != "project") { topLevelPackages.Add(installedPackage); } @@ -789,7 +788,7 @@ internal List GetResolvedVersions( .Build() }; - if (library.Type != "project" || includeProjects) + if (library.Type != "project") { transitivePackages.Add(installedPackage); } From 56e4a636a2988e27b9230754b20860436713079a Mon Sep 17 00:00:00 2001 From: Greybird Date: Mon, 7 Aug 2023 12:14:53 +0200 Subject: [PATCH 4/4] Fix tests --- .../DotnetListPackageTests.cs | 62 +++---------------- 1 file changed, 8 insertions(+), 54 deletions(-) diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs index 270d642510d..301e68c35d5 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Xml.Linq; @@ -122,8 +121,12 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } - [PlatformFact(Platform.Windows)] - public async Task DotnetListPackage_DoesNotReturnProjects() + [PlatformTheory(Platform.Windows)] + [InlineData("")] + [InlineData(" --outdated")] + [InlineData(" --vulnerable")] + [InlineData(" --deprecated")] + public async Task DotnetListPackage_DoesNotReturnProjects(string args) { using (var pathContext = _fixture.CreateSimpleTestPathContext()) { @@ -169,13 +172,13 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( $"restore {projectA.ProjectName}.csproj"); CommandRunnerResult listResult = _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, - $"list {projectA.ProjectPath} package"); + $"list {projectA.ProjectPath} package{args}"); Assert.False(ContainsIgnoringSpaces(listResult.AllOutput, projectB.ProjectName)); Assert.False(ContainsIgnoringSpaces(listResult.AllOutput, projectC.ProjectName)); listResult = _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, - $"list {projectA.ProjectPath} package --include-transitive"); + $"list {projectA.ProjectPath} package{args} --include-transitive"); Assert.False(ContainsIgnoringSpaces(listResult.AllOutput, projectB.ProjectName)); Assert.False(ContainsIgnoringSpaces(listResult.AllOutput, projectC.ProjectName)); @@ -346,55 +349,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } - [PlatformTheory(Platform.Windows)] - [InlineData(false, false)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(true, true)] - public void DotnetListPackage_ProjectReference_Succeeds(bool includeTransitive, bool outdated) - { - // Arrange - using (var pathContext = _fixture.CreateSimpleTestPathContext()) - { - var projectA = XPlatTestUtils.CreateProject("ProjectA", pathContext, "net46"); - var projectB = XPlatTestUtils.CreateProject("ProjectB", pathContext, "net46"); - - _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, - $"add {projectA.ProjectPath} reference {projectB.ProjectPath}"); - - _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, - $"restore {projectA.ProjectName}.csproj"); - - var argsBuilder = new StringBuilder(); - if (includeTransitive) - { - argsBuilder.Append(" --include-transitive"); - } - if (outdated) - { - argsBuilder.Append(" --outdated"); - } - - // Act - CommandRunnerResult listResult = _fixture.RunDotnetExpectSuccess(Directory.GetParent(projectA.ProjectPath).FullName, - $"list {projectA.ProjectPath} package {argsBuilder}"); - - // Assert - if (outdated) - { - Assert.Contains("The given project `ProjectA` has no updates given the current sources.", listResult.AllOutput); - } - else if (includeTransitive) - { - Assert.Contains("ProjectB", listResult.AllOutput); - } - else - { - Assert.Contains("No packages were found for this framework.", listResult.AllOutput); - } - } - } - [PlatformFact(Platform.Windows)] public async Task DotnetListPackage_OutdatedWithNoVersionsFound_Succeeds() {