Convert -ChildPath parameter to string[] for Join-Path cmdlet (#24677)

This commit is contained in:
Armaan Mcleod
2025-03-18 09:44:06 +05:00
committed by GitHub
parent 377f7e9867
commit 48c34fc3a7
2 changed files with 45 additions and 2 deletions
@@ -32,7 +32,8 @@ namespace Microsoft.PowerShell.Commands
[Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true)]
[AllowNull]
[AllowEmptyString]
public string ChildPath { get; set; }
[AllowEmptyCollection]
public string[] ChildPath { get; set; }
/// <summary>
/// Gets or sets additional childPaths to the command.
@@ -64,7 +65,15 @@ namespace Microsoft.PowerShell.Commands
Path != null,
"Since Path is a mandatory parameter, paths should never be null");
string combinedChildPath = ChildPath;
string combinedChildPath = string.Empty;
if (this.ChildPath != null)
{
foreach (string childPath in this.ChildPath)
{
combinedChildPath = SessionState.Path.Combine(combinedChildPath, childPath, CmdletProviderContext);
}
}
// join the ChildPath elements
if (AdditionalChildPath != null)
@@ -50,4 +50,38 @@ Describe "Join-Path cmdlet tests" -Tags "CI" {
$result.Count | Should -Be 1
$result | Should -BeExactly "one${sepChar}two${sepChar}three${sepChar}four${sepChar}five"
}
It "Join-Path -Path <Path> -ChildPath <ChildPath> should return '<ExpectedResult>'" -TestCases @(
@{
Path = 'one'
ChildPath = 'two', 'three'
ExpectedResult = "one${sepChar}two${sepChar}three"
}
@{
Path = 'one', 'two'
ChildPath = 'three', 'four'
ExpectedResult = @(
"one${sepChar}three${sepChar}four"
"two${sepChar}three${sepChar}four"
)
}
@{
Path = 'one'
ChildPath = @()
ExpectedResult = "one${sepChar}"
}
@{
Path = 'one'
ChildPath = $null
ExpectedResult = "one${sepChar}"
}
@{
Path = 'one'
ChildPath = [string]::Empty
ExpectedResult = "one${sepChar}"
}
) {
param($Path, $ChildPath, $ExpectedResult)
$result = Join-Path -Path $Path -ChildPath $ChildPath
$result | Should -BeExactly $ExpectedResult
}
}