Skip to content

Commit 55b6d64

Browse files
authored
Add more info for StdErrAfter assertion in host tests (#118712)
The assertion only ended up printing stderr, but it would be useful to have all the information about the command that was run (the exit code, stdout).
1 parent 6edbc50 commit 55b6d64

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/installer/tests/TestUtils/Assertions/CommandResultAssertions.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Linq;
65
using System.Text.RegularExpressions;
76
using FluentAssertions;
87
using FluentAssertions.Execution;
@@ -154,18 +153,6 @@ public AndConstraint<CommandResultAssertions> NotFileContains(string path, strin
154153
return new AndConstraint<CommandResultAssertions>(this);
155154
}
156155

157-
public string GetDiagnosticsInfo()
158-
=> $"""
159-
160-
File Name: {Result.StartInfo.FileName}
161-
Arguments: {Result.StartInfo.Arguments}
162-
Environment:
163-
{string.Join(Environment.NewLine, Result.StartInfo.Environment.Where(i => i.Key.StartsWith(Constants.DotnetRoot.EnvironmentVariable)).Select(i => $" {i.Key} = {i.Value}"))}
164-
Exit Code: 0x{Result.ExitCode:x}
165-
StdOut:
166-
{Result.StdOut}
167-
StdErr:
168-
{Result.StdErr}
169-
""";
156+
public string GetDiagnosticsInfo() => Result.GetDiagnosticsInfo();
170157
}
171158
}

src/installer/tests/TestUtils/Assertions/CommandResultExtensions.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using FluentAssertions;
55
using FluentAssertions.Execution;
66
using Microsoft.DotNet.Cli.Build.Framework;
7-
using System;
87

98
namespace Microsoft.DotNet.CoreSetup.Test
109
{
@@ -20,10 +19,7 @@ public static CommandResult StdErrAfter(this CommandResult commandResult, string
2019
int i = commandResult.StdErr.IndexOf(pattern);
2120
i.Should().BeGreaterThanOrEqualTo(
2221
0,
23-
"Trying to filter StdErr after '{0}', but such string can't be found in the StdErr.{1}{2}",
24-
pattern,
25-
Environment.NewLine,
26-
commandResult.StdErr);
22+
$"'{pattern}' should be in StdErr - cannot filter StdErr to after expected string.{commandResult.GetDiagnosticsInfo()}");
2723
string filteredStdErr = commandResult.StdErr.Substring(i);
2824

2925
return new CommandResult(commandResult.StartInfo, commandResult.ProcessId, commandResult.ExitCode, commandResult.StdOut, filteredStdErr);

src/installer/tests/TestUtils/CommandResult.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Text;
65
using System.Diagnostics;
6+
using System.Linq;
7+
using System.Text;
78

89
namespace Microsoft.DotNet.Cli.Build.Framework
910
{
@@ -23,5 +24,19 @@ public CommandResult(ProcessStartInfo startInfo, int pid, int exitCode, string s
2324
StdOut = stdOut;
2425
StdErr = stdErr;
2526
}
27+
28+
internal string GetDiagnosticsInfo()
29+
=> $"""
30+
31+
File Name: {StartInfo.FileName}
32+
Arguments: {StartInfo.Arguments}
33+
Environment:
34+
{string.Join(Environment.NewLine, StartInfo.Environment.Where(i => i.Key.StartsWith("DOTNET_", OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)).Select(i => $" {i.Key} = {i.Value}"))}
35+
Exit Code: 0x{ExitCode:x}
36+
StdOut:
37+
{StdOut}
38+
StdErr:
39+
{StdErr}
40+
""";
2641
}
2742
}

0 commit comments

Comments
 (0)