Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/dotnet/CommandDirectoryContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;

namespace Microsoft.DotNet.Cli
{
internal static class CommandDirectoryContext
Comment thread
peterhuene marked this conversation as resolved.
Resolved
{
[ThreadStatic]
private static string _basePath;

/// <summary>
/// Expands a path similar to Path.GetFullPath() but gives unit tests a hook to inject an overwrite to the
/// base path.
/// </summary>
/// <param name="path">A relative or absolute path specifier</param>
/// <returns>The full path to the target</returns>
public static string GetFullPath(string path)
=> _basePath != null
? Path.GetFullPath(path, _basePath)
: Path.GetFullPath(path);

/// <summary>
/// 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.
/// </summary>
/// <param name="basePath">Directory to be used as base path instead of the current working directory.</param>
/// <param name="action">Action to be executed with the overwritten directory in place.</param>
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;
}
}
}
}
4 changes: 2 additions & 2 deletions src/dotnet/commands/dotnet-build/BuildCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -40,4 +40,4 @@ public static Command Build() =>
CommonOptions.NoRestoreOption(),
CommonOptions.VerbosityOption());
}
}
}
4 changes: 2 additions & 2 deletions src/dotnet/commands/dotnet-clean/CleanCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
4 changes: 2 additions & 2 deletions src/dotnet/commands/dotnet-pack/PackCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -46,4 +46,4 @@ public static Command Pack() =>
CommonOptions.NoRestoreOption(),
CommonOptions.VerbosityOption());
}
}
}
5 changes: 3 additions & 2 deletions src/dotnet/commands/dotnet-publish/PublishCommandParser.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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),
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet/commands/dotnet-restore/RestoreCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions src/dotnet/commands/dotnet-store/StoreCommandParser.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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))}"
};
}
})),
Expand All @@ -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,
Expand All @@ -73,4 +74,4 @@ public static Command Store() =>
.ForwardAs("-property:CreateProfilingSymbols=false")),
CommonOptions.VerbosityOption());
}
}
}
10 changes: 5 additions & 5 deletions src/dotnet/commands/dotnet-test/TestCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions test/Microsoft.DotNet.Tools.Tests.Utilities/TestPathUtilities.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading