From 4cb0a7a04f833b81ef04cfabb0c691a9d45a81fe Mon Sep 17 00:00:00 2001 From: Roman Marusyk Date: Tue, 25 Jan 2022 00:14:18 +0200 Subject: [PATCH] Add alias for dotnet workload search command --- .../Search/DotNetWorkloadSearcherFixture.cs | 19 ++++++ .../Search/DotNetWorkloadSearchTests.cs | 58 +++++++++++++++++++ src/Cake.Common/Tools/DotNet/DotNetAliases.cs | 42 ++++++++++++++ .../Search/DotNetWorkloadSearchSettings.cs | 13 +++++ .../Workload/Search/DotNetWorkloadSearcher.cs | 55 ++++++++++++++++++ .../Tools/DotNet/DotNetAliases.cake | 16 ++++- 6 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 src/Cake.Common.Tests/Fixtures/Tools/DotNet/Workload/Search/DotNetWorkloadSearcherFixture.cs create mode 100644 src/Cake.Common.Tests/Unit/Tools/DotNet/Workload/Search/DotNetWorkloadSearchTests.cs create mode 100644 src/Cake.Common/Tools/DotNet/Workload/Search/DotNetWorkloadSearchSettings.cs create mode 100644 src/Cake.Common/Tools/DotNet/Workload/Search/DotNetWorkloadSearcher.cs diff --git a/src/Cake.Common.Tests/Fixtures/Tools/DotNet/Workload/Search/DotNetWorkloadSearcherFixture.cs b/src/Cake.Common.Tests/Fixtures/Tools/DotNet/Workload/Search/DotNetWorkloadSearcherFixture.cs new file mode 100644 index 0000000000..aebbb216ae --- /dev/null +++ b/src/Cake.Common.Tests/Fixtures/Tools/DotNet/Workload/Search/DotNetWorkloadSearcherFixture.cs @@ -0,0 +1,19 @@ +// 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.Workload.Search; + +namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Search +{ + internal sealed class DotNetWorkloadSearcherFixture : DotNetFixture + { + public string SearchString { get; set; } + + protected override void RunTool() + { + var tool = new DotNetWorkloadSearcher(FileSystem, Environment, ProcessRunner, Tools); + tool.Search(SearchString); + } + } +} diff --git a/src/Cake.Common.Tests/Unit/Tools/DotNet/Workload/Search/DotNetWorkloadSearchTests.cs b/src/Cake.Common.Tests/Unit/Tools/DotNet/Workload/Search/DotNetWorkloadSearchTests.cs new file mode 100644 index 0000000000..5ed2743d50 --- /dev/null +++ b/src/Cake.Common.Tests/Unit/Tools/DotNet/Workload/Search/DotNetWorkloadSearchTests.cs @@ -0,0 +1,58 @@ +// 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.Search; +using Cake.Testing; +using Xunit; + +namespace Cake.Common.Tests.Unit.Tools.DotNet.Workload.Search +{ + public sealed class DotNetWorkloadSearchTests + { + public sealed class TheWorkloadSearchMethod + { + [Fact] + public void Should_Throw_If_Process_Was_Not_Started() + { + // Given + var fixture = new DotNetWorkloadSearcherFixture(); + 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 DotNetWorkloadSearcherFixture(); + 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_Add_SearchString_Argument() + { + // Given + var fixture = new DotNetWorkloadSearcherFixture(); + fixture.SearchString = "maui"; + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("workload search maui", result.Args); + } + } + } +} diff --git a/src/Cake.Common/Tools/DotNet/DotNetAliases.cs b/src/Cake.Common/Tools/DotNet/DotNetAliases.cs index 311f0b6a67..a0f51a45ac 100644 --- a/src/Cake.Common/Tools/DotNet/DotNetAliases.cs +++ b/src/Cake.Common/Tools/DotNet/DotNetAliases.cs @@ -22,6 +22,7 @@ using Cake.Common.Tools.DotNet.Test; using Cake.Common.Tools.DotNet.Tool; using Cake.Common.Tools.DotNet.VSTest; +using Cake.Common.Tools.DotNet.Workload.Search; using Cake.Common.Tools.DotNetCore.Build; using Cake.Common.Tools.DotNetCore.BuildServer; using Cake.Common.Tools.DotNetCore.Clean; @@ -1845,5 +1846,46 @@ public static void DotNetSDKCheck(this ICakeContext context) var checker = new DotNetSDKChecker(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); checker.Check(); } + + /// + /// Lists available workloads. + /// + /// The context. + /// + /// + /// DotNetWorkloadSearch(); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Workload")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Search")] + public static void DotNetWorkloadSearch(this ICakeContext context) + { + context.DotNetWorkloadSearch(null); + } + + /// + /// Lists available workloads by specifying all or part of the workload ID. + /// + /// The context. + /// The workload ID to search for, or part of it. + /// + /// + /// DotNetWorkloadSearch("maui"); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Workload")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Search")] + public static void DotNetWorkloadSearch(this ICakeContext context, string searchString) + { + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + var searcher = new DotNetWorkloadSearcher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + searcher.Search(searchString); + } } } diff --git a/src/Cake.Common/Tools/DotNet/Workload/Search/DotNetWorkloadSearchSettings.cs b/src/Cake.Common/Tools/DotNet/Workload/Search/DotNetWorkloadSearchSettings.cs new file mode 100644 index 0000000000..d94c6ee90d --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/Workload/Search/DotNetWorkloadSearchSettings.cs @@ -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.Search +{ + /// + /// Contains settings used by . + /// + public sealed class DotNetWorkloadSearchSettings : DotNetSettings + { + } +} diff --git a/src/Cake.Common/Tools/DotNet/Workload/Search/DotNetWorkloadSearcher.cs b/src/Cake.Common/Tools/DotNet/Workload/Search/DotNetWorkloadSearcher.cs new file mode 100644 index 0000000000..6479ca29dd --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/Workload/Search/DotNetWorkloadSearcher.cs @@ -0,0 +1,55 @@ +// 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; +using Cake.Core.IO; +using Cake.Core.Tooling; + +namespace Cake.Common.Tools.DotNet.Workload.Search +{ + /// + /// .NET workloads searcher. + /// + public sealed class DotNetWorkloadSearcher : DotNetTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + public DotNetWorkloadSearcher( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools) : base(fileSystem, environment, processRunner, tools) + { + } + + /// + /// Lists the latest available version of the .NET SDK and .NET Runtime, for each feature band. + /// + /// The workload ID to search for, or part of it. + public void Search(string searchString) + { + var settings = new DotNetWorkloadSearchSettings(); + RunCommand(settings, GetArguments(searchString, settings)); + } + + private ProcessArgumentBuilder GetArguments(string searchString, DotNetWorkloadSearchSettings settings) + { + var builder = CreateArgumentBuilder(settings); + + builder.Append("workload search"); + + if (!string.IsNullOrEmpty(searchString)) + { + builder.Append(searchString); + } + + return builder; + } + } +} diff --git a/tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake b/tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake index a43f983bc5..74a7b07928 100644 --- a/tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake +++ b/tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake @@ -246,7 +246,7 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetFormat") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup") .Does(() => { - // Given + // Given var path = Paths.Temp.Combine("./Cake.Common/Tools/DotNet"); var project = path.CombineWithFilePath("hwapp/hwapp.csproj"); @@ -259,7 +259,18 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSDKCheck") .Does(() => { // When - DotNetSDKCheck(); + DotNetSDKCheck(); +}); + +Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadSearch") + .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup") + .Does(() => +{ + // Given + var searchString = "maui"; + + // When + DotNetWorkloadSearch(searchString); }); Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown") @@ -279,6 +290,7 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetTest.Fail") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetFormat") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSDKCheck") + .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadSearch") .Does(() => { // When