From b813e2b849f02d64fc3c69cda7b7cec1af432194 Mon Sep 17 00:00:00 2001 From: Bryan Thornbury Date: Thu, 3 Mar 2016 15:31:04 -0800 Subject: [PATCH] Fixes #1649 make things work again fix Tests Passing cleanup fix fix --- .../AppWithToolDependency/project.json | 2 +- src/Microsoft.DotNet.Cli.Utils/Command.cs | 5 + .../CommandResolverArguments.cs | 2 + .../DefaultCommandResolver.cs | 1 - .../ProjectDependenciesCommandResolver.cs | 8 +- .../ProjectToolsPackageCommandResolver.cs | 141 --------------- .../CommandResolver.cs | 123 +------------ .../FixedPathCommandFactory.cs | 41 ----- .../ProjectDependenciesCommandFactory.cs | 92 ++++++++++ src/dotnet/commands/dotnet-test/Program.cs | 22 ++- .../GivenADefaultCommandResolver.cs | 164 +----------------- .../GivenAScriptCommandResolver.cs | 155 ----------------- .../Assertions/CommandResultAssertions.cs | 7 + test/dotnet.Tests/PackagedCommandTests.cs | 35 +++- 14 files changed, 163 insertions(+), 635 deletions(-) delete mode 100644 src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsPackageCommandResolver.cs delete mode 100644 src/Microsoft.DotNet.Cli.Utils/FixedPathCommandFactory.cs create mode 100644 src/Microsoft.DotNet.Cli.Utils/ProjectDependenciesCommandFactory.cs diff --git a/TestAssets/TestProjects/AppWithToolDependency/project.json b/TestAssets/TestProjects/AppWithToolDependency/project.json index f8982d688..9537feb8f 100644 --- a/TestAssets/TestProjects/AppWithToolDependency/project.json +++ b/TestAssets/TestProjects/AppWithToolDependency/project.json @@ -15,6 +15,6 @@ }, "tools": { - "dotnet-hello": { "version": "1.0.0", "target": "package" } + "dotnet-hello": { "version": "2.0.0", "target": "package" } } } diff --git a/src/Microsoft.DotNet.Cli.Utils/Command.cs b/src/Microsoft.DotNet.Cli.Utils/Command.cs index 3779deeb0..6bc62c57c 100644 --- a/src/Microsoft.DotNet.Cli.Utils/Command.cs +++ b/src/Microsoft.DotNet.Cli.Utils/Command.cs @@ -85,6 +85,11 @@ namespace Microsoft.DotNet.Cli.Utils return command; } + + public static Command Create(CommandSpec commandSpec) + { + return new Command(commandSpec); + } public static Command CreateForScript( string commandName, diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/CommandResolverArguments.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/CommandResolverArguments.cs index 2e2c5ee93..e888efc90 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/CommandResolverArguments.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/CommandResolverArguments.cs @@ -15,6 +15,8 @@ namespace Microsoft.DotNet.Cli.Utils public NuGetFramework Framework { get; set; } + public string OutputPath { get; set; } + public string ProjectDirectory { get; set; } public string Configuration { get; set; } diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/DefaultCommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/DefaultCommandResolver.cs index 1fdcb83ff..74239cbc4 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/DefaultCommandResolver.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/DefaultCommandResolver.cs @@ -37,7 +37,6 @@ namespace Microsoft.DotNet.Cli.Utils IPlatformCommandSpecFactory platformCommandSpecFactory) : base() { AddCommandResolver(new RootedCommandResolver()); - AddCommandResolver(new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory)); AddCommandResolver(new ProjectToolsCommandResolver(packagedCommandSpecFactory)); AddCommandResolver(new AppBaseCommandResolver(environment, platformCommandSpecFactory)); AddCommandResolver(new PathCommandResolver(environment, platformCommandSpecFactory)); diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectDependenciesCommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectDependenciesCommandResolver.cs index 0c96fc2b2..ac51862b7 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectDependenciesCommandResolver.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectDependenciesCommandResolver.cs @@ -52,7 +52,8 @@ namespace Microsoft.DotNet.Cli.Utils commandResolverArguments.Framework, commandResolverArguments.Configuration, commandResolverArguments.CommandName, - commandResolverArguments.CommandArguments); + commandResolverArguments.CommandArguments, + commandResolverArguments.OutputPath); } private CommandSpec ResolveFromProjectDependencies( @@ -60,7 +61,8 @@ namespace Microsoft.DotNet.Cli.Utils NuGetFramework framework, string configuration, string commandName, - IEnumerable commandArguments) + IEnumerable commandArguments, + string outputPath) { var allowedExtensions = GetAllowedCommandExtensionsFromEnvironment(_environment); @@ -73,7 +75,7 @@ namespace Microsoft.DotNet.Cli.Utils return null; } - var depsFilePath = projectContext.GetOutputPaths(configuration).RuntimeFiles.Deps; + var depsFilePath = projectContext.GetOutputPaths(configuration, outputPath: outputPath).RuntimeFiles.Deps; var dependencyLibraries = GetAllDependencyLibraries(projectContext); diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsPackageCommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsPackageCommandResolver.cs deleted file mode 100644 index fca3b52d2..000000000 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsPackageCommandResolver.cs +++ /dev/null @@ -1,141 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices; -using Microsoft.DotNet.ProjectModel; -using Microsoft.DotNet.ProjectModel.Graph; -using Microsoft.Extensions.PlatformAbstractions; -using NuGet.Frameworks; -using NuGet.Packaging; - -namespace Microsoft.DotNet.Cli.Utils -{ - public class ProjectToolsCommandResolver : ICommandResolver - { - private static readonly NuGetFramework s_toolPackageFramework = FrameworkConstants.CommonFrameworks.DnxCore50; - private static readonly CommandResolutionStrategy s_commandResolutionStrategy = - CommandResolutionStrategy.ProjectToolsPackage; - - private List _allowedCommandExtensions; - private IPackagedCommandSpecFactory _packagedCommandSpecFactory; - - public ProjectToolsCommandResolver(IPackagedCommandSpecFactory packagedCommandSpecFactory) - { - _packagedCommandSpecFactory = packagedCommandSpecFactory; - - _allowedCommandExtensions = new List() - { - FileNameSuffixes.DotNet.DynamicLib - }; - } - - public CommandSpec Resolve(CommandResolverArguments commandResolverArguments) - { - return ResolveFromProjectTools( - commandResolverArguments.CommandName, - commandResolverArguments.CommandArguments, - commandResolverArguments.ProjectDirectory); - } - - private CommandSpec ResolveFromProjectTools( - string commandName, - IEnumerable args, - string projectDirectory) - { - var projectContext = GetProjectContextFromDirectory(projectDirectory, s_toolPackageFramework); - - if (projectContext == null) - { - return null; - } - - var toolsLibraries = projectContext.ProjectFile.Tools.EmptyIfNull(); - - return ResolveCommandSpecFromAllToolLibraries( - toolsLibraries, - commandName, - args, - projectContext); - } - - private CommandSpec ResolveCommandSpecFromAllToolLibraries( - IEnumerable toolsLibraries, - string commandName, - IEnumerable args, - ProjectContext projectContext) - { - foreach (var toolLibrary in toolsLibraries) - { - var commandSpec = ResolveCommandSpecFromToolLibrary(toolLibrary, commandName, args, projectContext); - - if (commandSpec != null) - { - return commandSpec; - } - } - - return null; - } - - private CommandSpec ResolveCommandSpecFromToolLibrary( - LibraryRange toolLibrary, - string commandName, - IEnumerable args, - ProjectContext projectContext) - { - //todo: change this for new resolution strategy - var lockFilePath = Path.Combine( - projectContext.ProjectDirectory, - "artifacts", "Tools", toolLibrary.Name, - "project.lock.json"); - - if (!File.Exists(lockFilePath)) - { - return null; - } - - var lockFile = LockFileReader.Read(lockFilePath); - - var lockFilePackageLibrary = lockFile.PackageLibraries.FirstOrDefault(l => l.Name == toolLibrary.Name); - - var nugetPackagesRoot = projectContext.PackagesDirectory; - - return _packagedCommandSpecFactory.CreateCommandSpecFromLibrary( - lockFilePackageLibrary, - commandName, - args, - _allowedCommandExtensions, - projectContext.PackagesDirectory, - s_commandResolutionStrategy, - null); - } - - private ProjectContext GetProjectContextFromDirectory(string directory, NuGetFramework framework) - { - if (directory == null || framework == null) - { - return null; - } - - var projectRootPath = directory; - - if (!File.Exists(Path.Combine(projectRootPath, Project.FileName))) - { - return null; - } - - var projectContext = ProjectContext.Create( - projectRootPath, - framework, - PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers()); - - if (projectContext.RuntimeIdentifier == null) - { - return null; - } - - return projectContext; - } - } -} diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs index 4eba9460f..b8261dea6 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs @@ -13,128 +13,8 @@ namespace Microsoft.DotNet.Cli.Utils { internal static class CommandResolver { -<<<<<<< HEAD - public static CommandSpec TryResolveCommandSpec( - string commandName, - IEnumerable args, - NuGetFramework framework = null, - string configuration = Constants.DefaultConfiguration, - string outputPath = null) - { - return ResolveFromRootedCommand(commandName, args) ?? - ResolveFromProjectDependencies(commandName, args, framework, configuration, outputPath) ?? - ResolveFromProjectTools(commandName, args) ?? - ResolveFromAppBase(commandName, args) ?? - ResolveFromPath(commandName, args); - } - - public static CommandSpec TryResolveScriptCommandSpec(string commandName, IEnumerable args, Project project, string[] inferredExtensionList) - { - return ResolveFromRootedCommand(commandName, args) ?? - ResolveFromProjectPath(commandName, args, project, inferredExtensionList) ?? - ResolveFromAppBase(commandName, args) ?? - ResolveFromPath(commandName, args); - } - - - private static CommandSpec ResolveFromPath(string commandName, IEnumerable args) - { - var commandPath = Env.GetCommandPath(commandName); - return commandPath == null - ? null - : CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.Path); - } - - private static CommandSpec ResolveFromAppBase(string commandName, IEnumerable args) - { - var commandPath = Env.GetCommandPathFromRootPath(PlatformServices.Default.Application.ApplicationBasePath, commandName); - return commandPath == null - ? null - : CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.BaseDirectory); - } - - private static CommandSpec ResolveFromProjectPath(string commandName, IEnumerable args, Project project, string[] inferredExtensionList) - { - var commandPath = Env.GetCommandPathFromRootPath(project.ProjectDirectory, commandName, inferredExtensionList); - return commandPath == null - ? null - : CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.ProjectLocal); - } - - private static CommandSpec ResolveFromRootedCommand(string commandName, IEnumerable args) - { - if (Path.IsPathRooted(commandName)) - { - var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args); - return new CommandSpec(commandName, escapedArgs, CommandResolutionStrategy.Path); - } - - return null; - } - - public static CommandSpec ResolveFromProjectDependencies( - string commandName, - IEnumerable args, - NuGetFramework framework, - string configuration, - string outputPath) - { - if (framework == null) return null; - - var projectContext = GetProjectContext(framework); - - if (projectContext == null) return null; - - var commandPackage = GetCommandPackage(projectContext, commandName); - - if (commandPackage == null) return null; - - var depsPath = projectContext.GetOutputPaths(configuration, outputPath: outputPath).RuntimeFiles.Deps; - - return ConfigureCommandFromPackage(commandName, args, commandPackage, projectContext, depsPath); - } - - private static ProjectContext GetProjectContext(NuGetFramework framework) - { - var projectRootPath = Directory.GetCurrentDirectory(); - - if (!File.Exists(Path.Combine(projectRootPath, Project.FileName))) - { - return null; - } - - var projectContext = ProjectContext.Create(projectRootPath, framework, PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers()); - return projectContext; - } - - private static PackageDescription GetCommandPackage(ProjectContext projectContext, string commandName) - { - return projectContext.LibraryManager.GetLibraries() - .Where(l => l.GetType() == typeof(PackageDescription)) - .Select(l => l as PackageDescription) - .FirstOrDefault(p => p.Library.Files - .Select(Path.GetFileName) - .Where(f => Path.GetFileNameWithoutExtension(f) == commandName) - .Select(Path.GetExtension) - .Any(e => Env.ExecutableExtensions.Contains(e) || - e == FileNameSuffixes.DotNet.DynamicLib)); - } - - public static CommandSpec ResolveFromProjectTools(string commandName, IEnumerable args) - { - var context = GetProjectContext(FrameworkConstants.CommonFrameworks.NetStandardApp15); - - if (context == null) - { - return null; - } - - var commandLibrary = context.ProjectFile.Tools - .FirstOrDefault(l => l.Name == commandName); -======= private static DefaultCommandResolver _defaultCommandResolver; private static ScriptCommandResolver _scriptCommandResolver; ->>>>>>> 9c4329a... Refactor CommandResolver into individual CommandResolver Implementation public static CommandSpec TryResolveCommandSpec( string commandName, @@ -149,7 +29,8 @@ namespace Microsoft.DotNet.Cli.Utils CommandArguments = args, Framework = framework, ProjectDirectory = Directory.GetCurrentDirectory(), - Configuration = configuration + Configuration = configuration, + OutputPath = outputPath }; if (_defaultCommandResolver == null) diff --git a/src/Microsoft.DotNet.Cli.Utils/FixedPathCommandFactory.cs b/src/Microsoft.DotNet.Cli.Utils/FixedPathCommandFactory.cs deleted file mode 100644 index f0eab5391..000000000 --- a/src/Microsoft.DotNet.Cli.Utils/FixedPathCommandFactory.cs +++ /dev/null @@ -1,41 +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 NuGet.Frameworks; - -namespace Microsoft.DotNet.Cli.Utils -{ - public class FixedPathCommandFactory : ICommandFactory - { - private readonly NuGetFramework _nugetFramework; - private readonly string _configuration; - private readonly string _outputPath; - - public FixedPathCommandFactory(NuGetFramework nugetFramework, string configuration, string outputPath) - { - _nugetFramework = nugetFramework; - _configuration = configuration; - _outputPath = outputPath; - } - - public ICommand Create( - string commandName, - IEnumerable args, - NuGetFramework framework = null, - string configuration = Constants.DefaultConfiguration) - { - if (string.IsNullOrEmpty(configuration)) - { - configuration = _configuration; - } - - if (framework == null) - { - framework = _nugetFramework; - } - - return Command.Create(commandName, args, framework, configuration, _outputPath); - } - } -} diff --git a/src/Microsoft.DotNet.Cli.Utils/ProjectDependenciesCommandFactory.cs b/src/Microsoft.DotNet.Cli.Utils/ProjectDependenciesCommandFactory.cs new file mode 100644 index 000000000..3b6d943f9 --- /dev/null +++ b/src/Microsoft.DotNet.Cli.Utils/ProjectDependenciesCommandFactory.cs @@ -0,0 +1,92 @@ +// 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 NuGet.Frameworks; + +namespace Microsoft.DotNet.Cli.Utils +{ + public class ProjectDependenciesCommandFactory : ICommandFactory + { + private readonly NuGetFramework _nugetFramework; + private readonly string _configuration; + private readonly string _outputPath; + private readonly string _projectDirectory; + + public ProjectDependenciesCommandFactory( + NuGetFramework nugetFramework, + string configuration, + string outputPath, + string projectDirectory) + { + _nugetFramework = nugetFramework; + _configuration = configuration; + _outputPath = outputPath; + _projectDirectory = projectDirectory; + } + + public ICommand Create( + string commandName, + IEnumerable args, + NuGetFramework framework = null, + string configuration = Constants.DefaultConfiguration) + { + if (string.IsNullOrEmpty(configuration)) + { + configuration = _configuration; + } + + if (framework == null) + { + framework = _nugetFramework; + } + + var commandSpec = FindProjectDependencyCommands( + commandName, + args, + configuration, + framework, + _outputPath, + _projectDirectory); + + return Command.Create(commandSpec); + } + + private CommandSpec FindProjectDependencyCommands( + string commandName, + IEnumerable commandArgs, + string configuration, + NuGetFramework framework, + string outputPath, + string projectDirectory) + { + var commandResolverArguments = new CommandResolverArguments + { + CommandName = commandName, + CommandArguments = commandArgs, + Framework = framework, + Configuration = configuration, + OutputPath = outputPath, + ProjectDirectory = projectDirectory + }; + + var commandResolver = GetProjectDependenciesCommandResolver(); + + var commandSpec = commandResolver.Resolve(commandResolverArguments); + if (commandSpec == null) + { + throw new CommandUnknownException(commandName); + } + + return commandSpec; + } + + private ICommandResolver GetProjectDependenciesCommandResolver() + { + var environment = new EnvironmentProvider(); + var packagedCommandSpecFactory = new PackagedCommandSpecFactory(); + + return new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory); + } + } +} diff --git a/src/dotnet/commands/dotnet-test/Program.cs b/src/dotnet/commands/dotnet-test/Program.cs index 7d9eed583..24d12c11b 100644 --- a/src/dotnet/commands/dotnet-test/Program.cs +++ b/src/dotnet/commands/dotnet-test/Program.cs @@ -102,12 +102,19 @@ namespace Microsoft.DotNet.Tools.Test var commandArgs = new List { GetAssemblyUnderTest(projectContext, configuration, outputPath) }; commandArgs.AddRange(app.RemainingArguments); - return Command.Create( + var commandFactory = + new ProjectDependenciesCommandFactory( + projectContext.TargetFramework, + configuration, + outputPath, + projectContext.ProjectDirectory); + + + return commandFactory.Create( $"dotnet-{GetCommandName(testRunner)}", commandArgs, projectContext.TargetFramework, - configuration: configuration, - outputPath: outputPath) + configuration) .ForwardStdErr() .ForwardStdOut() .Execute() @@ -159,8 +166,13 @@ namespace Microsoft.DotNet.Tools.Test var messages = new TestMessagesCollection(); using (var dotnetTest = new DotnetTest(messages, assemblyUnderTest)) { - var commandFactory = - new FixedPathCommandFactory(projectContext.TargetFramework, configuration, outputPath); + var commandFactory = + new ProjectDependenciesCommandFactory( + projectContext.TargetFramework, + configuration, + outputPath, + projectContext.ProjectDirectory); + var testRunnerFactory = new TestRunnerFactory(GetCommandName(testRunner), commandFactory); dotnetTest diff --git a/test/Microsoft.DotNet.Cli.Utils.Tests/GivenADefaultCommandResolver.cs b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenADefaultCommandResolver.cs index 20adb9293..85d58309f 100644 --- a/test/Microsoft.DotNet.Cli.Utils.Tests/GivenADefaultCommandResolver.cs +++ b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenADefaultCommandResolver.cs @@ -28,179 +28,17 @@ namespace Microsoft.DotNet.Cli.Utils.Tests var resolvers = defaultCommandResolver.OrderedCommandResolvers; - resolvers.Should().HaveCount(5); + resolvers.Should().HaveCount(4); 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 mockEnvironment = new Mock(); - // mockEnvironment.Setup(c => c - // .GetCommandPath(It.IsAny(), It.IsAny())) - // .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; - // } } } diff --git a/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAScriptCommandResolver.cs b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAScriptCommandResolver.cs index e5b852456..1363a07c5 100644 --- a/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAScriptCommandResolver.cs +++ b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAScriptCommandResolver.cs @@ -40,160 +40,5 @@ namespace Microsoft.DotNet.Cli.Utils.Tests 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 mockEnvironment = new Mock(); - // mockEnvironment.Setup(c => c - // .GetCommandPath(It.IsAny(), It.IsAny())) - // .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; - // } } } diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/CommandResultAssertions.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/CommandResultAssertions.cs index 38812e448..00516044b 100644 --- a/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/CommandResultAssertions.cs +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Assertions/CommandResultAssertions.cs @@ -32,6 +32,13 @@ namespace Microsoft.DotNet.Tools.Test.Utilities return new AndConstraint(this); } + public AndConstraint NotPass() + { + Execute.Assertion.ForCondition(_commandResult.ExitCode != 0) + .FailWith(AppendDiagnosticsTo($"Expected command to fail but it did not.")); + return new AndConstraint(this); + } + public AndConstraint Fail() { Execute.Assertion.ForCondition(_commandResult.ExitCode != 0) diff --git a/test/dotnet.Tests/PackagedCommandTests.cs b/test/dotnet.Tests/PackagedCommandTests.cs index c02b7b2b1..941d3f258 100644 --- a/test/dotnet.Tests/PackagedCommandTests.cs +++ b/test/dotnet.Tests/PackagedCommandTests.cs @@ -6,6 +6,7 @@ using System.IO; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Tools.Test.Utilities; using Xunit; +using FluentAssertions; namespace Microsoft.DotNet.Tests { @@ -20,11 +21,10 @@ namespace Microsoft.DotNet.Tests [Theory] [InlineData("AppWithDirectAndToolDependency")] - [InlineData("AppWithDirectDependency")] [InlineData("AppWithToolDependency")] - public void TestPackagedCommandDependency(string appName) + public void TestProjectToolIsAvailableThroughDriver(string appName) { - string appDirectory = Path.Combine(_testProjectsRoot, appName); + var appDirectory = Path.Combine(_testProjectsRoot, appName); new BuildCommand(Path.Combine(appDirectory, "project.json")) .Execute() @@ -38,7 +38,7 @@ namespace Microsoft.DotNet.Tests { CommandResult result = new HelloCommand().ExecuteWithCapturedOutput(); - result.Should().HaveStdOut("Hello" + Environment.NewLine); + result.Should().HaveStdOut("Hello World!" + Environment.NewLine); result.Should().NotHaveStdErr(); result.Should().Pass(); } @@ -48,6 +48,33 @@ namespace Microsoft.DotNet.Tests } } + [Fact] + public void TestProjectDependencyIsNotAvailableThroughDriver() + { + var appName = "AppWithDirectDependency"; + var appDirectory = Path.Combine(_testProjectsRoot, appName); + + new BuildCommand(Path.Combine(appDirectory, "project.json")) + .Execute() + .Should() + .Pass(); + + var currentDirectory = Directory.GetCurrentDirectory(); + Directory.SetCurrentDirectory(appDirectory); + + try + { + CommandResult result = new HelloCommand().ExecuteWithCapturedOutput(); + + result.StdOut.Should().Contain("No executable found matching command"); + result.Should().NotPass(); + } + finally + { + Directory.SetCurrentDirectory(currentDirectory); + } + } + class HelloCommand : TestCommand { public HelloCommand()