PR Feedback and nearly full test coverage for resolvers

fix

resolve rebase issues
This commit is contained in:
Bryan Thornbury 2016-03-07 11:50:52 -08:00
parent b813e2b849
commit 9f58651b6c
21 changed files with 726 additions and 97 deletions

View file

@ -1,18 +0,0 @@
// 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.Collections.Generic;
using System.Linq;
namespace Microsoft.DotNet.Cli.Utils
{
public static class CollectionsExtensions
{
public static IEnumerable<T> OrEmptyIfNull<T>(this IEnumerable<T> enumerable)
{
return enumerable == null
? Enumerable.Empty<T>()
: enumerable;
}
}
}

View file

@ -48,7 +48,7 @@ namespace Microsoft.DotNet.Cli.Utils
return _commandSpecFactory.CreateCommandSpec( return _commandSpecFactory.CreateCommandSpec(
commandResolverArguments.CommandName, commandResolverArguments.CommandName,
commandResolverArguments.CommandArguments.EmptyIfNull(), commandResolverArguments.CommandArguments.OrEmptyIfNull(),
commandPath, commandPath,
GetCommandResolutionStrategy(), GetCommandResolutionStrategy(),
_environment); _environment);

View file

@ -22,7 +22,5 @@ namespace Microsoft.DotNet.Cli.Utils
public string Configuration { get; set; } public string Configuration { get; set; }
public IEnumerable<string> InferredExtensions { get; set; } public IEnumerable<string> InferredExtensions { get; set; }
public IEnvironmentProvider Environment { get; set; }
} }
} }

View file

@ -11,9 +11,9 @@ using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils namespace Microsoft.DotNet.Cli.Utils
{ {
public class DefaultCommandResolver : CompositeCommandResolver public class DefaultCommandResolverPolicy
{ {
public static DefaultCommandResolver Create() public static CompositeCommandResolver Create()
{ {
var environment = new EnvironmentProvider(); var environment = new EnvironmentProvider();
var packagedCommandSpecFactory = new PackagedCommandSpecFactory(); var packagedCommandSpecFactory = new PackagedCommandSpecFactory();
@ -28,18 +28,22 @@ namespace Microsoft.DotNet.Cli.Utils
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory(); platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
} }
return new DefaultCommandResolver(environment, packagedCommandSpecFactory, platformCommandSpecFactory); return CreateDefaultCommandResolver(environment, packagedCommandSpecFactory, platformCommandSpecFactory);
} }
public DefaultCommandResolver( public static CompositeCommandResolver CreateDefaultCommandResolver(
IEnvironmentProvider environment, IEnvironmentProvider environment,
IPackagedCommandSpecFactory packagedCommandSpecFactory, IPackagedCommandSpecFactory packagedCommandSpecFactory,
IPlatformCommandSpecFactory platformCommandSpecFactory) : base() IPlatformCommandSpecFactory platformCommandSpecFactory)
{ {
AddCommandResolver(new RootedCommandResolver()); var compositeCommandResolver = new CompositeCommandResolver();
AddCommandResolver(new ProjectToolsCommandResolver(packagedCommandSpecFactory));
AddCommandResolver(new AppBaseCommandResolver(environment, platformCommandSpecFactory)); compositeCommandResolver.AddCommandResolver(new RootedCommandResolver());
AddCommandResolver(new PathCommandResolver(environment, platformCommandSpecFactory)); compositeCommandResolver.AddCommandResolver(new ProjectToolsCommandResolver(packagedCommandSpecFactory));
compositeCommandResolver.AddCommandResolver(new AppBaseCommandResolver(environment, platformCommandSpecFactory));
compositeCommandResolver.AddCommandResolver(new PathCommandResolver(environment, platformCommandSpecFactory));
return compositeCommandResolver;
} }
} }
} }

View file

@ -52,7 +52,7 @@ namespace Microsoft.DotNet.Cli.Utils
commandResolverArguments.Framework, commandResolverArguments.Framework,
commandResolverArguments.Configuration, commandResolverArguments.Configuration,
commandResolverArguments.CommandName, commandResolverArguments.CommandName,
commandResolverArguments.CommandArguments, commandResolverArguments.CommandArguments.OrEmptyIfNull(),
commandResolverArguments.OutputPath); commandResolverArguments.OutputPath);
} }

