Skip to content

Commit

Permalink
Merge pull request #4229 from Marusyk/rmarusyk/4224
Browse files Browse the repository at this point in the history
Add DotNetListPackage alias for dotnet list package command
  • Loading branch information
devlead authored Oct 4, 2024
2 parents 3b7ac98 + 43e5af6 commit 3e24d09
Show file tree
Hide file tree
Showing 15 changed files with 770 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tools.DotNet.Package.List;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Package.List
{
internal sealed class DotNetPackageListerFixture : DotNetFixture<DotNetPackageListSettings>
{
public string Project { get; set; }
public DotNetPackageList Result { get; set; }

public void GivenPackgeListResult()
{
ProcessRunner.Process.SetStandardOutput(new string[]
{
"{",
" \"version\": 1,",
" \"parameters\": \"\",",
" \"projects\": [",
" {",
" \"path\": \"src/lib/MyProject.csproj\",",
" \"frameworks\": [",
" {",
" \"framework\": \"netstandard2.0\",",
" \"topLevelPackages\": [",
" {",
" \"id\": \"NETStandard.Library\",",
" \"requestedVersion\": \"[2.0.3, )\",",
" \"resolvedVersion\": \"2.0.3\",",
" \"autoReferenced\": \"true\"",
" }",
" ]",
" }",
" ]",
" }",
" ]",
"}"
});
}

protected override void RunTool()
{
var tool = new DotNetPackageLister(FileSystem, Environment, ProcessRunner, Tools);
Result = tool.List(Project, Settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tests.Fixtures.Tools.DotNet.Package.List;
using Cake.Common.Tools.DotNet;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Package.List
{
public sealed class DotNetPackageListerTests
{
public sealed class TheListMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetPackageListerFixture();
fixture.GivenProcessCannotStart();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsCakeException(result, ".NET CLI: Process was not started.");
}

[Fact]
public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code()
{
// Given
var fixture = new DotNetPackageListerFixture();
fixture.GivenProcessExitsWithCode(1);

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1).");
}

[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
// Given
var fixture = new DotNetPackageListerFixture();
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsArgumentNullException(result, "settings");
}

[Fact]
public void Should_Add_Project_Argument()
{
// Given
var fixture = new DotNetPackageListerFixture();
fixture.Project = "ToDo.csproj";
fixture.GivenPackgeListResult();

// When
var result = fixture.Run();

// Then
Assert.Equal("list \"ToDo.csproj\" package --format json --output-version 1", result.Args);
}

[Fact]
public void Should_Add_Additional_Arguments()
{
// Given
var fixture = new DotNetPackageListerFixture();
fixture.Settings.ConfigFile = "./nuget.config";
fixture.Settings.Deprecated = true;
fixture.Settings.Framework = "net7.0";
fixture.Settings.HighestMinor = true;
fixture.Settings.HighestPatch = true;
fixture.Settings.Prerelease = true;
fixture.Settings.Transitive = true;
fixture.Settings.Interactive = true;
fixture.Settings.Outdated = true;
fixture.Settings.Source.Add("http://www.nuget.org/api/v2/package");
fixture.Settings.Source.Add("http://www.symbolserver.org/");
fixture.Settings.Vulnerable = true;
fixture.GivenPackgeListResult();

// When
var result = fixture.Run();

// Then
var expected = "list package --config \"/Working/nuget.config\" --deprecated --framework net7.0 --highest-minor --highest-patch --include-prerelease --include-transitive --interactive --outdated ";
expected += "--source \"http://www.nuget.org/api/v2/package\" --source \"http://www.symbolserver.org/\" --vulnerable --format json --output-version 1";
Assert.Equal(expected, result.Args);
}

[Fact]
public void Should_Return_Correct_Result()
{
// Given
var fixture = new DotNetPackageListerFixture();
fixture.GivenPackgeListResult();

// When
var result = fixture.Run();

// Then
Assert.Equal(1, fixture.Result.Version);
Assert.Contains(fixture.Result.Projects, item => item.Path == "src/lib/MyProject.csproj");
}
}
}
}
74 changes: 74 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Cake.Common.Tools.DotNet.NuGet.Source;
using Cake.Common.Tools.DotNet.Pack;
using Cake.Common.Tools.DotNet.Package.Add;
using Cake.Common.Tools.DotNet.Package.List;
using Cake.Common.Tools.DotNet.Package.Remove;
using Cake.Common.Tools.DotNet.Package.Search;
using Cake.Common.Tools.DotNet.Publish;
Expand Down Expand Up @@ -2622,5 +2623,78 @@ public static IEnumerable<DotNetPackageSearchItem> DotNetSearchPackage(this ICak
var runner = new DotNetPackageSearcher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return runner.Search(null, settings);
}

/// <summary>
/// Lists the package references for a project or solution.
/// </summary>
/// <param name="context">The context.</param>
/// <returns>The the package references.</returns>
/// <example>
/// <code>
/// DotNetPackageList output = DotNetListPackage();
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.List")]
public static DotNetPackageList DotNetListPackage(this ICakeContext context)
{
return context.DotNetListPackage(null);
}

/// <summary>
/// Lists the package references for a project or solution.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The project or solution file to operate on. If not specified, the command searches the current directory for one. If more than one solution or project is found, an error is thrown.</param>
/// <returns>The the package references.</returns>
/// <example>
/// <code>
/// DotNetPackageList output = DotNetListPackage("./src/MyProject/MyProject.csproj");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.List")]
public static DotNetPackageList DotNetListPackage(this ICakeContext context, string project)
{
return context.DotNetListPackage(project, null);
}

/// <summary>
/// Lists the package references for a project or solution.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The project or solution file to operate on. If not specified, the command searches the current directory for one. If more than one solution or project is found, an error is thrown.</param>
/// <param name="settings">The settings.</param>
/// <returns>The the package references.</returns>
/// <example>
/// <code>
/// var settings = new DotNetPackageListSettings
/// {
/// Outdated = true
/// };
///
/// DotNetPackageList output = DotNetListPackage("./src/MyProject/MyProject.csproj", settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.List")]
public static DotNetPackageList DotNetListPackage(this ICakeContext context, string project, DotNetPackageListSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings is null)
{
settings = new DotNetPackageListSettings();
}

var lister = new DotNetPackageLister(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return lister.List(project, settings);
}
}
}
6 changes: 5 additions & 1 deletion src/Cake.Common/Tools/DotNet/Format/DotNetFormatSeverity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace Cake.Common.Tools.DotNet.Format
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Cake.Common.Tools.DotNet.Format
{
/// <summary>
/// Severity of dotnet format.
Expand Down
45 changes: 45 additions & 0 deletions src/Cake.Common/Tools/DotNet/Package/List/DotNetPackageList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Cake.Common.Tools.DotNet.Package.List
{
/// <summary>
/// An result as returned by <see cref="DotNetPackageLister"/>.
/// </summary>
public sealed class DotNetPackageList
{
/// <summary>
/// Gets the output version.
/// </summary>
[JsonInclude]
public int Version { get; private set; }

/// <summary>
/// Gets the specified parameters.
/// </summary>
[JsonInclude]
public string Parameters { get; private set; }

/// <summary>
/// Gets the problems.
/// </summary>
[JsonInclude]
public IEnumerable<DotNetPackageListProblemItem> Problems { get; private set; }

/// <summary>
/// Gets the used sources.
/// </summary>
[JsonInclude]
public IEnumerable<string> Sources { get; private set; }

/// <summary>
/// Gets the projects.
/// </summary>
[JsonInclude]
public IEnumerable<DotNetPackageListProjectItem> Projects { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Text.Json.Serialization;

namespace Cake.Common.Tools.DotNet.Package.List
{
/// <summary>
/// The alternative package information.
/// </summary>
public sealed class DotNetPackageListAlternativePackageItem
{
/// <summary>
/// Gets the alternative package id.
/// </summary>
[JsonInclude]
public string Id { get; private set; }

/// <summary>
/// Gets the alternative package versions.
/// </summary>
[JsonInclude]
public string VersionRange { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Cake.Common.Tools.DotNet.Package.List
{
/// <summary>
/// The framework information.
/// </summary>
public sealed class DotNetPackageListFrameworkItem
{
/// <summary>
/// Gets the framework name.
/// </summary>
[JsonInclude]
public string Framework { get; private set; }

/// <summary>
/// Gets the top-level packages.
/// </summary>
[JsonInclude]
public IEnumerable<DotNetPackageListPackageItem> TopLevelPackages { get; private set; }

/// <summary>
/// Gets transitive packages.
/// </summary>
[JsonInclude]
public IEnumerable<DotNetPackageListPackageItem> TransitivePackages { get; private set; }
}
}
Loading

0 comments on commit 3e24d09

Please sign in to comment.