Skip to content

Commit

Permalink
(cake-buildGH-3482) Add alias for dotnet workload list command
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Oct 9, 2022
1 parent 1cccdc4 commit df8d545
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 3 deletions.
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 Cake.Common.Tools.DotNet.Workload.List;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.List
{
internal sealed class DotNetWorkloadListerFixture : DotNetFixture<DotNetWorkloadListSettings>
{
public IEnumerable<DotNetWorkloadListItem> Workloads { get; set; }

public void GivenInstalledWorkloadsResult()
{
ProcessRunner.Process.SetStandardOutput(new string[]
{
"Installed Workload Ids Manifest Version Installation Source",
"--------------------------------------------------------------------------------------",
"maui-ios 6.0.312/6.0.300 VS 17.3.32804.467, VS 17.4.32804.182",
"maui-windows 6.0.312/6.0.300 VS 17.3.32804.467, VS 17.4.32804.182",
"android 32.0.301/6.0.300 VS 17.3.32804.467, VS 17.4.32804.182",
"",
"Use `dotnet workload search` to find additional workloads to install."
});
}

public void GivenEmptyInstalledWorkloadsResult()
{
ProcessRunner.Process.SetStandardOutput(new string[]
{
"Installed Workload Ids Manifest Version Installation Source",
"---------------------------------------------------------------------",
"",
"Use `dotnet workload search` to find additional workloads to install."
});
}

protected override void RunTool()
{
var tool = new DotNetWorkloadLister(FileSystem, Environment, ProcessRunner, Tools);
Workloads = tool.List(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.Workload.List;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Workload.List
{
public sealed class DotNetWorkloadListTests
{
public sealed class TheWorkloadListMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetWorkloadListerFixture();
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 DotNetWorkloadListerFixture();
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 DotNetWorkloadListerFixture();
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Add_Verbosity_Argument()
{
// Given
var fixture = new DotNetWorkloadListerFixture();
fixture.Settings.Verbosity = Common.Tools.DotNet.DotNetVerbosity.Normal;

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

// Then
Assert.Equal("workload list --verbosity normal", result.Args);
}

[Fact]
public void Should_Return_Correct_List_Of_Workloads()
{
// Given
var fixture = new DotNetWorkloadListerFixture();
fixture.GivenInstalledWorkloadsResult();

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

// Then
Assert.Collection(fixture.Workloads,
item =>
{
Assert.Equal(item.Id, "maui-ios");
Assert.Equal(item.ManifestVersion, "6.0.312/6.0.300");
Assert.Equal(item.InstallationSource, "VS 17.3.32804.467, VS 17.4.32804.182");
},
item =>
{
Assert.Equal(item.Id, "maui-windows");
Assert.Equal(item.ManifestVersion, "6.0.312/6.0.300");
Assert.Equal(item.InstallationSource, "VS 17.3.32804.467, VS 17.4.32804.182");
},
item =>
{
Assert.Equal(item.Id, "android");
Assert.Equal(item.ManifestVersion, "32.0.301/6.0.300");
Assert.Equal(item.InstallationSource, "VS 17.3.32804.467, VS 17.4.32804.182");
});
}

[Fact]
public void Should_Return_Empty_List_Of_Workloads()
{
// Given
var fixture = new DotNetWorkloadListerFixture();
fixture.GivenEmptyInstalledWorkloadsResult();

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

// Then
Assert.Empty(fixture.Workloads);
}
}
}
}
70 changes: 67 additions & 3 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Cake.Common.Tools.DotNet.Tool;
using Cake.Common.Tools.DotNet.VSTest;
using Cake.Common.Tools.DotNet.Workload.Install;
using Cake.Common.Tools.DotNet.Workload.List;
using Cake.Common.Tools.DotNet.Workload.Search;
using Cake.Common.Tools.DotNet.Workload.Uninstall;
using Cake.Common.Tools.DotNetCore.Build;
Expand Down Expand Up @@ -182,7 +183,7 @@ public static void DotNetRestore(this ICakeContext context, string root)
/// Sources = new[] {"https://www.example.com/nugetfeed", "https://www.example.com/nugetfeed2"},
/// FallbackSources = new[] {"https://www.example.com/fallbacknugetfeed"},
/// PackagesDirectory = "./packages",
/// Verbosity = Information,
/// DotNetVerbosity.Information,
/// DisableParallel = true,
/// InferRuntimes = new[] {"runtime1", "runtime2"}
/// };
Expand Down Expand Up @@ -211,7 +212,7 @@ public static void DotNetRestore(this ICakeContext context, DotNetRestoreSetting
/// Sources = new[] {"https://www.example.com/nugetfeed", "https://www.example.com/nugetfeed2"},
/// FallbackSources = new[] {"https://www.example.com/fallbacknugetfeed"},
/// PackagesDirectory = "./packages",
/// Verbosity = Information,
/// DotNetVerbosity.Information,
/// DisableParallel = true,
/// InferRuntimes = new[] {"runtime1", "runtime2"}
/// };
Expand Down Expand Up @@ -1907,7 +1908,7 @@ public static IEnumerable<DotNetWorkload> DotNetWorkloadSearch(this ICakeContext
/// <code>
/// var settings = new DotNetWorkloadSearchSettings
/// {
/// Verbosity = Detailed
/// DotNetVerbosity.Detailed
/// };
///
/// var workloads = DotNetWorkloadSearch("maui", settings);
Expand Down Expand Up @@ -2075,5 +2076,68 @@ public static void DotNetWorkloadInstall(this ICakeContext context, IEnumerable<
var installer = new DotNetWorkloadInstaller(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
installer.Install(workloadIds, settings);
}

/// <summary>
/// Lists all installed workloads.
/// </summary>
/// <param name="context">The context.</param>
/// <returns>The list of installed workloads.</returns>
/// <example>
/// <code>
/// var workloadIds = DotNetWorkloadList();
///
/// foreach (var workloadId in workloadIds)
/// {
/// Information($"Installed Workload Id: {workloadId}");
/// }
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.List")]
public static IEnumerable<DotNetWorkloadListItem> DotNetWorkloadList(this ICakeContext context)
{
return context.DotNetWorkloadList(null);
}

/// <summary>
/// Lists all installed workloads.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="settings">The settings.</param>
/// <returns>The list of installed workloads.</returns>
/// <example>
/// <code>
/// var settings = new DotNetWorkloadListSettings
/// {
/// Verbosity = DotNetVerbosity.Detailed
/// };
///
/// var workloads = DotNetWorkloadList(settings);
///
/// foreach (var workload in workloads)
/// {
/// Information($"Installed Workload Id: {workload.Id}\t Manifest Version: {workload.ManifestVersion}\t Installation Source: {workload.InstallationSource}");
/// }
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.List")]
public static IEnumerable<DotNetWorkloadListItem> DotNetWorkloadList(this ICakeContext context, DotNetWorkloadListSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings == null)
{
settings = new DotNetWorkloadListSettings();
}

var lister = new DotNetWorkloadLister(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return lister.List(settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.Workload.List
{
/// <summary>
/// An item as returned by <see cref="DotNetWorkloadLister"/>.
/// </summary>
public sealed class DotNetWorkloadListItem
{
/// <summary>
/// Initializes a new instance of the <see cref="DotNetWorkloadListItem" /> class.
/// </summary>
/// <param name="id">The workload Id.</param>
/// <param name="manifestVersion">The workload manifest version.</param>
/// <param name="installationSource">The workload installation source.</param>
public DotNetWorkloadListItem(string id, string manifestVersion, string installationSource)
{
Id = id;
ManifestVersion = manifestVersion;
InstallationSource = installationSource;
}

/// <summary>
/// Gets the workload ID.
/// </summary>
public string Id { get; }

/// <summary>
/// Gets the manifest version of the workload as string.
/// </summary>
public string ManifestVersion { get; }

/// <summary>
/// Gets the installation source of the workload as string.
/// </summary>
public string InstallationSource { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.Workload.List
{
/// <summary>
/// Contains settings used by <see cref="DotNetWorkloadLister" />.
/// </summary>
public sealed class DotNetWorkloadListSettings : DotNetSettings
{
}
}
Loading

0 comments on commit df8d545

Please sign in to comment.