-
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
GH3947: Add Command aliases
- Loading branch information
Showing
14 changed files
with
1,486 additions
and
1 deletion.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/Cake.Common.Tests/Fixtures/Tools/Command/CommandRunnerFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 System.Collections.Generic; | ||
using Cake.Common.Tools.Command; | ||
using Cake.Core.IO; | ||
using Cake.Testing.Fixtures; | ||
|
||
namespace Cake.Common.Tests.Fixtures.Tools.Command | ||
{ | ||
internal class CommandRunnerFixture : ToolFixture<CommandSettings> | ||
{ | ||
public ProcessArgumentBuilder Arguments { get; set; } | ||
|
||
public string ToolName | ||
{ | ||
get => Settings.ToolName; | ||
set => Settings.ToolName = value; | ||
} | ||
|
||
public ICollection<string> ToolExecutableNames | ||
{ | ||
get => Settings.ToolExecutableNames; | ||
set => Settings.ToolExecutableNames = value; | ||
} | ||
|
||
public CommandRunnerFixture() | ||
: base("dotnet.exe") | ||
{ | ||
Arguments = new ProcessArgumentBuilder(); | ||
Settings.ToolName = "dotnet"; | ||
Settings.ToolExecutableNames = new[] { "dotnet.exe", "dotnet" }; | ||
} | ||
|
||
protected override void RunTool() | ||
{ | ||
GetRunner().RunCommand(Arguments); | ||
} | ||
|
||
protected CommandRunner GetRunner() | ||
=> new CommandRunner( | ||
Settings, | ||
FileSystem, | ||
Environment, | ||
ProcessRunner, | ||
Tools); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Cake.Common.Tests/Fixtures/Tools/Command/CommandRunnerStandardErrorFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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.Tests.Fixtures.Tools.Command | ||
{ | ||
internal class CommandRunnerStandardErrorFixture : CommandRunnerStandardOutputFixture | ||
{ | ||
public string StandardError { get; private set; } | ||
|
||
protected override void RunTool() | ||
{ | ||
ExitCode = GetRunner().RunCommand(Arguments, out var standardOutput, out var standardError); | ||
StandardOutput = standardOutput; | ||
StandardError = standardError; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Cake.Common.Tests/Fixtures/Tools/Command/CommandRunnerStandardOutFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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.Tests.Fixtures.Tools.Command | ||
{ | ||
internal class CommandRunnerStandardOutputFixture : CommandRunnerFixture | ||
{ | ||
public int ExitCode { get; protected set; } | ||
public string StandardOutput { get; protected set; } | ||
|
||
protected override void RunTool() | ||
{ | ||
ExitCode = GetRunner().RunCommand(Arguments, out var standardOutput); | ||
StandardOutput = standardOutput; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Cake.Common.Tests/Fixtures/Tools/Command/CommandRunnerStandardOutputFixtureExtentions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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.Tests.Fixtures.Tools.Command | ||
{ | ||
internal static class CommandRunnerStandardOutputFixtureExtentions | ||
{ | ||
public static T GivenStandardOutput<T>(this T fixture, params string[] standardOutput) | ||
where T : CommandRunnerStandardOutputFixture | ||
{ | ||
fixture.ProcessRunner.Process.SetStandardOutput(standardOutput); | ||
return fixture; | ||
} | ||
|
||
public static T GivenStandardError<T>(this T fixture, params string[] standardError) | ||
where T : CommandRunnerStandardOutputFixture | ||
{ | ||
fixture.ProcessRunner.Process.SetStandardError(standardError); | ||
return fixture; | ||
} | ||
} | ||
} |
207 changes: 207 additions & 0 deletions
207
src/Cake.Common.Tests/Unit/Tools/Command/CommandRunnerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
// 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; | ||
using Cake.Common.Tests.Fixtures.Tools.Command; | ||
using Xunit; | ||
|
||
namespace Cake.Common.Tests.Unit.Tools.Command | ||
{ | ||
public sealed class CommandRunnerTests | ||
{ | ||
public sealed class TheRunCommandMethod | ||
{ | ||
[Fact] | ||
public void Should_Throw_If_Arguments_Was_Null() | ||
{ | ||
// Given | ||
var fixture = new CommandRunnerFixture | ||
{ | ||
Arguments = null | ||
}; | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
AssertEx.IsArgumentNullException(result, "arguments"); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_Settings_Was_Null() | ||
{ | ||
// Given | ||
var fixture = new CommandRunnerFixture | ||
{ | ||
Settings = null | ||
}; | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
AssertEx.IsArgumentNullException(result, "settings"); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_ToolName_Was_Null() | ||
{ | ||
// Given | ||
var fixture = new CommandRunnerFixture | ||
{ | ||
ToolName = null | ||
}; | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
AssertEx.IsArgumentNullException(result, "ToolName"); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_ToolExecutableNames_Was_Null() | ||
{ | ||
// Given | ||
var fixture = new CommandRunnerFixture | ||
{ | ||
ToolExecutableNames = null | ||
}; | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
AssertEx.IsArgumentNullException(result, "ToolExecutableNames"); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_ToolExecutableNames_Was_Empty() | ||
{ | ||
// Given | ||
var fixture = new CommandRunnerFixture | ||
{ | ||
ToolExecutableNames = Array.Empty<string>() | ||
}; | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
AssertEx.IsArgumentNullException(result, "ToolExecutableNames"); | ||
} | ||
|
||
[Fact] | ||
public void Should_Call_Settings_PostAction() | ||
{ | ||
// Given | ||
var called = false; | ||
var fixture = new CommandRunnerFixture | ||
{ | ||
Settings = { PostAction = _ => called = true } | ||
}; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.True(called, "Settings PostAction not called"); | ||
} | ||
|
||
[Fact] | ||
public void Should_Return_StandardOutput() | ||
{ | ||
// Given | ||
const string expectedStandardOutput = "LINE1"; | ||
const int expectedExitCode = 0; | ||
|
||
var fixture = new CommandRunnerStandardOutputFixture() | ||
.GivenStandardOutput(expectedStandardOutput); | ||
|
||
// When | ||
fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal(expectedStandardOutput, fixture.StandardOutput); | ||
Assert.Equal(expectedExitCode, fixture.ExitCode); | ||
} | ||
|
||
[Fact] | ||
public void Should_Return_StandardOutput_ExitCode() | ||
{ | ||
// Given | ||
const string expectedStandardOutput = "LINE1"; | ||
const int expectedExitCode = 1337; | ||
|
||
var fixture = new CommandRunnerStandardOutputFixture | ||
{ | ||
Settings = | ||
{ | ||
HandleExitCode = exitCode => exitCode == expectedExitCode | ||
} | ||
} | ||
.GivenStandardOutput(expectedStandardOutput); | ||
|
||
fixture.ProcessRunner.Process.SetExitCode(expectedExitCode); | ||
|
||
// When | ||
fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal(expectedStandardOutput, fixture.StandardOutput); | ||
Assert.Equal(expectedExitCode, fixture.ExitCode); | ||
} | ||
|
||
[Fact] | ||
public void Should_Return_StandardError() | ||
{ | ||
// Given | ||
const string expectedStandardOutput = "LINE1"; | ||
const string expectedStandardError = "ERRORLINE1"; | ||
const int expectedExitCode = 0; | ||
|
||
var fixture = new CommandRunnerStandardErrorFixture() | ||
.GivenStandardError(expectedStandardError) | ||
.GivenStandardOutput(expectedStandardOutput); | ||
|
||
// When | ||
fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal(expectedStandardOutput, fixture.StandardOutput); | ||
Assert.Equal(expectedStandardError, fixture.StandardError); | ||
Assert.Equal(expectedExitCode, fixture.ExitCode); | ||
} | ||
|
||
[Fact] | ||
public void Should_Return_StandardError_ExitCode() | ||
{ | ||
// Given | ||
const string expectedStandardOutput = "LINE1"; | ||
const string expectedStandardError = "ERRORLINE1"; | ||
const int expectedExitCode = 1337; | ||
|
||
var fixture = new CommandRunnerStandardErrorFixture | ||
{ | ||
Settings = | ||
{ | ||
HandleExitCode = exitCode => exitCode == expectedExitCode | ||
} | ||
} | ||
.GivenStandardError(expectedStandardError) | ||
.GivenStandardOutput(expectedStandardOutput); | ||
|
||
fixture.ProcessRunner.Process.SetExitCode(expectedExitCode); | ||
|
||
// When | ||
fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal(expectedStandardOutput, fixture.StandardOutput); | ||
Assert.Equal(expectedStandardError, fixture.StandardError); | ||
Assert.Equal(expectedExitCode, fixture.ExitCode); | ||
} | ||
} | ||
} |
Oops, something went wrong.