View file

@ -26,7 +26,7 @@ namespace Microsoft.DotNet.Cli.Utils
return _environment.GetCommandPathFromRootPath( return _environment.GetCommandPathFromRootPath(
commandResolverArguments.ProjectDirectory, commandResolverArguments.ProjectDirectory,
commandResolverArguments.CommandName, commandResolverArguments.CommandName,
commandResolverArguments.InferredExtensions.EmptyIfNull()); commandResolverArguments.InferredExtensions.OrEmptyIfNull());
} }
internal override CommandResolutionStrategy GetCommandResolutionStrategy() internal override CommandResolutionStrategy GetCommandResolutionStrategy()

View file

@ -32,9 +32,15 @@ namespace Microsoft.DotNet.Cli.Utils
public CommandSpec Resolve(CommandResolverArguments commandResolverArguments) public CommandSpec Resolve(CommandResolverArguments commandResolverArguments)
{ {
if (commandResolverArguments.CommandName == null
|| commandResolverArguments.ProjectDirectory == null)
{
return null;
}
return ResolveFromProjectTools( return ResolveFromProjectTools(
commandResolverArguments.CommandName, commandResolverArguments.CommandName,
commandResolverArguments.CommandArguments, commandResolverArguments.CommandArguments.OrEmptyIfNull(),
commandResolverArguments.ProjectDirectory); commandResolverArguments.ProjectDirectory);
} }
@ -50,7 +56,7 @@ namespace Microsoft.DotNet.Cli.Utils
return null; return null;
} }
var toolsLibraries = projectContext.ProjectFile.Tools.EmptyIfNull(); var toolsLibraries = projectContext.ProjectFile.Tools.OrEmptyIfNull();
return ResolveCommandSpecFromAllToolLibraries( return ResolveCommandSpecFromAllToolLibraries(
toolsLibraries, toolsLibraries,

View file

@ -23,7 +23,7 @@ namespace Microsoft.DotNet.Cli.Utils
if (Path.IsPathRooted(commandResolverArguments.CommandName)) if (Path.IsPathRooted(commandResolverArguments.CommandName))
{ {
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart( var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(
commandResolverArguments.CommandArguments.EmptyIfNull()); commandResolverArguments.CommandArguments.OrEmptyIfNull());
return new CommandSpec(commandResolverArguments.CommandName, escapedArgs, CommandResolutionStrategy.RootedPath); return new CommandSpec(commandResolverArguments.CommandName, escapedArgs, CommandResolutionStrategy.RootedPath);
} }

View file

@ -11,9 +11,9 @@ using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils namespace Microsoft.DotNet.Cli.Utils
{ {
public class ScriptCommandResolver : CompositeCommandResolver public class ScriptCommandResolverPolicy
{ {
public static ScriptCommandResolver Create() public static CompositeCommandResolver Create()
{ {
var environment = new EnvironmentProvider(); var environment = new EnvironmentProvider();
@ -27,17 +27,21 @@ namespace Microsoft.DotNet.Cli.Utils
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory(); platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
} }
return new ScriptCommandResolver(environment, platformCommandSpecFactory); return CreateScriptCommandResolver(environment, platformCommandSpecFactory);
} }
public ScriptCommandResolver( public static CompositeCommandResolver CreateScriptCommandResolver(
IEnvironmentProvider environment, IEnvironmentProvider environment,
IPlatformCommandSpecFactory platformCommandSpecFactory) IPlatformCommandSpecFactory platformCommandSpecFactory)
{ {
AddCommandResolver(new RootedCommandResolver()); var compositeCommandResolver = new CompositeCommandResolver();
AddCommandResolver(new ProjectPathCommandResolver(environment, platformCommandSpecFactory));
AddCommandResolver(new AppBaseCommandResolver(environment, platformCommandSpecFactory)); compositeCommandResolver.AddCommandResolver(new RootedCommandResolver());
AddCommandResolver(new PathCommandResolver(environment, platformCommandSpecFactory)); compositeCommandResolver.AddCommandResolver(new ProjectPathCommandResolver(environment, platformCommandSpecFactory));
compositeCommandResolver.AddCommandResolver(new AppBaseCommandResolver(environment, platformCommandSpecFactory));
compositeCommandResolver.AddCommandResolver(new PathCommandResolver(environment, platformCommandSpecFactory));
return compositeCommandResolver;
} }
} }
} }

