Merge pull request #5797 from krwq/2017-02-17-clean-up-cli-utils

Add unit tests around command line parsing
This commit is contained in:
Piotr Puszkiewicz 2017-02-23 14:07:28 -08:00 committed by GitHub
commit f5f6adccb0
21 changed files with 629 additions and 342 deletions

View file

@ -1,52 +0,0 @@
using System;
using System.IO;
namespace Microsoft.DotNet.Cli.Utils
{
public static class CoreHost
{
internal static string _hostDir;
internal static string _hostExePath;
public static string HostExePath
{
get
{
if (_hostExePath == null)
{
_hostExePath = Path.Combine(HostDir, Constants.HostExecutableName);
}
return _hostExePath;
}
}
private static string HostDir
{
get
{
if (_hostDir == null)
{
var fxDepsFile = Muxer.GetDataFromAppDomain("FX_DEPS_FILE");
_hostDir = Path.GetDirectoryName(fxDepsFile);
}
return _hostDir;
}
}
public static void CopyTo(string destinationPath, string hostExeName)
{
foreach (var binaryName in Constants.HostBinaryNames)
{
var outputBinaryName = binaryName.Equals(Constants.HostExecutableName)
? hostExeName : binaryName;
var outputBinaryPath = Path.Combine(destinationPath, outputBinaryName);
var hostBinaryPath = Path.Combine(HostDir, binaryName);
File.Copy(hostBinaryPath, outputBinaryPath, overwrite: true);
// Update the last write time so this file can be treated as an output of a build
File.SetLastWriteTimeUtc(outputBinaryPath, DateTime.UtcNow);
}
}
}
}

View file

