2016-01-06 23:30:56 +00:00
|
|
|
|
// 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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-04-28 23:30:32 +00:00
|
|
|
|
using Microsoft.DotNet.InternalAbstractions;
|
2016-01-06 23:30:56 +00:00
|
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
2016-04-28 23:30:32 +00:00
|
|
|
|
using Xunit;
|
2016-01-06 23:30:56 +00:00
|
|
|
|
|
|
|
|
|
namespace StreamForwarderTests
|
|
|
|
|
{
|
2016-02-05 02:18:08 +00:00
|
|
|
|
public class StreamForwarderTests : TestBase
|
2016-01-06 23:30:56 +00:00
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
|
private static readonly string s_rid = RuntimeEnvironmentRidExtensions.GetLegacyRestoreRuntimeIdentifier();
|
2016-02-09 22:51:02 +00:00
|
|
|
|
private static readonly string s_testProjectRoot = Path.Combine(AppContext.BaseDirectory, "TestAssets", "TestProjects");
|
2016-02-05 02:18:08 +00:00
|
|
|
|
|
|
|
|
|
private TempDirectory _root;
|
|
|
|
|
|
2016-01-06 23:30:56 +00:00
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Dummy Entrypoint");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
public StreamForwarderTests()
|
2016-01-06 23:30:56 +00:00
|
|
|
|
{
|
2016-02-05 02:18:08 +00:00
|
|
|
|
_root = Temp.CreateDirectory();
|
2016-01-06 23:30:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
public static IEnumerable<object[]> ForwardingTheoryVariations
|
2016-01-06 23:30:56 +00:00
|
|
|
|
{
|
2016-02-05 02:18:08 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
new object[] { "123", new string[]{"123"} },
|
|
|
|
|
new object[] { "123\n", new string[] {"123"} },
|
|
|
|
|
new object[] { "123\r\n", new string[] {"123"} },
|
|
|
|
|
new object[] { "1234\n5678", new string[] {"1234", "5678"} },
|
|
|
|
|
new object[] { "1234\r\n5678", new string[] {"1234", "5678"} },
|
|
|
|
|
new object[] { "1234\n5678\n", new string[] {"1234", "5678"} },
|
|
|
|
|
new object[] { "1234\r\n5678\r\n", new string[] {"1234", "5678"} },
|
|
|
|
|
new object[] { "1234\n5678\nabcdefghijklmnopqrstuvwxyz", new string[] {"1234", "5678", "abcdefghijklmnopqrstuvwxyz"} },
|
|
|
|
|
new object[] { "1234\r\n5678\r\nabcdefghijklmnopqrstuvwxyz", new string[] {"1234", "5678", "abcdefghijklmnopqrstuvwxyz"} },
|
|
|
|
|
new object[] { "1234\n5678\nabcdefghijklmnopqrstuvwxyz\n", new string[] {"1234", "5678", "abcdefghijklmnopqrstuvwxyz"} },
|
|
|
|
|
new object[] { "1234\r\n5678\r\nabcdefghijklmnopqrstuvwxyz\r\n", new string[] {"1234", "5678", "abcdefghijklmnopqrstuvwxyz"} }
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-01-06 23:30:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData("123")]
|
|
|
|
|
[InlineData("123\n")]
|
|
|
|
|
public void TestNoForwardingNoCapture(string inputStr)
|
2016-04-28 23:30:32 +00:00
|
|
|
|
{
|
2016-02-05 02:18:08 +00:00
|
|
|
|
TestCapturingAndForwardingHelper(ForwardOptions.None, inputStr, null, new string[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData("ForwardingTheoryVariations")]
|
|
|
|
|
public void TestForwardingOnly(string inputStr, string[] expectedWrites)
|
2016-01-06 23:30:56 +00:00
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
|
for (int i = 0; i < expectedWrites.Length; ++i)
|
2016-02-05 02:18:08 +00:00
|
|
|
|
{
|
|
|
|
|
expectedWrites[i] += Environment.NewLine;
|
|
|
|
|
}
|
2016-04-28 23:30:32 +00:00
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
TestCapturingAndForwardingHelper(ForwardOptions.WriteLine, inputStr, null, expectedWrites);
|
|
|
|
|
}
|
2016-01-06 23:30:56 +00:00
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData("ForwardingTheoryVariations")]
|
|
|
|
|
public void TestCaptureOnly(string inputStr, string[] expectedWrites)
|
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
|
for (int i = 0; i < expectedWrites.Length; ++i)
|
2016-02-05 02:18:08 +00:00
|
|
|
|
{
|
|
|
|
|
expectedWrites[i] += Environment.NewLine;
|
|
|
|
|
}
|
2016-01-06 23:30:56 +00:00
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
var expectedCaptured = string.Join("", expectedWrites);
|
2016-04-28 23:30:32 +00:00
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
TestCapturingAndForwardingHelper(ForwardOptions.Capture, inputStr, expectedCaptured, new string[0]);
|
|
|
|
|
}
|
2016-01-06 23:30:56 +00:00
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData("ForwardingTheoryVariations")]
|
|
|
|
|
public void TestCaptureAndForwardingTogether(string inputStr, string[] expectedWrites)
|
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
|
for (int i = 0; i < expectedWrites.Length; ++i)
|
2016-02-05 02:18:08 +00:00
|
|
|
|
{
|
|
|
|
|
expectedWrites[i] += Environment.NewLine;
|
|
|
|
|
}
|
2016-01-06 23:30:56 +00:00
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
var expectedCaptured = string.Join("", expectedWrites);
|
2016-01-06 23:30:56 +00:00
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
TestCapturingAndForwardingHelper(ForwardOptions.WriteLine | ForwardOptions.Capture, inputStr, expectedCaptured, expectedWrites);
|
2016-01-06 23:30:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private enum ForwardOptions
|
|
|
|
|
{
|
|
|
|
|
None = 0x0,
|
|
|
|
|
Capture = 0x1,
|
2016-02-05 02:18:08 +00:00
|
|
|
|
WriteLine = 0x02,
|
2016-01-06 23:30:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-05 02:18:08 +00:00
|
|
|
|
private void TestCapturingAndForwardingHelper(ForwardOptions options, string str, string expectedCaptured, string[] expectedWrites)
|
2016-01-06 23:30:56 +00:00
|
|
|
|
{
|
2016-02-05 02:18:08 +00:00
|
|
|
|
var forwarder = new StreamForwarder();
|
2016-01-06 23:30:56 +00:00
|
|
|
|
var writes = new List<string>();
|
2016-02-05 02:18:08 +00:00
|
|
|
|
|
2016-01-06 23:30:56 +00:00
|
|
|
|
if ((options & ForwardOptions.WriteLine) != 0)
|
|
|
|
|
{
|
2016-02-05 02:18:08 +00:00
|
|
|
|
forwarder.ForwardTo(writeLine: s => writes.Add(s + Environment.NewLine));
|
2016-01-06 23:30:56 +00:00
|
|
|
|
}
|
|
|
|
|
if ((options & ForwardOptions.Capture) != 0)
|
|
|
|
|
{
|
|
|
|
|
forwarder.Capture();
|
|
|
|
|
}
|
2016-02-05 02:18:08 +00:00
|
|
|
|
|
2016-01-06 23:30:56 +00:00
|
|
|
|
forwarder.Read(new StringReader(str));
|
|
|
|
|
Assert.Equal(expectedWrites, writes);
|
2016-02-05 02:18:08 +00:00
|
|
|
|
|
|
|
|
|
var captured = forwarder.CapturedOutput;
|
2016-01-06 23:30:56 +00:00
|
|
|
|
Assert.Equal(expectedCaptured, captured);
|
|
|
|
|
}
|
2016-02-05 02:18:08 +00:00
|
|
|
|
|
|
|
|
|
private string SetupTestProject()
|
|
|
|
|
{
|
|
|
|
|
var sourceTestProjectPath = Path.Combine(s_testProjectRoot, "OutputStandardOutputAndError");
|
|
|
|
|
|
|
|
|
|
var binTestProjectPath = _root.CopyDirectory(sourceTestProjectPath).Path;
|
|
|
|
|
|
|
|
|
|
var buildCommand = new BuildCommand(Path.Combine(binTestProjectPath, "project.json"));
|
|
|
|
|
buildCommand.Execute();
|
|
|
|
|
|
|
|
|
|
var buildOutputExe = "OutputStandardOutputAndError" + Constants.ExeSuffix;
|
2016-04-13 00:29:07 +00:00
|
|
|
|
var buildOutputPath = Path.Combine(binTestProjectPath, "bin/Debug/netcoreapp1.0", buildOutputExe);
|
2016-02-05 02:18:08 +00:00
|
|
|
|
|
|
|
|
|
return buildOutputPath;
|
|
|
|
|
}
|
2016-01-06 23:30:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|