2016-02-25 00:05:55 +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.IO;
|
|
|
|
using FluentAssertions;
|
2016-08-09 18:05:00 +00:00
|
|
|
using Microsoft.DotNet.PlatformAbstractions;
|
2016-04-28 23:30:32 +00:00
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
|
|
|
using Xunit;
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils.Tests
|
|
|
|
{
|
|
|
|
public class GivenAProjectPathCommandResolver
|
|
|
|
{
|
|
|
|
private static readonly string s_testProjectDirectory = Path.Combine(AppContext.BaseDirectory, "testprojectdirectory");
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_null_when_CommandName_is_null()
|
|
|
|
{
|
2016-03-07 19:50:52 +00:00
|
|
|
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = null,
|
2016-04-28 23:30:32 +00:00
|
|
|
CommandArguments = new string[] { "" },
|
2016-02-25 00:05:55 +00:00
|
|
|
ProjectDirectory = "/some/directory"
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_null_when_ProjectDirectory_is_null()
|
|
|
|
{
|
2016-03-07 19:50:52 +00:00
|
|
|
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "command",
|
2016-04-28 23:30:32 +00:00
|
|
|
CommandArguments = new string[] { "" },
|
2016-02-25 00:05:55 +00:00
|
|
|
ProjectDirectory = null
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_null_when_CommandName_does_not_exist_in_ProjectDirectory()
|
|
|
|
{
|
2016-03-07 19:50:52 +00:00
|
|
|
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "nonexistent-command",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_testProjectDirectory
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_null_when_CommandName_exists_in_a_subdirectory_of_ProjectDirectory()
|
|
|
|
{
|
|
|
|
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
|
2016-03-07 19:50:52 +00:00
|
|
|
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
var testDir = Path.Combine(s_testProjectDirectory, "projectpathtestsubdir");
|
|
|
|
CommandResolverTestUtils.CreateNonRunnableTestCommand(testDir, "projectpathtestsubdircommand", ".exe");
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "projectpathtestsubdircommand",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_testProjectDirectory
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_in_ProjectDirectory()
|
|
|
|
{
|
|
|
|
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
|
2016-03-07 19:50:52 +00:00
|
|
|
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "projectpathtestcommand1",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_testProjectDirectory
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
|
|
|
|
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
|
|
|
|
|
|
|
|
commandFile.Should().Be("projectpathtestcommand1");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
|
|
|
|
{
|
|
|
|
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
|
2016-03-07 19:50:52 +00:00
|
|
|
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "projectpathtestcommand1",
|
2016-04-28 23:30:32 +00:00
|
|
|
CommandArguments = new[] { "arg with space" },
|
2016-02-25 00:05:55 +00:00
|
|
|
ProjectDirectory = s_testProjectDirectory
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.Args.Should().Be("\"arg with space\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_resolves_commands_with_extensions_defined_in_InferredExtensions()
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
var extensions = new string[] { ".sh", ".cmd", ".foo", ".exe" };
|
2016-03-07 19:50:52 +00:00
|
|
|
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
foreach (var extension in extensions)
|
|
|
|
{
|
|
|
|
var extensionTestDir = Path.Combine(s_testProjectDirectory, "testext" + extension);
|
|
|
|
|
|
|
|
CommandResolverTestUtils.CreateNonRunnableTestCommand(extensionTestDir, "projectpathexttest", extension);
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "projectpathexttest",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = extensionTestDir,
|
|
|
|
InferredExtensions = extensions
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
|
|
|
|
var commandFileName = Path.GetFileName(result.Path);
|
|
|
|
commandFileName.Should().Be("projectpathexttest" + extension);
|
2016-04-28 23:30:32 +00:00
|
|
|
}
|
2016-02-25 00:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null()
|
|
|
|
{
|
|
|
|
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
|
2016-03-07 19:50:52 +00:00
|
|
|
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "projectpathtestcommand1",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_testProjectDirectory
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.Args.Should().Be(string.Empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_prefers_EXE_over_CMD_when_two_command_candidates_exist_and_using_WindowsExePreferredCommandSpecFactory()
|
|
|
|
{
|
|
|
|
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
|
|
|
|
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
|
|
|
|
|
|
|
|
var projectPathCommandResolver = new ProjectPathCommandResolver(environment, platformCommandSpecFactory);
|
|
|
|
|
|
|
|
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
|
|
|
|
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".cmd");
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "projectpathtestcommand1",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_testProjectDirectory
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
|
|
|
|
var commandFile = Path.GetFileName(result.Path);
|
|
|
|
commandFile.Should().Be("projectpathtestcommand1.exe");
|
|
|
|
}
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_using_WindowsExePreferredCommandSpecFactory()
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
var environment = new EnvironmentProvider(new[] { ".cmd" });
|
2016-03-07 19:50:52 +00:00
|
|
|
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
|
|
|
|
|
|
|
|
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
|
|
|
|
|
2016-04-28 23:30:32 +00:00
|
|
|
var testCommandPath =
|
2016-03-07 19:50:52 +00:00
|
|
|
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "cmdWrapCommand", ".cmd");
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "cmdWrapCommand",
|
|
|
|
CommandArguments = null
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = pathCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
|
|
|
|
var commandFile = Path.GetFileName(result.Path);
|
|
|
|
commandFile.Should().Be("cmd.exe");
|
|
|
|
|
|
|
|
result.Args.Should().Contain(testCommandPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ProjectPathCommandResolver SetupPlatformProjectPathCommandResolver(
|
|
|
|
IEnvironmentProvider environment = null,
|
|
|
|
bool forceGeneric = false)
|
2016-02-25 00:05:55 +00:00
|
|
|
{
|
|
|
|
environment = environment ?? new EnvironmentProvider();
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
IPlatformCommandSpecFactory platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
|
2016-02-25 00:05:55 +00:00
|
|
|
|
2016-04-28 23:30:32 +00:00
|
|
|
if (RuntimeEnvironment.OperatingSystemPlatform == Platform.Windows
|
2016-03-07 19:50:52 +00:00
|
|
|
&& !forceGeneric)
|
2016-02-25 00:05:55 +00:00
|
|
|
{
|
|
|
|
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
|
|
|
|
}
|
|
|
|
|
|
|
|
var projectPathCommandResolver = new ProjectPathCommandResolver(environment, platformCommandSpecFactory);
|
|
|
|
|
|
|
|
return projectPathCommandResolver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|