@ -5,7 +5,6 @@ using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.ProjectModel;
@ -13,11 +12,6 @@ namespace Microsoft.DotNet.Cli.Utils
{
public static class LockFileFormatExtensions
{
private const int NumberOfRetries = 3000;
private static readonly TimeSpan SleepDuration = TimeSpan.FromMilliseconds(10);
public static async Task<LockFile> ReadWithLock(this LockFileFormat subject, string path)
{
return await ConcurrencyUtilities.ExecuteWithFileLockedAsync(

View file

@ -1,11 +0,0 @@
using System;
namespace Microsoft.DotNet.Cli.Utils
{
public class InvalidProjectException : Exception
{
public InvalidProjectException() { }
public InvalidProjectException(string message) : base(message) { }
public InvalidProjectException(string message, Exception innerException) : base(message, innerException) { }
}
}

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.DotNet.Cli
{
internal class CommandCreationException : Exception
{
public int ExitCode { get; private set; }
public CommandCreationException(int exitCode)
{
ExitCode = exitCode;
}
}
}

View file

@ -11,13 +11,11 @@ using Microsoft.DotNet.Cli;
namespace Microsoft.DotNet.Tools.Build
{
public class BuildCommand
public class BuildCommand : MSBuildForwardingApp
{
private MSBuildForwardingApp _forwardingApp;
public BuildCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
{
_forwardingApp = new MSBuildForwardingApp(msbuildArgs, msbuildPath);
}
public static BuildCommand FromArgs(string[] args, string msbuildPath = null)
@ -132,25 +130,5 @@ namespace Microsoft.DotNet.Tools.Build
return cmd.Execute();
}
public ProcessStartInfo GetProcessStartInfo()
{
return _forwardingApp.GetProcessStartInfo();
}
public int Execute()
{
return GetProcessStartInfo().Execute();
}
private class CommandCreationException : Exception
{
public int ExitCode { get; private set; }
public CommandCreationException(int exitCode)
{
ExitCode = exitCode;
}
}
}
}

View file

@ -1,92 +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;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools.Restore;
using Microsoft.DotNet.Cli.CommandLine;
namespace Microsoft.DotNet.Tools.Cache
{
public partial class CacheCommand
{
public string ProjectArgument { get; set; }
public string Framework { get; set; }
public string Runtime { get; set; }
public string OutputPath { get; set; }
public string FrameworkVersion { get; set; }
public string IntermediateDir { get; set; }
public string Verbosity { get; set; }
private bool SkipOptimization { get; set; }
private bool PreserveIntermediateDir { get; set; }
public List<string> ExtraMSBuildArguments { get; set; }
private CacheCommand()
{
}
public int Execute()
{
var msbuildArgs = new List<string>();
if (string.IsNullOrEmpty(ProjectArgument))
{
throw new InvalidOperationException(LocalizableStrings.SpecifyEntries);
}
msbuildArgs.Add("/t:ComposeCache");
msbuildArgs.Add(ProjectArgument);
if (!string.IsNullOrEmpty(Framework))
{
msbuildArgs.Add($"/p:TargetFramework={Framework}");
}
if (!string.IsNullOrEmpty(Runtime))
{
msbuildArgs.Add($"/p:RuntimeIdentifier={Runtime}");
}
if (!string.IsNullOrEmpty(OutputPath))
{
OutputPath = Path.GetFullPath(OutputPath);
msbuildArgs.Add($"/p:ComposeDir={OutputPath}");
}
if (!string.IsNullOrEmpty(FrameworkVersion))
{
msbuildArgs.Add($"/p:FX_Version={FrameworkVersion}");
}
if (!string.IsNullOrEmpty(IntermediateDir))
{
msbuildArgs.Add($"/p:ComposeWorkingDir={IntermediateDir}");
}
if (SkipOptimization)
{
msbuildArgs.Add($"/p:SkipOptimization={SkipOptimization}");
}
if (PreserveIntermediateDir)
{
msbuildArgs.Add($"/p:PreserveComposeWorkingDir={PreserveIntermediateDir}");
}
if (!string.IsNullOrEmpty(Verbosity))
{
msbuildArgs.Add($"/verbosity:{Verbosity}");
}
msbuildArgs.AddRange(ExtraMSBuildArguments);
return new MSBuildForwardingApp(msbuildArgs).Execute();
}
}
}

View file

@ -5,12 +5,21 @@ using System.Collections.Generic;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Cli;
using System.Diagnostics;
using System;
using System.IO;
namespace Microsoft.DotNet.Tools.Cache
{
public partial class CacheCommand
public partial class CacheCommand : MSBuildForwardingApp
{
public static int Run(string[] args)
private CacheCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
{
}
public static CacheCommand FromArgs(string[] args, string msbuildPath = null)
{
DebugHelper.HandleDebugSwitch(ref args);
@ -56,25 +65,89 @@ namespace Microsoft.DotNet.Tools.Cache
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app);
List<string> msbuildArgs = null;
app.OnExecute(() =>
{
var cache = new CacheCommand();
msbuildArgs = new List<string>();
cache.Framework = frameworkOption.Value();
cache.Runtime = runtimeOption.Value();
cache.OutputPath = outputOption.Value();
cache.FrameworkVersion = fxOption.Value();
cache.Verbosity = verbosityOption.Value();
cache.SkipOptimization = skipOptimizationOption.HasValue();
cache.IntermediateDir = workingDir.Value();
cache.PreserveIntermediateDir = preserveWorkingDir.HasValue();
cache.ExtraMSBuildArguments = app.RemainingArguments;
cache.ProjectArgument = projectArgument.Value();
return cache.Execute();
if (string.IsNullOrEmpty(projectArgument.Value()))
{
throw new InvalidOperationException(LocalizableStrings.SpecifyEntries);
}
msbuildArgs.Add("/t:ComposeCache");
msbuildArgs.Add(projectArgument.Value());
if (!string.IsNullOrEmpty(frameworkOption.Value()))
{
msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}");
}
if (!string.IsNullOrEmpty(runtimeOption.Value()))
{
msbuildArgs.Add($"/p:RuntimeIdentifier={runtimeOption.Value()}");
}
if (!string.IsNullOrEmpty(outputOption.Value()))
{
var outputPath = Path.GetFullPath(outputOption.Value());
msbuildArgs.Add($"/p:ComposeDir={outputPath}");
}
if (!string.IsNullOrEmpty(fxOption.Value()))
{
msbuildArgs.Add($"/p:FX_Version={fxOption.Value()}");
}
if (!string.IsNullOrEmpty(workingDir.Value()))
{
msbuildArgs.Add($"/p:ComposeWorkingDir={workingDir.Value()}");
}
if (skipOptimizationOption.HasValue())
{
msbuildArgs.Add($"/p:SkipOptimization={skipOptimizationOption.HasValue()}");
}
if (preserveWorkingDir.HasValue())
{
msbuildArgs.Add($"/p:PreserveComposeWorkingDir={preserveWorkingDir.HasValue()}");
}
if (!string.IsNullOrEmpty(verbosityOption.Value()))
{
msbuildArgs.Add($"/verbosity:{verbosityOption.Value()}");
}
msbuildArgs.AddRange(app.RemainingArguments);
return 0;
});
return app.Execute(args);
int exitCode = app.Execute(args);
if (msbuildArgs == null)
{
throw new CommandCreationException(exitCode);
}
return new CacheCommand(msbuildArgs, msbuildPath);
}
public static int Run(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
CacheCommand cmd;
try
{
cmd = FromArgs(args);
}
catch (CommandCreationException e)
{
return e.ExitCode;
}
return cmd.Execute();
}
}
}

View file

@ -5,12 +5,19 @@ using System.Collections.Generic;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Cli;
using System.Diagnostics;
namespace Microsoft.DotNet.Tools.Clean
{
public class CleanCommand
public class CleanCommand : MSBuildForwardingApp
{
public static int Run(string[] args)
public CleanCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
{
}
public static CleanCommand FromArgs(string[] args, string msbuildPath = null)
{
DebugHelper.HandleDebugSwitch(ref args);
@ -40,11 +47,12 @@ namespace Microsoft.DotNet.Tools.Clean
$"-c|--configuration <{LocalizableStrings.CmdConfiguration}>",
LocalizableStrings.CmdConfigurationDescription,
CommandOptionType.SingleValue);
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app);
CommandOption verbosityOption = AddVerbosityOption(app);
List<string> msbuildArgs = null;
app.OnExecute(() =>
{
List<string> msbuildArgs = new List<string>();
msbuildArgs = new List<string>();
if (!string.IsNullOrEmpty(projectArgument.Value))
{
@ -75,10 +83,33 @@ namespace Microsoft.DotNet.Tools.Clean
msbuildArgs.AddRange(app.RemainingArguments);
return new MSBuildForwardingApp(msbuildArgs).Execute();
return 0;
});
return app.Execute(args);
int exitCode = app.Execute(args);
if (msbuildArgs == null)
{
throw new CommandCreationException(exitCode);
}
return new CleanCommand(msbuildArgs, msbuildPath);
}
public static int Run(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
CleanCommand cmd;
try
{
cmd = FromArgs(args);
}
catch (CommandCreationException e)
{
return e.ExitCode;
}
return cmd.Execute();
}
}
}