View file

@ -13,9 +13,6 @@ namespace Microsoft.DotNet.Cli.Utils
{ {
internal static class CommandResolver internal static class CommandResolver
{ {
private static DefaultCommandResolver _defaultCommandResolver;
private static ScriptCommandResolver _scriptCommandResolver;
public static CommandSpec TryResolveCommandSpec( public static CommandSpec TryResolveCommandSpec(
string commandName, string commandName,
IEnumerable<string> args, IEnumerable<string> args,
@ -33,12 +30,9 @@ namespace Microsoft.DotNet.Cli.Utils
OutputPath = outputPath OutputPath = outputPath
}; };
if (_defaultCommandResolver == null) var defaultCommandResolver = DefaultCommandResolverPolicy.Create();
{
_defaultCommandResolver = DefaultCommandResolver.Create();
}
return _defaultCommandResolver.Resolve(commandResolverArgs); return defaultCommandResolver.Resolve(commandResolverArgs);
} }
public static CommandSpec TryResolveScriptCommandSpec( public static CommandSpec TryResolveScriptCommandSpec(
@ -55,12 +49,9 @@ namespace Microsoft.DotNet.Cli.Utils
InferredExtensions = inferredExtensionList InferredExtensions = inferredExtensionList
}; };
if (_scriptCommandResolver == null) var scriptCommandResolver = ScriptCommandResolverPolicy.Create();
{
_scriptCommandResolver = ScriptCommandResolver.Create();
}
return _scriptCommandResolver.Resolve(commandResolverArgs); return scriptCommandResolver.Resolve(commandResolverArgs);
} }
} }
} }

View file

@ -1,3 +1,4 @@
using System;
using System.IO; using System.IO;
using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.PlatformAbstractions;

View file

@ -50,12 +50,12 @@ namespace Microsoft.DotNet.Cli.Utils
} }
} }
public EnvironmentProvider(IEnumerable<string> extensionsOverride = null) public EnvironmentProvider(
{ IEnumerable<string> extensionsOverride = null,
if (extensionsOverride != null) IEnumerable<string> searchPathsOverride = null)
{ {
_executableExtensions = extensionsOverride; _executableExtensions = extensionsOverride;
} _searchPaths = searchPathsOverride;
} }
public string GetCommandPath(string commandName, params string[] extensions) public string GetCommandPath(string commandName, params string[] extensions)
@ -89,7 +89,7 @@ namespace Microsoft.DotNet.Cli.Utils
public string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions) public string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions)
{ {
var extensionsArr = extensions.EmptyIfNull().ToArray(); var extensionsArr = extensions.OrEmptyIfNull().ToArray();
return GetCommandPathFromRootPath(rootPath, commandName, extensionsArr); return GetCommandPathFromRootPath(rootPath, commandName, extensionsArr);
} }

View file

