2015-11-24 18:48:31 -08: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 ;
2015-12-07 14:18:09 -08:00
using System.Collections.Generic ;
2015-11-24 17:47:33 -08:00
using System.IO ;
2015-11-25 19:46:01 -08:00
using System.Runtime.InteropServices ;
2015-12-07 14:18:09 -08:00
using System.Text ;
2015-11-24 17:47:33 -08:00
using Xunit ;
using Microsoft.DotNet.Cli.Utils ;
2015-11-30 11:25:13 -08:00
using Microsoft.DotNet.ProjectModel ;
2015-11-24 17:47:33 -08:00
namespace ConsoleApplication
{
public class E2ETest
{
2015-11-25 19:46:01 -08:00
private static readonly string EXPECTED_OUTPUT = "Hello World!" + Environment . NewLine ;
private static readonly string TESTDIR_NAME = "hellotest" ;
private static readonly string OUTPUTDIR_NAME = "testbin" ;
private static string RootPath { get ; set ; }
private string TestDirectory { get ; set ; }
private string OutputDirectory { get ; set ; }
private string Rid { get ; set ; }
2015-11-24 17:47:33 -08:00
public static void Main ( )
{
Console . WriteLine ( "Dummy Entrypoint." ) ;
}
2015-11-25 19:46:01 -08:00
public E2ETest ( )
{
if ( RootPath = = null )
{
RootPath = Directory . GetCurrentDirectory ( ) ;
}
TestDirectory = Path . Combine ( RootPath , TESTDIR_NAME ) ;
OutputDirectory = Path . Combine ( RootPath , OUTPUTDIR_NAME ) ;
Rid = RuntimeIdentifier . Current ;
}
2015-11-24 17:47:33 -08:00
[Fact]
2015-11-25 19:46:01 -08:00
public void TestDotnetCompile ( )
2015-11-24 17:47:33 -08:00
{
2015-11-25 19:46:01 -08:00
TestSetup ( ) ;
2015-11-24 17:47:33 -08:00
2015-12-10 13:06:33 -08:00
TestRunCommand ( "dotnet" , $"build -o {OutputDirectory}" ) ;
2015-11-25 19:46:01 -08:00
TestOutputExecutable ( OutputDirectory ) ;
}
2015-11-24 17:47:33 -08:00
2015-11-25 19:46:01 -08:00
[Fact]
public void TestDotnetCompileNativeRyuJit ( )
{
2015-12-16 15:01:27 -08:00
if ( SkipForOS ( OSPlatform . Linux , "https://github.com/dotnet/cli/issues/527" ) )
{
return ;
}
2015-11-25 19:46:01 -08:00
TestSetup ( ) ;
2015-11-24 17:47:33 -08:00
2015-12-10 13:06:33 -08:00
TestRunCommand ( "dotnet" , $"build --native -o {OutputDirectory}" ) ;
2015-11-24 17:47:33 -08:00
2015-11-25 19:46:01 -08:00
var nativeOut = Path . Combine ( OutputDirectory , "native" ) ;
TestOutputExecutable ( nativeOut ) ;
}
2015-11-24 17:47:33 -08:00
2015-11-25 19:46:01 -08:00
[Fact]
public void TestDotnetCompileNativeCpp ( )
{
TestSetup ( ) ;
2015-11-24 17:47:33 -08:00
2015-12-10 13:06:33 -08:00
TestRunCommand ( "dotnet" , $"build --native --cpp -o {OutputDirectory}" ) ;
2015-11-25 19:46:01 -08:00
var nativeOut = Path . Combine ( OutputDirectory , "native" ) ;
TestOutputExecutable ( nativeOut ) ;
}
2015-11-24 17:47:33 -08:00
2015-11-25 19:46:01 -08:00
[Fact]
public void TestDotnetRun ( )
{
TestSetup ( ) ;
2015-11-24 17:47:33 -08:00
2015-11-25 19:46:01 -08:00
TestRunCommand ( "dotnet" , $"run" ) ;
2015-11-24 17:47:33 -08:00
}
2015-12-18 11:34:55 -08:00
[Fact]
2015-11-25 19:46:01 -08:00
public void TestDotnetPack ( )
{
TestSetup ( ) ;
TestRunCommand ( "dotnet" , $"pack" ) ;
}
[Fact]
public void TestDotnetPublish ( )
{
TestSetup ( ) ;
TestRunCommand ( "dotnet" , $"publish --framework dnxcore50 --runtime {Rid} -o {OutputDirectory}" ) ;
TestOutputExecutable ( OutputDirectory ) ;
}
private void TestSetup ( )
{
Directory . SetCurrentDirectory ( RootPath ) ;
CleanOrCreateDirectory ( TestDirectory ) ;
CleanOrCreateDirectory ( OutputDirectory ) ;
Directory . SetCurrentDirectory ( TestDirectory ) ;
2015-12-03 02:16:52 -08:00
TestRunCommand ( "dotnet" , "new" ) ;
2015-12-03 00:01:19 +00:00
TestRunCommand ( "dotnet" , "restore --quiet" ) ;
2015-11-25 19:46:01 -08:00
}
private bool SkipForOS ( OSPlatform os , string reason )
{
if ( RuntimeInformation . IsOSPlatform ( os ) )
{
Console . WriteLine ( "Skipping Test for reason: " + reason ) ;
return true ;
}
return false ;
}
private void TestRunCommand ( string command , string args )
2015-11-24 17:47:33 -08:00
{
var result = Command . Create ( command , args )
. ForwardStdErr ( )
. ForwardStdOut ( )
. Execute ( ) ;
Assert . Equal ( 0 , result . ExitCode ) ;
}
2015-11-25 19:46:01 -08:00
private void TestOutputExecutable ( string outputDir )
2015-11-24 17:47:33 -08:00
{
2015-11-25 19:46:01 -08:00
var executableName = TESTDIR_NAME + Constants . ExeSuffix ;
2015-11-24 17:47:33 -08:00
var executablePath = Path . Combine ( outputDir , executableName ) ;
var result = Command . Create ( executablePath , "" )
. CaptureStdErr ( )
. CaptureStdOut ( )
. Execute ( ) ;
var outText = result . StdOut ;
2016-01-04 12:56:22 -08:00
var errText = result . StdErr ;
Assert . Equal ( "" , errText ) ;
2015-11-25 19:46:01 -08:00
Assert . Equal ( EXPECTED_OUTPUT , outText ) ;
2015-11-24 17:47:33 -08:00
}
2015-11-25 19:46:01 -08:00
private void CleanOrCreateDirectory ( string path )
2015-11-24 17:47:33 -08:00
{
if ( Directory . Exists ( path ) )
{
Directory . Delete ( path , true ) ;
}
Directory . CreateDirectory ( path ) ;
}
}
}
2015-12-07 14:18:09 -08:00
public class StreamForwarderTests
{
[Fact]
public void Unbuffered ( )
{
Forward ( 4 , true , "" ) ;
Forward ( 4 , true , "123" , "123" ) ;
Forward ( 4 , true , "1234" , "1234" ) ;
Forward ( 3 , true , "123456789" , "123" , "456" , "789" ) ;
Forward ( 4 , true , "\r\n" , "\n" ) ;
Forward ( 4 , true , "\r\n34" , "\n" , "34" ) ;
Forward ( 4 , true , "1\r\n4" , "1\n" , "4" ) ;
Forward ( 4 , true , "12\r\n" , "12\n" ) ;
Forward ( 4 , true , "123\r\n" , "123\n" ) ;
Forward ( 4 , true , "1234\r\n" , "1234" , "\n" ) ;
Forward ( 3 , true , "\r\n3456\r\n9" , "\n" , "3456" , "\n" , "9" ) ;
Forward ( 4 , true , "\n" , "\n" ) ;
Forward ( 4 , true , "\n234" , "\n" , "234" ) ;
Forward ( 4 , true , "1\n34" , "1\n" , "34" ) ;
Forward ( 4 , true , "12\n4" , "12\n" , "4" ) ;
Forward ( 4 , true , "123\n" , "123\n" ) ;
Forward ( 4 , true , "1234\n" , "1234" , "\n" ) ;
Forward ( 3 , true , "\n23456\n89" , "\n" , "23456" , "\n" , "89" ) ;
}
[Fact]
public void LineBuffered ( )
{
Forward ( 4 , false , "" ) ;
Forward ( 4 , false , "123" , "123\n" ) ;
Forward ( 4 , false , "1234" , "1234\n" ) ;
Forward ( 3 , false , "123456789" , "123456789\n" ) ;
Forward ( 4 , false , "\r\n" , "\n" ) ;
Forward ( 4 , false , "\r\n34" , "\n" , "34\n" ) ;
Forward ( 4 , false , "1\r\n4" , "1\n" , "4\n" ) ;
Forward ( 4 , false , "12\r\n" , "12\n" ) ;
Forward ( 4 , false , "123\r\n" , "123\n" ) ;
Forward ( 4 , false , "1234\r\n" , "1234\n" ) ;
Forward ( 3 , false , "\r\n3456\r\n9" , "\n" , "3456\n" , "9\n" ) ;
Forward ( 4 , false , "\n" , "\n" ) ;
Forward ( 4 , false , "\n234" , "\n" , "234\n" ) ;
Forward ( 4 , false , "1\n34" , "1\n" , "34\n" ) ;
Forward ( 4 , false , "12\n4" , "12\n" , "4\n" ) ;
Forward ( 4 , false , "123\n" , "123\n" ) ;
Forward ( 4 , false , "1234\n" , "1234\n" ) ;
Forward ( 3 , false , "\n23456\n89" , "\n" , "23456\n" , "89\n" ) ;
}
private static void Forward ( int bufferSize , bool unbuffered , string str , params string [ ] expectedWrites )
{
var expectedCaptured = str . Replace ( "\r" , "" ) . Replace ( "\n" , Environment . NewLine ) ;
// No forwarding.
Forward ( bufferSize , ForwardOptions . None , str , null , new string [ 0 ] ) ;
// Capture only.
Forward ( bufferSize , ForwardOptions . Capture , str , expectedCaptured , new string [ 0 ] ) ;
var writeOptions = unbuffered ?
ForwardOptions . Write | ForwardOptions . WriteLine :
ForwardOptions . WriteLine ;
// Forward.
Forward ( bufferSize , writeOptions , str , null , expectedWrites ) ;
// Forward and capture.
Forward ( bufferSize , writeOptions | ForwardOptions . Capture , str , expectedCaptured , expectedWrites ) ;
}
private enum ForwardOptions
{
None = 0x0 ,
Capture = 0x1 ,
Write = 0x02 ,
WriteLine = 0x04 ,
}
private static void Forward ( int bufferSize , ForwardOptions options , string str , string expectedCaptured , string [ ] expectedWrites )
{
var forwarder = new StreamForwarder ( bufferSize ) ;
var writes = new List < string > ( ) ;
if ( ( options & ForwardOptions . WriteLine ) ! = 0 )
{
forwarder . ForwardTo (
write : ( options & ForwardOptions . Write ) = = 0 ? ( Action < string > ) null : writes . Add ,
writeLine : s = > writes . Add ( s + "\n" ) ) ;
}
if ( ( options & ForwardOptions . Capture ) ! = 0 )
{
forwarder . Capture ( ) ;
}
forwarder . Read ( new StringReader ( str ) ) ;
Assert . Equal ( expectedWrites , writes ) ;
var captured = forwarder . GetCapturedOutput ( ) ;
Assert . Equal ( expectedCaptured , captured ) ;
}
}