|
| 1 | +# The PowerShell script binder consumes a bare -- before populating $args. |
| 2 | +# Exercise the generated wrapper with a real native child, without a registry. |
| 3 | +function Assert-Arguments($actual, $expected) { |
| 4 | + if ($LASTEXITCODE -ne 0 -or ($actual -join "`n") -ne $expected) { |
| 5 | + throw "Expected arguments $expected, got $actual (exit $LASTEXITCODE)" |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +Assert-Arguments (vp env exec node print-args.cjs -- --template react-ts) '["--","--template","react-ts"]' |
| 10 | +Assert-Arguments (vp env exec node print-args.cjs '--' --template react-ts) '["--","--template","react-ts"]' |
| 11 | +Assert-Arguments (vp env exec node print-args.cjs "--" --template react-ts) '["--","--template","react-ts"]' |
| 12 | +Assert-Arguments (vp env exec node print-args.cjs '--' -- --template react-ts) '["--","--","--template","react-ts"]' |
| 13 | +Assert-Arguments (vp env exec node print-args.cjs -- -- --template react-ts) '["--","--","--template","react-ts"]' |
| 14 | +Assert-Arguments (vp env exec node print-args.cjs --) '["--"]' |
| 15 | +Assert-Arguments (vp env exec node print-args.cjs 'value with spaces' -- --template 'react ts') '["value with spaces","--","--template","react ts"]' |
| 16 | + |
| 17 | +# The stop-parsing token preserves its literal remainder without consuming --. |
| 18 | +# Capture on a separate line because --% makes the rest of the line literal. |
| 19 | +$literalArguments = vp env exec node print-args.cjs --% -- |
| 20 | +Assert-Arguments $literalArguments '["--"]' |
| 21 | +$literalArguments = vp env exec node print-args.cjs --% -- -- |
| 22 | +Assert-Arguments $literalArguments '["--","--"]' |
| 23 | +$literalArguments = vp env exec node print-args.cjs -- --% -- |
| 24 | +Assert-Arguments $literalArguments '["--","--"]' |
| 25 | + |
| 26 | +# A quoted --% allows recovery; escaping its first dash still stops parsing. |
| 27 | +$literalArguments = vp env exec node print-args.cjs '--%' -- after |
| 28 | +Assert-Arguments $literalArguments '["--","after"]' |
| 29 | +$literalArguments = vp env exec node print-args.cjs `--% -- |
| 30 | +Assert-Arguments $literalArguments '["--"]' |
| 31 | + |
| 32 | +$prefix = @('env', 'exec', 'node', 'print-args.cjs') |
| 33 | +$suffix = @('--template', 'react-ts') |
| 34 | +$allArguments = $prefix + @('--') + $suffix |
| 35 | +Assert-Arguments (vp env exec node print-args.cjs -- @suffix) '["--","--template","react-ts"]' |
| 36 | +Assert-Arguments (vp @allArguments) '["--","--template","react-ts"]' |
| 37 | +Assert-Arguments (vp @prefix '--' @suffix) '["--","--template","react-ts"]' |
| 38 | +Assert-Arguments (vp env exec node print-args.cjs @('before', 'separator') -- @('after', 'separator')) '["before","separator","--","after","separator"]' |
| 39 | + |
| 40 | +# A splat before -- makes its position unknowable from syntax alone. Preserve |
| 41 | +# PowerShell's bound arguments in that case instead of guessing an insertion point. |
| 42 | +Assert-Arguments (vp @prefix -- --template react-ts) '["--template","react-ts"]' |
| 43 | +Assert-Arguments (vp @prefix -- @suffix) '["--template","react-ts"]' |
| 44 | +$emptyArguments = @() |
| 45 | +Assert-Arguments (vp @prefix @emptyArguments -- @emptyArguments @suffix) '["--template","react-ts"]' |
| 46 | +$script:scopedPrefix = $prefix |
| 47 | +$script:scopedSuffix = $suffix |
| 48 | +Assert-Arguments (vp @script:scopedPrefix -- @script:scopedSuffix) '["--template","react-ts"]' |
| 49 | +Assert-Arguments (vp @script:scopedPrefix '--' @script:scopedSuffix) '["--","--template","react-ts"]' |
| 50 | +Assert-Arguments (vp env exec node print-args.cjs -- @script:scopedSuffix) '["--","--template","react-ts"]' |
| 51 | + |
| 52 | +# Argument expressions can change a splatted variable after it has been expanded. |
| 53 | +Assert-Arguments (vp @prefix -- @script:scopedSuffix $($script:scopedSuffix = @(); 'last')) '["--template","react-ts","last"]' |
| 54 | + |
| 55 | +# The caller AST must identify the invocation inside a function or on a later line. |
| 56 | +function Invoke-ArgumentCheck { |
| 57 | + Assert-Arguments (vp env exec node print-args.cjs -- @suffix) '["--","--template","react-ts"]' |
| 58 | +} |
| 59 | +Invoke-ArgumentCheck |
| 60 | +Assert-Arguments (& vp env exec node print-args.cjs ` |
| 61 | + -- ` |
| 62 | + --template react-ts) '["--","--template","react-ts"]' |
| 63 | +Assert-Arguments (vp env exec node print-args.cjs first -- second | ForEach-Object { $_ }) '["first","--","second"]' |
| 64 | +$first = vp env exec node print-args.cjs one -- two; $second = vp env exec node print-args.cjs three -- four |
| 65 | +Assert-Arguments $first '["one","--","two"]' |
| 66 | +Assert-Arguments $second '["three","--","four"]' |
| 67 | + |
| 68 | +# Recovering the separator must not evaluate argument expressions a second time. |
| 69 | +$script:argumentEvaluations = 0 |
| 70 | +Assert-Arguments (vp env exec node print-args.cjs -- $($script:argumentEvaluations++; 'value with spaces')) '["--","value with spaces"]' |
| 71 | +if ($script:argumentEvaluations -ne 1) { throw 'An argument expression ran more than once' } |
| 72 | + |
| 73 | +Assert-Arguments (vp env exec node print-args.cjs 'text -- with spaces' --template react-ts) '["text -- with spaces","--template","react-ts"]' |
| 74 | +Assert-Arguments (vp env exec node print-args.cjs `-- --template react-ts) '["--","--template","react-ts"]' |
| 75 | +Assert-Arguments (vp env exec node print-args.cjs --template react-ts) '["--template","react-ts"]' |