From 220eaa1ba7d58daf7986bd87abdbd3c7f9e050c9 Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Sat, 11 Mar 2017 07:50:23 +0100 Subject: [PATCH] Adding -Extension and -LeafBase switches to Split-Path (#2721) * Refactoring ParsePathCommand.cs (SplitPathCommand) for readability - Using auto properties when no when there is no logic in getter/setter - Removing unused code - Removing redundant qualifiers - Removing Redundant initializers * Add -Extension and -Leafbase switches to Split-Path cmdlet - Extension and LeafBase are specializations of Leaf, and uses System.IO.Path.GetExtension and System.IO.Path.GetFilenameWithoutExtension to extract parts from the Leaf * Adding tests for Split-Path -LeafBase and Split-Path -Extension --- .../commands/management/ParsePathCommand.cs | 206 ++++++------------ .../FileSystem.Tests.ps1 | 17 ++ 2 files changed, 83 insertions(+), 140 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs index 7c62d450b..a1f29117d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs @@ -16,12 +16,14 @@ namespace Microsoft.PowerShell.Commands /// MSH paths that match the glob strings. /// [Cmdlet(VerbsCommon.Split, "Path", DefaultParameterSetName = "ParentSet", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113404")] - [OutputType(typeof(string), ParameterSetName = new string[] { SplitPathCommand.leafSet, - SplitPathCommand.noQualifierSet, - SplitPathCommand.parentSet, - SplitPathCommand.qualifierSet, - SplitPathCommand.literalPathSet})] - [OutputType(typeof(bool), ParameterSetName = new string[] { SplitPathCommand.isAbsoluteSet })] + [OutputType(typeof(string), ParameterSetName = new[] { leafSet, + leafBaseSet, + extensionSet, + noQualifierSet, + parentSet, + qualifierSet, + literalPathSet})] + [OutputType(typeof(bool), ParameterSetName = new[] { isAbsoluteSet })] public class SplitPathCommand : CoreCommandWithCredentialsBase { #region Parameters @@ -36,6 +38,17 @@ namespace Microsoft.PowerShell.Commands /// private const string leafSet = "LeafSet"; + /// + /// The parameter set name to get the leaf base name + /// + private const string leafBaseSet = "LeafBaseSet"; + + /// + /// The parameter set name to get the extension + /// + private const string extensionSet = "ExtensionSet"; + + /// /// The parameter set name to get the qualifier set. /// @@ -61,21 +74,12 @@ namespace Microsoft.PowerShell.Commands /// [Parameter(Position = 0, ParameterSetName = parentSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] [Parameter(Position = 0, ParameterSetName = leafSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] + [Parameter(Position = 0, ParameterSetName = leafBaseSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] + [Parameter(Position = 0, ParameterSetName = extensionSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] [Parameter(Position = 0, ParameterSetName = qualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] [Parameter(Position = 0, ParameterSetName = noQualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] [Parameter(Position = 0, ParameterSetName = isAbsoluteSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] - public string[] Path - { - get - { - return _paths; - } // get - - set - { - _paths = value; - } // set - } // Path + public string[] Path { get; set; } /// /// Gets or sets the literal path parameter to the command @@ -86,13 +90,13 @@ namespace Microsoft.PowerShell.Commands { get { - return _paths; + return Path; } // get set { base.SuppressWildcardExpansion = true; - _paths = value; + Path = value; } // set } // LiteralPath @@ -107,18 +111,7 @@ namespace Microsoft.PowerShell.Commands /// /// [Parameter(Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = qualifierSet, Mandatory = false)] - public SwitchParameter Qualifier - { - get - { - return _qualifier; - } // get - - set - { - _qualifier = value; - } //set - } // Qualifier + public SwitchParameter Qualifier { get; set; } /// /// Determines if the qualifier should be returned @@ -131,19 +124,7 @@ namespace Microsoft.PowerShell.Commands /// /// [Parameter(ParameterSetName = noQualifierSet, Mandatory = false, ValueFromPipelineByPropertyName = true)] - public SwitchParameter NoQualifier - { - get - { - return _noqualifier; - } // get - - set - { - _noqualifier = value; - } //set - } // NoQualifier - + public SwitchParameter NoQualifier { get; set; } /// /// Determines if the parent path should be returned @@ -154,18 +135,7 @@ namespace Microsoft.PowerShell.Commands /// /// [Parameter(ParameterSetName = parentSet, Mandatory = false, ValueFromPipelineByPropertyName = true)] - public SwitchParameter Parent - { - get - { - return _parent; - } // get - - set - { - _parent = value; - } //set - } // Parent + public SwitchParameter Parent { get; set; } = true; /// /// Determines if the leaf name should be returned @@ -176,98 +146,47 @@ namespace Microsoft.PowerShell.Commands /// /// [Parameter(ParameterSetName = leafSet, Mandatory = false, ValueFromPipelineByPropertyName = true)] - public SwitchParameter Leaf - { - get - { - return _leaf; - } // get + public SwitchParameter Leaf { get; set; } - set - { - _leaf = value; - } //set - } // Leaf + /// + /// Determines if the leaf base name (name without extension) should be returned + /// + /// + /// + /// If true the leaf base name of the path will be returned. + /// + /// + [Parameter(ParameterSetName = leafBaseSet, Mandatory = false, ValueFromPipelineByPropertyName = true)] + public SwitchParameter LeafBase { get; set; } + + /// + /// Determines if the extension should be returned + /// + /// + /// + /// If true the extension of the path will be returned. + /// + /// + [Parameter(ParameterSetName = extensionSet, Mandatory = false, ValueFromPipelineByPropertyName = true)] + public SwitchParameter Extension { get; set; } /// /// Determines if the path should be resolved before being parsed. /// /// [Parameter] - public SwitchParameter Resolve - { - get - { - return _resolve; - } // get - - set - { - _resolve = value; - } //set - } // Resolve + public SwitchParameter Resolve { get; set; } /// /// Determines if the path is an absolute path. /// - /// [Parameter(ParameterSetName = isAbsoluteSet)] - public SwitchParameter IsAbsolute - { - get - { - return _isAbsolute; - } // get + public SwitchParameter IsAbsolute { get; set; } - set - { - _isAbsolute = value; - } //set - } #endregion Parameters #region parameter data - /// - /// The path to resolve - /// - private string[] _paths; - - /// - /// Determines if the qualifier of the path should be returned. - /// The qualifier is either the drive name or provider name that - /// is qualifying the path. - /// - private bool _qualifier; - - /// - /// Determines if the qualifier of the path should be returned. - /// If false, the qualifier will be returned. If true, it will - /// be stripped from the path. - /// The qualifier is either the drive name or provider name that - /// is qualifying the path. - /// - private bool _noqualifier; - - /// - /// Determines if the parent path of the specified path should be returned. - /// - private bool _parent = true; - - /// - /// Determines if the leaf name of the specified path should be returned. - /// - private bool _leaf; - - /// - /// Determines if the path(s) should be resolved before being parsed. - /// - private bool _resolve; - - /// - /// Determines if the path(s) are absolute paths. - /// - private bool _isAbsolute; #endregion parameter data @@ -281,15 +200,15 @@ namespace Microsoft.PowerShell.Commands { StringCollection pathsToParse = new StringCollection(); - if (_resolve) + if (Resolve) { CmdletProviderContext currentContext = CmdletProviderContext; - foreach (string path in _paths) + foreach (string path in Path) { // resolve the paths and then parse each one. - Collection resolvedPaths = null; + Collection resolvedPaths; try { @@ -387,7 +306,7 @@ namespace Microsoft.PowerShell.Commands switch (ParameterSetName) { case isAbsoluteSet: - string ignored = null; + string ignored; bool isPathAbsolute = SessionState.Path.IsPSAbsolute(pathsToParse[index], out ignored); @@ -436,10 +355,6 @@ namespace Microsoft.PowerShell.Commands case parentSet: case literalPathSet: - bool pathStartsWithRoot = - pathsToParse[index].StartsWith("\\", StringComparison.CurrentCulture) || - pathsToParse[index].StartsWith("/", StringComparison.CurrentCulture); - try { result = @@ -462,13 +377,24 @@ namespace Microsoft.PowerShell.Commands break; case leafSet: + case leafBaseSet: + case extensionSet: try { + // default handles leafSet result = SessionState.Path.ParseChildName( pathsToParse[index], CmdletProviderContext, true); + if (LeafBase) + { + result = System.IO.Path.GetFileNameWithoutExtension(result); + } + else if (Extension) + { + result = System.IO.Path.GetExtension(result); + } } catch (PSNotSupportedException) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 9c88d72bf..748fb1831 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -569,6 +569,7 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur $level1_0 = "Level1_0" $level2_0 = "Level2_0" $level2_1 = "Level2_1" + $fileExt = ".ext" $root = Join-Path "TestDrive:" "" #adds correct / or \ $level1_0Full = Join-Path $root $level1_0 $level2_0Full = Join-Path $level1_0Full $level2_0 @@ -628,6 +629,22 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur $result = Split-Path -Path $level1_0Full -Leaf $result | Should Be $level1_0 } + + It 'Validate LeafBase' { + $result = Split-Path -Path "$level2_1Full$fileExt" -LeafBase + $result | Should Be $level2_1 + } + + It 'Validate LeafBase is not over-zealous' { + + $result = Split-Path -Path "$level2_1Full$fileExt$fileExt" -LeafBase + $result | Should Be "$level2_1$fileExt" + } + + It 'Validate LeafBase' { + $result = Split-Path -Path "$level2_1Full$fileExt" -Extension + $result | Should Be $fileExt + } It "Validate NoQualifier" { $result = Split-Path -Path $level1_0Full -NoQualifier