Refactor CommandResolver into individual CommandResolver Implementation

classes.

Write Unit Tests covering Composite DefaultCommandResolver and
ScriptCommandResolver.

baseline

Baseline2
This commit is contained in:
Bryan 2016-02-24 16:05:55 -08:00 committed by Bryan Thornbury
parent 1fccdbd6ec
commit 42cc39252e
37 changed files with 2361 additions and 206 deletions

View file

@ -0,0 +1,25 @@
// 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;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public static class CommandResolverTestUtils
{
public static void CreateNonRunnableTestCommand(string directory, string filename, string extension=".dll")
{
Directory.CreateDirectory(directory);
var filePath = Path.Combine(directory, filename + extension);
File.WriteAllText(filePath, "test command that does nothing.");
}
public static IEnvironmentProvider SetupEnvironmentProviderWhichFindsExtensions(params string[] extensions)
{
return new EnvironmentProvider(extensions);
}
}
}

View file

@ -0,0 +1,86 @@
// 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 GivenACompositeCommandResolver
{
[Fact]
public void It_iterates_through_all_added_resolvers_in_order_when_they_return_null()
{
var compositeCommandResolver = new CompositeCommandResolver();
var resolverCalls = new List<int>();
var mockResolver1 = new Mock<ICommandResolver>();
mockResolver1.Setup(r => r
.Resolve(It.IsAny<CommandResolverArguments>()))
.Returns(default(CommandSpec))
.Callback(() => resolverCalls.Add(1));
var mockResolver2 = new Mock<ICommandResolver>();
mockResolver2.Setup(r => r
.Resolve(It.IsAny<CommandResolverArguments>()))
.Returns(default(CommandSpec))
.Callback(() => resolverCalls.Add(2));
compositeCommandResolver.AddCommandResolver(mockResolver1.Object);
compositeCommandResolver.AddCommandResolver(mockResolver2.Object);
compositeCommandResolver.Resolve(default(CommandResolverArguments));
resolverCalls.Should()
.HaveCount(2)
.And
.ContainInOrder(new [] {1, 2});
}
[Fact]
public void It_stops_iterating_through_added_resolvers_when_one_returns_nonnull()
{
var compositeCommandResolver = new CompositeCommandResolver();
var resolverCalls = new List<int>();
var mockResolver1 = new Mock<ICommandResolver>();
mockResolver1.Setup(r => r
.Resolve(It.IsAny<CommandResolverArguments>()))
.Returns(new CommandSpec(null, null, default(CommandResolutionStrategy)))
.Callback(() => resolverCalls.Add(1));
var mockResolver2 = new Mock<ICommandResolver>();
mockResolver2.Setup(r => r
.Resolve(It.IsAny<CommandResolverArguments>()))
.Returns(default(CommandSpec))
.Callback(() => resolverCalls.Add(2));
compositeCommandResolver.AddCommandResolver(mockResolver1.Object);
compositeCommandResolver.AddCommandResolver(mockResolver2.Object);
compositeCommandResolver.Resolve(default(CommandResolverArguments));
resolverCalls.Should()
.HaveCount(1)
.And
.ContainInOrder(new [] {1});
}
}
}

View file