@ -6,7 +6,7 @@ namespace Microsoft.DotNet.Cli.Utils
{ {
public static class CollectionsExtensions public static class CollectionsExtensions
{ {
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> enumerable) public static IEnumerable<T> OrEmptyIfNull<T>(this IEnumerable<T> enumerable)
{ {
return enumerable == null return enumerable == null
? Enumerable.Empty<T>() ? Enumerable.Empty<T>()

View file

@ -8,13 +8,15 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
{ {
public static class CommandResolverTestUtils public static class CommandResolverTestUtils
{ {
public static void CreateNonRunnableTestCommand(string directory, string filename, string extension=".dll") public static string CreateNonRunnableTestCommand(string directory, string filename, string extension=".dll")
{ {
Directory.CreateDirectory(directory); Directory.CreateDirectory(directory);
var filePath = Path.Combine(directory, filename + extension); var filePath = Path.Combine(directory, filename + extension);
File.WriteAllText(filePath, "test command that does nothing."); File.WriteAllText(filePath, "test command that does nothing.");
return filePath;
} }
public static IEnvironmentProvider SetupEnvironmentProviderWhichFindsExtensions(params string[] extensions) public static IEnvironmentProvider SetupEnvironmentProviderWhichFindsExtensions(params string[] extensions)

View file

@ -24,7 +24,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
[Fact] [Fact]
public void It_contains_resolvers_in_the_right_order() public void It_contains_resolvers_in_the_right_order()
{ {
var defaultCommandResolver = DefaultCommandResolver.Create(); var defaultCommandResolver = DefaultCommandResolverPolicy.Create();
var resolvers = defaultCommandResolver.OrderedCommandResolvers; var resolvers = defaultCommandResolver.OrderedCommandResolvers;

View file

@ -0,0 +1,218 @@
// 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 System.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAPathCommandResolver
{
private static readonly string s_testDirectory = Path.Combine(AppContext.BaseDirectory, "pathTestDirectory");
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var pathCommandResolver = SetupPlatformPathCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_in_PATH()
{
var emptyPathEnvironmentMock = new Mock<IEnvironmentProvider>();
emptyPathEnvironmentMock.Setup(e => e
.GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns((string)null);
var pathCommandResolver = SetupPlatformPathCommandResolver(emptyPathEnvironmentMock.Object);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_in_PATH()
{
var testCommandPath = CommandResolverTestUtils.CreateNonRunnableTestCommand(
s_testDirectory,
"pathtestcommand1",
".exe");
var staticPathEnvironmentMock = new Mock<IEnvironmentProvider>();
staticPathEnvironmentMock.Setup(e => e
.GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(testCommandPath);
var pathCommandResolver = SetupPlatformPathCommandResolver(staticPathEnvironmentMock.Object, forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = Path.GetFileNameWithoutExtension(testCommandPath),
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be(Path.GetFileNameWithoutExtension(testCommandPath));
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var testCommandPath = CommandResolverTestUtils.CreateNonRunnableTestCommand(
s_testDirectory,
"pathtestcommand1",
".exe");
var staticPathEnvironmentMock = new Mock<IEnvironmentProvider>();
staticPathEnvironmentMock.Setup(e => e
.GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(testCommandPath);
var pathCommandResolver = SetupPlatformPathCommandResolver(staticPathEnvironmentMock.Object, forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = Path.GetFileNameWithoutExtension(testCommandPath),
CommandArguments = new [] {"arg with space"}
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Be("\"arg with space\"");
}
[Fact]
public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var testCommandPath = CommandResolverTestUtils.CreateNonRunnableTestCommand(
s_testDirectory,
"pathtestcommand1",
".exe");
var staticPathEnvironmentMock = new Mock<IEnvironmentProvider>();
staticPathEnvironmentMock.Setup(e => e
.GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(testCommandPath);
var pathCommandResolver = SetupPlatformPathCommandResolver(staticPathEnvironmentMock.Object, forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = Path.GetFileNameWithoutExtension(testCommandPath),
CommandArguments = null
};
var result = pathCommandResolver.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 = new EnvironmentProvider(new [] {".exe", ".cmd"}, new[] { s_testDirectory });
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testDirectory, "extensionPreferenceCommand", ".exe");
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testDirectory, "extensionPreferenceCommand", ".cmd");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "extensionPreferenceCommand",
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("extensionPreferenceCommand.exe");
}
[Fact]
public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = new EnvironmentProvider(new [] {".cmd"}, new[] { s_testDirectory });
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
var testCommandPath =
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testDirectory, "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 PathCommandResolver SetupPlatformPathCommandResolver(
IEnvironmentProvider environment = null,
bool forceGeneric = false)
{
environment = environment ?? new EnvironmentProvider();
IPlatformCommandSpecFactory platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows
&& !forceGeneric)
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
}
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
return pathCommandResolver;
}
}
}

View file

@ -0,0 +1,221 @@
// 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 System.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAProjectDependenciesCommandResolver
{
private static readonly string s_liveProjectDirectory =
Path.Combine(AppContext.BaseDirectory, "TestAssets/TestProjects/AppWithDirectDependency");
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = new string[] {""},
ProjectDirectory = "/some/directory",
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_ProjectDirectory_is_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = null,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_Framework_is_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = null
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_Configuration_is_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = s_liveProjectDirectory,
Configuration = null,
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_in_ProjectDependencies()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CoreHost_as_FileName_and_CommandName_in_Args_when_CommandName_exists_in_ProjectDependencies()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be("corehost");
result.Args.Should().Contain(commandResolverArguments.CommandName);
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = new [] { "arg with space"},
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("\"arg with space\"");
}
[Fact]
public void It_passes_depsfile_arg_to_corehost_when_returning_a_commandspec()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("--depsfile");
}
[Fact]
public void It_returns_a_CommandSpec_with_CommandName_in_Args_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("dotnet-hello");
}
private ProjectDependenciesCommandResolver SetupProjectDependenciesCommandResolver(
IEnvironmentProvider environment = null,
IPackagedCommandSpecFactory packagedCommandSpecFactory = null)
{
environment = environment ?? new EnvironmentProvider();
packagedCommandSpecFactory = packagedCommandSpecFactory ?? new PackagedCommandSpecFactory();
var projectDependenciesCommandResolver = new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory);
return projectDependenciesCommandResolver;
}
}
}

View file

@ -26,7 +26,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
[Fact] [Fact]
public void It_returns_null_when_CommandName_is_null() public void It_returns_null_when_CommandName_is_null()
{ {
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(); var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments() var commandResolverArguments = new CommandResolverArguments()
{ {
@ -43,7 +43,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
[Fact] [Fact]
public void It_returns_null_when_ProjectDirectory_is_null() public void It_returns_null_when_ProjectDirectory_is_null()
{ {
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(); var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments() var commandResolverArguments = new CommandResolverArguments()
{ {
@ -60,7 +60,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
[Fact] [Fact]
public void It_returns_null_when_CommandName_does_not_exist_in_ProjectDirectory() public void It_returns_null_when_CommandName_does_not_exist_in_ProjectDirectory()
{ {
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(); var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments() var commandResolverArguments = new CommandResolverArguments()
{ {
@ -78,7 +78,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
public void It_returns_null_when_CommandName_exists_in_a_subdirectory_of_ProjectDirectory() public void It_returns_null_when_CommandName_exists_in_a_subdirectory_of_ProjectDirectory()
{ {
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe"); var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment); var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
var testDir = Path.Combine(s_testProjectDirectory, "projectpathtestsubdir"); var testDir = Path.Combine(s_testProjectDirectory, "projectpathtestsubdir");
CommandResolverTestUtils.CreateNonRunnableTestCommand(testDir, "projectpathtestsubdircommand", ".exe"); CommandResolverTestUtils.CreateNonRunnableTestCommand(testDir, "projectpathtestsubdircommand", ".exe");
@ -99,7 +99,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_in_ProjectDirectory() public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_in_ProjectDirectory()
{ {
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe"); var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment); var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe"); CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
@ -123,7 +123,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
public void It_escapes_CommandArguments_when_returning_a_CommandSpec() public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{ {
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe"); var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment); var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe"); CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
@ -144,7 +144,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
public void It_resolves_commands_with_extensions_defined_in_InferredExtensions() public void It_resolves_commands_with_extensions_defined_in_InferredExtensions()
{ {
var extensions = new string[] {".sh", ".cmd", ".foo", ".exe"}; var extensions = new string[] {".sh", ".cmd", ".foo", ".exe"};
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(); var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
foreach (var extension in extensions) foreach (var extension in extensions)
{ {
@ -173,7 +173,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null() public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{ {
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe"); var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment); var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe"); CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
@ -216,20 +216,45 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
commandFile.Should().Be("projectpathtestcommand1.exe"); commandFile.Should().Be("projectpathtestcommand1.exe");
} }
private ProjectPathCommandResolver SetupPlatformProjectPathCommandResolver(IEnvironmentProvider environment = null) public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = new EnvironmentProvider(new [] {".cmd"});
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
var testCommandPath =
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)
{ {
environment = environment ?? new EnvironmentProvider(); environment = environment ?? new EnvironmentProvider();
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory); IPlatformCommandSpecFactory platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows) if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows
&& !forceGeneric)
{ {
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory(); platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
} }
else
{
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
}
var projectPathCommandResolver = new ProjectPathCommandResolver(environment, platformCommandSpecFactory); var projectPathCommandResolver = new ProjectPathCommandResolver(environment, platformCommandSpecFactory);

View file

@ -0,0 +1,152 @@
// 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 System.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAProjectToolsCommandResolver
{
private static readonly string s_liveProjectDirectory =
Path.Combine(AppContext.BaseDirectory, "TestAssets/TestProjects/AppWithToolDependency");
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = new string[] {""},
ProjectDirectory = "/some/directory"
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_ProjectDirectory_is_null()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = null
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_in_ProjectTools()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CoreHost_as_FileName_and_CommandName_in_Args_when_CommandName_exists_in_ProjectTools()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be("corehost");
result.Args.Should().Contain(commandResolverArguments.CommandName);
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = new [] { "arg with space"},
ProjectDirectory = s_liveProjectDirectory
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("\"arg with space\"");
}
[Fact]
public void It_returns_a_CommandSpec_with_Args_as_CommandPath_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandPath = result.Args.Trim('"');
commandPath.Should().Contain("dotnet-hello");
File.Exists(commandPath).Should().BeTrue();
}
private ProjectToolsCommandResolver SetupProjectToolsCommandResolver(
IPackagedCommandSpecFactory packagedCommandSpecFactory = null)
{
packagedCommandSpecFactory = packagedCommandSpecFactory ?? new PackagedCommandSpecFactory();
var projectToolsCommandResolver = new ProjectToolsCommandResolver(packagedCommandSpecFactory);
return projectToolsCommandResolver;
}
}
}

View file

@ -24,7 +24,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
[Fact] [Fact]
public void It_contains_resolvers_in_the_right_order() public void It_contains_resolvers_in_the_right_order()
{ {
var scriptCommandResolver = ScriptCommandResolver.Create(); var scriptCommandResolver = ScriptCommandResolverPolicy.Create();
var resolvers = scriptCommandResolver.OrderedCommandResolvers; var resolvers = scriptCommandResolver.OrderedCommandResolvers;

View file

@ -24,7 +24,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
[Fact] [Fact]
public void It_returns_null_when_CommandName_is_null() public void It_returns_null_when_CommandName_is_null()
{ {
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(); var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments() var commandResolverArguments = new CommandResolverArguments()
{ {
@ -40,7 +40,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
[Fact] [Fact]
public void It_returns_null_when_CommandName_does_not_exist_applocal() public void It_returns_null_when_CommandName_does_not_exist_applocal()
{ {
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(); var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments() var commandResolverArguments = new CommandResolverArguments()
{ {
@ -57,7 +57,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_applocal() public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_applocal()
{ {
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe"); var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment); var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe"); CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
@ -80,7 +80,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
public void It_returns_null_when_CommandName_exists_applocal_in_a_subdirectory() public void It_returns_null_when_CommandName_exists_applocal_in_a_subdirectory()
{ {
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe"); var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment); var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment, forceGeneric: true);
var testDir = Path.Combine(AppContext.BaseDirectory, "appbasetestsubdir"); var testDir = Path.Combine(AppContext.BaseDirectory, "appbasetestsubdir");
CommandResolverTestUtils.CreateNonRunnableTestCommand(testDir, "appbasetestsubdircommand", ".exe"); CommandResolverTestUtils.CreateNonRunnableTestCommand(testDir, "appbasetestsubdircommand", ".exe");
@ -100,7 +100,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
public void It_escapes_CommandArguments_when_returning_a_CommandSpec() public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{ {
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe"); var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment); var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe"); CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
@ -120,7 +120,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null() public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{ {
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe"); var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment); var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe"); CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
@ -161,20 +161,45 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
commandFile.Should().Be("appbasetestcommand1.exe"); commandFile.Should().Be("appbasetestcommand1.exe");
} }
private AppBaseCommandResolver SetupPlatformAppBaseCommandResolver(IEnvironmentProvider environment = null) public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = new EnvironmentProvider(new [] {".cmd"});
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
var testCommandPath =
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "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 AppBaseCommandResolver SetupPlatformAppBaseCommandResolver(
IEnvironmentProvider environment = null,
bool forceGeneric = false)
{ {
environment = environment ?? new EnvironmentProvider(); environment = environment ?? new EnvironmentProvider();
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory); IPlatformCommandSpecFactory platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows) if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows
&& !forceGeneric)
{ {
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory(); platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
} }
else
{
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
}
var appBaseCommandResolver = new AppBaseCommandResolver(environment, platformCommandSpecFactory); var appBaseCommandResolver = new AppBaseCommandResolver(environment, platformCommandSpecFactory);