8000 Adding -Extension and -LeafBase switches to Split-Path by powercode · Pull Request #2721 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Adding -Extension and -LeafBase switches to Split-Path #2721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2017
Merged
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
10000 9E81
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ namespace Microsoft.PowerShell.Commands
/// MSH paths that match the glob strings.
/// </summary>
[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
Expand All @@ -36,6 +38,17 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase
/// </summary>
private const string leafSet = "LeafSet";

/// <summary>
/// The parameter set name to get the leaf base name
/// </summary>
private const string leafBaseSet = "LeafBaseSet";

/// <summary>
/// The parameter set name to get the extension
/// </summary>
private const string extensionSet = "ExtensionSet";


/// <summary>
/// The parameter set name to get the qualifier set.
/// </summary>
Expand All @@ -61,21 +74,12 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase
/// </summary>
[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; }

/// <summary>
/// Gets or sets the literal path parameter to the command
Expand All @@ -86,13 +90,13 @@ public string[] LiteralPath
{
get
{
return _paths;
return Path;
} // get

set
{
base.SuppressWildcardExpansion = true;
_paths = value;
Path = value;
} // set
} // LiteralPath

Expand All @@ -107,18 +111,7 @@ public string[] LiteralPath
/// </value>
///
[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; }

/// <summary>
/// Determines if the qualifier should be returned
Expand All @@ -131,19 +124,7 @@ public SwitchParameter Qualifier
/// </value>
///
[Parameter(ParameterSetName = noQualifierSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter NoQualifier
{
get
{
return _noqualifier;
} // get

set
{
_noqualifier = value;
} //set
} // NoQualifier

public SwitchParameter NoQualifier { get; set; }

/// <summary>
/// Determines if the parent path should be returned
Expand All @@ -154,18 +135,7 @@ public SwitchParameter NoQualifier
/// </value>
///
[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;

/// <summary>
/// Determines if the leaf name should be returned
Expand All @@ -176,98 +146,47 @@ public SwitchParameter Parent
/// </value>
///
[Parameter(ParameterSetName = leafSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Leaf
{
get
{
return _leaf;
} // get
public SwitchParameter Leaf { get; set; }

set
{
_leaf = value;
} //set
} // Leaf
/// <summary>
/// Determines if the leaf base name (name without extension) should be returned
/// </summary>
///
/// <value>
/// If true the leaf base name of the path will be returned.
/// </value>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value of _parent used to be true. The default for a switch is False. Please revisit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixing and rebasing.

///
[Parameter(ParameterSetName = leafBaseSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter LeafBase { get; set; }

/// <summary>
/// Determines if the extension should be returned
/// </summary>
///
/// <value>
/// If true the extension of the path will be returned.
/// </value>
///
[Parameter(ParameterSetName = extensionSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Extension { get; set; }

/// <summary>
/// Determines if the path should be resolved before being parsed.
/// </summary>
/// <value></value>
[Parameter]
public SwitchParameter Resolve
{
get
{
return _resolve;
} // get

set
{
_resolve = value;
} //set
} // Resolve
public SwitchParameter Resolve { get; set; }

/// <summary>
/// Determines if the path is an absolute path.
/// </summary>
///
[Parameter(ParameterSetName = isAbsoluteSet)]
public SwitchParameter IsAbsolute
{
get
{
return _isAbsolute;
} // get
public SwitchParameter IsAbsolute { get; set; }

set
{
_isAbsolute = value;
} //set
}
#endregion Parameters

#region parameter data

/// <summary>
/// The path to resolve
/// </summary>
private string[] _paths;

/// <summary>
/// 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.
/// </summary>
private bool _qualifier;

/// <summary>
/// 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.
/// </summary>
private bool _noqualifier;

/// <summary>
/// Determines if the parent path of the specified path should be returned.
/// </summary>
private bool _parent = true;

/// <summary>
/// Determines if the leaf name of the specified path should be returned.
/// </summary>
private bool _leaf;

/// <summary>
/// Determines if the path(s) should be resolved before being parsed.
/// </summary>
private bool _resolve;

/// <summary>
/// Determines if the path(s) are absolute paths.
/// </summary>
private bool _isAbsolute;

#endregion parameter data

Expand All @@ -281,15 +200,15 @@ protected override void ProcessRecord()
{
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<PathInfo> resolvedPaths = null;
Collection<PathInfo> resolvedPaths;

try
{
Expand Down Expand Up @@ -387,7 +306,7 @@ protected override void ProcessRecord()
switch (ParameterSetName)
{
case isAbsoluteSet:
string ignored = null;
string ignored;
bool isPathAbsolute =
SessionState.Path.IsPSAbsolute(pathsToParse[index], out ignored);

Expand Down Expand Up @@ -436,10 +355,6 @@ protected override void ProcessRecord()

case parentSet:
case literalPathSet:
bool pathStartsWithRoot =
pathsToParse[index].StartsWith("\\", StringComparison.CurrentCulture) ||
pathsToParse[index].StartsWith("/", StringComparison.CurrentCulture);

try
{
result =
Expand All @@ -462,13 +377,24 @@ protected override void ProcessRecord()
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,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
Expand Down Expand Up @@ -577,6 +578,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
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be explicit tests for things like:
"$level2_1Full$fileExt$fileExt"
to ensure that the trimming is not over-zealous?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.


It "Validate NoQualifier" {
$result = Split-Path -Path $level1_0Full -NoQualifier
Expand Down
0