3

I need to pass the string "fail log" as a tasty pattern argument when running tasty tests out of stack.

This is quite straight forward in bash:

PS C:\Pyrethrum> stack test --fast --ta "-p \"fail log\""

But trying to achieve the same using Powershell is driving me nuts:


PS C:\Pyrethrum> stack test --fast --ta "-p ""fail"" "

works (runs tests) when there is no space


PS C:\Pyrethrum> stack test --fast --ta "-p ""fail log"" "

Error parsing targets: Directory not found: log


PS C:\Pyrethrum> stack test --fast --ta "-p `"fail log`""

tries install a package called log


PS C:\Pyrethrum> stack test --fast --ta "-p \"fail log\""

option --ta: unterminated string: endOfInput


PS C:\Pyrethrum> stack test --fast --ta "-p /fail log/ "

... builds but pattern needs quotes

pyrethrum-0.1.0.0: test (suite: pyrethrum-test, args: -p /fail log/)

option -p: Could not parse pattern


What is the correct command line to get this to run in Powershell ?

John Walker
  • 513
  • 4
  • 16
  • 2
    Possible duplicate of [How to run an EXE file in PowerShell with parameters with spaces and quotes](https://stackoverflow.com/questions/1673967/how-to-run-an-exe-file-in-powershell-with-parameters-with-spaces-and-quotes) – Kory Gill Feb 08 '19 at 00:20
  • pass params in an array. – Kory Gill Feb 08 '19 at 00:21

1 Answers1

6

Unfortunately, the way PowerShell passes arguments to external programs is broken up to at least PowerShell 7.2.x, and requires you to not just satisfy PowerShell's own syntax requirements with respect to embedded ", but to additionally escape them for the target executable, typically with \:

 # `" is needed for PowerShell, \ is needed for stack
 stack test --fast --ta "-p \`"fail log\`""

Since your outer "..." string references no PowerShell variables and contains no subexpressions, i.e., since no string interpolation is required, you can use a '...', a literal PowerShell string instead, which simplifies matters a bit:

 stack test --fast --ta '-p \"fail log\"'

In short: To pass " characters embedded in an argument to an external program from PowerShell:

  • use \`" inside "..."
  • use \" inside '...'

See this answer for details, also with respect to a potential future fix and a helper function that hides the bug.

mklement0
  • 382,024
  • 64
  • 607
  • 775