ShipProductTests and ShipModCatalogTests capture Console through ConsoleProbe.Capture so restores cannot interleave
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
eee5f213bf
commit
58c4fa10c1
3 changed files with 23 additions and 18 deletions
|
|
@ -13,15 +13,28 @@ internal static class ConsoleProbe
|
|||
private static readonly object Gate = new object();
|
||||
|
||||
public static string WrittenBy(Action act)
|
||||
{
|
||||
Capture<int>(() => { act(); return 0; }, out var output, out _);
|
||||
return output;
|
||||
}
|
||||
|
||||
// The action's result with what it wrote to Console.Out and Console.Error on this thread.
|
||||
public static T Capture<T>(Func<T> act, out string output, out string error)
|
||||
{
|
||||
lock (Gate)
|
||||
{
|
||||
var previous = Console.Out;
|
||||
var probe = new ThreadWriter(Environment.CurrentManagedThreadId);
|
||||
Console.SetOut(probe);
|
||||
try { act(); }
|
||||
finally { Console.SetOut(previous); }
|
||||
return probe.Mine.ToString();
|
||||
var (previousOut, previousError) = (Console.Out, Console.Error);
|
||||
var (probeOut, probeError) = (new ThreadWriter(Environment.CurrentManagedThreadId), new ThreadWriter(Environment.CurrentManagedThreadId));
|
||||
Console.SetOut(probeOut);
|
||||
Console.SetError(probeError);
|
||||
try { return act(); }
|
||||
finally
|
||||
{
|
||||
Console.SetOut(previousOut);
|
||||
Console.SetError(previousError);
|
||||
output = probeOut.Mine.ToString();
|
||||
error = probeError.Mine.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -546,12 +546,8 @@ public sealed class ShipModCatalogTests : IDisposable
|
|||
|
||||
private static (int Code, string Output, string Error) Run(params string[] args)
|
||||
{
|
||||
var (output, error) = (new StringWriter(), new StringWriter());
|
||||
var (previousOut, previousError) = (Console.Out, Console.Error);
|
||||
Console.SetOut(output);
|
||||
Console.SetError(error);
|
||||
try { return (ShipAuthoringCommands.Run(args), output.ToString(), error.ToString()); }
|
||||
finally { Console.SetOut(previousOut); Console.SetError(previousError); }
|
||||
var code = ConsoleProbe.Capture(() => ShipAuthoringCommands.Run(args), out var output, out var error);
|
||||
return (code, output, error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
|
|
@ -247,12 +247,8 @@ public sealed class ShipProductTests : IDisposable
|
|||
|
||||
private static (int Code, string Output, string Error) Run(params string[] args)
|
||||
{
|
||||
var (output, error) = (new StringWriter(), new StringWriter());
|
||||
var (previousOut, previousError) = (Console.Out, Console.Error);
|
||||
Console.SetOut(output);
|
||||
Console.SetError(error);
|
||||
try { return (ShipAuthoringCommands.Run(args), output.ToString(), error.ToString()); }
|
||||
finally { Console.SetOut(previousOut); Console.SetError(previousError); }
|
||||
var code = ConsoleProbe.Capture(() => ShipAuthoringCommands.Run(args), out var output, out var error);
|
||||
return (code, output, error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
Loading…
Reference in a new issue