@ -0,0 +1,206 @@
// 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 GivenADefaultCommandResolver
{
[Fact]
public void It_contains_resolvers_in_the_right_order()
{
var defaultCommandResolver = DefaultCommandResolver.Create();
var resolvers = defaultCommandResolver.OrderedCommandResolvers;
resolvers.Should().HaveCount(5);
resolvers.Select(r => r.GetType())
.Should()
.ContainInOrder(
new []{
typeof(RootedCommandResolver),
typeof(ProjectDependenciesCommandResolver),
typeof(ProjectToolsCommandResolver),
typeof(AppBaseCommandResolver),
typeof(PathCommandResolver)
});
}
// [Fact]
// public void It_Resolves_Rooted_Commands_Correctly()
// {
// var path = Path.Combine(AppContext.BaseDirectory, "rooteddir");
// Directory.CreateDirectory(path);
// var testCommandPath = CreateTestCommandFile(path, ".dll", "rootedcommand");
// var defaultCommandResolver = new DefaultCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = testCommandPath,
// CommandArguments = new string[] {}
// };
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().NotBeNull();
// commandSpec.Path.Should().Be(testCommandPath);
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.RootedPath);
// }
// [Fact]
// public void It_Resolves_AppBase_Commands_Correctly()
// {
// var testCommandPath = CreateTestCommandFile(AppContext.BaseDirectory, ".exe", "appbasecommand");
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
// var defaultCommandResolver = new DefaultCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = testCommandName,
// CommandArguments = new string[] {},
// Environment = new EnvironmentProvider()
// };
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().NotBeNull();
// commandSpec.Path.Should().Be(testCommandPath);
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.BaseDirectory);
// }
// [Fact]
// public void It_Resolves_PATH_Commands_Correctly()
// {
// var path = Path.Combine(AppContext.BaseDirectory, "pathdir");
// var testCommandPath = CreateTestCommandFile(path, ".dll", "pathcommmand");
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
// Mock<IEnvironmentProvider> mockEnvironment = new Mock<IEnvironmentProvider>();
// mockEnvironment.Setup(c => c
// .GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
// .Returns(testCommandPath);
// var defaultCommandResolver = new DefaultCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = testCommandName,
// CommandArguments = new string[] {},
// Environment = mockEnvironment.Object
// };
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().NotBeNull();
// commandSpec.Path.Should().Be(testCommandPath);
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.Path);
// }
// [Fact]
// public void It_Resolves_Project_Tools_Commands_Correctly()
// {
// var testAppPath = Path.Combine(AppContext.BaseDirectory,
// "TestAssets/TestProjects/AppWithToolDependency");
// var defaultCommandResolver = new DefaultCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = "dotnet-hello",
// CommandArguments = new string[] {},
// ProjectDirectory = testAppPath,
// Environment = new EnvironmentProvider()
// };
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().NotBeNull();
// commandSpec.Path.Should().NotBeNull();
// commandSpec.Args.Should().NotContain("--depsfile");
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.NugetPackage);
// }
// [Fact]
// public void It_Resolves_Project_Dependencies_Commands_Correctly()
// {
// var testAppPath = Path.Combine(AppContext.BaseDirectory,
// "TestAssets/TestProjects/AppWithDirectDependency");
// var defaultCommandResolver = new DefaultCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = "dotnet-hello",
// CommandArguments = new string[] {},
// ProjectDirectory = testAppPath,
// Environment = new EnvironmentProvider(),
// Framework = FrameworkConstants.CommonFrameworks.DnxCore50,
// Configuration = "Debug"
// };
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().NotBeNull();
// commandSpec.Path.Should().NotBeNull();
// commandSpec.Args.Should().Contain("--depsfile");
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.NugetPackage);
// }
// [Fact]
// public void It_does_not_resolve_ProjectLocal_commands()
// {
// var path = Path.Combine(AppContext.BaseDirectory,
// "testdir");
// var testCommandPath = CreateTestCommandFile(path, ".exe", "projectlocalcommand");
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
// var defaultCommandResolver = new DefaultCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = testCommandName,
// CommandArguments = new string[] {},
// ProjectDirectory = path,
// Environment = new EnvironmentProvider()
// };
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().Be(null);
// }
// public string CreateTestCommandFile(string path, string extension, string name = "testcommand")
// {
// Directory.CreateDirectory(path);
// var filename = name + extension;
// var filepath = Path.Combine(path, filename);
// File.WriteAllText(filepath, "hello world");
// return filepath;
// }
}
}

View file

