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
Provenance
- Commit
a33c011- Tree
54c1068- Author
Vlastimil Ovčáčík <vovcacik@github.ovcacik.org>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
7c3f42b
1 file changed+49 added−0 removed
modified:src/terminal_test.go+49 added
286 unmodified lines287 // quoting the tilde is required (to be treated as string)288 {give{`grep {} ~/test`, ``, newItems(`~`)}, want{output: `grep '~' ~/test`}},289 290 // 2) problematic examples291+ // (not necessarily unexpected)292 293 // 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 lines315 // 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\^"^"`}},317 318 // 2) problematic examples319+ // (not necessarily unexpected)320 321 // 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 lines328 {give{`cmd /c {}`, ``, newItems(`cat "C:\test.txt"`)}, want{output: `cmd /c ^"cat \^"C:\\test.txt\^"^"`}},329 330 // 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 }337 338+// purpose of this test is to demonstrate some shortcomings of fzf's templating system on Windows in Powershell339+func TestPowershellCommands(t *testing.T) {340+ if !util.IsWindows() {341+ t.SkipNow()342+ }343+344+ tests := []testCase{345+ // reference: give{template, query, items}, want{output OR match}346+347+ // 1) working examples348+349+ {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'`}},351+352+ // example of escaping single quotes353+ {give{`rg -- {}`, ``, newItems(`'foobar'`)}, want{output: `rg -- '''foobar'''`}},354+355+ // chaining powershells356+ {give{`powershell -NoProfile -Command {}`, ``, newItems(`cat "C:\test.txt"`)}, want{output: `powershell -NoProfile -Command 'cat \"C:\test.txt\"'`}},357+358+ // 2) problematic examples359+ // (not necessarily unexpected)360+361+ // 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\"'`}},365+366+ // 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\"'`}},369+370+ // 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+ }376+377+ // 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+}383+384 /*385 Test typical valid placeholders and parsing of them.386 387 Also since the parser assumes the input is matched with `placeholder` regex,212 unmodified lines