From ff7f29adf2f77356bc321ee68013149ae0c7f994 Mon Sep 17 00:00:00 2001 From: Luke Sampson Date: Sat, 22 Jun 2013 10:27:06 +1000 Subject: [PATCH] testing re-parsing for stubs when invoked with & --- lib/core.ps1 | 10 ++++++++-- stubtest.ps1 | 15 ++++++++++----- test.ps1 | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 4446af8f..f90d91da 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -73,8 +73,14 @@ function stub($path) { } function exec_stub($path, $inv) { - $len = $inv.positionmessage.split("`n")[2] | sls '~+' | % { $_.matches[0].value.length } - $cmd = $inv.line.substring($inv.offsetinline-1,$len) + # re-parse the command line to work out which part of it to pass to the bin + # needs to handle brackets, pipes and `& 'cmd'` + $cmdName = $inv.InvocationName + $ast = [System.Management.Automation.Language.Parser]::ParseInput($inv.line, [ref]$null, [ref]$null) + + $cmd = $ast.find({ + ($args[0].gettype().name -eq 'commandast') -and ($args.getcommandname() -eq $cmdName) + }, $true) $rawargs = $cmd -replace "^\s*&?\s*(('?[^']*')|([^\s]*))\s*", "" diff --git a/stubtest.ps1 b/stubtest.ps1 index fad49303..7871e0ad 100644 --- a/stubtest.ps1 +++ b/stubtest.ps1 @@ -1,13 +1,18 @@ + $inv = $myinvocation +#$inv $name = $inv.InvocationName -[System.Management.Automation.Language.Token[]]$tokens = $null -[System.Management.Automation.Language.ScriptBlockAst]$ast = - [System.Management.Automation.Language.Parser]::ParseInput($inv.line, [ref]$tokens, [ref]$null) +$tokens = $null -#$tokens | % { $_ } +$ast = [System.Management.Automation.Language.Parser]::ParseInput($inv.line, [ref]$tokens, [ref]$null) -$commands = $ast.FindAll({ ($args[0] -is [System.Management.Automation.Language.CommandAst]) -and ($args.getcommandname() -eq $name)}, $true) + +$commands = $ast.FindAll({ ($args[0].gettype().name -eq 'commandast') -and ($args[0].invocationoperator -eq 'Ampersand')}, $true) + +#$ast + +#$tokens | where { $_.text -eq '&' } | % { $_.gettype() } $commands | % { $_.extent.text } diff --git a/test.ps1 b/test.ps1 index 5905ff5e..8c6de95a 100644 --- a/test.ps1 +++ b/test.ps1 @@ -1 +1 @@ -( .\stubtest -am "testing the params" 2>&1 ) | sort-object \ No newline at end of file +( & '.\stubtest' -am "testing the params" 2>&1 ) | sort-object \ No newline at end of file