2016-02-10 13:16:23 -06: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.IO ;
using Microsoft.DotNet.Cli.Utils ;
using Microsoft.DotNet.Tools.Test.Utilities ;
2016-03-28 01:35:25 -07:00
using System.Runtime.InteropServices ;
2016-02-10 13:16:23 -06:00
using Xunit ;
2016-03-03 15:31:04 -08:00
using FluentAssertions ;
2016-02-10 13:16:23 -06:00
namespace Microsoft.DotNet.Tests
{
public class PackagedCommandTests : TestBase
{
private readonly string _testProjectsRoot ;
2016-03-28 16:54:06 -07:00
private readonly string _desktopTestProjectsRoot ;
2016-02-10 13:16:23 -06:00
public PackagedCommandTests ( )
{
_testProjectsRoot = Path . Combine ( AppContext . BaseDirectory , "TestAssets" , "TestProjects" ) ;
2016-03-28 16:54:06 -07:00
_desktopTestProjectsRoot = Path . Combine ( AppContext . BaseDirectory , "TestAssets" , "DesktopTestProjects" ) ;
2016-02-10 13:16:23 -06:00
}
[Theory]
[InlineData("AppWithDirectAndToolDependency")]
[InlineData("AppWithToolDependency")]
2016-03-03 15:31:04 -08:00
public void TestProjectToolIsAvailableThroughDriver ( string appName )
2016-02-10 13:16:23 -06:00
{
2016-03-03 15:31:04 -08:00
var appDirectory = Path . Combine ( _testProjectsRoot , appName ) ;
2016-02-10 13:16:23 -06:00
new BuildCommand ( Path . Combine ( appDirectory , "project.json" ) )
. Execute ( )
. Should ( )
. Pass ( ) ;
2016-03-28 03:18:13 -07:00
CommandResult result = new PortableCommand { WorkingDirectory = appDirectory }
. ExecuteWithCapturedOutput ( ) ;
2016-02-10 13:16:23 -06:00
2016-03-28 03:18:13 -07:00
result . Should ( ) . HaveStdOut ( "Hello Portable World!" + Environment . NewLine ) ;
result . Should ( ) . NotHaveStdErr ( ) ;
result . Should ( ) . Pass ( ) ;
2016-02-10 13:16:23 -06:00
}
2016-03-28 01:35:25 -07:00
// need conditional theories so we can skip on non-Windows
2016-03-28 01:15:09 -07:00
[Theory]
2016-03-28 04:18:59 -07:00
[InlineData(".NETStandardApp,Version=v1.5", "CoreFX")]
[InlineData(".NETFramework,Version=v4.5.1", "NetFX")]
public void TestFrameworkSpecificDependencyToolsCanBeInvoked ( string framework , string args )
2016-03-28 01:15:09 -07:00
{
2016-03-28 01:35:25 -07:00
if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
{
return ;
}
2016-03-28 16:54:06 -07:00
var appDirectory = Path . Combine ( _desktopTestProjectsRoot , "AppWithDirectDependencyDesktopAndPortable" ) ;
2016-03-28 01:15:09 -07:00
new BuildCommand ( Path . Combine ( appDirectory , "project.json" ) )
. Execute ( )
. Should ( )
. Pass ( ) ;
2016-03-28 03:18:13 -07:00
CommandResult result = new DependencyToolInvokerCommand { WorkingDirectory = appDirectory }
2016-03-28 04:18:59 -07:00
. ExecuteWithCapturedOutput ( framework , args ) ;
2016-03-28 01:15:09 -07:00
result . Should ( ) . HaveStdOutContaining ( framework ) ;
2016-03-28 04:18:59 -07:00
result . Should ( ) . HaveStdOutContaining ( args ) ;
2016-03-28 01:15:09 -07:00
result . Should ( ) . NotHaveStdErr ( ) ;
result . Should ( ) . Pass ( ) ;
}
2016-03-03 15:31:04 -08:00
[Fact]
public void TestProjectDependencyIsNotAvailableThroughDriver ( )
{
var appName = "AppWithDirectDependency" ;
var appDirectory = Path . Combine ( _testProjectsRoot , appName ) ;
new BuildCommand ( Path . Combine ( appDirectory , "project.json" ) )
. Execute ( )
. Should ( )
. Pass ( ) ;
var currentDirectory = Directory . GetCurrentDirectory ( ) ;
Directory . SetCurrentDirectory ( appDirectory ) ;
try
{
CommandResult result = new HelloCommand ( ) . ExecuteWithCapturedOutput ( ) ;
result . StdOut . Should ( ) . Contain ( "No executable found matching command" ) ;
result . Should ( ) . NotPass ( ) ;
}
finally
{
Directory . SetCurrentDirectory ( currentDirectory ) ;
}
}
2016-02-10 13:16:23 -06:00
class HelloCommand : TestCommand
{
public HelloCommand ( )
: base ( "dotnet" )
{
}
public override CommandResult Execute ( string args = "" )
{
args = $"hello {args}" ;
return base . Execute ( args ) ;
}
public override CommandResult ExecuteWithCapturedOutput ( string args = "" )
{
args = $"hello {args}" ;
return base . ExecuteWithCapturedOutput ( args ) ;
}
}
2016-03-17 11:43:17 -07:00
class PortableCommand : TestCommand
{
public PortableCommand ( )
: base ( "dotnet" )
{
}
public override CommandResult Execute ( string args = "" )
{
args = $"portable {args}" ;
return base . Execute ( args ) ;
}
public override CommandResult ExecuteWithCapturedOutput ( string args = "" )
{
args = $"portable {args}" ;
return base . ExecuteWithCapturedOutput ( args ) ;
}
}
2016-03-28 01:15:09 -07:00
class DependencyToolInvokerCommand : TestCommand
{
public DependencyToolInvokerCommand ( )
: base ( "dotnet" )
{
}
2016-03-28 04:18:59 -07:00
public CommandResult Execute ( string framework , string additionalArgs )
2016-03-28 01:15:09 -07:00
{
2016-03-28 04:18:59 -07:00
var args = $"dependency-tool-invoker desktop-and-portable --framework {framework} {additionalArgs}" ;
2016-03-28 01:15:09 -07:00
return base . Execute ( args ) ;
}
2016-03-28 04:18:59 -07:00
public CommandResult ExecuteWithCapturedOutput ( string framework , string additionalArgs )
2016-03-28 01:15:09 -07:00
{
2016-03-28 04:18:59 -07:00
var args = $"dependency-tool-invoker desktop-and-portable --framework {framework} {additionalArgs}" ;
2016-03-28 01:15:09 -07:00
return base . ExecuteWithCapturedOutput ( args ) ;
}
}
2016-02-10 13:16:23 -06:00
}
}