Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,37 @@ public SwitchParameter Relative
private SwitchParameter _relative;

/// <summary>
/// Gets or sets the path the resolved relative path should be based off.
/// Gets or sets the base path the resolved path should be based off.
/// </summary>
[Parameter]
[Alias("RelativeBasePath")]
public string BasePath
Comment thread
soroshsabz marked this conversation as resolved.
Resolved
{
get
{
return _basePath;
}

set
{
_basePath = value;
}
}

/// <summary>
/// Gets or sets the base path the resolved path should be based off.
/// This property is kept for backwards compatibility; use <see cref="BasePath"/> instead.
/// </summary>
public string RelativeBasePath
{
get
{
return _relativeBasePath;
return BasePath;
}

set
{
_relativeBasePath = value;
BasePath = value;
}
}

Expand All @@ -113,7 +131,7 @@ public override SwitchParameter Force
private string[] _paths;

private PSDriveInfo _relativeDrive;
private string _relativeBasePath;
private string _basePath;

#endregion parameter data

Expand All @@ -138,11 +156,11 @@ private static bool StartsWithRelativePathToken(string path, ProviderInfo provid
/// </summary>
protected override void BeginProcessing()
{
if (!string.IsNullOrEmpty(RelativeBasePath))
if (!string.IsNullOrEmpty(BasePath))
{
try
{
_relativeBasePath = SessionState.Internal.Globber.GetProviderPath(RelativeBasePath, CmdletProviderContext, out _, out _relativeDrive);
_basePath = SessionState.Internal.Globber.GetProviderPath(BasePath, CmdletProviderContext, out _, out _relativeDrive);
}
catch (ProviderNotFoundException providerNotFound)
{
Expand All @@ -168,20 +186,20 @@ protected override void BeginProcessing()
catch (NotSupportedException notSupported)
{
ThrowTerminatingError(
new ErrorRecord(notSupported, "ProviderIsNotNavigationCmdletProvider", ErrorCategory.InvalidArgument, RelativeBasePath));
new ErrorRecord(notSupported, "ProviderIsNotNavigationCmdletProvider", ErrorCategory.InvalidArgument, BasePath));
}
catch (InvalidOperationException invalidOperation)
{
ThrowTerminatingError(
new ErrorRecord(invalidOperation, "InvalidHomeLocation", ErrorCategory.InvalidOperation, RelativeBasePath));
new ErrorRecord(invalidOperation, "InvalidHomeLocation", ErrorCategory.InvalidOperation, BasePath));
}

return;
}
else if (_relative)
{
_relativeDrive = SessionState.Path.CurrentLocation.Drive;
_relativeBasePath = SessionState.Path.CurrentLocation.ProviderPath;
_basePath = SessionState.Path.CurrentLocation.ProviderPath;
}
}

Expand All @@ -196,14 +214,14 @@ protected override void ProcessRecord()
Collection<PathInfo> result = null;
try
{
if (MyInvocation.BoundParameters.ContainsKey("RelativeBasePath"))
if (MyInvocation.BoundParameters.ContainsKey("BasePath"))
{
// Pushing and popping the location is done because GetResolvedPSPathFromPSPath uses the current path to resolve relative paths.
// It's important that we pop the location before writing an object to the pipeline to avoid affecting downstream commands.
try
{
SessionState.Path.PushCurrentLocation(string.Empty);
_ = SessionState.Path.SetLocation(_relativeBasePath);
_ = SessionState.Path.SetLocation(_basePath);
result = SessionState.Path.GetResolvedPSPathFromPSPath(path, CmdletProviderContext);
}
finally
Expand Down Expand Up @@ -241,7 +259,7 @@ protected override void ProcessRecord()
}

baseCache = basePath;
string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path, _relativeBasePath);
string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path, _basePath);

// Do not insert './' if result path is not relative
if (!adjustedPath.StartsWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4657,7 +4657,7 @@ internal static IEnumerable<CompletionResult> CompleteFilename(CompletionContext
&& lastAst.Extent.File is not null)
{
relativeBasePath = Directory.GetParent(lastAst.Extent.File).FullName;
_ = currentPS.AddParameter("RelativeBasePath", relativeBasePath);
_ = currentPS.AddParameter("BasePath", relativeBasePath);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Describe "Resolve-Path returns proper path" -Tag "CI" {
}
}

It 'Resolve-Path RelativeBasePath should handle <Scenario>' -TestCases @(
It 'Resolve-Path BasePath should handle <Scenario>' -TestCases @(
@{
Scenario = "Absolute Path, Absolute ReleativeBasePath"
Path = $root
Expand Down Expand Up @@ -133,8 +133,8 @@ Describe "Resolve-Path returns proper path" -Tag "CI" {

if ($null -eq $Expected)
{
{Resolve-Path -Path $Path -RelativeBasePath $BasePath -ErrorAction Stop} | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand"
{Resolve-Path -Path $Path -RelativeBasePath $BasePath -ErrorAction Stop -Relative} | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand"
{Resolve-Path -Path $Path -BasePath $BasePath -ErrorAction Stop} | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand"
{Resolve-Path -Path $Path -BasePath $BasePath -ErrorAction Stop -Relative} | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand"
}
else
{
Expand All @@ -146,8 +146,8 @@ Describe "Resolve-Path returns proper path" -Tag "CI" {
Set-Location $CD
}

(Resolve-Path -Path $Path -RelativeBasePath $BasePath).ProviderPath | Should -BeExactly $Expected[0]
Resolve-Path -Path $Path -RelativeBasePath $BasePath -Relative | Should -BeExactly $Expected[1]
(Resolve-Path -Path $Path -BasePath $BasePath).ProviderPath | Should -BeExactly $Expected[0]
Resolve-Path -Path $Path -BasePath $BasePath -Relative | Should -BeExactly $Expected[1]
}
finally
{
Expand All @@ -159,7 +159,7 @@ Describe "Resolve-Path returns proper path" -Tag "CI" {
}
}

It "Resolve-Path -Path '<Path>' -RelativeBasePath '<BasePath>' -Force:<Force> should return '<ExpectedResult>'" -TestCases @(
It "Resolve-Path -Path '<Path>' -BasePath '<BasePath>' -Force:<Force> should return '<ExpectedResult>'" -TestCases @(
@{
Path = $relativeHiddenFilePath1
BasePath = $TestDrive
Expand Down Expand Up @@ -198,6 +198,11 @@ Describe "Resolve-Path returns proper path" -Tag "CI" {
}
) {
param($Path, $BasePath, $Force, $ExpectedResult)
(Resolve-Path -Path $Path -RelativeBasePath $BasePath -Force:$Force).Path | Should -BeExactly $ExpectedResult
(Resolve-Path -Path $Path -BasePath $BasePath -Force:$Force).Path | Should -BeExactly $ExpectedResult
}

It "Resolve-Path supports the '-RelativeBasePath' alias for '-BasePath'" {
$expected = (Resolve-Path -Path $relativeHiddenFilePath1 -BasePath $TestDrive).Path
(Resolve-Path -Path $relativeHiddenFilePath1 -RelativeBasePath $TestDrive).Path | Should -BeExactly $expected
}
}