Skip to content

Commit b32b396

Browse files
committed
Merge pull request #91 from rkeithhill/rkeithhill/fix-path-processing-snippet
Improvements to path processing snippets, added tests for these. Udpa…
2 parents 2bcc084 + 522249f commit b32b396

6 files changed

+318
-33
lines changed

Diff for: examples/PathProcessing.Tests.ps1

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
New-Item -Path 'foo[1].txt' -Force
2+
3+
. $PSScriptRoot\PathProcessingNonExistingPaths.ps1
4+
Describe 'Verify Path Processing for Non-existing Paths Allowed Impl' {
5+
It 'Processes non-wildcard absolute path to non-existing file via -Path param' {
6+
New-File -Path $PSScriptRoot\ReadmeNew.md | Should Be "$PSScriptRoot\READMENew.md"
7+
}
8+
It 'Processes multiple absolute paths via -Path param' {
9+
New-File -Path $PSScriptRoot\Readme.md, $PSScriptRoot\XYZZY.ps1 |
10+
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\XYZZY.ps1")
11+
}
12+
It 'Processes relative path via -Path param' {
13+
New-File -Path ..\examples\READMENew.md | Should Be "$PSScriptRoot\READMENew.md"
14+
}
15+
It 'Processes multiple relative path via -Path param' {
16+
New-File -Path ..\examples\README.md, XYZZY.ps1 |
17+
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\XYZZY.ps1")
18+
}
19+
20+
It 'Should accept pipeline input to Path' {
21+
Get-ChildItem -LiteralPath "$pwd\foo[1].txt" | New-File | Should Be "$PSScriptRoot\foo[1].txt"
22+
}
23+
}
24+
25+
. $PSScriptRoot\PathProcessingNoWildcards.ps1
26+
Describe 'Verify Path Processing for NO Wildcards Allowed Impl' {
27+
It 'Processes non-wildcard absolute path via -Path param' {
28+
Import-FileNoWildcard -Path $PSScriptRoot\Readme.md | Should Be "$PSScriptRoot\README.md"
29+
}
30+
It 'Processes multiple absolute paths via -Path param' {
31+
Import-FileNoWildcard -Path $PSScriptRoot\Readme.md, $PSScriptRoot\PathProcessingWildcards.ps1 |
32+
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\PathProcessingWildcards.ps1")
33+
}
34+
It 'Processes relative path via -Path param' {
35+
Import-FileNoWildcard -Path ..\examples\README.md | Should Be "$PSScriptRoot\README.md"
36+
}
37+
It 'Processes multiple relative path via -Path param' {
38+
Import-FileNoWildcard -Path ..\examples\README.md, .vscode\launch.json |
39+
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\.vscode\launch.json")
40+
}
41+
42+
It 'Should accept pipeline input to Path' {
43+
Get-ChildItem -LiteralPath "$pwd\foo[1].txt" | Import-FileNoWildcard | Should Be "$PSScriptRoot\foo[1].txt"
44+
}
45+
}
46+
47+
. $PSScriptRoot\PathProcessingWildcards.ps1
48+
Describe 'Verify Path Processing for Wildcards Allowed Impl' {
49+
It 'Processes non-wildcard absolute path via -Path param' {
50+
Import-FileWildcard -Path $PSScriptRoot\Readme.md | Should Be "$PSScriptRoot\README.md"
51+
}
52+
It 'Processes multiple absolute paths via -Path param' {
53+
Import-FileWildcard -Path $PSScriptRoot\Readme.md, $PSScriptRoot\PathProcessingWildcards.ps1 |
54+
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\PathProcessingWildcards.ps1")
55+
}
56+
It 'Processes wildcard absolute path via -Path param' {
57+
Import-FileWildcard -Path $PSScriptRoot\*.md | Should Be "$PSScriptRoot\README.md"
58+
}
59+
It 'Processes wildcard relative path via -Path param' {
60+
Import-FileWildcard -Path *.md | Should Be "$PSScriptRoot\README.md"
61+
}
62+
It 'Processes relative path via -Path param' {
63+
Import-FileWildcard -Path ..\examples\README.md | Should Be "$PSScriptRoot\README.md"
64+
}
65+
It 'Processes multiple relative path via -Path param' {
66+
Import-FileWildcard -Path ..\examples\README.md, .vscode\launch.json |
67+
Should Be @("$PSScriptRoot\README.md", "$PSScriptRoot\.vscode\launch.json")
68+
}
69+
70+
It 'DefaultParameterSet should be Path' {
71+
Import-FileWildcard *.md | Should Be "$PSScriptRoot\README.md"
72+
}
73+
74+
It 'Should process absolute literal paths via -LiteralPath param'{
75+
Import-FileWildcard -LiteralPath "$PSScriptRoot\foo[1].txt" | Should Be "$PSScriptRoot\foo[1].txt"
76+
}
77+
It 'Should process relative literal paths via -LiteralPath param'{
78+
Import-FileWildcard -LiteralPath "..\examples\foo[1].txt" | Should Be "$PSScriptRoot\foo[1].txt"
79+
}
80+
It 'Should process multiple literal paths via -LiteralPath param'{
81+
Import-FileWildcard -LiteralPath "..\examples\foo[1].txt", "$PSScriptRoot\README.md" |
82+
Should Be @("$PSScriptRoot\foo[1].txt", "$PSScriptRoot\README.md")
83+
}
84+
85+
It 'Should accept pipeline input to LiteralPath' {
86+
Get-ChildItem -LiteralPath "$pwd\foo[1].txt" | Import-FileWildcard | Should Be "$PSScriptRoot\foo[1].txt"
87+
}
88+
}

