Skip to content

Commit

Permalink
Add IgnoreExitCode to ToolSettings
Browse files Browse the repository at this point in the history
- When true, then ProcessExitCode method is not called
  • Loading branch information
flcdrg committed Jul 8, 2020
1 parent daa3c4f commit d55a15a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
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_IgnoreExitCode_Is_True()
{
// Given
var fixture = new DummyToolFixture();
fixture.Settings.IgnoreExitCode = 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 @@ -117,7 +117,10 @@ protected void Run(
// Post action specified?
postAction?.Invoke(process);

ProcessExitCode(process.GetExitCode());
if (!settings.IgnoreExitCode)
{
ProcessExitCode(process.GetExitCode());
}
}

/// <summary>
Expand All @@ -131,6 +134,7 @@ protected virtual void ProcessExitCode(int exitCode)
if (exitCode != 0)
{
const string message = "{0}: Process returned an error (exit code {1}).";

throw new CakeException(string.Format(CultureInfo.InvariantCulture, message, GetToolName(), exitCode));
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Cake.Core/Tooling/ToolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,17 @@ public class ToolSettings
/// </code>
/// </example>
public IDictionary<string, string> EnvironmentVariables { get; set; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// Gets or sets a value indicating whether the exit code from the tool process causes an exception to be thrown.
/// </summary>
/// <example>
/// <code>
/// DotNetCoreTest("MyProject.csproj", new DotNetCoreTestSettings {
/// IgnoreExitCode = true
/// });
/// </code>
/// </example>
public bool IgnoreExitCode { get; set; }
}
}

0 comments on commit d55a15a

Please sign in to comment.