Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH2825: Add HandleExitCode to ToolSettings #2826

Merged
merged 1 commit into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Cake.Core.Tests/Unit/Tooling/ToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ public void Should_Throw_On_Invalid_ExitCode_Validated_By_Custom_Validator()
// Then
AssertEx.IsCakeException(result, "UnitTest");
}

[Fact]
public void Should_Not_Throw_On_Invalid_ExitCode_When_HandleExitCode_Returns_True()
{
// Given
var fixture = new DummyToolFixture();
fixture.Settings.HandleExitCode = _ => true;
fixture.GivenProcessExitsWithCode(10);

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

// Then
Assert.IsNotType<Exception>(result);
}
}
}
}
6 changes: 5 additions & 1 deletion src/Cake.Core/Tooling/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected void Run(
// Post action specified?
postAction?.Invoke(process);

ProcessExitCode(process.GetExitCode());
var exitCode = process.GetExitCode();
if (!settings.HandleExitCode?.Invoke(exitCode) ?? true)
{
ProcessExitCode(process.GetExitCode());
}
}

/// <summary>
Expand Down
36 changes: 36 additions & 0 deletions src/Cake.Core/Tooling/ToolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,41 @@ public class ToolSettings
/// </code>
/// </example>
public IDictionary<string, string> EnvironmentVariables { get; set; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// Gets or sets whether the exit code from the tool process causes an exception to be thrown.
/// <para>
/// If the delegate is null (the default) or returns false, then an exception is thrown upon a non-zero exit code.
/// </para>
/// <para>
/// If the delegate returns true then no exception is thrown.
/// </para>
/// <para>
/// This can be useful when the exit code should be ignored, or if there is a desire to apply logic that is conditional
/// on the exit code value.
/// </para>
/// </summary>
/// <example>
/// Don't throw exceptions if DotNetCoreTest returns non-zero:
/// <code>
/// DotNetCoreTest("MyProject.csproj", new DotNetCoreTestSettings {
/// HandleExitCode = _=&gt; true
/// });
/// </code>
/// </example>
/// <example>
/// Use custom logic for exit code:
/// <code>
/// DotNetCoreTest("MyProject.csproj", new DotNetCoreTestSettings {
/// HandleExitCode = exitCode =&gt; exitCode switch {
/// 0 =&gt; throw new CakeException("ZERO"),
/// 1 =&gt; true, // treat 1 and 2 as handled "ok".
/// 2 =&gt; true,
/// _ =&gt; false // everything else will throw via default implementation
/// };
/// });
/// </code>
/// </example>
public Func<int, bool> HandleExitCode { get; set; }
}
}