-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from rkeithhill/rkeithhill/fix-path-processing…
…-snippet Improvements to path processing snippets, added tests for these. Udpa…
- Loading branch information
Showing
6 changed files
with
318 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
New-Item -Path 'foo[1].txt' -Force | ||
|
||
. $PSScriptRoot\PathProcessingNonExistingPaths.ps1 | ||
Describe 'Verify Path Processing for Non-existing Paths Allowed Impl' { | ||
It 'Processes non-wildcard absolute path to non-existing file via -Path param' { | ||
New-File -Path $PSScriptRoot\ReadmeNew.md | Should Be "$PSScriptRoot\READMENew.md" | ||
} | ||
It 'Processes multiple absolute paths via -Path param' { | ||
New-File -Path $PSScriptRoot\Readme.md, $PSScriptRoot\XYZZY.ps1 | | ||
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\XYZZY.ps1") | ||
} | ||
It 'Processes relative path via -Path param' { | ||
New-File -Path ..\examples\READMENew.md | Should Be "$PSScriptRoot\READMENew.md" | ||
} | ||
It 'Processes multiple relative path via -Path param' { | ||
New-File -Path ..\examples\README.md, XYZZY.ps1 | | ||
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\XYZZY.ps1") | ||
} | ||
|
||
It 'Should accept pipeline input to Path' { | ||
Get-ChildItem -LiteralPath "$pwd\foo[1].txt" | New-File | Should Be "$PSScriptRoot\foo[1].txt" | ||
} | ||
} | ||
|
||
. $PSScriptRoot\PathProcessingNoWildcards.ps1 | ||
Describe 'Verify Path Processing for NO Wildcards Allowed Impl' { | ||
It 'Processes non-wildcard absolute path via -Path param' { | ||
Import-FileNoWildcard -Path $PSScriptRoot\Readme.md | Should Be "$PSScriptRoot\README.md" | ||
} | ||
It 'Processes multiple absolute paths via -Path param' { | ||
Import-FileNoWildcard -Path $PSScriptRoot\Readme.md, $PSScriptRoot\PathProcessingWildcards.ps1 | | ||
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\PathProcessingWildcards.ps1") | ||
} | ||
It 'Processes relative path via -Path param' { | ||
Import-FileNoWildcard -Path ..\examples\README.md | Should Be "$PSScriptRoot\README.md" | ||
} | ||
It 'Processes multiple relative path via -Path param' { | ||
Import-FileNoWildcard -Path ..\examples\README.md, .vscode\launch.json | | ||
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\.vscode\launch.json") | ||
} | ||
|
||
It 'Should accept pipeline input to Path' { | ||
Get-ChildItem -LiteralPath "$pwd\foo[1].txt" | Import-FileNoWildcard | Should Be "$PSScriptRoot\foo[1].txt" | ||
} | ||
} | ||
|
||
. $PSScriptRoot\PathProcessingWildcards.ps1 | ||
Describe 'Verify Path Processing for Wildcards Allowed Impl' { | ||
It 'Processes non-wildcard absolute path via -Path param' { | ||
Import-FileWildcard -Path $PSScriptRoot\Readme.md | Should Be "$PSScriptRoot\README.md" | ||
} | ||
It 'Processes multiple absolute paths via -Path param' { | ||
Import-FileWildcard -Path $PSScriptRoot\Readme.md, $PSScriptRoot\PathProcessingWildcards.ps1 | | ||
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\PathProcessingWildcards.ps1") | ||
} | ||
It 'Processes wildcard absolute path via -Path param' { | ||
Import-FileWildcard -Path $PSScriptRoot\*.md | Should Be "$PSScriptRoot\README.md" | ||
} | ||
It 'Processes wildcard relative path via -Path param' { | ||
Import-FileWildcard -Path *.md | Should Be "$PSScriptRoot\README.md" | ||
} | ||
It 'Processes relative path via -Path param' { | ||
Import-FileWildcard -Path ..\examples\README.md | Should Be "$PSScriptRoot\README.md" | ||
} | ||
It 'Processes multiple relative path via -Path param' { | ||
Import-FileWildcard -Path ..\examples\README.md, .vscode\launch.json | | ||
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\.vscode\launch.json") | ||
} | ||
|
||
It 'DefaultParameterSet should be Path' { | ||
Import-FileWildcard *.md | Should Be "$PSScriptRoot\README.md" | ||
} | ||
|
||
It 'Should process absolute literal paths via -LiteralPath param'{ | ||
Import-FileWildcard -LiteralPath "$PSScriptRoot\foo[1].txt" | Should Be "$PSScriptRoot\foo[1].txt" | ||
} | ||
It 'Should process relative literal paths via -LiteralPath param'{ | ||
Import-FileWildcard -LiteralPath "..\examples\foo[1].txt" | Should Be "$PSScriptRoot\foo[1].txt" | ||
} | ||
It 'Should process multiple literal paths via -LiteralPath param'{ | ||
Import-FileWildcard -LiteralPath "..\examples\foo[1].txt", "$PSScriptRoot\README.md" | | ||
Should Be @("$PSScriptRoot\foo[1].txt", "$PSScriptRoot\README.md") | ||
} | ||
|
||
It 'Should accept pipeline input to LiteralPath' { | ||
Get-ChildItem -LiteralPath "$pwd\foo[1].txt" | Import-FileWildcard | Should Be "$PSScriptRoot\foo[1].txt" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
function Import-FileNoWildcard { | ||
[CmdletBinding(SupportsShouldProcess=$true)] | ||
param( | ||
# Specifies a path to one or more locations. | ||
[Parameter(Mandatory=$true, | ||
Position=0, | ||
ParameterSetName="Path", | ||
ValueFromPipeline=$true, | ||
ValueFromPipelineByPropertyName=$true, | ||
HelpMessage="Path to one or more locations.")] | ||
[Alias("PSPath")] | ||
[ValidateNotNullOrEmpty()] | ||
[string[]] | ||
$Path | ||
) | ||
|
||
begin { | ||
} | ||
|
||
process { | ||
# Modify [CmdletBinding()] to [CmdletBinding(SupportsShouldProcess=$true)] | ||
$paths = @() | ||
foreach ($aPath in $Path) { | ||
if (!(Test-Path -LiteralPath $aPath)) { | ||
$ex = New-Object System.Management.Automation.ItemNotFoundException "Cannot find path '$aPath' because it does not exist." | ||
$category = [System.Management.Automation.ErrorCategory]::ObjectNotFound | ||
$errRecord = New-Object System.Management.Automation.ErrorRecord $ex,'PathNotFound',$category,$aPath | ||
$psCmdlet.WriteError($errRecord) | ||
continue | ||
} | ||
|
||
# Resolve any relative paths | ||
$paths += $psCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($aPath) | ||
} | ||
|
||
foreach ($aPath in $paths) { | ||
if ($pscmdlet.ShouldProcess($aPath, 'Operation')) { | ||
# Process each path | ||
$aPath | ||
} | ||
} | ||
} | ||
|
||
end { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
function New-File { | ||
[CmdletBinding(SupportsShouldProcess=$true)] | ||
param( | ||
# Specifies a path to one or more locations. | ||
[Parameter(Mandatory=$true, | ||
Position=0, | ||
ParameterSetName="Path", | ||
ValueFromPipeline=$true, | ||
ValueFromPipelineByPropertyName=$true, | ||
HelpMessage="Path to one or more locations.")] | ||
[Alias("PSPath")] | ||
[ValidateNotNullOrEmpty()] | ||
[string[]] | ||
$Path | ||
) | ||
|
||
begin { | ||
} | ||
|
||
process { | ||
# Modify [CmdletBinding()] to [CmdletBinding(SupportsShouldProcess=$true)] | ||
$paths = @() | ||
foreach ($aPath in $Path) { | ||
# Resolve any relative paths | ||
$paths += $psCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($aPath) | ||
} | ||
|
||
foreach ($aPath in $paths) { | ||
if ($pscmdlet.ShouldProcess($aPath, 'Operation')) { | ||
# Process each path | ||
$aPath | ||
} | ||
} | ||
} | ||
|
||
end { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
function Import-FileWildcard { | ||
[CmdletBinding(SupportsShouldProcess=$true, DefaultParameterSetName='Path')] | ||
param( | ||
# Specifies a path to one or more locations. Wildcards are permitted. | ||
[Parameter(Mandatory=$true, | ||
Position=0, | ||
ParameterSetName="Path", | ||
ValueFromPipeline=$true, | ||
ValueFromPipelineByPropertyName=$true, | ||
HelpMessage="Path to one or more locations.")] | ||
[ValidateNotNullOrEmpty()] | ||
[SupportsWildcards()] | ||
[string[]] | ||
$Path, | ||
|
||
# Specifies a path to one or more locations. Unlike the Path parameter, the value of the LiteralPath parameter is | ||
# used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, | ||
# enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any | ||
# characters as escape sequences. | ||
[Parameter(Mandatory=$true, | ||
Position=0, | ||
ParameterSetName="LiteralPath", | ||
ValueFromPipelineByPropertyName=$true, | ||
HelpMessage="Literal path to one or more locations.")] | ||
[Alias("PSPath")] | ||
[ValidateNotNullOrEmpty()] | ||
[string[]] | ||
$LiteralPath | ||
) | ||
|
||
begin { | ||
} | ||
|
||
process { | ||
# Modify [CmdletBinding()] to [CmdletBinding(SupportsShouldProcess=$true, DefaultParameterSetName='Path')] | ||
$paths = @() | ||
if ($psCmdlet.ParameterSetName -eq 'Path') { | ||
foreach ($aPath in $Path) { | ||
if (!(Test-Path -Path $aPath)) { | ||
$ex = New-Object System.Management.Automation.ItemNotFoundException "Cannot find path '$aPath' because it does not exist." | ||
$category = [System.Management.Automation.ErrorCategory]::ObjectNotFound | ||
$errRecord = New-Object System.Management.Automation.ErrorRecord $ex,'PathNotFound',$category,$aPath | ||
$psCmdlet.WriteError($errRecord) | ||
continue | ||
} | ||
|
||
# Resolve any wildcards that might be in the path | ||
$provider = $null | ||
$paths += $psCmdlet.SessionState.Path.GetResolvedProviderPathFromPSPath($aPath, [ref]$provider) | ||
} | ||
} | ||
else { | ||
foreach ($aPath in $LiteralPath) { | ||
if (!(Test-Path -LiteralPath $aPath)) { | ||
$ex = New-Object System.Management.Automation.ItemNotFoundException "Cannot find path '$aPath' because it does not exist." | ||
$category = [System.Management.Automation.ErrorCategory]::ObjectNotFound | ||
$errRecord = New-Object System.Management.Automation.ErrorRecord $ex,'PathNotFound',$category,$aPath | ||
$psCmdlet.WriteError($errRecord) | ||
continue | ||
} | ||
|
||
# Resolve any relative paths | ||
$paths += $psCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($aPath) | ||
} | ||
} | ||
|
||
foreach ($aPath in $paths) { | ||
if ($pscmdlet.ShouldProcess($aPath, 'Operation')) { | ||
# Process each path | ||
$aPath | ||
} | ||
} | ||
} | ||
|
||
end { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.