@ -0,0 +1,239 @@
// 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 GivenAProjectPathCommandResolver
{
private static readonly string s_testProjectDirectory = Path.Combine(AppContext.BaseDirectory, "testprojectdirectory");
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = new string[] {""},
ProjectDirectory = "/some/directory"
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_ProjectDirectory_is_null()
{
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = null
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_in_ProjectDirectory()
{
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver();
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");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment);
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");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment);
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");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "projectpathtestcommand1",
CommandArguments = new [] { "arg with space"},
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()
{
var extensions = new string[] {".sh", ".cmd", ".foo", ".exe"};
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver();
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);
}
}
[Fact]
public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment);
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");
}
private ProjectPathCommandResolver SetupPlatformProjectPathCommandResolver(IEnvironmentProvider environment = null)
{
environment = environment ?? new EnvironmentProvider();
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory);
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
}
else
{
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
}
var projectPathCommandResolver = new ProjectPathCommandResolver(environment, platformCommandSpecFactory);
return projectPathCommandResolver;
}
}
}

View file

@ -0,0 +1,111 @@
// 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 GivenARootedCommandResolver
{
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = null
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_is_not_rooted()
{
var rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "some/relative/path",
CommandArguments = null
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CommandName_as_Path_when_CommandName_is_rooted()
{
var rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "/some/rooted/path",
CommandArguments = null
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Path.Should().Be(commandResolverArguments.CommandName);
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "/some/rooted/path",
CommandArguments = new [] { "arg with space"}
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Path.Should().Be(commandResolverArguments.CommandName);
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 rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "/some/rooted/path",
CommandArguments = null
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Path.Should().Be(commandResolverArguments.CommandName);
result.Args.Should().Be(string.Empty);
}
}
}

View file

@ -0,0 +1,199 @@
// 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.Linq;
using System.Runtime.InteropServices;
using System.Text;
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 GivenAScriptCommandResolver
{
[Fact]
public void It_contains_resolvers_in_the_right_order()
{
var scriptCommandResolver = ScriptCommandResolver.Create();
var resolvers = scriptCommandResolver.OrderedCommandResolvers;
resolvers.Should().HaveCount(4);
resolvers.Select(r => r.GetType())
.Should()
.ContainInOrder(
new []{
typeof(RootedCommandResolver),
typeof(ProjectPathCommandResolver),
typeof(AppBaseCommandResolver),
typeof(PathCommandResolver)
});
}
// [Fact]
// public void It_Resolves_Rooted_Commands_Correctly()
// {
// var path = Path.Combine(AppContext.BaseDirectory, "rooteddir");
// Directory.CreateDirectory(path);
// var testCommandPath = CreateTestCommandFile(path, ".dll", "scriptrootedcommand");
// var scriptCommandResolver = new ScriptCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = testCommandPath,
// CommandArguments = new string[] {}
// };
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().NotBeNull();
// commandSpec.Path.Should().Be(testCommandPath);
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.RootedPath);
// }
// [Fact]
// public void It_Resolves_AppBase_Commands_Correctly()
// {
// var testCommandPath = CreateTestCommandFile(AppContext.BaseDirectory, ".exe", "scriptappbasecommand");
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
// var scriptCommandResolver = new ScriptCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = testCommandName,
// CommandArguments = new string[] {},
// Environment = new EnvironmentProvider()
// };
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().NotBeNull();
// commandSpec.Path.Should().Be(testCommandPath);
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.BaseDirectory);
// }
// [Fact]
// public void It_Resolves_PATH_Commands_Correctly()
// {
// var path = Path.Combine(AppContext.BaseDirectory, "pathdir");
// var testCommandPath = CreateTestCommandFile(path, ".dll", "scriptpathcommmand");
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
// Mock<IEnvironmentProvider> mockEnvironment = new Mock<IEnvironmentProvider>();
// mockEnvironment.Setup(c => c
// .GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
// .Returns(testCommandPath);
// var scriptCommandResolver = new ScriptCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = testCommandName,
// CommandArguments = new string[] {},
// Environment = mockEnvironment.Object
// };
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().NotBeNull();
// commandSpec.Path.Should().Be(testCommandPath);
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.Path);
// }
// [Fact]
// public void It_does_NOT_Resolve_Project_Tools_Commands()
// {
// var testAppPath = Path.Combine(AppContext.BaseDirectory,
// "TestAssets/TestProjects/AppWithToolDependency");
// var scriptCommandResolver = new ScriptCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = "dotnet-hello",
// CommandArguments = new string[] {},
// ProjectDirectory = testAppPath,
// Environment = new EnvironmentProvider()
// };
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().BeNull();
// }
// [Fact]
// public void It_does_NOT_Resolve_Project_Dependencies_Commands()
// {
// var testAppPath = Path.Combine(AppContext.BaseDirectory,
// "TestAssets/TestProjects/AppWithDirectDependency");
// var scriptCommandResolver = new ScriptCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = "dotnet-hello",
// CommandArguments = new string[] {},
// ProjectDirectory = testAppPath,
// Environment = new EnvironmentProvider(),
// Framework = FrameworkConstants.CommonFrameworks.DnxCore50,
// Configuration = "Debug"
// };
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().BeNull();
// }
// [Fact]
// public void It_resolves_ProjectLocal_commands_correctly()
// {
// var path = Path.Combine(AppContext.BaseDirectory,
// "testdir");
// var testCommandPath = CreateTestCommandFile(path, ".exe", "scriptprojectlocalcommand");
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
// var scriptCommandResolver = new ScriptCommandResolver();
// var commandResolverArgs = new CommandResolverArguments
// {
// CommandName = testCommandName,
// CommandArguments = new string[] {},
// ProjectDirectory = path,
// Environment = new EnvironmentProvider()
// };
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
// commandSpec.Should().NotBeNull();
// commandSpec.Path.Should().Be(testCommandPath);
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.ProjectLocal);
// }
// public string CreateTestCommandFile(string path, string extension, string name = "testcommand")
// {
// Directory.CreateDirectory(path);
// var filename = name + extension;
// var filepath = Path.Combine(path, filename);
// File.WriteAllText(filepath, "hello world");
// return filepath;
// }
}
}