View file

@ -5,12 +5,19 @@ using System.Collections.Generic;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Cli;
using System.Diagnostics;
namespace Microsoft.DotNet.Tools.Pack
{
public class PackCommand
public class PackCommand : MSBuildForwardingApp
{
public static int Run(string[] args)
public PackCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
{
}
public static PackCommand FromArgs(string[] args, string msbuildPath = null)
{
DebugHelper.HandleDebugSwitch(ref args);
@ -57,11 +64,12 @@ namespace Microsoft.DotNet.Tools.Pack
$"<{LocalizableStrings.CmdArgumentProject}>",
LocalizableStrings.CmdArgumentDescription,
multipleValues:true);
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(cmd);
CommandOption verbosityOption = AddVerbosityOption(cmd);
List<string> msbuildArgs = null;
cmd.OnExecute(() =>
{
var msbuildArgs = new List<string>()
msbuildArgs = new List<string>()
{
"/t:pack"
};
@ -109,10 +117,33 @@ namespace Microsoft.DotNet.Tools.Pack
msbuildArgs.AddRange(argRoot.Values);
msbuildArgs.AddRange(cmd.RemainingArguments);
return new MSBuildForwardingApp(msbuildArgs).Execute();
return 0;
});
return cmd.Execute(args);
int exitCode = cmd.Execute(args);
if (msbuildArgs == null)
{
throw new CommandCreationException(exitCode);
}
return new PackCommand(msbuildArgs, msbuildPath);
}
public static int Run(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
PackCommand cmd;
try
{
cmd = FromArgs(args);
}
catch (CommandCreationException e)
{
return e.ExitCode;
}
return cmd.Execute();
}
}
}

View file

@ -1,15 +1,23 @@
// 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 Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using System.Collections.Generic;
using System.Diagnostics;
namespace Microsoft.DotNet.Tools.Publish
{
public partial class PublishCommand
public partial class PublishCommand : MSBuildForwardingApp
{
public static int Run(string[] args)
private PublishCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
{
}
public static PublishCommand FromArgs(string[] args, string msbuildPath = null)
{
DebugHelper.HandleDebugSwitch(ref args);
@ -48,26 +56,84 @@ namespace Microsoft.DotNet.Tools.Publish
$"--filter <{LocalizableStrings.FilterProjOption}>", LocalizableStrings.FilterProjOptionDescription,
CommandOptionType.SingleValue);
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app);
CommandOption verbosityOption = AddVerbosityOption(app);
List<string> msbuildArgs = null;
app.OnExecute(() =>
{
var publish = new PublishCommand();
msbuildArgs = new List<string>();
publish.ProjectPath = projectArgument.Value;
publish.Framework = frameworkOption.Value();
publish.Runtime = runtimeOption.Value();
publish.OutputPath = outputOption.Value();
publish.Configuration = configurationOption.Value();
publish.VersionSuffix = versionSuffixOption.Value();
publish.FilterProject = filterProjOption.Value();
publish.Verbosity = verbosityOption.Value();
publish.ExtraMSBuildArguments = app.RemainingArguments;
msbuildArgs.Add("/t:Publish");
return publish.Execute();
if (!string.IsNullOrEmpty(projectArgument.Value))
{
msbuildArgs.Add(projectArgument.Value);
}
if (!string.IsNullOrEmpty(frameworkOption.Value()))
{
msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}");
}
if (!string.IsNullOrEmpty(runtimeOption.Value()))
{
msbuildArgs.Add($"/p:RuntimeIdentifier={runtimeOption.Value()}");
}
if (!string.IsNullOrEmpty(outputOption.Value()))
{
msbuildArgs.Add($"/p:PublishDir={outputOption.Value()}");
}
if (!string.IsNullOrEmpty(configurationOption.Value()))
{
msbuildArgs.Add($"/p:Configuration={configurationOption.Value()}");
}
if (!string.IsNullOrEmpty(versionSuffixOption.Value()))
{
msbuildArgs.Add($"/p:VersionSuffix={versionSuffixOption.Value()}");
}
if (!string.IsNullOrEmpty(filterProjOption.Value()))
{
msbuildArgs.Add($"/p:FilterProjFile={filterProjOption.Value()}");
}
if (!string.IsNullOrEmpty(verbosityOption.Value()))
{
msbuildArgs.Add($"/verbosity:{verbosityOption.Value()}");
}
msbuildArgs.AddRange(app.RemainingArguments);
return 0;
});
return app.Execute(args);
int exitCode = app.Execute(args);
if (msbuildArgs == null)
{
throw new CommandCreationException(exitCode);
}
return new PublishCommand(msbuildArgs, msbuildPath);
}
public static int Run(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
PublishCommand cmd;
try
{
cmd = FromArgs(args);
}
catch (CommandCreationException e)
{
return e.ExitCode;
}
return cmd.Execute();
}
}
}

