Skip to content

Commit 169f07c

Browse files
Capture test output for tools tests (#19240)
1 parent 78ce7b6 commit 169f07c

File tree

6 files changed

+100
-91
lines changed

6 files changed

+100
-91
lines changed

src/Tools/Shared/TestHelpers/TestConsole.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -15,12 +15,13 @@ public class TestConsole : IConsole
1515
{
1616
private event ConsoleCancelEventHandler _cancelKeyPress;
1717
private readonly TaskCompletionSource<bool> _cancelKeySubscribed = new TaskCompletionSource<bool>();
18+
private readonly TestOutputWriter _testWriter;
1819

1920
public TestConsole(ITestOutputHelper output)
2021
{
21-
var writer = new TestOutputWriter(output);
22-
Error = writer;
23-
Out = writer;
22+
_testWriter = new TestOutputWriter(output);
23+
Error = _testWriter;
24+
Out = _testWriter;
2425
}
2526

2627
public event ConsoleCancelEventHandler CancelKeyPress
@@ -35,8 +36,8 @@ public event ConsoleCancelEventHandler CancelKeyPress
3536

3637
public Task CancelKeyPressSubscribed => _cancelKeySubscribed.Task;
3738

38-
public TextWriter Error { get; set; }
39-
public TextWriter Out { get; set; }
39+
public TextWriter Error { get; }
40+
public TextWriter Out { get; }
4041
public TextReader In { get; set; } = new StringReader(string.Empty);
4142
public bool IsInputRedirected { get; set; } = false;
4243
public bool IsOutputRedirected { get; } = false;
@@ -58,10 +59,21 @@ public void ResetColor()
5859
{
5960
}
6061

62+
public string GetOutput()
63+
{
64+
return _testWriter.GetOutput();
65+
}
66+
67+
public void ClearOutput()
68+
{
69+
_testWriter.ClearOutput();
70+
}
71+
6172
private class TestOutputWriter : TextWriter
6273
{
6374
private readonly ITestOutputHelper _output;
6475
private readonly StringBuilder _sb = new StringBuilder();
76+
private readonly StringBuilder _currentOutput = new StringBuilder();
6577

6678
public TestOutputWriter(ITestOutputHelper output)
6779
{
@@ -83,8 +95,19 @@ public override void Write(char value)
8395
else
8496
{
8597
_sb.Append(value);
98+
_currentOutput.Append(value);
8699
}
87100
}
101+
102+
public string GetOutput()
103+
{
104+
return _currentOutput.ToString();
105+
}
106+
107+
public void ClearOutput()
108+
{
109+
_currentOutput.Clear();
110+
}
88111
}
89112
}
90113
}

src/Tools/dotnet-user-secrets/src/Internal/ProjectIdResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public string Resolve(string project, string configuration)
6666
_reporter.Verbose($"Invoking '{psi.FileName} {psi.Arguments}'");
6767
#endif
6868

69-
var process = Process.Start(psi);
69+
using var process = Process.Start(psi);
7070
process.WaitForExit();
7171

7272
if (process.ExitCode != 0)

src/Tools/dotnet-user-secrets/test/InitCommandTest.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,13 @@ public class InitCommandTests : IClassFixture<UserSecretsTestFixture>
1919
private UserSecretsTestFixture _fixture;
2020
private ITestOutputHelper _output;
2121
private TestConsole _console;
22-
private StringBuilder _textOutput;
2322

2423
public InitCommandTests(UserSecretsTestFixture fixture, ITestOutputHelper output)
2524
{
2625
_fixture = fixture;
2726
_output = output;
28-
_textOutput = new StringBuilder();
2927

30-
_console = new TestConsole(output)
31-
{
32-
Error = new StringWriter(_textOutput),
33-
Out = new StringWriter(_textOutput),
34-
};
28+
_console = new TestConsole(output);
3529
}
3630

3731
private CommandContext MakeCommandContext() => new CommandContext(null, new TestReporter(_output), _console);

0 commit comments

Comments
 (0)