Merge pull request #3013 from eerhardt/StreamForwarder
Console.Write() doesn't show output until a newline
This commit is contained in:
commit
eb7054c90d
7 changed files with 173 additions and 20 deletions
|
@ -111,6 +111,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Performance", "test\Perform
|
|||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "binding-redirects.Tests", "test\binding-redirects.Tests\binding-redirects.Tests.xproj", "{27DBF851-F2E3-4FD5-BF4D-A73C81933283}"
|
||||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "dotnet-run.UnitTests", "test\dotnet-run.UnitTests\dotnet-run.UnitTests.xproj", "{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -795,6 +797,22 @@ Global
|
|||
{27DBF851-F2E3-4FD5-BF4D-A73C81933283}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
|
||||
{27DBF851-F2E3-4FD5-BF4D-A73C81933283}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
|
||||
{27DBF851-F2E3-4FD5-BF4D-A73C81933283}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.MinSizeRel|x64.Build.0 = Debug|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Release|x64.Build.0 = Release|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -847,5 +865,6 @@ Global
|
|||
{35E3C2DC-9B38-4EC5-8DD7-C32458DC485F} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
|
||||
{6A3095FF-A7C5-4300-85A9-C025C384401D} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
|
||||
{27DBF851-F2E3-4FD5-BF4D-A73C81933283} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
|
||||
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
@ -31,6 +31,7 @@ namespace Microsoft.DotNet.Cli.Build
|
|||
"dotnet-publish.Tests",
|
||||
"dotnet-resgen.Tests",
|
||||
"dotnet-run.Tests",
|
||||
"dotnet-run.UnitTests",
|
||||
"dotnet-test.Tests",
|
||||
"dotnet-test.UnitTests",
|
||||
"Kestrel.Tests",
|
||||
|
|
|
@ -16,8 +16,8 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
public class Command : ICommand
|
||||
{
|
||||
private readonly Process _process;
|
||||
private readonly StreamForwarder _stdOut;
|
||||
private readonly StreamForwarder _stdErr;
|
||||
private StreamForwarder _stdOut;
|
||||
private StreamForwarder _stdErr;
|
||||
|
||||
private bool _running = false;
|
||||
|
||||
|
@ -27,14 +27,9 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
{
|
||||
FileName = commandSpec.Path,
|
||||
Arguments = commandSpec.Args,
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false
|
||||
};
|
||||
|
||||
_stdOut = new StreamForwarder();
|
||||
_stdErr = new StreamForwarder();
|
||||
|
||||
_process = new Process
|
||||
{
|
||||
StartInfo = psi
|
||||
|
@ -134,11 +129,12 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
|
||||
Reporter.Verbose.WriteLine($"Process ID: {_process.Id}");
|
||||
|
||||
var taskOut = _stdOut.BeginRead(_process.StandardOutput);
|
||||
var taskErr = _stdErr.BeginRead(_process.StandardError);
|
||||
var taskOut = _stdOut?.BeginRead(_process.StandardOutput);
|
||||
var taskErr = _stdErr?.BeginRead(_process.StandardError);
|
||||
_process.WaitForExit();
|
||||
|
||||
Task.WaitAll(taskOut, taskErr);
|
||||
taskOut?.Wait();
|
||||
taskErr?.Wait();
|
||||
}
|
||||
|
||||
var exitCode = _process.ExitCode;
|
||||
|
@ -158,8 +154,8 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
return new CommandResult(
|
||||
this._process.StartInfo,
|
||||
exitCode,
|
||||
_stdOut.CapturedOutput,
|
||||
_stdErr.CapturedOutput);
|
||||
_stdOut?.CapturedOutput,
|
||||
_stdErr?.CapturedOutput);
|
||||
}
|
||||
|
||||
public ICommand WorkingDirectory(string projectDirectory)
|
||||
|
@ -181,6 +177,7 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
public ICommand CaptureStdOut()
|
||||
{
|
||||
ThrowIfRunning();
|
||||
EnsureStdOut();
|
||||
_stdOut.Capture();
|
||||
return this;
|
||||
}
|
||||
|
@ -188,6 +185,7 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
public ICommand CaptureStdErr()
|
||||
{
|
||||
ThrowIfRunning();
|
||||
EnsureStdErr();
|
||||
_stdErr.Capture();
|
||||
return this;
|
||||
}
|
||||
|
@ -197,6 +195,8 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
ThrowIfRunning();
|
||||
if (!onlyIfVerbose || CommandContext.IsVerbose())
|
||||
{
|
||||
EnsureStdOut();
|
||||
|
||||
if (to == null)
|
||||
{
|
||||
_stdOut.ForwardTo(writeLine: Reporter.Output.WriteLine);
|
||||
|
@ -215,6 +215,8 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
ThrowIfRunning();
|
||||
if (!onlyIfVerbose || CommandContext.IsVerbose())
|
||||
{
|
||||
EnsureStdErr();
|
||||
|
||||
if (to == null)
|
||||
{
|
||||
_stdErr.ForwardTo(writeLine: Reporter.Error.WriteLine);
|
||||
|
@ -231,6 +233,8 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
public ICommand OnOutputLine(Action<string> handler)
|
||||
{
|
||||
ThrowIfRunning();
|
||||
EnsureStdOut();
|
||||
|
||||
_stdOut.ForwardTo(writeLine: handler);
|
||||
return this;
|
||||
}
|
||||
|
@ -238,6 +242,8 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
public ICommand OnErrorLine(Action<string> handler)
|
||||
{
|
||||
ThrowIfRunning();
|
||||
EnsureStdErr();
|
||||
|
||||
_stdErr.ForwardTo(writeLine: handler);
|
||||
return this;
|
||||
}
|
||||
|
@ -258,6 +264,18 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
return info.FileName + " " + info.Arguments;
|
||||
}
|
||||
|
||||
private void EnsureStdOut()
|
||||
{
|
||||
_stdOut = _stdOut ?? new StreamForwarder();
|
||||
_process.StartInfo.RedirectStandardOutput = true;
|
||||
}
|
||||
|
||||
private void EnsureStdErr()
|
||||
{
|
||||
_stdErr = _stdErr ?? new StreamForwarder();
|
||||
_process.StartInfo.RedirectStandardError = true;
|
||||
}
|
||||
|
||||
private void ThrowIfRunning([CallerMemberName] string memberName = null)
|
||||
{
|
||||
if (_running)
|
||||
|
|
|
@ -20,16 +20,26 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
public string Project = null;
|
||||
public IReadOnlyList<string> Args = null;
|
||||
|
||||
private readonly ICommandFactory _commandFactory;
|
||||
private ProjectContext _context;
|
||||
private List<string> _args;
|
||||
private BuildWorkspace _workspace;
|
||||
|
||||
public static readonly string[] DefaultFrameworks = new[]
|
||||
{
|
||||
FrameworkConstants.FrameworkIdentifiers.NetCoreApp,
|
||||
FrameworkConstants.FrameworkIdentifiers.NetStandardApp,
|
||||
};
|
||||
|
||||
public RunCommand()
|
||||
: this(new RunCommandFactory())
|
||||
{
|
||||
}
|
||||
|
||||
ProjectContext _context;
|
||||
List<string> _args;
|
||||
private BuildWorkspace _workspace;
|
||||
public RunCommand(ICommandFactory commandFactory)
|
||||
{
|
||||
_commandFactory = commandFactory;
|
||||
}
|
||||
|
||||
public int Start()
|
||||
{
|
||||
|
@ -166,24 +176,22 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
}
|
||||
}
|
||||
|
||||
Command command;
|
||||
ICommand command;
|
||||
if (outputName.EndsWith(FileNameSuffixes.DotNet.DynamicLib, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// The executable is a ".dll", we need to call it through dotnet.exe
|
||||
var muxer = new Muxer();
|
||||
|
||||
command = Command.Create(muxer.MuxerPath, Enumerable.Concat(
|
||||
command = _commandFactory.Create(muxer.MuxerPath, Enumerable.Concat(
|
||||
Enumerable.Concat(new string[] { "exec" }, hostArgs),
|
||||
Enumerable.Concat(new string[] { outputName }, _args)));
|
||||
}
|
||||
else
|
||||
{
|
||||
command = Command.Create(outputName, Enumerable.Concat(hostArgs, _args));
|
||||
command = _commandFactory.Create(outputName, Enumerable.Concat(hostArgs, _args));
|
||||
}
|
||||
|
||||
result = command
|
||||
.ForwardStdOut()
|
||||
.ForwardStdErr()
|
||||
.Execute()
|
||||
.ExitCode;
|
||||
|
||||
|
@ -198,5 +206,13 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
var result = command.Execute();
|
||||
return result.ExitCode;
|
||||
}
|
||||
|
||||
private class RunCommandFactory : ICommandFactory
|
||||
{
|
||||
public ICommand Create(string commandName, IEnumerable<string> args, NuGetFramework framework = null, string configuration = "Debug")
|
||||
{
|
||||
return Command.Create(commandName, args, framework, configuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
53
test/dotnet-run.UnitTests/GivenARunCommand.cs
Normal file
53
test/dotnet-run.UnitTests/GivenARunCommand.cs
Normal file
|
@ -0,0 +1,53 @@
|
|||
// 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 System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.TestFramework;
|
||||
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||
using Moq;
|
||||
using NuGet.Frameworks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Run.Tests
|
||||
{
|
||||
public class GivenARunCommand : TestBase
|
||||
{
|
||||
private const int RunExitCode = 29;
|
||||
|
||||
[Fact]
|
||||
public void ItDoesntRedirectStandardOutAndError()
|
||||
{
|
||||
TestInstance instance = TestAssetsManager.CreateTestInstance("TestAppSimple")
|
||||
.WithLockFiles();
|
||||
|
||||
new BuildCommand(instance.TestRoot)
|
||||
.Execute()
|
||||
.Should()
|
||||
.Pass();
|
||||
|
||||
// use MockBehavior.Strict to ensure the RunCommand doesn't call CaptureStdOut, ForwardStdOut, etc.
|
||||
Mock<ICommand> failOnRedirectOutputCommand = new Mock<ICommand>(MockBehavior.Strict);
|
||||
failOnRedirectOutputCommand
|
||||
.Setup(c => c.Execute())
|
||||
.Returns(new CommandResult(null, RunExitCode, null, null));
|
||||
|
||||
Mock<ICommandFactory> commandFactoryMock = new Mock<ICommandFactory>();
|
||||
commandFactoryMock
|
||||
.Setup(c => c.Create(
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<IEnumerable<string>>(),
|
||||
It.IsAny<NuGetFramework>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns(failOnRedirectOutputCommand.Object);
|
||||
|
||||
RunCommand runCommand = new RunCommand(commandFactoryMock.Object);
|
||||
runCommand.Project = instance.TestRoot;
|
||||
|
||||
runCommand.Start()
|
||||
.Should()
|
||||
.Be(RunExitCode);
|
||||
}
|
||||
}
|
||||
}
|
18
test/dotnet-run.UnitTests/dotnet-run.UnitTests.xproj
Normal file
18
test/dotnet-run.UnitTests/dotnet-run.UnitTests.xproj
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0.24720" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.24720</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>2a5eec38-3030-44ef-9e58-6771e7bab01d</ProjectGuid>
|
||||
<RootNamespace>Microsoft.DotNet.Tools.Run.UnitTests</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin</OutputPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
28
test/dotnet-run.UnitTests/project.json
Normal file
28
test/dotnet-run.UnitTests/project.json
Normal file
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"version": "1.0.0-*",
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"type": "platform",
|
||||
"version": "1.0.0-rc3-*"
|
||||
},
|
||||
"System.Runtime.Serialization.Primitives": "4.1.1-rc2-24027",
|
||||
"dotnet": {
|
||||
"target": "project"
|
||||
},
|
||||
"Microsoft.DotNet.Tools.Tests.Utilities": {
|
||||
"target": "project"
|
||||
},
|
||||
"xunit": "2.1.0",
|
||||
"moq.netcore": "4.4.0-beta8",
|
||||
"dotnet-test-xunit": "1.0.0-rc2-173361-36"
|
||||
},
|
||||
"frameworks": {
|
||||
"netcoreapp1.0": {
|
||||
"imports": [
|
||||
"dotnet5.4",
|
||||
"portable-net451+win8"
|
||||
]
|
||||
}
|
||||
},
|
||||
"testRunner": "xunit"
|
||||
}
|
Loading…
Reference in a new issue