Add FailWithPreformatted which saves the failure message before it gets reformated by fluent

This commit is contained in:
Marc Paine 2023-12-15 09:43:15 -08:00
parent d9d1e85d54
commit f4311e2723
2 changed files with 36 additions and 16 deletions

View file

@ -0,0 +1,20 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using FluentAssertions.Execution;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public static class AssertionScopeExtensions
{
public static Continuation FailWithPreformatted(this AssertionScope assertionScope, string message)
{
if (!assertionScope.Succeeded)
{
assertionScope.AddFailure(message);
}
return new Continuation(assertionScope, assertionScope.Succeeded);
}
}
}

View file

@ -21,35 +21,35 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
public AndConstraint<CommandResultAssertions> ExitWith(int expectedExitCode)
{
Execute.Assertion.ForCondition(_commandResult.ExitCode == expectedExitCode)
.FailWith(AppendDiagnosticsTo($"Expected command to exit with {expectedExitCode} but it did not."));
.FailWithPreformatted(AppendDiagnosticsTo($"Expected command to exit with {expectedExitCode} but it did not."));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> Pass()
{
Execute.Assertion.ForCondition(_commandResult.ExitCode == 0)
.FailWith(AppendDiagnosticsTo($"Expected command to pass but it did not."));
.FailWithPreformatted(AppendDiagnosticsTo($"Expected command to pass but it did not."));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> Fail()
{
Execute.Assertion.ForCondition(_commandResult.ExitCode != 0)
.FailWith(AppendDiagnosticsTo($"Expected command to fail but it did not."));
.FailWithPreformatted(AppendDiagnosticsTo($"Expected command to fail but it did not."));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> HaveStdOut()
{
Execute.Assertion.ForCondition(!string.IsNullOrEmpty(_commandResult.StdOut))
.FailWith(AppendDiagnosticsTo($"Command did not output anything to stdout"));
.FailWithPreformatted(AppendDiagnosticsTo($"Command did not output anything to stdout"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> HaveStdOut(string expectedOutput)
{
Execute.Assertion.ForCondition(_commandResult.StdOut.Equals(expectedOutput, StringComparison.Ordinal))
.FailWith(AppendDiagnosticsTo($"Command did not output with Expected Output. Expected: {expectedOutput}"));
.FailWithPreformatted(AppendDiagnosticsTo($"Command did not output with Expected Output. Expected: {expectedOutput}"));
return new AndConstraint<CommandResultAssertions>(this);
}
@ -57,7 +57,7 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
{
Execute.Assertion
.ForCondition(_commandResult.StdOut.Contains(pattern))
.FailWith(AppendDiagnosticsTo($"The command output did not contain expected result: {pattern}{Environment.NewLine}"));
.FailWithPreformatted(AppendDiagnosticsTo($"The command output did not contain expected result: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
@ -68,7 +68,7 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
Execute.Assertion
.ForCondition(commandResultNoSpaces.Contains(pattern))
.FailWith(AppendDiagnosticsTo($"The command output did not contain expected result: {pattern}{Environment.NewLine}"));
.FailWithPreformatted(AppendDiagnosticsTo($"The command output did not contain expected result: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
@ -76,63 +76,63 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
public AndConstraint<CommandResultAssertions> HaveStdOutContainingIgnoreCase(string pattern)
{
Execute.Assertion.ForCondition(_commandResult.StdOut.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0)
.FailWith(AppendDiagnosticsTo($"The command output did not contain expected result (ignoring case): {pattern}{Environment.NewLine}"));
.FailWithPreformatted(AppendDiagnosticsTo($"The command output did not contain expected result (ignoring case): {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> NotHaveStdOutContaining(string pattern)
{
Execute.Assertion.ForCondition(!_commandResult.StdOut.Contains(pattern))
.FailWith(AppendDiagnosticsTo($"The command output contained a result it should not have contained: {pattern}{Environment.NewLine}"));
.FailWithPreformatted(AppendDiagnosticsTo($"The command output contained a result it should not have contained: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> HaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None)
{
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdOut, pattern, options).Success)
.FailWith(AppendDiagnosticsTo($"Matching the command output failed. Pattern: {pattern}{Environment.NewLine}"));
.FailWithPreformatted(AppendDiagnosticsTo($"Matching the command output failed. Pattern: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> HaveStdErr()
{
Execute.Assertion.ForCondition(!string.IsNullOrEmpty(_commandResult.StdErr))
.FailWith(AppendDiagnosticsTo($"Command did not output anything to stderr."));
.FailWithPreformatted(AppendDiagnosticsTo($"Command did not output anything to stderr."));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> HaveStdErrContaining(string pattern)
{
Execute.Assertion.ForCondition(_commandResult.StdErr.Contains(pattern))
.FailWith(AppendDiagnosticsTo($"The command error output did not contain expected result: {pattern}{Environment.NewLine}"));
.FailWithPreformatted(AppendDiagnosticsTo($"The command error output did not contain expected result: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> NotHaveStdErrContaining(string pattern)
{
Execute.Assertion.ForCondition(!_commandResult.StdErr.Contains(pattern))
.FailWith(AppendDiagnosticsTo($"The command error output contained a result it should not have contained: {pattern}{Environment.NewLine}"));
.FailWithPreformatted(AppendDiagnosticsTo($"The command error output contained a result it should not have contained: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> HaveStdErrMatching(string pattern, RegexOptions options = RegexOptions.None)
{
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdErr, pattern, options).Success)
.FailWith(AppendDiagnosticsTo($"Matching the command error output failed. Pattern: {pattern}{Environment.NewLine}"));
.FailWithPreformatted(AppendDiagnosticsTo($"Matching the command error output failed. Pattern: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> NotHaveStdOut()
{
Execute.Assertion.ForCondition(string.IsNullOrEmpty(_commandResult.StdOut))
.FailWith(AppendDiagnosticsTo($"Expected command to not output to stdout but it was not:"));
.FailWithPreformatted(AppendDiagnosticsTo($"Expected command to not output to stdout but it was not:"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> NotHaveStdErr()
{
Execute.Assertion.ForCondition(string.IsNullOrEmpty(_commandResult.StdErr))
.FailWith(AppendDiagnosticsTo($"Expected command to not output to stderr but it was not:"));
.FailWithPreformatted(AppendDiagnosticsTo($"Expected command to not output to stderr but it was not:"));
return new AndConstraint<CommandResultAssertions>(this);
}