View file

@ -1,80 +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.Linq;
using System.Collections.Generic;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools.Restore;
namespace Microsoft.DotNet.Tools.Publish
{
public partial class PublishCommand
{
public string ProjectPath { get; set; }
public string Framework { get; set; }
public string Runtime { get; set; }
public string OutputPath { get; set; }
public string Configuration { get; set; }
public string VersionSuffix { get; set; }
public string FilterProject { get; set; }
public string Verbosity { get; set; }
public List<string> ExtraMSBuildArguments { get; set; }
private PublishCommand()
{
}
public int Execute()
{
List<string> msbuildArgs = new List<string>();
if (!string.IsNullOrEmpty(ProjectPath))
{
msbuildArgs.Add(ProjectPath);
}
msbuildArgs.Add("/t:Publish");
if (!string.IsNullOrEmpty(Framework))
{
msbuildArgs.Add($"/p:TargetFramework={Framework}");
}
if (!string.IsNullOrEmpty(Runtime))
{
msbuildArgs.Add($"/p:RuntimeIdentifier={Runtime}");
}
if (!string.IsNullOrEmpty(OutputPath))
{
msbuildArgs.Add($"/p:PublishDir={OutputPath}");
}
if (!string.IsNullOrEmpty(Configuration))
{
msbuildArgs.Add($"/p:Configuration={Configuration}");
}
if (!string.IsNullOrEmpty(VersionSuffix))
{
msbuildArgs.Add($"/p:VersionSuffix={VersionSuffix}");
}
if (!string.IsNullOrEmpty(FilterProject))
{
msbuildArgs.Add($"/p:FilterProjFile={FilterProject}");
}
if (!string.IsNullOrEmpty(Verbosity))
{
msbuildArgs.Add($"/verbosity:{Verbosity}");
}
msbuildArgs.AddRange(ExtraMSBuildArguments);
return new MSBuildForwardingApp(msbuildArgs).Execute();
}
}
}

View file

