-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shift dynamic positional parameters, fix #217
- Loading branch information
1 parent
f8502cc
commit b273813
Showing
6 changed files
with
104 additions
and
51 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
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
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
49 changes: 49 additions & 0 deletions
49
Tests/Issues/152-positional-script-parameters/152.test.ps1
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,49 @@ | ||
|
||
# Issue #152 (1). Also test the bootstrapping scenario. | ||
# This used to fail on the second call of z.ps1. | ||
task DoNotMakeScriptParametersNamed { | ||
# self-invoking build script | ||
Set-Content z.ps1 { | ||
param( | ||
[Parameter()]$Tasks | ||
) | ||
if (!$MyInvocation.ScriptName.EndsWith('Invoke-Build.ps1')) { | ||
return Invoke-Build $Tasks $MyInvocation.MyCommand.Path @PSBoundParameters | ||
} | ||
task Test {} | ||
} | ||
|
||
# invoke the script twice with the task name | ||
./z.ps1 Test | ||
./z.ps1 Test | ||
|
||
remove z.ps1 | ||
} | ||
|
||
<# | ||
Issue #152 (2). Test positional parameters. | ||
Potential problem: | ||
- `Task` and `P1` both have position 0. | ||
- `File` and `P2` both have position 1. | ||
UPDATE: yes, it is a problem, see #217 | ||
Fortunate current behaviour: | ||
PowerShell somehow does what we expect, shift positions of P1, P2, P3. | ||
This may change in the future, so we cover this by test. | ||
UPDATE: #217 now shifts positions but we keep the test anyway. | ||
Workarounds for the future: | ||
- Use `[CmdletBinding(PositionalBinding=$false)]` (PowerShell v3+) to enforce named parameters. | ||
- Set parameter positions explicitly starting with 2 (0, 1 are consumed by Task, File). | ||
UPDATE: The second is kind of used by #217 (shift +2) | ||
#> | ||
task PositionalParameters { | ||
Set-Content z.build.ps1 { | ||
param($P1, $P2, $P3) | ||
task Parameters {"$P1|$P2|$P3"} | ||
} | ||
|
||
# invoke the script with 5 positional parameters | ||
($r = Invoke-Build Parameters z.build.ps1 v1 v2 v3) | ||
assert ($r -contains 'v1|v2|v3') | ||
|
||
remove z.build.ps1 | ||
} |
12 changes: 12 additions & 0 deletions
12
Tests/Issues/217-bad-error-on-unknown-parameter/217.build.ps1
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,12 @@ | ||
<# https://github.com/nightroman/Invoke-Build/issues/217 | ||
#152 fixed one issue but added another, cryptic errors. | ||
#217 shifts script parameters positions (+2) avoiding conflicts with IB Task and File. | ||
#> | ||
|
||
param( | ||
$Param1, | ||
$Param2, | ||
$Param3 | ||
) | ||
|
||
task . |
32 changes: 32 additions & 0 deletions
32
Tests/Issues/217-bad-error-on-unknown-parameter/217.test.ps1
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,32 @@ | ||
|
||
task UnknownParameter { | ||
# parameter positions before IB | ||
$a1, $a2, $a3 = Get-ParameterPosition | ||
equals $a2 ($a1 + 1) | ||
equals $a3 ($a2 + 1) | ||
|
||
# call IB with an unknown parameter, this should fail | ||
# and the error should be friendly, not some cryptic | ||
try { | ||
throw Invoke-Build -bar | ||
} | ||
catch { | ||
equals "$_" "A parameter cannot be found that matches parameter name 'bar'." | ||
} | ||
|
||
# parameter positions after IB shifted +2 on every call | ||
$b1, $b2, $b3 = Get-ParameterPosition | ||
equals $b1 ($a1 + 2) | ||
equals $b2 ($a2 + 2) | ||
equals $b3 ($a3 + 2) | ||
} | ||
|
||
function Get-ParameterPosition { | ||
foreach($p in (Get-Command "$BuildRoot\217.build.ps1").Parameters.Values) { | ||
foreach ($a in $p.Attributes) { | ||
if ($a -is [System.Management.Automation.ParameterAttribute]) { | ||
$a.Position | ||
} | ||
} | ||
} | ||
} |