mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Convert -ChildPath parameter to string[] for Join-Path cmdlet (#24677)
This commit is contained in:
+11
-2
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user