@ -5,12 +5,19 @@ using System.Collections.Generic;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Cli;
using System.Diagnostics;
namespace Microsoft.DotNet.Tools.Restore
{
public class RestoreCommand
public class RestoreCommand : MSBuildForwardingApp
{
public static int Run(string[] args)
public RestoreCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
{
}
public static RestoreCommand FromArgs(string[] args, string msbuildPath = null)
{
DebugHelper.HandleDebugSwitch(ref args);
@ -72,9 +79,10 @@ namespace Microsoft.DotNet.Tools.Restore
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(cmd);
List<string> msbuildArgs = null;
cmd.OnExecute(() =>
{
var msbuildArgs = new List<string>()
msbuildArgs = new List<string>()
{
"/NoLogo",
"/t:Restore",
@ -132,10 +140,33 @@ namespace Microsoft.DotNet.Tools.Restore
// Add remaining arguments that the parser did not understand
msbuildArgs.AddRange(cmd.RemainingArguments);
return new MSBuildForwardingApp(msbuildArgs).Execute();
return 0;
});
return cmd.Execute(args);
int exitCode = cmd.Execute(args);
if (msbuildArgs == null)
{
throw new CommandCreationException(exitCode);
}
return new RestoreCommand(msbuildArgs, msbuildPath);
}
public static int Run(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
RestoreCommand cmd;
try
{
cmd = FromArgs(args);
}
catch (CommandCreationException e)
{
return e.ExitCode;
}
return cmd.Execute();
}
}
}

View file

@ -4,28 +4,21 @@
using Microsoft.DotNet.Cli.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace Microsoft.DotNet.Cli
{
public class VSTestForwardingApp
public class VSTestForwardingApp : ForwardingApp
{
private const string VstestAppName = "vstest.console.dll";
private readonly ForwardingApp _forwardingApp;
public VSTestForwardingApp(IEnumerable<string> argsToForward)
: base(GetVSTestExePath(), argsToForward)
{
_forwardingApp = new ForwardingApp(
GetVSTestExePath(),
argsToForward);
}
public int Execute()
{
return _forwardingApp.Execute();
}
private string GetVSTestExePath()
private static string GetVSTestExePath()
{
return Path.Combine(AppContext.BaseDirectory, VstestAppName);
}

View file

@ -6,28 +6,33 @@ namespace Microsoft.DotNet.Cli.MSBuild.Tests
{
public class GivenDotnetBuildInvocation
{
const string ExpectedPrefix = "exec <msbuildpath> /m /v:m";
const string ExpectedSuffix = "/clp:Summary";
[Theory]
[InlineData(new string[] { }, @"exec <msbuildpath> /m /v:m /t:Build /clp:Summary")]
[InlineData(new string[] { "-o", "foo" }, @"exec <msbuildpath> /m /v:m /t:Build /p:OutputPath=foo /clp:Summary")]
[InlineData(new string[] { "--output", "foo" }, @"exec <msbuildpath> /m /v:m /t:Build /p:OutputPath=foo /clp:Summary")]
[InlineData(new string[] { "-o", "foo1 foo2" }, @"exec <msbuildpath> /m /v:m /t:Build ""/p:OutputPath=foo1 foo2"" /clp:Summary")]
[InlineData(new string[] { "--no-incremental" }, @"exec <msbuildpath> /m /v:m /t:Rebuild /clp:Summary")]
[InlineData(new string[] { "-f", "framework" }, @"exec <msbuildpath> /m /v:m /t:Build /p:TargetFramework=framework /clp:Summary")]
[InlineData(new string[] { "--framework", "framework" }, @"exec <msbuildpath> /m /v:m /t:Build /p:TargetFramework=framework /clp:Summary")]
[InlineData(new string[] { "-r", "runtime" }, @"exec <msbuildpath> /m /v:m /t:Build /p:RuntimeIdentifier=runtime /clp:Summary")]
[InlineData(new string[] { "--runtime", "runtime" }, @"exec <msbuildpath> /m /v:m /t:Build /p:RuntimeIdentifier=runtime /clp:Summary")]
[InlineData(new string[] { "-c", "configuration" }, @"exec <msbuildpath> /m /v:m /t:Build /p:Configuration=configuration /clp:Summary")]
[InlineData(new string[] { "--configuration", "configuration" }, @"exec <msbuildpath> /m /v:m /t:Build /p:Configuration=configuration /clp:Summary")]
[InlineData(new string[] { "--version-suffix", "mysuffix" }, @"exec <msbuildpath> /m /v:m /t:Build /p:VersionSuffix=mysuffix /clp:Summary")]
[InlineData(new string[] { "--no-dependencies" }, @"exec <msbuildpath> /m /v:m /t:Build /p:BuildProjectReferences=false /clp:Summary")]
[InlineData(new string[] { "-v", "verbosity" }, @"exec <msbuildpath> /m /v:m /t:Build /verbosity:verbosity /clp:Summary")]
[InlineData(new string[] { "--verbosity", "verbosity" }, @"exec <msbuildpath> /m /v:m /t:Build /verbosity:verbosity /clp:Summary")]
[InlineData(new string[] { "--no-incremental", "-o", "myoutput", "-r", "myruntime", "-v", "diag" }, @"exec <msbuildpath> /m /v:m /t:Rebuild /p:OutputPath=myoutput /p:RuntimeIdentifier=myruntime /verbosity:diag /clp:Summary")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedCommand)
[InlineData(new string[] { }, "/t:Build")]
[InlineData(new string[] { "-o", "foo" }, "/t:Build /p:OutputPath=foo")]
[InlineData(new string[] { "--output", "foo" }, "/t:Build /p:OutputPath=foo")]
[InlineData(new string[] { "-o", "foo1 foo2" }, "/t:Build \"/p:OutputPath=foo1 foo2\"")]
[InlineData(new string[] { "--no-incremental" }, "/t:Rebuild")]
[InlineData(new string[] { "-f", "framework" }, "/t:Build /p:TargetFramework=framework")]
[InlineData(new string[] { "--framework", "framework" }, "/t:Build /p:TargetFramework=framework")]
[InlineData(new string[] { "-r", "runtime" }, "/t:Build /p:RuntimeIdentifier=runtime")]
[InlineData(new string[] { "--runtime", "runtime" }, "/t:Build /p:RuntimeIdentifier=runtime")]
[InlineData(new string[] { "-c", "configuration" }, "/t:Build /p:Configuration=configuration")]
[InlineData(new string[] { "--configuration", "configuration" }, "/t:Build /p:Configuration=configuration")]
[InlineData(new string[] { "--version-suffix", "mysuffix" }, "/t:Build /p:VersionSuffix=mysuffix")]
[InlineData(new string[] { "--no-dependencies" }, "/t:Build /p:BuildProjectReferences=false")]
[InlineData(new string[] { "-v", "verbosity" }, "/t:Build /verbosity:verbosity")]
[InlineData(new string[] { "--verbosity", "verbosity" }, "/t:Build /verbosity:verbosity")]
[InlineData(new string[] { "--no-incremental", "-o", "myoutput", "-r", "myruntime", "-v", "diag" }, "/t:Rebuild /p:OutputPath=myoutput /p:RuntimeIdentifier=myruntime /verbosity:diag")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs)
{
expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}");
var msbuildPath = "<msbuildpath>";
BuildCommand.FromArgs(args, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be(expectedCommand);
.GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs} {ExpectedSuffix}");
}
}
}

View file

@ -0,0 +1,54 @@
using Microsoft.DotNet.Tools.Cache;
using FluentAssertions;
using Xunit;
using System;
using System.Linq;
using System.IO;
namespace Microsoft.DotNet.Cli.MSBuild.Tests
{
public class GivenDotnetCacheInvocation
{
const string ExpectedPrefix = "exec <msbuildpath> /m /v:m /t:ComposeCache <project>";
static readonly string[] ArgsPrefix = { "-e", "<project>" };
[Theory]
[InlineData("-e")]
[InlineData("--entries")]
public void ItAddsProjectToMsbuildInvocation(string optionName)
{
var msbuildPath = "<msbuildpath>";
string[] args = new string[] { optionName, "<project>" };
CacheCommand.FromArgs(args, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}");
}
[Theory]
[InlineData(new string[] { "-f", "<framework>" }, @"/p:TargetFramework=<framework>")]
[InlineData(new string[] { "--framework", "<framework>" }, @"/p:TargetFramework=<framework>")]
[InlineData(new string[] { "-r", "<runtime>" }, @"/p:RuntimeIdentifier=<runtime>")]
[InlineData(new string[] { "--runtime", "<runtime>" }, @"/p:RuntimeIdentifier=<runtime>")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs)
{
args = ArgsPrefix.Concat(args).ToArray();
expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}");
var msbuildPath = "<msbuildpath>";
CacheCommand.FromArgs(args, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}");
}
[Theory]
[InlineData("-o")]
[InlineData("--output")]
public void ItAddsOutputPathToMsBuildInvocation(string optionName)
{
string path = "/some/path";
var args = ArgsPrefix.Concat(new string[] { optionName, path }).ToArray();
var msbuildPath = "<msbuildpath>";
CacheCommand.FromArgs(args, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix} /p:ComposeDir={Path.GetFullPath(path)}");
}
}
}

View file

@ -0,0 +1,39 @@
using Microsoft.DotNet.Tools.Clean;
using FluentAssertions;
using Xunit;
using System;
namespace Microsoft.DotNet.Cli.MSBuild.Tests
{
public class GivenDotnetCleanInvocation
{
const string ExpectedPrefix = "exec <msbuildpath> /m /v:m /t:Clean";
[Fact]
public void ItAddsProjectToMsbuildInvocation()
{
var msbuildPath = "<msbuildpath>";
CleanCommand.FromArgs(new string[] { "<project>" }, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be("exec <msbuildpath> /m /v:m <project> /t:Clean");
}
[Theory]
[InlineData(new string[] { }, "")]
[InlineData(new string[] { "-o", "<output>" }, "/p:OutputPath=<output>")]
[InlineData(new string[] { "--output", "<output>" }, "/p:OutputPath=<output>")]
[InlineData(new string[] { "-f", "<framework>" }, "/p:TargetFramework=<framework>")]
[InlineData(new string[] { "--framework", "<framework>" }, "/p:TargetFramework=<framework>")]
[InlineData(new string[] { "-c", "<configuration>" }, "/p:Configuration=<configuration>")]
[InlineData(new string[] { "--configuration", "<configuration>" }, "/p:Configuration=<configuration>")]
[InlineData(new string[] { "-v", "<verbosity>" }, "/verbosity:<verbosity>")]
[InlineData(new string[] { "--verbosity", "<verbosity>" }, "/verbosity:<verbosity>")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs)
{
expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}");
var msbuildPath = "<msbuildpath>";
CleanCommand.FromArgs(args, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}");
}
}
}

View file

@ -0,0 +1,37 @@
using Microsoft.DotNet.Tools.Pack;
using FluentAssertions;
using Xunit;
using System;
using System.Linq;
namespace Microsoft.DotNet.Cli.MSBuild.Tests
{
public class GivenDotnetPackInvocation
{
const string ExpectedPrefix = "exec <msbuildpath> /m /v:m /t:pack";
[Theory]
[InlineData(new string[] { }, "")]
[InlineData(new string[] { "-o", "<output>" }, "/p:PackageOutputPath=<output>")]
[InlineData(new string[] { "--output", "<output>" }, "/p:PackageOutputPath=<output>")]
[InlineData(new string[] { "--no-build" }, "/p:NoBuild=true")]
[InlineData(new string[] { "--include-symbols" }, "/p:IncludeSymbols=true")]
[InlineData(new string[] { "--include-source" }, "/p:IncludeSource=true")]
[InlineData(new string[] { "-c", "<configuration>" }, "/p:Configuration=<configuration>")]
[InlineData(new string[] { "--configuration", "<configuration>" }, "/p:Configuration=<configuration>")]
[InlineData(new string[] { "--version-suffix", "<version-suffix>" }, "/p:VersionSuffix=<version-suffix>")]
[InlineData(new string[] { "-s" }, "/p:Serviceable=true")]
[InlineData(new string[] { "--serviceable" }, "/p:Serviceable=true")]
[InlineData(new string[] { "-v", "<verbosity>" }, @"/verbosity:<verbosity>")]
[InlineData(new string[] { "--verbosity", "<verbosity>" }, @"/verbosity:<verbosity>")]
[InlineData(new string[] { "<project>" }, "<project>")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs)
{
expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}");
var msbuildPath = "<msbuildpath>";
PackCommand.FromArgs(args, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}");
}
}
}