Diff for: examples/PathProcessingNoWildcards.ps1

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function Import-FileNoWildcard {
2+
[CmdletBinding(SupportsShouldProcess=$true)]
3+
param(
4+
# Specifies a path to one or more locations.
5+
[Parameter(Mandatory=$true,
6+
Position=0,
7+
ParameterSetName="Path",
8+
ValueFromPipeline=$true,
9+
ValueFromPipelineByPropertyName=$true,
10+
HelpMessage="Path to one or more locations.")]
11+
[Alias("PSPath")]
12+
[ValidateNotNullOrEmpty()]
13+
[string[]]
14+
$Path
15+
)
16+
17+
begin {
18+
}
19+
20+
process {
21+
# Modify [CmdletBinding()] to [CmdletBinding(SupportsShouldProcess=$true)]
22+
$paths = @()
23+
foreach ($aPath in $Path) {
24+
if (!(Test-Path -LiteralPath $aPath)) {
25+
$ex = New-Object System.Management.Automation.ItemNotFoundException "Cannot find path '$aPath' because it does not exist."
26+
$category = [System.Management.Automation.ErrorCategory]::ObjectNotFound
27+
$errRecord = New-Object System.Management.Automation.ErrorRecord $ex,'PathNotFound',$category,$aPath
28+
$psCmdlet.WriteError($errRecord)
29+
continue
30+
}
31+
32+
# Resolve any relative paths
33+
$paths += $psCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($aPath)
34+
}
35+
36+
foreach ($aPath in $paths) {
37+
if ($pscmdlet.ShouldProcess($aPath, 'Operation')) {
38+
# Process each path
39+
$aPath
40+
}
41+
}
42+
}
43+
44+
end {
45+
}
46+
}

Diff for: examples/PathProcessingNonExistingPaths.ps1

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function New-File {
2+
[CmdletBinding(SupportsShouldProcess=$true)]
3+
param(
4+
# Specifies a path to one or more locations.
5+
[Parameter(Mandatory=$true,
6+
Position=0,
7+
ParameterSetName="Path",
8+
ValueFromPipeline=$true,
9+
ValueFromPipelineByPropertyName=$true,
10+
HelpMessage="Path to one or more locations.")]
11+
[Alias("PSPath")]
12+
[ValidateNotNullOrEmpty()]
13+
[string[]]
14+
$Path
15+
)
16+
17+
begin {
18+
}
19+
20+
process {
21+
# Modify [CmdletBinding()] to [CmdletBinding(SupportsShouldProcess=$true)]
22+
$paths = @()
23+
foreach ($aPath in $Path) {
24+
# Resolve any relative paths
25+
$paths += $psCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($aPath)
26+
}
27+
28+
foreach ($aPath in $paths) {
29+
if ($pscmdlet.ShouldProcess($aPath, 'Operation')) {
30+
# Process each path
31+
$aPath
32+
}
33+
}
34+
}
35+
36+
end {
37+
}
38+
}

