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 @@ -23,7 +23,13 @@ namespace Microsoft.PowerShell.Commands
parentSet,
qualifierSet,
literalPathSet})]
[OutputType(typeof(string), ParameterSetName = new[] { LiteralPathLeafSet })]
[OutputType(typeof(string), ParameterSetName = new[] { LiteralPathLeafBaseSet })]
[OutputType(typeof(string), ParameterSetName = new[] { LiteralPathExtensionSet })]
[OutputType(typeof(string), ParameterSetName = new[] { LiteralPathNoQualifierSet })]
[OutputType(typeof(string), ParameterSetName = new[] { LiteralPathQualifierSet })]
[OutputType(typeof(bool), ParameterSetName = new[] { isAbsoluteSet })]
[OutputType(typeof(bool), ParameterSetName = new[] { LiteralPathIsAbsoluteSet })]
public class SplitPathCommand : CoreCommandWithCredentialsBase
{
#region Parameters
Expand Down Expand Up @@ -68,6 +74,36 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase
/// </summary>
private const string literalPathSet = "LiteralPathSet";

/// <summary>
/// The parameter set name to get the leaf name for LiteralPath.
/// </summary>
private const string LiteralPathLeafSet = "LiteralPathLeafSet";

/// <summary>
/// The parameter set name to get the leaf base name for LiteralPath.
/// </summary>
private const string LiteralPathLeafBaseSet = "LiteralPathLeafBaseSet";

/// <summary>
/// The parameter set name to get the extension for LiteralPath.
/// </summary>
private const string LiteralPathExtensionSet = "LiteralPathExtensionSet";

/// <summary>
/// The parameter set name to get the qualifier for LiteralPath.
/// </summary>
private const string LiteralPathQualifierSet = "LiteralPathQualifierSet";

/// <summary>
/// The parameter set name to return the path without the qualifier for LiteralPath.
/// </summary>
private const string LiteralPathNoQualifierSet = "LiteralPathNoQualifierSet";

/// <summary>
/// The parameter set name to get the IsAbsolute set for LiteralPath.
/// </summary>
private const string LiteralPathIsAbsoluteSet = "LiteralPathIsAbsoluteSet";

/// <summary>
/// Gets or sets the path parameter to the command.
/// </summary>
Expand All @@ -83,7 +119,13 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase
/// <summary>
/// Gets or sets the literal path parameter to the command.
/// </summary>
[Parameter(ParameterSetName = "LiteralPathSet", Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = literalPathSet, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathLeafSet, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathLeafBaseSet, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathExtensionSet, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathQualifierSet, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathNoQualifierSet, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathIsAbsoluteSet, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Alias("PSPath", "LP")]
public string[] LiteralPath
{
Expand All @@ -108,6 +150,7 @@ public string[] LiteralPath
/// the PowerShell path.
/// </value>
[Parameter(ParameterSetName = qualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathQualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Qualifier { get; set; }

/// <summary>
Expand All @@ -119,6 +162,7 @@ public string[] LiteralPath
/// the PowerShell path.
/// </value>
[Parameter(ParameterSetName = noQualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathNoQualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter NoQualifier { get; set; }

/// <summary>
Expand All @@ -128,6 +172,7 @@ public string[] LiteralPath
/// If true the parent of the path will be returned.
/// </value>
[Parameter(ParameterSetName = parentSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = literalPathSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Parent { get; set; } = true;

/// <summary>
Expand All @@ -137,6 +182,7 @@ public string[] LiteralPath
/// If true the leaf name of the path will be returned.
/// </value>
[Parameter(ParameterSetName = leafSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathLeafSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Leaf { get; set; }

/// <summary>
Expand All @@ -146,6 +192,7 @@ public string[] LiteralPath
/// If true the leaf base name of the path will be returned.
/// </value>
[Parameter(ParameterSetName = leafBaseSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathLeafBaseSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter LeafBase { get; set; }

/// <summary>
Expand All @@ -155,6 +202,7 @@ public string[] LiteralPath
/// If true the extension of the path will be returned.
/// </value>
[Parameter(ParameterSetName = extensionSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = LiteralPathExtensionSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Extension { get; set; }

/// <summary>
Expand All @@ -168,6 +216,7 @@ public string[] LiteralPath
/// Determines if the path is an absolute path.
/// </summary>
[Parameter(ParameterSetName = isAbsoluteSet, Mandatory = true)]
[Parameter(ParameterSetName = LiteralPathIsAbsoluteSet, Mandatory = true)]
public SwitchParameter IsAbsolute { get; set; }

#endregion Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ Describe "Split-Path" -Tags "CI" {
Split-Path -Parent "\\server1\share1" | Should -BeExactly "${dirSep}${dirSep}server1"
}

It "Should support parsing switches with LiteralPath" {
$literalDir = Join-Path -Path $TestDrive -ChildPath 'parent[dir]'
$literalPath = Join-Path -Path $literalDir -ChildPath 'child[file].txt'
[void][System.IO.Directory]::CreateDirectory($literalDir)
[System.IO.File]::WriteAllText($literalPath, '')

Split-Path -LiteralPath $literalPath -Parent | Should -BeExactly $literalDir
Split-Path -LiteralPath $literalPath -Leaf | Should -BeExactly 'child[file].txt'
Split-Path -LiteralPath $literalPath -LeafBase | Should -BeExactly 'child[file]'
Split-Path -LiteralPath $literalPath -Extension | Should -BeExactly '.txt'
Split-Path -LiteralPath $literalPath -IsAbsolute | Should -BeTrue
Split-Path -LiteralPath env:PATH -Qualifier | Should -BeExactly 'env:'
Split-Path -LiteralPath env:PATH -NoQualifier | Should -BeExactly 'PATH'
}

It "Should reject conflicting parsing switches with LiteralPath" {
{ Split-Path -LiteralPath TestDrive:\file.txt -Parent -Leaf -ErrorAction Stop } |
Should -Throw -ErrorId 'AmbiguousParameterSet,Microsoft.PowerShell.Commands.SplitPathCommand'
}

It 'Does not split a drive letter' {
Split-Path -Path 'C:\' | Should -BeNullOrEmpty
}
Expand Down