From f4311e2723cafabaa3678fa510e51ffe9ad203fd Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Fri, 15 Dec 2023 09:43:15 -0800 Subject: [PATCH] Add FailWithPreformatted which saves the failure message before it gets reformated by fluent --- .../Assertions/AssertionScopeExtensions.cs | 20 ++++++++++++ .../Assertions/CommandResultAssertions.cs | 32 +++++++++---------- 2 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/AssertionScopeExtensions.cs diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/AssertionScopeExtensions.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/AssertionScopeExtensions.cs new file mode 100644 index 000000000..4256b1ef7 --- /dev/null +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/AssertionScopeExtensions.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/CommandResultAssertions.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/CommandResultAssertions.cs index c140ec1cd..e6bf83b15 100644 --- a/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/CommandResultAssertions.cs +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/CommandResultAssertions.cs @@ -21,35 +21,35 @@ namespace Microsoft.DotNet.Tools.Test.Utilities public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(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(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(this); } @@ -76,63 +76,63 @@ namespace Microsoft.DotNet.Tools.Test.Utilities public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(this); } public AndConstraint 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(this); }