Refactor ScriptExecutor, add test cases for scriptexecutor.

Add TargetFramework and FullTargetFramework to compile and publish script
variables.

Add ProjectLocal Command Resolution Strategy.

Fixup ArgumentEscaper to not always quote things.

Fixes #1216
Fixes #1016
Fixes #982
This commit is contained in:
Bryan Thornbury 2016-02-09 15:30:04 -08:00
parent dbc9032202
commit c1e28ae921
21 changed files with 421 additions and 164 deletions

View file

@ -0,0 +1,155 @@
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using System.Runtime.InteropServices;
using Xunit;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.ScriptExecutorTests
{
public class ScriptExecutorTests : TestBase
{
private static readonly string s_testProjectRoot = Path.Combine(AppContext.BaseDirectory, "TestAssets/TestProjects");
private TempDirectory _root;
public ScriptExecutorTests()
{
_root = Temp.CreateDirectory();
}
[Fact]
public void Test_Project_Local_Script_is_Resolved()
{
var sourceTestProjectPath = Path.Combine(s_testProjectRoot, "TestApp");
var binTestProjectPath = _root.CopyDirectory(sourceTestProjectPath).Path;
var project = ProjectContext.Create(binTestProjectPath, NuGetFramework.Parse("dnxcore50")).ProjectFile;
CreateTestFile("some.script", binTestProjectPath);
var scriptCommandLine = "some.script";
var command = ScriptExecutor.CreateCommandForScript(project, scriptCommandLine, new Dictionary<string, string>());
command.Should().NotBeNull();
command.ResolutionStrategy.Should().Be(CommandResolutionStrategy.ProjectLocal);
}
[Fact]
public void Test_Nonexistent_Project_Local_Script_is_not_Resolved()
{
var sourceTestProjectPath = Path.Combine(s_testProjectRoot, "TestApp");
var binTestProjectPath = _root.CopyDirectory(sourceTestProjectPath).Path;
var project = ProjectContext.Create(binTestProjectPath, NuGetFramework.Parse("dnxcore50")).ProjectFile;
var scriptCommandLine = "nonexistent.script";
Action action = () => ScriptExecutor.CreateCommandForScript(project, scriptCommandLine, new Dictionary<string, string>());
action.ShouldThrow<CommandUnknownException>();
}
[Fact]
public void Test_Extension_Inference_in_Resolution_for_Project_Local_Scripts()
{
var extensionList = new string[] {".cmd", ".sh"};
var expectedExtension = default(string);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
expectedExtension = ".cmd";
}
else
{
expectedExtension = ".sh";
}
var sourceTestProjectPath = Path.Combine(s_testProjectRoot, "TestApp");
var binTestProjectPath = _root.CopyDirectory(sourceTestProjectPath).Path;
var project = ProjectContext.Create(binTestProjectPath, NuGetFramework.Parse("dnxcore50")).ProjectFile;
foreach (var extension in extensionList)
{
CreateTestFile("uniquescriptname" + extension, binTestProjectPath);
}
// Don't include extension
var scriptCommandLine = "uniquescriptname";
var command = ScriptExecutor.CreateCommandForScript(project, scriptCommandLine, new Dictionary<string, string>());
command.Should().NotBeNull();
command.ResolutionStrategy.Should().Be(CommandResolutionStrategy.ProjectLocal);
command.CommandArgs.Should().Contain(scriptCommandLine + expectedExtension);
}
[Fact]
public void Test_Script_Exe_Files_Dont_Use_Cmd_or_Sh()
{
var sourceTestProjectPath = Path.Combine(s_testProjectRoot, "TestApp");
var binTestProjectPath = _root.CopyDirectory(sourceTestProjectPath).Path;
var project = ProjectContext.Create(binTestProjectPath, NuGetFramework.Parse("dnxcore50")).ProjectFile;
CreateTestFile("some.exe", binTestProjectPath);
var scriptCommandLine = "some.exe";
var command = ScriptExecutor.CreateCommandForScript(project, scriptCommandLine, new Dictionary<string, string>());
command.Should().NotBeNull();
command.ResolutionStrategy.Should().Be(CommandResolutionStrategy.ProjectLocal);
Path.GetFileName(command.CommandName).Should().NotBe("cmd.exe");
Path.GetFileName(command.CommandName).Should().NotBe("sh");
}
[Fact]
public void Test_Script_Cmd_Files_Use_CmdExe()
{
if (! RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}
var sourceTestProjectPath = Path.Combine(s_testProjectRoot, "TestApp");
var binTestProjectPath = _root.CopyDirectory(sourceTestProjectPath).Path;
var project = ProjectContext.Create(binTestProjectPath, NuGetFramework.Parse("dnxcore50")).ProjectFile;
CreateTestFile("some.cmd", binTestProjectPath);
var scriptCommandLine = "some.cmd";
var command = ScriptExecutor.CreateCommandForScript(project, scriptCommandLine, new Dictionary<string, string>());
command.Should().NotBeNull();
command.ResolutionStrategy.Should().Be(CommandResolutionStrategy.ProjectLocal);
Path.GetFileName(command.CommandName).Should().Be("cmd.exe");
}
[Fact]
public void Test_Script_Builtins_Fail()
{
var sourceTestProjectPath = Path.Combine(s_testProjectRoot, "TestApp");
var binTestProjectPath = _root.CopyDirectory(sourceTestProjectPath).Path;
var project = ProjectContext.Create(binTestProjectPath, NuGetFramework.Parse("dnxcore50")).ProjectFile;
var scriptCommandLine = "echo";
Action action = () => ScriptExecutor.CreateCommandForScript(project, scriptCommandLine, new Dictionary<string, string>());
action.ShouldThrow<CommandUnknownException>();
}
private void CreateTestFile(string filename, string directory)
{
string path = Path.Combine(directory, filename);
File.WriteAllText(path, "echo hello");
}
}
}

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.23107" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.23107</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>833ffee1-7eed-4f51-8dfd-946d48833333</ProjectGuid>
<RootNamespace>Microsoft.DotNet.Cli.Utils.ScriptExecutorTests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -0,0 +1,26 @@
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23805",
"Microsoft.DotNet.ProjectModel": { "target": "project" },
"Microsoft.DotNet.Cli.Utils": { "target": "project" },
"Microsoft.DotNet.Tools.Tests.Utilities": { "target": "project" },
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-dev-48273-16"
},
"frameworks": {
"dnxcore50": {
"imports": "portable-net45+win8"
}
},
"content": [
"../../TestAssets/TestProjects/TestApp/**/*"
],
"testRunner": "xunit"
}