Skip to content

Capture test output for tools tests #19240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/Tools/Shared/TestHelpers/TestConsole.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

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

public TestConsole(ITestOutputHelper output)
{
var writer = new TestOutputWriter(output);
Error = writer;
Out = writer;
_testWriter = new TestOutputWriter(output);
Error = _testWriter;
Out = _testWriter;
}

public event ConsoleCancelEventHandler CancelKeyPress
Expand All @@ -35,8 +36,8 @@ public event ConsoleCancelEventHandler CancelKeyPress

public Task CancelKeyPressSubscribed => _cancelKeySubscribed.Task;

public TextWriter Error { get; set; }
public TextWriter Out { get; set; }
public TextWriter Error { get; }
public TextWriter Out { get; }
public TextReader In { get; set; } = new StringReader(string.Empty);
public bool IsInputRedirected { get; set; } = false;
public bool IsOutputRedirected { get; } = false;
Expand All @@ -58,10 +59,21 @@ public void ResetColor()
{
}

public string GetOutput()
{
return _testWriter.GetOutput();
}

public void ClearOutput()
{
_testWriter.ClearOutput();
}

private class TestOutputWriter : TextWriter
{
private readonly ITestOutputHelper _output;
private readonly StringBuilder _sb = new StringBuilder();
private readonly StringBuilder _currentOutput = new StringBuilder();

public TestOutputWriter(ITestOutputHelper output)
{
Expand All @@ -83,8 +95,19 @@ public override void Write(char value)
else
{
_sb.Append(value);
_currentOutput.Append(value);
}
}

public string GetOutput()
{
return _currentOutput.ToString();
}

public void ClearOutput()
{
_currentOutput.Clear();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public string Resolve(string project, string configuration)
_reporter.Verbose($"Invoking '{psi.FileName} {psi.Arguments}'");
#endif

var process = Process.Start(psi);
using var process = Process.Start(psi);
process.WaitForExit();

if (process.ExitCode != 0)
Expand Down
8 changes: 1 addition & 7 deletions src/Tools/dotnet-user-secrets/test/InitCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@ public class InitCommandTests : IClassFixture<UserSecretsTestFixture>
private UserSecretsTestFixture _fixture;
private ITestOutputHelper _output;
private TestConsole _console;
private StringBuilder _textOutput;

public InitCommandTests(UserSecretsTestFixture fixture, ITestOutputHelper output)
{
_fixture = fixture;
_output = output;
_textOutput = new StringBuilder();

_console = new TestConsole(output)
{
Error = new StringWriter(_textOutput),
Out = new StringWriter(_textOutput),
};
_console = new TestConsole(output);
}

private CommandContext MakeCommandContext() => new CommandContext(null, new TestReporter(_output), _console);
Expand Down
Loading