View file

@ -0,0 +1,38 @@
using Microsoft.DotNet.Tools.Publish;
using FluentAssertions;
using Xunit;
using System;
using System.Linq;
namespace Microsoft.DotNet.Cli.MSBuild.Tests
{
public class GivenDotnetPublishInvocation
{
const string ExpectedPrefix = "exec <msbuildpath> /m /v:m /t:Publish";
[Theory]
[InlineData(new string[] { }, "")]
[InlineData(new string[] { "-f", "<framework>" }, "/p:TargetFramework=<framework>")]
[InlineData(new string[] { "--framework", "<framework>" }, "/p:TargetFramework=<framework>")]
[InlineData(new string[] { "-r", "<runtime>" }, "/p:RuntimeIdentifier=<runtime>")]
[InlineData(new string[] { "--runtime", "<runtime>" }, "/p:RuntimeIdentifier=<runtime>")]
[InlineData(new string[] { "-o", "<output>" }, "/p:PublishDir=<output>")]
[InlineData(new string[] { "--output", "<output>" }, "/p:PublishDir=<output>")]
[InlineData(new string[] { "-c", "<configuration>" }, "/p:Configuration=<configuration>")]
[InlineData(new string[] { "--configuration", "<configuration>" }, "/p:Configuration=<configuration>")]
[InlineData(new string[] { "--version-suffix", "<version-suffix>" }, "/p:VersionSuffix=<version-suffix>")]
[InlineData(new string[] { "--filter", "<filter>" }, "/p:FilterProjFile=<filter>")]
[InlineData(new string[] { "-v", "<verbosity>" }, "/verbosity:<verbosity>")]
[InlineData(new string[] { "--verbosity", "<verbosity>" }, "/verbosity:<verbosity>")]
[InlineData(new string[] { "<project>" }, "<project>")]
[InlineData(new string[] { "<project>", "<extra-args>" }, "<project> <extra-args>")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs)
{
expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}");
var msbuildPath = "<msbuildpath>";
PublishCommand.FromArgs(args, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}");
}
}
}

