diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs index ca616301ebb..5a7a82a2b4f 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs @@ -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 @@ -68,6 +74,36 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase /// private const string literalPathSet = "LiteralPathSet"; + /// + /// The parameter set name to get the leaf name for LiteralPath. + /// + private const string LiteralPathLeafSet = "LiteralPathLeafSet"; + + /// + /// The parameter set name to get the leaf base name for LiteralPath. + /// + private const string LiteralPathLeafBaseSet = "LiteralPathLeafBaseSet"; + + /// + /// The parameter set name to get the extension for LiteralPath. + /// + private const string LiteralPathExtensionSet = "LiteralPathExtensionSet"; + + /// + /// The parameter set name to get the qualifier for LiteralPath. + /// + private const string LiteralPathQualifierSet = "LiteralPathQualifierSet"; + + /// + /// The parameter set name to return the path without the qualifier for LiteralPath. + /// + private const string LiteralPathNoQualifierSet = "LiteralPathNoQualifierSet"; + + /// + /// The parameter set name to get the IsAbsolute set for LiteralPath. + /// + private const string LiteralPathIsAbsoluteSet = "LiteralPathIsAbsoluteSet"; + /// /// Gets or sets the path parameter to the command. /// @@ -83,7 +119,13 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase /// /// Gets or sets the literal path parameter to the command. /// - [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 { @@ -108,6 +150,7 @@ public string[] LiteralPath /// the PowerShell path. /// [Parameter(ParameterSetName = qualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] + [Parameter(ParameterSetName = LiteralPathQualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] public SwitchParameter Qualifier { get; set; } /// @@ -119,6 +162,7 @@ public string[] LiteralPath /// the PowerShell path. /// [Parameter(ParameterSetName = noQualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] + [Parameter(ParameterSetName = LiteralPathNoQualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] public SwitchParameter NoQualifier { get; set; } /// @@ -128,6 +172,7 @@ public string[] LiteralPath /// If true the parent of the path will be returned. /// [Parameter(ParameterSetName = parentSet, Mandatory = false, ValueFromPipelineByPropertyName = true)] + [Parameter(ParameterSetName = literalPathSet, Mandatory = false, ValueFromPipelineByPropertyName = true)] public SwitchParameter Parent { get; set; } = true; /// @@ -137,6 +182,7 @@ public string[] LiteralPath /// If true the leaf name of the path will be returned. /// [Parameter(ParameterSetName = leafSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] + [Parameter(ParameterSetName = LiteralPathLeafSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] public SwitchParameter Leaf { get; set; } /// @@ -146,6 +192,7 @@ public string[] LiteralPath /// If true the leaf base name of the path will be returned. /// [Parameter(ParameterSetName = leafBaseSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] + [Parameter(ParameterSetName = LiteralPathLeafBaseSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] public SwitchParameter LeafBase { get; set; } /// @@ -155,6 +202,7 @@ public string[] LiteralPath /// If true the extension of the path will be returned. /// [Parameter(ParameterSetName = extensionSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] + [Parameter(ParameterSetName = LiteralPathExtensionSet, Mandatory = true, ValueFromPipelineByPropertyName = true)] public SwitchParameter Extension { get; set; } /// @@ -168,6 +216,7 @@ public string[] LiteralPath /// Determines if the path is an absolute path. /// [Parameter(ParameterSetName = isAbsoluteSet, Mandatory = true)] + [Parameter(ParameterSetName = LiteralPathIsAbsoluteSet, Mandatory = true)] public SwitchParameter IsAbsolute { get; set; } #endregion Parameters diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Split-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Split-Path.Tests.ps1 index e34126b101f..333289021c6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Split-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Split-Path.Tests.ps1 @@ -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 }