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", "