View file

@ -0,0 +1,37 @@
using Microsoft.DotNet.Tools.Restore;
using FluentAssertions;
using Xunit;
using System;
namespace Microsoft.DotNet.Cli.MSBuild.Tests
{
public class GivenDotnetRestoreInvocation
{
const string ExpectedPrefix = "exec <msbuildpath> /m /v:m /NoLogo /t:Restore /ConsoleLoggerParameters:Verbosity=Minimal";
[Theory]
[InlineData(new string[] { }, "")]
[InlineData(new string[] { "-s", "<source>" }, "/p:RestoreSources=<source>")]
[InlineData(new string[] { "--source", "<source>" }, "/p:RestoreSources=<source>")]
[InlineData(new string[] { "-s", "<source0>", "-s", "<source1>" }, "/p:RestoreSources=<source0>%3B<source1>")]
[InlineData(new string[] { "-r", "<runtime>" }, "/p:RuntimeIdentifiers=<runtime>")]
[InlineData(new string[] { "--runtime", "<runtime>" }, "/p:RuntimeIdentifiers=<runtime>")]
[InlineData(new string[] { "-r", "<runtime0>", "-r", "<runtime1>" }, "/p:RuntimeIdentifiers=<runtime0>%3B<runtime1>")]
[InlineData(new string[] { "--packages", "<packages>" }, "/p:RestorePackagesPath=<packages>")]
[InlineData(new string[] { "--disable-parallel" }, "/p:RestoreDisableParallel=true")]
[InlineData(new string[] { "--configfile", "<config>" }, "/p:RestoreConfigFile=<config>")]
[InlineData(new string[] { "--no-cache" }, "/p:RestoreNoCache=true")]
[InlineData(new string[] { "--ignore-failed-sources" }, "/p:RestoreIgnoreFailedSources=true")]
[InlineData(new string[] { "--no-dependencies" }, "/p:RestoreRecursive=false")]
[InlineData(new string[] { "-v", "<verbosity>" }, @"/verbosity:<verbosity>")]
[InlineData(new string[] { "--verbosity", "<verbosity>" }, @"/verbosity:<verbosity>")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs)
{
expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}");
var msbuildPath = "<msbuildpath>";
RestoreCommand.FromArgs(args, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}");
}
}
}

