Add support for multiple paths to Join-Path

Multiple child paths can be specified via the new positional parameter -AdditionalChildPath.
This commit is contained in:
Dave Wyatt
2016-08-31 13:48:31 -07:00
committed by Jason Shirk (POWERSHELL)
parent 77a37f7773
commit 22aac12c3e
2 changed files with 28 additions and 2 deletions
@@ -3,6 +3,7 @@ Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Text;
using System.Collections.ObjectModel;
using System.Management.Automation;
using Dbg = System.Management.Automation;
@@ -32,7 +33,16 @@ namespace Microsoft.PowerShell.Commands
[Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true)]
[AllowNull]
[AllowEmptyString]
public string ChildPath { get; set; } = String.Empty;
public string ChildPath { get; set; }
/// <summary>
/// Gets or sets additional childPaths to the command.
/// </summary>
[Parameter(Position = 2, Mandatory = false, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = true)]
[AllowNull]
[AllowEmptyString]
[AllowEmptyCollection]
public string[] AdditionalChildPath { get; set; } = Utils.EmptyArray<string>();
/// <summary>
/// Determines if the path should be resolved after being joined
@@ -55,6 +65,17 @@ namespace Microsoft.PowerShell.Commands
Path != null,
"Since Path is a mandatory parameter, paths should never be null");
string combinedChildPath = ChildPath;
// join the ChildPath elements
if (AdditionalChildPath != null)
{
foreach (string childPath in AdditionalChildPath)
{
combinedChildPath = SessionState.Path.Combine(combinedChildPath, childPath, CmdletProviderContext);
}
}
foreach (string path in Path)
{
// First join the path elements
@@ -64,7 +85,7 @@ namespace Microsoft.PowerShell.Commands
try
{
joinedPath =
SessionState.Path.Combine(path, ChildPath, CmdletProviderContext);
SessionState.Path.Combine(path, combinedChildPath, CmdletProviderContext);
}
catch (PSNotSupportedException notSupported)
{
@@ -43,4 +43,9 @@ Describe "Join-Path cmdlet tests" -Tags "CI" {
$result.Count | Should be 1
$result | Should BeExactly ("Env:"+$SepChar+"foo")
}
It "should be able to join multiple child paths passed by position with remaining arguments" {
$result = Join-Path one two three four five
$result.Count | Should Be 1
$result | Should BeExactly "one${sepChar}two${sepChar}three${sepChar}four${sepChar}five"
}
}