add dotnet-publish unit tests

This commit is contained in:
Krzysztof Wicher 2017-02-22 12:45:11 -08:00
parent 584b4a93c0
commit 1514dd5e16
3 changed files with 74 additions and 32 deletions

View file

@ -1,15 +1,17 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // 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. // 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.CommandLine;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild; using Microsoft.DotNet.Tools.MSBuild;
using System.Diagnostics;
namespace Microsoft.DotNet.Tools.Publish namespace Microsoft.DotNet.Tools.Publish
{ {
public partial class PublishCommand public partial class PublishCommand
{ {
public static int Run(string[] args) public static PublishCommand FromArgs(string[] args, string msbuildPath = null)
{ {
DebugHelper.HandleDebugSwitch(ref args); DebugHelper.HandleDebugSwitch(ref args);
@ -50,10 +52,11 @@ namespace Microsoft.DotNet.Tools.Publish
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app); CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app);
var publish = new PublishCommand(msbuildPath);
bool commandExecuted = false;
app.OnExecute(() => app.OnExecute(() =>
{ {
var publish = new PublishCommand(); commandExecuted = true;
publish.ProjectPath = projectArgument.Value; publish.ProjectPath = projectArgument.Value;
publish.Framework = frameworkOption.Value(); publish.Framework = frameworkOption.Value();
publish.Runtime = runtimeOption.Value(); publish.Runtime = runtimeOption.Value();
@ -64,10 +67,43 @@ namespace Microsoft.DotNet.Tools.Publish
publish.Verbosity = verbosityOption.Value(); publish.Verbosity = verbosityOption.Value();
publish.ExtraMSBuildArguments = app.RemainingArguments; publish.ExtraMSBuildArguments = app.RemainingArguments;
return publish.Execute(); return 0;
}); });
return app.Execute(args); int exitCode = app.Execute(args);
if (!commandExecuted)
{
throw new CommandCreationException(exitCode);
}
return publish;
}
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();
}
public ProcessStartInfo GetProcessStartInfo()
{
return CreateForwardingApp(_msbuildPath).GetProcessStartInfo();
}
public int Execute()
{
return GetProcessStartInfo().Execute();
} }
} }
} }

View file