View file

@ -0,0 +1,17 @@
using Microsoft.DotNet.Tools.VSTest;
using FluentAssertions;
using Xunit;
using System;
namespace Microsoft.DotNet.Cli.MSBuild.Tests
{
public class GivenDotnetVsTestForwardingApp
{
[Fact]
public void ItRunsVsTestApp()
{
new VSTestForwardingApp(new string[0])
.GetProcessStartInfo().Arguments.Should().EndWith("vstest.console.dll");
}
}
}

View file

@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using FluentAssertions;
using Microsoft.DotNet.Tools.Test.Utilities;
namespace Microsoft.DotNet.Cli
{
public class GivenForwardingApp
{
[WindowsOnlyFact]
public void DotnetExeIsExecuted()
{
new ForwardingApp("<apppath>", new string[0])
.GetProcessStartInfo().FileName.Should().Be("dotnet.exe");
}
[NonWindowsOnlyFact]
public void DotnetIsExecuted()
{
new ForwardingApp("<apppath>", new string[0])
.GetProcessStartInfo().FileName.Should().Be("dotnet");
}
[Fact]
public void ItForwardsArgs()
{
new ForwardingApp("<apppath>", new string[] { "one", "two", "three" })
.GetProcessStartInfo().Arguments.Should().Be("exec <apppath> one two three");
}
[Fact]
public void ItAddsDepsFileArg()
{
new ForwardingApp("<apppath>", new string[] { "<arg>" }, depsFile: "<deps-file>")
.GetProcessStartInfo().Arguments.Should().Be("exec --depsfile <deps-file> <apppath> <arg>");
}
[Fact]
public void ItAddsRuntimeConfigArg()
{
new ForwardingApp("<apppath>", new string[] { "<arg>" }, runtimeConfig: "<runtime-config>")
.GetProcessStartInfo().Arguments.Should().Be("exec --runtimeconfig <runtime-config> <apppath> <arg>");
}
[Fact]
public void ItAddsAdditionalProbingPathArg()
{
new ForwardingApp("<apppath>", new string[] { "<arg>" }, additionalProbingPath: "<additionalprobingpath>")
.GetProcessStartInfo().Arguments.Should().Be("exec --additionalprobingpath <additionalprobingpath> <apppath> <arg>");
}
[Fact]
public void ItQuotesArgsWithSpaces()
{
new ForwardingApp("<apppath>", new string[] { "a b c" })
.GetProcessStartInfo().Arguments.Should().Be("exec <apppath> \"a b c\"");
}
[Fact]
public void ItEscapesArgs()
{
new ForwardingApp("<apppath>", new string[] { "a\"b\"c" })
.GetProcessStartInfo().Arguments.Should().Be("exec <apppath> a\\\"b\\\"c");
}
[Fact]
public void ItSetsEnvironmentalVariables()
{
var startInfo = new ForwardingApp("<apppath>", new string[0], environmentVariables: new Dictionary<string, string>
{
{ "env1", "env1value" },
{ "env2", "env2value" }
})
.GetProcessStartInfo();
startInfo.EnvironmentVariables["env1"].Should().Be("env1value");
startInfo.EnvironmentVariables["env2"].Should().Be("env2value");
}
}
}