Skip to content
Myr

Test escaping of powershell commands on Windows

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Vlastimil Ovčáčík · committed by Junegunn Choi · parent 7c3f42b · Browse files · 1 file+49 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+49 added−0 removed

  • other:+49 added−0 removed

1 file+49 added−0 removed

modified:src/terminal_test.go+49 added
286 unmodified lines
287 // quoting the tilde is required (to be treated as string)288 {give{`grep {} ~/test`, ``, newItems(`~`)}, want{output: `grep '~' ~/test`}},289290 // 2) problematic examples291 // (not necessarily unexpected)292293 // paths that need to expand some part of it won't work (special characters and variables)294 {give{`cat {}`, ``, newItems(`~/test`)}, want{output: `cat '~/test'`}},295 {give{`cat {}`, ``, newItems(`$HOME/test`)}, want{output: `cat '$HOME/test'`}},
19 unmodified lines
315 // example of mandatorily escaped double quote in the output, otherwise `rg -- ""C:\\test.txt""` is not matching for the double quotes around the path316 {give{`rg -- {}`, ``, newItems(`"C:\test.txt"`)}, want{output: `rg -- ^"\^"C:\\test.txt\^"^"`}},317318 // 2) problematic examples319 // (not necessarily unexpected)320321 // notepad++'s parser can't handle `-n"12"` generate by fzf, expects `-n12`322 {give{`notepad++ -n{1} {2}`, ``, newItems(`12 C:\Work\Test Folder\File.txt`)}, want{output: `notepad++ -n^"12^" ^"C:\\Work\\Test Folder\\File.txt^"`}},323
4 unmodified lines
328 {give{`cmd /c {}`, ``, newItems(`cat "C:\test.txt"`)}, want{output: `cmd /c ^"cat \^"C:\\test.txt\^"^"`}},329330 // the "file" flag in the pattern won't create *.bat or *.cmd file so the command in the output tries to edit the file, instead of executing it331 // the temp file contains: `cat "C:\test.txt"`332 // TODO this should actually work333 {give{`cmd /c {f}`, ``, newItems(`cat "C:\test.txt"`)}, want{match: `^cmd /c .*\fzf-preview-[0-9]{9}$`}},334 }335 testCommands(t, tests)336}337338// purpose of this test is to demonstrate some shortcomings of fzf's templating system on Windows in Powershell339func TestPowershellCommands(t *testing.T) {340 if !util.IsWindows() {341 t.SkipNow()342 }343344 tests := []testCase{345 // reference: give{template, query, items}, want{output OR match}346347 // 1) working examples348349 {give{`Get-Content {}`, ``, newItems(`C:\test.txt`)}, want{output: `Get-Content 'C:\test.txt'`}},350 {give{`rg -- "package" {}`, ``, newItems(`.\test.go`)}, want{output: `rg -- "package" '.\test.go'`}},351352 // example of escaping single quotes353 {give{`rg -- {}`, ``, newItems(`'foobar'`)}, want{output: `rg -- '''foobar'''`}},354355 // chaining powershells356 {give{`powershell -NoProfile -Command {}`, ``, newItems(`cat "C:\test.txt"`)}, want{output: `powershell -NoProfile -Command 'cat \"C:\test.txt\"'`}},357358 // 2) problematic examples359 // (not necessarily unexpected)360361 // looking for a path string will only work with escaped backslashes362 {give{`rg -- {}`, ``, newItems(`C:\test.txt`)}, want{output: `rg -- 'C:\test.txt'`}},363 // looking for a literal double quote will only work with triple escaped double quotes364 {give{`rg -- {}`, ``, newItems(`"C:\test.txt"`)}, want{output: `rg -- '\"C:\test.txt\"'`}},365366 // Get-Content (i.e. cat alias) is parsing `"` as a part of the file path, returns an error:367 // Get-Content : Cannot find drive. A drive with the name '"C:' does not exist.368 {give{`cat {}`, ``, newItems(`"C:\test.txt"`)}, want{output: `cat '\"C:\test.txt\"'`}},369370 // the "file" flag in the pattern won't create *.ps1 file so the powershell will offload this "unknown" filetype371 // to explorer, which will prompt user to pick editing program for the fzf-preview file372 // the temp file contains: `cat "C:\test.txt"`373 // TODO this should actually work374 {give{`powershell -NoProfile -Command {f}`, ``, newItems(`cat "C:\test.txt"`)}, want{match: `^powershell -NoProfile -Command .*\fzf-preview-[0-9]{9}$`}},375 }376377 // to force powershell-style escaping we temporarily set environment variable that fzf honors378 shellBackup := os.Getenv("SHELL")379 os.Setenv("SHELL", "powershell")380 testCommands(t, tests)381 os.Setenv("SHELL", shellBackup)382}383384/*385 Test typical valid placeholders and parsing of them.386387 Also since the parser assumes the input is matched with `placeholder` regex,
212 unmodified lines