Skip to content

Commit

Permalink
Add DotNetAddPackage alias for dotnet add package command (cake-build…
Browse files Browse the repository at this point in the history
…#4195)

* Add DotNetAddPackage alias for dotnet add package command

* Add an integration test for DotNetAddPackage alias

* Add an integration test for DotNetAddPackage alias

* Change the package in integration test for DotNetAddPackage alias
  • Loading branch information
Marusyk authored Oct 10, 2023
1 parent 4ae30f3 commit 27a28a5
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.Add;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Package.Add
{
internal sealed class DotNetPackageAdderFixture : DotNetFixture<DotNetPackageAddSettings>
{
public string PackageName { get; set; }

public string Project { get; set; }

protected override void RunTool()
{
var tool = new DotNetPackageAdder(FileSystem, Environment, ProcessRunner, Tools);
tool.Add(PackageName, Project, Settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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.Add;
using Cake.Common.Tests.Fixtures.Tools.DotNet.SDKCheck;
using Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Restore;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Package.Add;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Package.Add
{
public sealed class DotNetPackageAdderTests
{
public sealed class TheAddMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetPackageAdderFixture();
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
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 DotNetPackageAdderFixture();
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
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 DotNetPackageAdderFixture();
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Throw_If_PackageName_Is_Null()
{
// Given
var fixture = new DotNetPackageAdderFixture();
fixture.PackageName = null;
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Add_Project_Argument()
{
// Given
var fixture = new DotNetPackageAdderFixture();
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
fixture.Project = "ToDo.csproj";

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

// Then
Assert.Equal("add \"ToDo.csproj\" package Microsoft.AspNetCore.StaticFiles", result.Args);
}

[Fact]
public void Should_Add_Additional_Arguments()
{
// Given
var fixture = new DotNetPackageAdderFixture();
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
fixture.Settings.Framework = "net7.0";
fixture.Settings.Interactive = true;
fixture.Settings.NoRestore = true;
fixture.Settings.PackageDirectory = "./src/project";
fixture.Settings.Prerelease = true;
fixture.Settings.Source = "http://www.nuget.org/api/v2/package";
fixture.Settings.Version = "1.0.0";
fixture.Settings.Verbosity = DotNetVerbosity.Diagnostic;

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

// Then
var expected = "add package Microsoft.AspNetCore.StaticFiles --framework net7.0 --interactive --no-restore --package-directory \"/Working/src/project\" --prerelease --source \"http://www.nuget.org/api/v2/package\" --version \"1.0.0\" --verbosity diagnostic";
Assert.Equal(expected, result.Args);
}
}
}
}
100 changes: 100 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Cake.Common.Tools.DotNet.NuGet.Push;
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.Publish;
using Cake.Common.Tools.DotNet.Restore;
using Cake.Common.Tools.DotNet.Run;
Expand Down Expand Up @@ -2285,5 +2286,104 @@ public static void DotNetWorkloadRestore(this ICakeContext context, string proje
var restorer = new DotNetWorkloadRestorer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
restorer.Restore(project, settings);
}

/// <summary>
/// Adds or updates a package reference in a project file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="packageName">The package reference to add.</param>
/// <example>
/// <code>
/// DotNetAddPackage("Cake.FileHelper");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddPackage")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
public static void DotNetAddPackage(this ICakeContext context, string packageName)
{
context.DotNetAddPackage(packageName, null, null);
}

/// <summary>
/// Adds or updates a package reference in a project file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="packageName">The package reference to add.</param>
/// <param name="project">The project or solution file to install workloads for.</param>
/// <example>
/// <code>
/// DotNetAddPackage("Cake.FileHelper", "ToDo.csproj");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddPackage")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
public static void DotNetAddPackage(this ICakeContext context, string packageName, string project)
{
context.DotNetAddPackage(packageName, project, null);
}

/// <summary>
/// Adds or updates a package reference in a project file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="packageName">The package reference to add.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetPackageAddSettings
/// {
/// NoRestore = true,
/// Version = "6.1.3"
/// };
///
/// DotNetAddPackage("Cake.FileHelper", settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddPackage")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
public static void DotNetAddPackage(this ICakeContext context, string packageName, DotNetPackageAddSettings settings)
{
context.DotNetAddPackage(packageName, null, settings);
}

/// <summary>
/// Adds or updates a package reference in a project file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="packageName">The package reference to add.</param>
/// <param name="project">The project or solution file to install workloads for.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetPackageAddSettings
/// {
/// NoRestore = true,
/// Version = "6.1.3"
/// };
///
/// DotNetAddPackage("Cake.FileHelper", "ToDo.csproj", settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddPackage")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
public static void DotNetAddPackage(this ICakeContext context, string packageName, string project, DotNetPackageAddSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

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

var adder = new DotNetPackageAdder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
adder.Add(packageName, project, settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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.Core.IO;

namespace Cake.Common.Tools.DotNet.Package.Add
{
/// <summary>
/// Contains settings used by <see cref="DotNetPackageAdder" />.
/// </summary>
public sealed class DotNetPackageAddSettings : DotNetSettings
{
/// <summary>
/// Gets or sets a specific framework to compile.
/// </summary>
public string Framework { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to allow the command to stop and wait for user input or action.
/// For example, to complete authentication.
/// </summary>
public bool Interactive { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to not do implicit NuGet package restore.
/// This makes run faster, but requires restore to be done before run is executed.
/// </summary>
public bool NoRestore { get; set; }

/// <summary>
/// Gets or sets the directory path where to restore the packages.
/// The default package restore location is %userprofile%\.nuget\packages on Windows and ~/.nuget/packages on macOS and Linux.
/// </summary>
public DirectoryPath PackageDirectory { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to allow installation of prerelease packages.
/// Available since .NET Core 5 SDK.
/// </summary>
public bool Prerelease { get; set; }

/// <summary>
/// Gets or sets the URI of the NuGet package source to use during the restore operation.
/// </summary>
public string Source { get; set; }

/// <summary>
/// Gets or sets the version of the package to install.
/// If none specified, the latest will be used.
/// </summary>
public string Version { get; set; }
}
}
Loading

0 comments on commit 27a28a5

Please sign in to comment.