View file

@ -0,0 +1,184 @@
// 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 GivenAnAppBaseCommandResolver
{
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_applocal()
{
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_applocal()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestcommand1",
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be("appbasetestcommand1");
}
[Fact]
public void It_returns_null_when_CommandName_exists_applocal_in_a_subdirectory()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment);
var testDir = Path.Combine(AppContext.BaseDirectory, "appbasetestsubdir");
CommandResolverTestUtils.CreateNonRunnableTestCommand(testDir, "appbasetestsubdircommand", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestsubdircommand",
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestcommand1",
CommandArguments = new [] { "arg with space"}
};
var result = appBaseCommandResolver.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 environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestcommand1",
CommandArguments = null
};
var result = appBaseCommandResolver.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 appBaseCommandResolver = new AppBaseCommandResolver(environment, platformCommandSpecFactory);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".cmd");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestcommand1",
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("appbasetestcommand1.exe");
}
private AppBaseCommandResolver SetupPlatformAppBaseCommandResolver(IEnvironmentProvider environment = null)
{
environment = environment ?? new EnvironmentProvider();
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory);
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
}
else
{
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
}
var appBaseCommandResolver = new AppBaseCommandResolver(environment, platformCommandSpecFactory);
return appBaseCommandResolver;
}
}
}

View file

@ -13,6 +13,7 @@
"Microsoft.DotNet.Tools.Tests.Utilities": { "target": "project" },
"moq.netcore": "4.4.0-beta8",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-dev-79755-47"
},
@ -27,7 +28,11 @@
},
"content": [
"../../TestAssets/TestProjects/OutputStandardOutputAndError/*"
"../../TestAssets/TestProjects/OutputStandardOutputAndError/*",
"../../TestAssets/TestProjects/TestAppWithArgs/*",
"../../TestAssets/TestProjects/AppWithDirectAndToolDependency/**/*",
"../../TestAssets/TestProjects/AppWithDirectDependency/**/*",
"../../TestAssets/TestProjects/AppWithToolDependency/**/*"
],
"testRunner": "xunit"