Diff for: examples/PathProcessingWildcards.ps1

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
function Import-FileWildcard {
2+
[CmdletBinding(SupportsShouldProcess=$true, DefaultParameterSetName='Path')]
3+
param(
4+
# Specifies a path to one or more locations. Wildcards are permitted.
5+
[Parameter(Mandatory=$true,
6+
Position=0,
7+
ParameterSetName="Path",
8+
ValueFromPipeline=$true,
9+
ValueFromPipelineByPropertyName=$true,
10+
HelpMessage="Path to one or more locations.")]
11+
[ValidateNotNullOrEmpty()]
12+
[SupportsWildcards()]
13+
[string[]]
14+
$Path,
15+
16+
# Specifies a path to one or more locations. Unlike the Path parameter, the value of the LiteralPath parameter is
17+
# used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters,
18+
# enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any
19+
# characters as escape sequences.
20+
[Parameter(Mandatory=$true,
21+
Position=0,
22+
ParameterSetName="LiteralPath",
23+
ValueFromPipelineByPropertyName=$true,
24+
HelpMessage="Literal path to one or more locations.")]
25+
[Alias("PSPath")]
26+
[ValidateNotNullOrEmpty()]
27+
[string[]]
28+
$LiteralPath
29+
)
30+
31+
begin {
32+
}
33+
34+
process {
35+
# Modify [CmdletBinding()] to [CmdletBinding(SupportsShouldProcess=$true, DefaultParameterSetName='Path')]
36+
$paths = @()
37+
if ($psCmdlet.ParameterSetName -eq 'Path') {
38+
foreach ($aPath in $Path) {
39+
if (!(Test-Path -Path $aPath)) {
40+
$ex = New-Object System.Management.Automation.ItemNotFoundException "Cannot find path '$aPath' because it does not exist."
41+
$category = [System.Management.Automation.ErrorCategory]::ObjectNotFound
42+
$errRecord = New-Object System.Management.Automation.ErrorRecord $ex,'PathNotFound',$category,$aPath
43+
$psCmdlet.WriteError($errRecord)
44+
continue
45+
}
46+
47+
# Resolve any wildcards that might be in the path
48+
$provider = $null
49+
$paths += $psCmdlet.SessionState.Path.GetResolvedProviderPathFromPSPath($aPath, [ref]$provider)
50+
}
51+
}
52+
else {
53+
foreach ($aPath in $LiteralPath) {
54+
if (!(Test-Path -LiteralPath $aPath)) {
55+
$ex = New-Object System.Management.Automation.ItemNotFoundException "Cannot find path '$aPath' because it does not exist."
56+
$category = [System.Management.Automation.ErrorCategory]::ObjectNotFound
57+
$errRecord = New-Object System.Management.Automation.ErrorRecord $ex,'PathNotFound',$category,$aPath
58+
$psCmdlet.WriteError($errRecord)
59+
continue
60+
}
61+
62+
# Resolve any relative paths
63+
$paths += $psCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($aPath)
64+
}
65+
}
66+
67+
foreach ($aPath in $paths) {
68+
if ($pscmdlet.ShouldProcess($aPath, 'Operation')) {
69+
# Process each path
70+
$aPath
71+
}
72+
}
73+
}
74+
75+
end {
76+
}
77+
}

Diff for: examples/README.md

+34-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,40 @@ Try these steps:
4949
9. Observe that every time the breakpoint is hit, the watch variables get updated.
5050
10. When you're done debugging, click the red **Stop** button or press `Shift+F5`
5151

52-
If you would like to debug a different script, you will need to edit the
53-
`.vscode\launch.json` file and change the `program` parameter to point to
54-
the script file to be debugged. In the future we hope to remove the
55-
necessity of this setting so that the current script file will be executed
56-
when `F5` is pressed.
52+
The debugger will attempt to execute the file in the active editor pane.
53+
If you would like to configure a single script to always be executed upon
54+
launch of the debugger, you will need to edit the `.vscode\launch.json`
55+
file and change the `program` parameter to point to the script file to be
56+
debugged. The path must be absolute but you can use the ${workspaceRoot} variable
57+
to refer to the open folder in VSCode e.g.
58+
`"program": "${workspaceRoot}\\DebugTest.ps1"`
59+
60+
### Passing Arguments to the Script
61+
62+
If you would like to pass arguments to your script, open the `.vscode\launch.json`
63+
file in your workspace and modify the `args` parameter e.g.:
64+
65+
`"args": [ "-Param1 foo -Recurse" ]`
66+
67+
You can pass all your script arguments in a single string or break them up
68+
into individual strings e.g.:
69+
70+
`"args": [ "-Param1", "foo" "-Recurse" ],`
71+
72+
At runtime these arguments will be concatenated togehter using a space
73+
delimiter so it will result in the same string as the first `args` example.
74+
75+
### Setting the Working Directory
76+
77+
When the debugger starts it will set the working directory of the PowerShell
78+
environment depending on the value of the `cwd` parameter in the
79+
`.vscode\launch.json` file in your workspace. If this parameter is missing or
80+
is set to an empty string, the working directory will be set to the workspace directory.
81+
By default it is set to `${file}` which will set the working directory to the parent
82+
directory of the file in the active editor pane when the debugger is launched.
83+
You can also set the parameter explicitly e.g.:
84+
85+
`"cwd": "C:\\Users\\JSnover\\Documents\\MonadUberAlles"`
5786

5887
## Feedback
5988

0 commit comments

Comments
 (0)