@ -11,6 +11,8 @@ namespace Microsoft.DotNet.Tools.Publish
{ {
public partial class PublishCommand public partial class PublishCommand
{ {
private string _msbuildPath;
public string ProjectPath { get; set; } public string ProjectPath { get; set; }
public string Framework { get; set; } public string Framework { get; set; }
public string Runtime { get; set; } public string Runtime { get; set; }
@ -22,21 +24,22 @@ namespace Microsoft.DotNet.Tools.Publish
public List<string> ExtraMSBuildArguments { get; set; } public List<string> ExtraMSBuildArguments { get; set; }
private PublishCommand() private PublishCommand(string msbuildPath = null)
{ {
_msbuildPath = msbuildPath;
} }
public int Execute() private MSBuildForwardingApp CreateForwardingApp(string msbuildPath)
{ {
List<string> msbuildArgs = new List<string>(); List<string> msbuildArgs = new List<string>();
msbuildArgs.Add("/t:Publish");
if (!string.IsNullOrEmpty(ProjectPath)) if (!string.IsNullOrEmpty(ProjectPath))
{ {
msbuildArgs.Add(ProjectPath); msbuildArgs.Add(ProjectPath);
} }
msbuildArgs.Add("/t:Publish");
if (!string.IsNullOrEmpty(Framework)) if (!string.IsNullOrEmpty(Framework))
{ {
msbuildArgs.Add($"/p:TargetFramework={Framework}"); msbuildArgs.Add($"/p:TargetFramework={Framework}");
@ -74,7 +77,7 @@ namespace Microsoft.DotNet.Tools.Publish
msbuildArgs.AddRange(ExtraMSBuildArguments); msbuildArgs.AddRange(ExtraMSBuildArguments);
return new MSBuildForwardingApp(msbuildArgs).Execute(); return new MSBuildForwardingApp(msbuildArgs, msbuildPath);
} }
} }
} }

View file

@ -1,35 +1,38 @@
using Microsoft.DotNet.Tools.Build; using Microsoft.DotNet.Tools.Publish;
using FluentAssertions; using FluentAssertions;
using Xunit; using Xunit;
using System; using System;
using System.Linq;
namespace Microsoft.DotNet.Cli.MSBuild.Tests namespace Microsoft.DotNet.Cli.MSBuild.Tests
{ {
public class GivenDotnetPublishInvocation public class GivenDotnetPublishInvocation
{ {
[Theory(Skip = "finish me")] const string ExpectedPrefix = "exec <msbuildpath> /m /v:m /t:Publish";
[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")] [Theory]
[InlineData(new string[] { "--output", "foo" }, @"exec <msbuildpath> /m /v:m /t:Build /p:OutputPath=foo /clp:Summary")] [InlineData(new string[] { }, "")]
[InlineData(new string[] { "-o", "foo1 foo2" }, @"exec <msbuildpath> /m /v:m /t:Build ""/p:OutputPath=foo1 foo2"" /clp:Summary")] [InlineData(new string[] { "-f", "<framework>" }, "/p:TargetFramework=<framework>")]
[InlineData(new string[] { "--no-incremental" }, @"exec <msbuildpath> /m /v:m /t:Rebuild /clp:Summary")] [InlineData(new string[] { "--framework", "<framework>" }, "/p:TargetFramework=<framework>")]
[InlineData(new string[] { "-f", "framework" }, @"exec <msbuildpath> /m /v:m /t:Build /p:TargetFramework=framework /clp:Summary")] [InlineData(new string[] { "-r", "<runtime>" }, "/p:RuntimeIdentifier=<runtime>")]
[InlineData(new string[] { "--framework", "framework" }, @"exec <msbuildpath> /m /v:m /t:Build /p:TargetFramework=framework /clp:Summary")] [InlineData(new string[] { "--runtime", "<runtime>" }, "/p:RuntimeIdentifier=<runtime>")]
[InlineData(new string[] { "-r", "runtime" }, @"exec <msbuildpath> /m /v:m /t:Build /p:RuntimeIdentifier=runtime /clp:Summary")] [InlineData(new string[] { "-o", "<output>" }, "/p:PublishDir=<output>")]
[InlineData(new string[] { "--runtime", "runtime" }, @"exec <msbuildpath> /m /v:m /t:Build /p:RuntimeIdentifier=runtime /clp:Summary")] [InlineData(new string[] { "--output", "<output>" }, "/p:PublishDir=<output>")]
[InlineData(new string[] { "-c", "configuration" }, @"exec <msbuildpath> /m /v:m /t:Build /p:Configuration=configuration /clp:Summary")] [InlineData(new string[] { "-c", "<configuration>" }, "/p:Configuration=<configuration>")]
[InlineData(new string[] { "--configuration", "configuration" }, @"exec <msbuildpath> /m /v:m /t:Build /p:Configuration=configuration /clp:Summary")] [InlineData(new string[] { "--configuration", "<configuration>" }, "/p:Configuration=<configuration>")]
[InlineData(new string[] { "--version-suffix", "mysuffix" }, @"exec <msbuildpath> /m /v:m /t:Build /p:VersionSuffix=mysuffix /clp:Summary")] [InlineData(new string[] { "--version-suffix", "<version-suffix>" }, "/p:VersionSuffix=<version-suffix>")]
[InlineData(new string[] { "--no-dependencies" }, @"exec <msbuildpath> /m /v:m /t:Build /p:BuildProjectReferences=false /clp:Summary")] [InlineData(new string[] { "--filter", "<filter>" }, "/p:FilterProjFile=<filter>")]
[InlineData(new string[] { "-v", "verbosity" }, @"exec <msbuildpath> /m /v:m /t:Build /verbosity:verbosity /clp:Summary")] [InlineData(new string[] { "-v", "<verbosity>" }, "/verbosity:<verbosity>")]
[InlineData(new string[] { "--verbosity", "verbosity" }, @"exec <msbuildpath> /m /v:m /t:Build /verbosity:verbosity /clp:Summary")] [InlineData(new string[] { "--verbosity", "<verbosity>" }, "/verbosity:<verbosity>")]
[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")] [InlineData(new string[] { "<project>" }, "<project>")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedCommand) [InlineData(new string[] { "<project>", "<extra-args>" }, "<project> <extra-args>")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs)
{ {
expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}");
var msbuildPath = "<msbuildpath>"; var msbuildPath = "<msbuildpath>";
BuildCommand.FromArgs(args, msbuildPath) PublishCommand.FromArgs(args, msbuildPath)
.GetProcessStartInfo().Arguments.Should().Be(expectedCommand); .GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}");
throw new NotImplementedException();
} }
} }
} }