diff --git a/src/dotnet/CommandDirectoryContext.cs b/src/dotnet/CommandDirectoryContext.cs new file mode 100644 index 0000000000..6c378ab921 --- /dev/null +++ b/src/dotnet/CommandDirectoryContext.cs @@ -0,0 +1,46 @@ +using System; +using System.IO; + +namespace Microsoft.DotNet.Cli +{ + internal static class CommandDirectoryContext + { + [ThreadStatic] + private static string _basePath; + + /// + /// Expands a path similar to Path.GetFullPath() but gives unit tests a hook to inject an overwrite to the + /// base path. + /// + /// A relative or absolute path specifier + /// The full path to the target + public static string GetFullPath(string path) + => _basePath != null + ? Path.GetFullPath(path, _basePath) + : Path.GetFullPath(path); + + /// + /// Meant to be used only in unit test to remove dependency on special characters in the absolute repo path + /// The overwrite will only affect the current thread. + /// + /// Directory to be used as base path instead of the current working directory. + /// Action to be executed with the overwritten directory in place. + public static void PerformActionWithBasePath(string basePath, Action action) + { + if (_basePath != null) + { + throw new InvalidOperationException( + $"Calls to {nameof(CommandDirectoryContext)}.{nameof(PerformActionWithBasePath)} cannot be nested."); + } + _basePath = basePath; + try + { + action(); + } + finally + { + _basePath = null; + } + } + } +} diff --git a/src/dotnet/commands/dotnet-build/BuildCommandParser.cs b/src/dotnet/commands/dotnet-build/BuildCommandParser.cs index 073279cc6d..b11b7eb04b 100644 --- a/src/dotnet/commands/dotnet-build/BuildCommandParser.cs +++ b/src/dotnet/commands/dotnet-build/BuildCommandParser.cs @@ -24,7 +24,7 @@ public static Command Build() => LocalizableStrings.OutputOptionDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.OutputOptionName) - .ForwardAsSingle(o => $"-property:OutputPath={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:OutputPath={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), CommonOptions.FrameworkOption(LocalizableStrings.FrameworkOptionDescription), CommonOptions.ConfigurationOption(LocalizableStrings.ConfigurationOptionDescription), CommonOptions.RuntimeOption(LocalizableStrings.RuntimeOptionDescription), @@ -40,4 +40,4 @@ public static Command Build() => CommonOptions.NoRestoreOption(), CommonOptions.VerbosityOption()); } -} \ No newline at end of file +} diff --git a/src/dotnet/commands/dotnet-clean/CleanCommandParser.cs b/src/dotnet/commands/dotnet-clean/CleanCommandParser.cs index 740d9fb849..796c15739a 100644 --- a/src/dotnet/commands/dotnet-clean/CleanCommandParser.cs +++ b/src/dotnet/commands/dotnet-clean/CleanCommandParser.cs @@ -22,10 +22,10 @@ public static Command Clean() => LocalizableStrings.CmdOutputDirDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.CmdOutputDir) - .ForwardAsSingle(o => $"-property:OutputPath={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:OutputPath={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), CommonOptions.FrameworkOption(LocalizableStrings.FrameworkOptionDescription), CommonOptions.RuntimeOption(LocalizableStrings.RuntimeOptionDescription), CommonOptions.ConfigurationOption(LocalizableStrings.ConfigurationOptionDescription), CommonOptions.VerbosityOption()); } -} \ No newline at end of file +} diff --git a/src/dotnet/commands/dotnet-pack/PackCommandParser.cs b/src/dotnet/commands/dotnet-pack/PackCommandParser.cs index 871f39e07d..19cd0d9911 100644 --- a/src/dotnet/commands/dotnet-pack/PackCommandParser.cs +++ b/src/dotnet/commands/dotnet-pack/PackCommandParser.cs @@ -24,7 +24,7 @@ public static Command Pack() => LocalizableStrings.CmdOutputDirDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.CmdOutputDir) - .ForwardAsSingle(o => $"-property:PackageOutputPath={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:PackageOutputPath={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), Create.Option( "--no-build", LocalizableStrings.CmdNoBuildOptionDescription, @@ -46,4 +46,4 @@ public static Command Pack() => CommonOptions.NoRestoreOption(), CommonOptions.VerbosityOption()); } -} \ No newline at end of file +} diff --git a/src/dotnet/commands/dotnet-publish/PublishCommandParser.cs b/src/dotnet/commands/dotnet-publish/PublishCommandParser.cs index 19ee3ec12b..c9ea664151 100644 --- a/src/dotnet/commands/dotnet-publish/PublishCommandParser.cs +++ b/src/dotnet/commands/dotnet-publish/PublishCommandParser.cs @@ -1,6 +1,7 @@ // 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.IO; using System.Linq; using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Tools; @@ -23,7 +24,7 @@ public static Command Publish() => LocalizableStrings.OutputOptionDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.OutputOption) - .ForwardAsSingle(o => $"-property:PublishDir={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:PublishDir={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), CommonOptions.FrameworkOption(LocalizableStrings.FrameworkOptionDescription), CommonOptions.RuntimeOption(LocalizableStrings.RuntimeOptionDescription), CommonOptions.ConfigurationOption(LocalizableStrings.ConfigurationOptionDescription), @@ -33,7 +34,7 @@ public static Command Publish() => LocalizableStrings.ManifestOptionDescription, Accept.OneOrMoreArguments() .With(name: LocalizableStrings.ManifestOption) - .ForwardAsSingle(o => $"-property:TargetManifestFiles={string.Join("%3B", o.Arguments)}")), + .ForwardAsSingle(o => $"-property:TargetManifestFiles={string.Join("%3B", o.Arguments.Select(CommandDirectoryContext.GetFullPath))}")), Create.Option( "--no-build", LocalizableStrings.NoBuildOptionDescription, diff --git a/src/dotnet/commands/dotnet-restore/RestoreCommandParser.cs b/src/dotnet/commands/dotnet-restore/RestoreCommandParser.cs index 71afaabb41..adca098d0c 100644 --- a/src/dotnet/commands/dotnet-restore/RestoreCommandParser.cs +++ b/src/dotnet/commands/dotnet-restore/RestoreCommandParser.cs @@ -70,7 +70,7 @@ private static Option[] ImplicitRestoreOptions(bool showHelp = false, bool useSh showHelp ? LocalizableStrings.CmdPackagesOptionDescription : string.Empty, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.CmdPackagesOption) - .ForwardAsSingle(o => $"-property:RestorePackagesPath={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:RestorePackagesPath={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), Create.Option( "--disable-parallel", showHelp ? LocalizableStrings.CmdDisableParallelOptionDescription : string.Empty, @@ -81,7 +81,7 @@ private static Option[] ImplicitRestoreOptions(bool showHelp = false, bool useSh showHelp ? LocalizableStrings.CmdConfigFileOptionDescription : string.Empty, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.CmdConfigFileOption) - .ForwardAsSingle(o => $"-property:RestoreConfigFile={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:RestoreConfigFile={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), Create.Option( "--no-cache", showHelp ? LocalizableStrings.CmdNoCacheOptionDescription : string.Empty, diff --git a/src/dotnet/commands/dotnet-store/StoreCommandParser.cs b/src/dotnet/commands/dotnet-store/StoreCommandParser.cs index 811668d694..ca47c26b63 100644 --- a/src/dotnet/commands/dotnet-store/StoreCommandParser.cs +++ b/src/dotnet/commands/dotnet-store/StoreCommandParser.cs @@ -1,7 +1,6 @@ // 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.IO; using System.Linq; using Microsoft.DotNet.Cli.CommandLine; using LocalizableStrings = Microsoft.DotNet.Tools.Store.LocalizableStrings; @@ -23,6 +22,8 @@ public static Command Store() => .With(name: LocalizableStrings.ProjectManifest) .ForwardAsMany(o => { + // the first path doesn't need to go through CommandDirectoryContext.ExpandPath + // since it is a direct argument to MSBuild, not a property var materializedString = $"{o.Arguments.First()}"; if (o.Arguments.Count == 1) @@ -37,7 +38,7 @@ public static Command Store() => return new[] { materializedString, - $"-property:AdditionalProjects={string.Join("%3B", o.Arguments.Skip(1))}" + $"-property:AdditionalProjects={string.Join("%3B", o.Arguments.Skip(1).Select(CommandDirectoryContext.GetFullPath))}" }; } })), @@ -54,13 +55,13 @@ public static Command Store() => LocalizableStrings.OutputOptionDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.OutputOption) - .ForwardAsSingle(o => $"-property:ComposeDir={Path.GetFullPath(o.Arguments.Single())}")), + .ForwardAsSingle(o => $"-property:ComposeDir={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), Create.Option( "-w|--working-dir", LocalizableStrings.IntermediateWorkingDirOptionDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.IntermediateWorkingDirOption) - .ForwardAsSingle(o => $"-property:ComposeWorkingDir={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:ComposeWorkingDir={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), Create.Option( "--skip-optimization", LocalizableStrings.SkipOptimizationOptionDescription, @@ -73,4 +74,4 @@ public static Command Store() => .ForwardAs("-property:CreateProfilingSymbols=false")), CommonOptions.VerbosityOption()); } -} \ No newline at end of file +} diff --git a/src/dotnet/commands/dotnet-test/TestCommandParser.cs b/src/dotnet/commands/dotnet-test/TestCommandParser.cs index 765a52072f..e6fdf7de2c 100644 --- a/src/dotnet/commands/dotnet-test/TestCommandParser.cs +++ b/src/dotnet/commands/dotnet-test/TestCommandParser.cs @@ -25,7 +25,7 @@ public static Command Test() => LocalizableStrings.CmdSettingsDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.CmdSettingsFile) - .ForwardAsSingle(o => $"-property:VSTestSetting={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:VSTestSetting={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), Create.Option( "-t|--list-tests", LocalizableStrings.CmdListTestsDescription, @@ -42,7 +42,7 @@ public static Command Test() => LocalizableStrings.CmdTestAdapterPathDescription, Accept.OneOrMoreArguments() .With(name: LocalizableStrings.CmdTestAdapterPath) - .ForwardAsSingle(o => $"-property:VSTestTestAdapterPath=\"{string.Join(";", o.Arguments)}\"")), + .ForwardAsSingle(o => $"-property:VSTestTestAdapterPath=\"{string.Join(";", o.Arguments.Select(CommandDirectoryContext.GetFullPath))}\"")), Create.Option( "-l|--logger", LocalizableStrings.CmdLoggerDescription, @@ -62,13 +62,13 @@ public static Command Test() => LocalizableStrings.CmdOutputDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.CmdOutputDir) - .ForwardAsSingle(o => $"-property:OutputPath={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:OutputPath={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), Create.Option( "-d|--diag", LocalizableStrings.CmdPathTologFileDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.CmdPathToLogFile) - .ForwardAsSingle(o => $"-property:VSTestDiag={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:VSTestDiag={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), Create.Option( "--no-build", LocalizableStrings.CmdNoBuildDescription, @@ -79,7 +79,7 @@ public static Command Test() => LocalizableStrings.CmdResultsDirectoryDescription, Accept.ExactlyOneArgument() .With(name: LocalizableStrings.CmdPathToResultsDirectory) - .ForwardAsSingle(o => $"-property:VSTestResultsDirectory={o.Arguments.Single()}")), + .ForwardAsSingle(o => $"-property:VSTestResultsDirectory={CommandDirectoryContext.GetFullPath(o.Arguments.Single())}")), Create.Option( "--collect", LocalizableStrings.cmdCollectDescription, diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/TestPathUtilities.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/TestPathUtilities.cs new file mode 100644 index 0000000000..ff7b144896 --- /dev/null +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/TestPathUtilities.cs @@ -0,0 +1,18 @@ +// 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.IO; +using System.Runtime.InteropServices; + +namespace Microsoft.DotNet.Tools.Tests.Utilities +{ + public static class TestPathUtilities + { + public static string FormatAbsolutePath(string directoryName = null) + => Path.Combine( + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + ? "A:" + : Path.DirectorySeparatorChar.ToString(), + directoryName ?? "TestWorkDir") + Path.DirectorySeparatorChar; + } +} diff --git a/test/dotnet-msbuild.Tests/GivenDotnetBuildInvocation.cs b/test/dotnet-msbuild.Tests/GivenDotnetBuildInvocation.cs index 3a4821f5ac..3b638dac85 100644 --- a/test/dotnet-msbuild.Tests/GivenDotnetBuildInvocation.cs +++ b/test/dotnet-msbuild.Tests/GivenDotnetBuildInvocation.cs @@ -1,8 +1,11 @@ // 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.IO; +using System.Runtime.InteropServices; using Microsoft.DotNet.Tools.Build; using FluentAssertions; +using Microsoft.DotNet.Tools.Tests.Utilities; using Xunit; namespace Microsoft.DotNet.Cli.MSBuild.Tests @@ -11,12 +14,15 @@ public class GivenDotnetBuildInvocation { const string ExpectedPrefix = "exec -maxcpucount -verbosity:m"; + private static readonly string WorkingDirectory = + TestPathUtilities.FormatAbsolutePath(nameof(GivenDotnetBuildInvocation)); + [Theory] [InlineData(new string[] { }, "-target:Build")] - [InlineData(new string[] { "-o", "foo" }, "-target:Build -property:OutputPath=foo")] + [InlineData(new string[] { "-o", "foo" }, "-target:Build -property:OutputPath=foo")] [InlineData(new string[] { "-property:Verbosity=diag" }, "-target:Build -property:Verbosity=diag")] - [InlineData(new string[] { "--output", "foo" }, "-target:Build -property:OutputPath=foo")] - [InlineData(new string[] { "-o", "foo1 foo2" }, "-target:Build \"-property:OutputPath=foo1 foo2\"")] + [InlineData(new string[] { "--output", "foo" }, "-target:Build -property:OutputPath=foo")] + [InlineData(new string[] { "-o", "foo1 foo2" }, "-target:Build \"-property:OutputPath=foo1 foo2\"")] [InlineData(new string[] { "--no-incremental" }, "-target:Rebuild")] [InlineData(new string[] { "-r", "rid" }, "-target:Build -property:RuntimeIdentifier=rid")] [InlineData(new string[] { "--runtime", "rid" }, "-target:Build -property:RuntimeIdentifier=rid")] @@ -27,43 +33,54 @@ public class GivenDotnetBuildInvocation [InlineData(new string[] { "-v", "diag" }, "-target:Build -verbosity:diag")] [InlineData(new string[] { "--verbosity", "diag" }, "-target:Build -verbosity:diag")] [InlineData(new string[] { "--no-incremental", "-o", "myoutput", "-r", "myruntime", "-v", "diag", "/ArbitrarySwitchForMSBuild" }, - "-target:Rebuild -property:OutputPath=myoutput -property:RuntimeIdentifier=myruntime -verbosity:diag /ArbitrarySwitchForMSBuild")] + "-target:Rebuild -property:OutputPath=myoutput -property:RuntimeIdentifier=myruntime -verbosity:diag /ArbitrarySwitchForMSBuild")] public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs) { - expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}"); + CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => + { + expectedAdditionalArgs = + (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}") + .Replace("", WorkingDirectory); - var msbuildPath = ""; - var command = BuildCommand.FromArgs(args, msbuildPath); + var msbuildPath = ""; + var command = BuildCommand.FromArgs(args, msbuildPath); - command.SeparateRestoreCommand.Should().BeNull(); + command.SeparateRestoreCommand.Should().BeNull(); - command.GetProcessStartInfo() - .Arguments.Should() - .Be($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary{expectedAdditionalArgs}"); + command.GetProcessStartInfo() + .Arguments.Should() + .Be($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary{expectedAdditionalArgs}"); + }); } [Theory] [InlineData(new string[] { "-f", "tfm" }, "-target:Restore", "-target:Build -property:TargetFramework=tfm")] [InlineData(new string[] { "-o", "myoutput", "-f", "tfm", "-v", "diag", "/ArbitrarySwitchForMSBuild" }, - "-target:Restore -property:OutputPath=myoutput -verbosity:diag /ArbitrarySwitchForMSBuild", - "-target:Build -property:OutputPath=myoutput -property:TargetFramework=tfm -verbosity:diag /ArbitrarySwitchForMSBuild")] + "-target:Restore -property:OutputPath=myoutput -verbosity:diag /ArbitrarySwitchForMSBuild", + "-target:Build -property:OutputPath=myoutput -property:TargetFramework=tfm -verbosity:diag /ArbitrarySwitchForMSBuild")] public void MsbuildInvocationIsCorrectForSeparateRestore( string[] args, string expectedAdditionalArgsForRestore, string expectedAdditionalArgs) { - expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}"); + CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => + { + expectedAdditionalArgsForRestore = expectedAdditionalArgsForRestore.Replace("", WorkingDirectory); + + expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}"); + expectedAdditionalArgs = expectedAdditionalArgs.Replace("", WorkingDirectory); - var msbuildPath = ""; - var command = BuildCommand.FromArgs(args, msbuildPath); + var msbuildPath = ""; + var command = BuildCommand.FromArgs(args, msbuildPath); - command.SeparateRestoreCommand.GetProcessStartInfo() - .Arguments.Should() - .Be($"{ExpectedPrefix} {expectedAdditionalArgsForRestore}"); + command.SeparateRestoreCommand.GetProcessStartInfo() + .Arguments.Should() + .Be($"{ExpectedPrefix} {expectedAdditionalArgsForRestore}"); - command.GetProcessStartInfo() - .Arguments.Should() - .Be($"{ExpectedPrefix} -nologo -consoleloggerparameters:Summary{expectedAdditionalArgs}"); + command.GetProcessStartInfo() + .Arguments.Should() + .Be($"{ExpectedPrefix} -nologo -consoleloggerparameters:Summary{expectedAdditionalArgs}"); + }); } } diff --git a/test/dotnet-msbuild.Tests/GivenDotnetCleanInvocation.cs b/test/dotnet-msbuild.Tests/GivenDotnetCleanInvocation.cs index 4c7d898900..41de5f675d 100644 --- a/test/dotnet-msbuild.Tests/GivenDotnetCleanInvocation.cs +++ b/test/dotnet-msbuild.Tests/GivenDotnetCleanInvocation.cs @@ -5,6 +5,9 @@ using FluentAssertions; using Xunit; using System; +using System.IO; +using System.Runtime.InteropServices; +using Microsoft.DotNet.Tools.Tests.Utilities; namespace Microsoft.DotNet.Cli.MSBuild.Tests { @@ -12,6 +15,9 @@ public class GivenDotnetCleanInvocation { const string ExpectedPrefix = "exec -maxcpucount -verbosity:m -verbosity:normal -target:Clean"; + private static readonly string WorkingDirectory = + TestPathUtilities.FormatAbsolutePath(nameof(GivenDotnetCleanInvocation)); + [Fact] public void ItAddsProjectToMsbuildInvocation() { @@ -22,8 +28,8 @@ public void ItAddsProjectToMsbuildInvocation() [Theory] [InlineData(new string[] { }, "")] - [InlineData(new string[] { "-o", "" }, "-property:OutputPath=")] - [InlineData(new string[] { "--output", "" }, "-property:OutputPath=")] + [InlineData(new string[] { "-o", "" }, "-property:OutputPath=")] + [InlineData(new string[] { "--output", "" }, "-property:OutputPath=")] [InlineData(new string[] { "-f", "" }, "-property:TargetFramework=")] [InlineData(new string[] { "--framework", "" }, "-property:TargetFramework=")] [InlineData(new string[] { "-c", "" }, "-property:Configuration=")] @@ -32,11 +38,16 @@ public void ItAddsProjectToMsbuildInvocation() [InlineData(new string[] { "--verbosity", "diag" }, "-verbosity:diag")] public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs) { - expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}"); + CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => + { + expectedAdditionalArgs = + (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}") + .Replace("", WorkingDirectory); - var msbuildPath = ""; - CleanCommand.FromArgs(args, msbuildPath) - .GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}"); + var msbuildPath = ""; + CleanCommand.FromArgs(args, msbuildPath) + .GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}"); + }); } } } diff --git a/test/dotnet-msbuild.Tests/GivenDotnetPackInvocation.cs b/test/dotnet-msbuild.Tests/GivenDotnetPackInvocation.cs index e0b766da54..a0c348395b 100644 --- a/test/dotnet-msbuild.Tests/GivenDotnetPackInvocation.cs +++ b/test/dotnet-msbuild.Tests/GivenDotnetPackInvocation.cs @@ -5,7 +5,10 @@ using FluentAssertions; using Xunit; using System; +using System.IO; using System.Linq; +using System.Runtime.InteropServices; +using Microsoft.DotNet.Tools.Tests.Utilities; namespace Microsoft.DotNet.Cli.MSBuild.Tests { @@ -14,10 +17,13 @@ public class GivenDotnetPackInvocation const string ExpectedPrefix = "exec -maxcpucount -verbosity:m -restore -target:pack"; const string ExpectedNoBuildPrefix = "exec -maxcpucount -verbosity:m -target:pack"; + private static readonly string WorkingDirectory = + TestPathUtilities.FormatAbsolutePath(nameof(GivenDotnetPackInvocation)); + [Theory] [InlineData(new string[] { }, "")] - [InlineData(new string[] { "-o", "" }, "-property:PackageOutputPath=")] - [InlineData(new string[] { "--output", "" }, "-property:PackageOutputPath=")] + [InlineData(new string[] { "-o", "" }, "-property:PackageOutputPath=")] + [InlineData(new string[] { "--output", "" }, "-property:PackageOutputPath=")] [InlineData(new string[] { "--no-build" }, "-property:NoBuild=true")] [InlineData(new string[] { "--include-symbols" }, "-property:IncludeSymbols=true")] [InlineData(new string[] { "--include-source" }, "-property:IncludeSource=true")] @@ -31,14 +37,19 @@ public class GivenDotnetPackInvocation [InlineData(new string[] { "" }, "")] public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs) { - expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}"); + CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => + { + expectedAdditionalArgs = + (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}") + .Replace("", WorkingDirectory); - var msbuildPath = ""; - var command = PackCommand.FromArgs(args, msbuildPath); - var expectedPrefix = args.FirstOrDefault() == "--no-build" ? ExpectedNoBuildPrefix : ExpectedPrefix; + var msbuildPath = ""; + var command = PackCommand.FromArgs(args, msbuildPath); + var expectedPrefix = args.FirstOrDefault() == "--no-build" ? ExpectedNoBuildPrefix : ExpectedPrefix; - command.SeparateRestoreCommand.Should().BeNull(); - command.GetProcessStartInfo().Arguments.Should().Be($"{expectedPrefix}{expectedAdditionalArgs}"); + command.SeparateRestoreCommand.Should().BeNull(); + command.GetProcessStartInfo().Arguments.Should().Be($"{expectedPrefix}{expectedAdditionalArgs}"); + }); } } } diff --git a/test/dotnet-msbuild.Tests/GivenDotnetPublishInvocation.cs b/test/dotnet-msbuild.Tests/GivenDotnetPublishInvocation.cs index b661552615..b9526c0a90 100644 --- a/test/dotnet-msbuild.Tests/GivenDotnetPublishInvocation.cs +++ b/test/dotnet-msbuild.Tests/GivenDotnetPublishInvocation.cs @@ -2,10 +2,12 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.IO; using FluentAssertions; using System.Linq; using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Tools.Publish; +using Microsoft.DotNet.Tools.Tests.Utilities; using Xunit; using Xunit.Abstractions; @@ -13,6 +15,8 @@ namespace Microsoft.DotNet.Cli.MSBuild.Tests { public class GivenDotnetPublishInvocation { + private static readonly string WorkingDirectory = + TestPathUtilities.FormatAbsolutePath(nameof(GivenDotnetPublishInvocation)); private readonly ITestOutputHelper output; public GivenDotnetPublishInvocation(ITestOutputHelper output) @@ -26,30 +30,35 @@ public GivenDotnetPublishInvocation(ITestOutputHelper output) [InlineData(new string[] { }, "")] [InlineData(new string[] { "-r", "" }, "-property:RuntimeIdentifier=")] [InlineData(new string[] { "--runtime", "" }, "-property:RuntimeIdentifier=")] - [InlineData(new string[] { "-o", "" }, "-property:PublishDir=")] - [InlineData(new string[] { "--output", "" }, "-property:PublishDir=")] + [InlineData(new string[] { "-o", "" }, "-property:PublishDir=")] + [InlineData(new string[] { "--output", "" }, "-property:PublishDir=")] [InlineData(new string[] { "-c", "" }, "-property:Configuration=")] [InlineData(new string[] { "--configuration", "" }, "-property:Configuration=")] [InlineData(new string[] { "--version-suffix", "" }, "-property:VersionSuffix=")] - [InlineData(new string[] { "--manifest", "" }, "-property:TargetManifestFiles=")] + [InlineData(new string[] { "--manifest", "" }, "-property:TargetManifestFiles=")] [InlineData(new string[] { "-v", "minimal" }, "-verbosity:minimal")] [InlineData(new string[] { "--verbosity", "minimal" }, "-verbosity:minimal")] [InlineData(new string[] { "" }, "")] [InlineData(new string[] { "", "" }, " ")] public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs) { - expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}"); - - var msbuildPath = ""; - var command = PublishCommand.FromArgs(args, msbuildPath); - - command.SeparateRestoreCommand - .Should() - .BeNull(); - - command.GetProcessStartInfo() - .Arguments.Should() - .Be($"{ExpectedPrefix} -restore -target:Publish{expectedAdditionalArgs}"); + CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => + { + expectedAdditionalArgs = + (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}") + .Replace("", WorkingDirectory); + + var msbuildPath = ""; + var command = PublishCommand.FromArgs(args, msbuildPath); + + command.SeparateRestoreCommand + .Should() + .BeNull(); + + command.GetProcessStartInfo() + .Arguments.Should() + .Be($"{ExpectedPrefix} -restore -target:Publish{expectedAdditionalArgs}"); + }); } [Theory] @@ -95,27 +104,32 @@ public void MsbuildInvocationIsCorrectForNoBuild() [InlineData(new string[] { "--framework", "" }, "-property:TargetFramework=")] [InlineData(new string[] { "-r", "" }, "-property:RuntimeIdentifier=")] [InlineData(new string[] { "--runtime", "" }, "-property:RuntimeIdentifier=")] - [InlineData(new string[] { "-o", "" }, "-property:PublishDir=")] - [InlineData(new string[] { "--output", "" }, "-property:PublishDir=")] + [InlineData(new string[] { "-o", "" }, "-property:PublishDir=")] + [InlineData(new string[] { "--output", "" }, "-property:PublishDir=")] [InlineData(new string[] { "-c", "" }, "-property:Configuration=")] [InlineData(new string[] { "--configuration", "" }, "-property:Configuration=")] [InlineData(new string[] { "--version-suffix", "" }, "-property:VersionSuffix=")] - [InlineData(new string[] { "--manifest", "" }, "-property:TargetManifestFiles=")] + [InlineData(new string[] { "--manifest", "" }, "-property:TargetManifestFiles=")] [InlineData(new string[] { "-v", "minimal" }, "-verbosity:minimal")] [InlineData(new string[] { "--verbosity", "minimal" }, "-verbosity:minimal")] [InlineData(new string[] { "--no-build" }, "-property:NoBuild=true")] public void OptionForwardingIsCorrect(string[] args, string expectedAdditionalArgs) { - var expectedArgs = expectedAdditionalArgs.Split(' ', StringSplitOptions.RemoveEmptyEntries); + CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => + { + var expectedArgs = expectedAdditionalArgs + .Replace("", WorkingDirectory) + .Split(' ', StringSplitOptions.RemoveEmptyEntries); - var parser = Parser.Instance; + var parser = Parser.Instance; - var result = parser.ParseFrom("dotnet publish", args); + var result = parser.ParseFrom("dotnet publish", args); - result["dotnet"]["publish"] - .OptionValuesToBeForwarded() - .Should() - .BeEquivalentTo(expectedArgs); + result["dotnet"]["publish"] + .OptionValuesToBeForwarded() + .Should() + .BeEquivalentTo(expectedArgs); + }); } } } diff --git a/test/dotnet-msbuild.Tests/GivenDotnetRestoreInvocation.cs b/test/dotnet-msbuild.Tests/GivenDotnetRestoreInvocation.cs index e4ae267414..9a80ddf163 100644 --- a/test/dotnet-msbuild.Tests/GivenDotnetRestoreInvocation.cs +++ b/test/dotnet-msbuild.Tests/GivenDotnetRestoreInvocation.cs @@ -5,6 +5,9 @@ using FluentAssertions; using Xunit; using System; +using System.IO; +using System.Runtime.InteropServices; +using Microsoft.DotNet.Tools.Tests.Utilities; namespace Microsoft.DotNet.Cli.MSBuild.Tests { @@ -12,6 +15,8 @@ public class GivenDotnetRestoreInvocation { private const string ExpectedPrefix = "exec -maxcpucount -verbosity:m -nologo -target:Restore"; + private static readonly string WorkingDirectory = + TestPathUtilities.FormatAbsolutePath(nameof(GivenDotnetRestoreInvocation)); [Theory] [InlineData(new string[] { }, "")] @@ -21,9 +26,9 @@ public class GivenDotnetRestoreInvocation [InlineData(new string[] { "-r", "" }, "-property:RuntimeIdentifiers=")] [InlineData(new string[] { "--runtime", "" }, "-property:RuntimeIdentifiers=")] [InlineData(new string[] { "-r", "", "-r", "" }, "-property:RuntimeIdentifiers=%3B")] - [InlineData(new string[] { "--packages", "" }, "-property:RestorePackagesPath=")] + [InlineData(new string[] { "--packages", "" }, "-property:RestorePackagesPath=")] [InlineData(new string[] { "--disable-parallel" }, "-property:RestoreDisableParallel=true")] - [InlineData(new string[] { "--configfile", "" }, "-property:RestoreConfigFile=")] + [InlineData(new string[] { "--configfile", "" }, "-property:RestoreConfigFile=")] [InlineData(new string[] { "--no-cache" }, "-property:RestoreNoCache=true")] [InlineData(new string[] { "--ignore-failed-sources" }, "-property:RestoreIgnoreFailedSources=true")] [InlineData(new string[] { "--no-dependencies" }, "-property:RestoreRecursive=false")] @@ -31,12 +36,17 @@ public class GivenDotnetRestoreInvocation [InlineData(new string[] { "--verbosity", "minimal" }, @"-verbosity:minimal")] public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs) { - expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}"); + CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => + { + expectedAdditionalArgs = + (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}") + .Replace("", WorkingDirectory); - var msbuildPath = ""; - RestoreCommand.FromArgs(args, msbuildPath) - .GetProcessStartInfo().Arguments - .Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}"); + var msbuildPath = ""; + RestoreCommand.FromArgs(args, msbuildPath) + .GetProcessStartInfo().Arguments + .Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}"); + }); } } } diff --git a/test/dotnet-msbuild.Tests/GivenDotnetStoreInvocation.cs b/test/dotnet-msbuild.Tests/GivenDotnetStoreInvocation.cs index 47854f5699..724a7c2363 100644 --- a/test/dotnet-msbuild.Tests/GivenDotnetStoreInvocation.cs +++ b/test/dotnet-msbuild.Tests/GivenDotnetStoreInvocation.cs @@ -5,6 +5,8 @@ using Microsoft.DotNet.Tools.Store; using System.IO; using System.Linq; +using System.Runtime.InteropServices; +using Microsoft.DotNet.Tools.Tests.Utilities; using Xunit; namespace Microsoft.DotNet.Cli.MSBuild.Tests @@ -13,6 +15,8 @@ public class GivenDotnetStoreInvocation { const string ExpectedPrefix = "exec -maxcpucount -verbosity:m -target:ComposeStore "; static readonly string[] ArgsPrefix = { "--manifest", "" }; + private static readonly string WorkingDirectory = + TestPathUtilities.FormatAbsolutePath(nameof(GivenDotnetStoreInvocation)); [Theory] [InlineData("-m")] @@ -30,15 +34,20 @@ public void ItAddsProjectToMsbuildInvocation(string optionName) [InlineData(new string[] { "--framework", "" }, @"-property:TargetFramework=")] [InlineData(new string[] { "-r", "" }, @"-property:RuntimeIdentifier=")] [InlineData(new string[] { "--runtime", "" }, @"-property:RuntimeIdentifier=")] - [InlineData(new string[] { "--manifest", "one.xml", "--manifest", "two.xml", "--manifest", "three.xml" }, @"-property:AdditionalProjects=one.xml%3Btwo.xml%3Bthree.xml")] + [InlineData(new string[] { "--manifest", "one.xml", "--manifest", "two.xml", "--manifest", "three.xml" }, @"-property:AdditionalProjects=one.xml%3Btwo.xml%3Bthree.xml")] public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs) { - args = ArgsPrefix.Concat(args).ToArray(); - expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}"); + CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => + { + args = ArgsPrefix.Concat(args).ToArray(); + expectedAdditionalArgs = + (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}") + .Replace("", WorkingDirectory); - var msbuildPath = ""; - StoreCommand.FromArgs(args, msbuildPath) - .GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}"); + var msbuildPath = ""; + StoreCommand.FromArgs(args, msbuildPath) + .GetProcessStartInfo().Arguments.Should().Be($"{ExpectedPrefix}{expectedAdditionalArgs}"); + }); } [Theory] @@ -46,7 +55,7 @@ public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalA [InlineData("--output")] public void ItAddsOutputPathToMsBuildInvocation(string optionName) { - string path = "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/some/path"; + string path = Path.Combine("some", "path"); var args = ArgsPrefix.Concat(new string[] { optionName, path }).ToArray(); var msbuildPath = "";