add more unit tests
This commit is contained in:
parent
9c00966956
commit
8ebc06da46
4 changed files with 28 additions and 24 deletions
|
@ -1,4 +1,5 @@
|
|||
using System.Diagnostics;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Microsoft.DotNet.Cli
|
||||
{
|
||||
|
@ -6,6 +7,11 @@ namespace Microsoft.DotNet.Cli
|
|||
{
|
||||
public static int Execute(this ProcessStartInfo startInfo)
|
||||
{
|
||||
if (startInfo == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(startInfo));
|
||||
}
|
||||
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = startInfo
|
||||
|
|
|
@ -17,12 +17,12 @@ namespace Microsoft.DotNet.Tools.Build
|
|||
|
||||
private BuildCommand() { }
|
||||
|
||||
public BuildCommand(IEnumerable<string> msbuildArgs)
|
||||
public BuildCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
|
||||
{
|
||||
_forwardingApp = new MSBuildForwardingApp(msbuildArgs);
|
||||
_forwardingApp = new MSBuildForwardingApp(msbuildArgs, msbuildPath);
|
||||
}
|
||||
|
||||
public static BuildCommand FromArgs(params string[] args)
|
||||
public static BuildCommand FromArgs(string[] args, string msbuildPath = null)
|
||||
{
|
||||
var ret = new BuildCommand();
|
||||
|
||||
|
@ -48,10 +48,8 @@ namespace Microsoft.DotNet.Tools.Build
|
|||
CommandOption noDependenciesOption = app.Option("--no-dependencies", LocalizableStrings.NoDependenciesOptionDescription, CommandOptionType.NoValue);
|
||||
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app);
|
||||
|
||||
bool codeExecuted = false;
|
||||
app.OnExecute(() =>
|
||||
{
|
||||
codeExecuted = true;
|
||||
List<string> msbuildArgs = new List<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(projectArgument.Value))
|
||||
|
@ -107,15 +105,15 @@ namespace Microsoft.DotNet.Tools.Build
|
|||
|
||||
msbuildArgs.AddRange(app.RemainingArguments);
|
||||
|
||||
ret._forwardingApp = new MSBuildForwardingApp(msbuildArgs);
|
||||
ret._forwardingApp = new MSBuildForwardingApp(msbuildArgs, msbuildPath);
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
int exitCode = app.Execute();
|
||||
if (!codeExecuted)
|
||||
int exitCode = app.Execute(args);
|
||||
if (ret._forwardingApp == null)
|
||||
{
|
||||
throw new NonZeroExitCodeException(exitCode);
|
||||
throw new CodeNotExecutedException(exitCode);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -130,7 +128,7 @@ namespace Microsoft.DotNet.Tools.Build
|
|||
{
|
||||
cmd = FromArgs(args);
|
||||
}
|
||||
catch (NonZeroExitCodeException e)
|
||||
catch (CodeNotExecutedException e)
|
||||
{
|
||||
return e.ExitCode;
|
||||
}
|
||||
|
@ -148,11 +146,11 @@ namespace Microsoft.DotNet.Tools.Build
|
|||
return GetProcessStartInfo().Execute();
|
||||
}
|
||||
|
||||
private class NonZeroExitCodeException : Exception
|
||||
private class CodeNotExecutedException : Exception
|
||||
{
|
||||
public int ExitCode { get; private set; }
|
||||
|
||||
public NonZeroExitCodeException(int exitCode)
|
||||
public CodeNotExecutedException(int exitCode)
|
||||
{
|
||||
ExitCode = exitCode;
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ namespace Microsoft.DotNet.Tools.MSBuild
|
|||
private readonly IEnumerable<string> _msbuildRequiredParameters =
|
||||
new List<string> { "/m", "/v:m" };
|
||||
|
||||
public MSBuildForwardingApp(IEnumerable<string> argsToForward)
|
||||
public MSBuildForwardingApp(IEnumerable<string> argsToForward, string msbuildPath = null)
|
||||
{
|
||||
if (Telemetry.CurrentSessionId != null)
|
||||
{
|
||||
|
@ -55,7 +55,7 @@ namespace Microsoft.DotNet.Tools.MSBuild
|
|||
}
|
||||
|
||||
_forwardingApp = new ForwardingApp(
|
||||
GetMSBuildExePath(),
|
||||
msbuildPath ?? GetMSBuildExePath(),
|
||||
_msbuildRequiredParameters.Concat(argsToForward.Select(Escape)),
|
||||
environmentVariables: _msbuildRequiredEnvironmentVariables);
|
||||
}
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
using Microsoft.DotNet.Tools.Build;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using System.IO;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.MSBuild.Tests
|
||||
{
|
||||
public class GivenDotnetBuildInvocation
|
||||
{
|
||||
[Fact]
|
||||
public void WhenNoArgsArePassedThenMsbuildInvocationIsCorrect()
|
||||
[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")]
|
||||
public void WhenNoArgsArePassedThenMsbuildInvocationIsCorrect(string[] args, string expectedCommand)
|
||||
{
|
||||
var msbuildPath = Path.Combine(Directory.GetCurrentDirectory(), "MSBuild.dll");
|
||||
BuildCommand.FromArgs()
|
||||
.GetProcessStartInfo().Arguments.Should().Be($@"exec {msbuildPath} /m /v:m /t:Build /clp:Summary");
|
||||
var msbuildPath = "<msbuildpath>";
|
||||
BuildCommand.FromArgs(args, msbuildPath)
|
||||
.GetProcessStartInfo().Arguments.Should().Be(expectedCommand);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue