diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2022-10-01 17:29:15 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-01 17:29:15 -0400 |
commit | 01721aaa667f7fe32f5b9fbf7b6fd99cb775de48 (patch) | |
tree | 265b9840b6239cd9107bc9513b3160bd83c7c101 /test/functional/helpers.lua | |
parent | 618d21fcc9635051a16b67310e51f2729252a46f (diff) | |
parent | f2482b3b533c8ed131a33aaead332c226a9e6aca (diff) | |
download | rneovim-01721aaa667f7fe32f5b9fbf7b6fd99cb775de48.tar.gz rneovim-01721aaa667f7fe32f5b9fbf7b6fd99cb775de48.tar.bz2 rneovim-01721aaa667f7fe32f5b9fbf7b6fd99cb775de48.zip |
Merge #19438 from 3N4N/fix/pwsh
Reverts #16271
Fixs #15913
Problem:
Since #16271, `make_filter_cmd` uses `Start-Process` cmdlet to execute the user
provided shell command for `:%!`. `Start-Process` requires the command to be
split into the shell command and its arguments. This was implemented in #19268
by parsing (splitting the user-provided command at the first space) which didn't
handle cases such as --
- commands with escaped space in their filepath
- quoted commands with space in their filepath
Solution: Use piping.
The total shell command formats (excluding noise of unimportant parameters):
1. Before #16271
```powershell
pwsh -C "(shell_cmd) < tmp.in | 2>&1 Out-File -Encoding UTF8 <tmp.out>"
# not how powershell commands work
```
2. Since #16271
```powershell
pwsh -C "Start-Process shell_cmd -RedirectStandardInput <tmp.in> -RedirectStandardOutput <tmp.out>"
# doesn't handle executable path with space in it
# doesn't write error to <tmp.out>
```
3. This PR
```powershell
pwsh -C "& { Get-Content <tmp.in> | & 'path\with space\to\shell_cmd.exe' arg1 arg2 } 2>&1 | Out-File -Encoding UTF8 <tmp.out>"
# also works with forward slash in the filepath
# also works with double quotes around shell command
```
After this PR, the user can use the following formats:
:%!c:\Program` Files\Git\usr\bin\sort.exe
:%!'c:\Program Files\Git\usr\bin\sort.exe'
:%!"c:\Program Files\Git\usr\bin\sort.exe"
:%!"c:\Program` Files\Git\usr\bin\sort.exe"
They can even chain different commands:
:%!"c:\Program` Files\Git\usr\bin\sort.exe" | sort.exe -r
But if they want to call a stringed executable path, they have to provide the
Invoke-Command operator (&). In fact, the first stringed executable path also
needs this & operator, but this PR adds that behind the scene.
:%!"c:\Program` Files\Git\usr\bin\sort.exe" | sort.exe -r | & 'c:\Program Files\Git\usr\bin\sort.exe'
## What this PR solves
- Having to parse the user-provided bang ex-command (for splitting into shell
cmd and its args).
- Removes a lot of human-unreadable `#ifdef` blocks.
- Accepting escaped spaces in executable path.
- Accepting quoted string of executable path.
- Redirects error and exception to tmp.out (exception for when `wrong_cmd.exe
not found`)
## What this PR doesn't solve
- Handling wrongly escaped path to executable, which the user may pass because
of cmdline tab-completion. #18592
## Edge cases
- (Not handled) If the user themself provides the `&` sign (means `call
this.exe` in powershell)
- (Not handled) Use `-Encoding utf8` parameter for `Get-Content`?
- (Handled) Doesn't write to tmp.out if shell command is not found.
- fix: use anonymous function (`{wrong_cmd.exe}`).
## Changes other than `make_filter_cmd()` function
- Encoding for piping to external executables. See BOM-less UTF8:
https://github.com/PowerShell/PowerShell/issues/4681
Diffstat (limited to 'test/functional/helpers.lua')
-rw-r--r-- | test/functional/helpers.lua | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index 3aec834bea..4fcc190dee 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -562,16 +562,16 @@ function module.set_shell_powershell(fake) assert(found) end local shell = found and (iswin() and 'powershell' or 'pwsh') or module.testprg('pwsh-test') - local set_encoding = '[Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;' + local set_encoding = '[Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new();' local cmd = set_encoding..'Remove-Item -Force '..table.concat(iswin() - and {'alias:cat', 'alias:echo', 'alias:sleep'} + and {'alias:cat', 'alias:echo', 'alias:sleep', 'alias:sort'} or {'alias:echo'}, ',')..';' module.exec([[ let &shell = ']]..shell..[[' set shellquote= shellxquote= let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command ]]..cmd..[[' let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode' - let &shellredir = '-RedirectStandardOutput %s -NoNewWindow -Wait' + let &shellredir = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode' ]]) return found end |