From 61339a8ae2f6be27f28c243f00a41cc3aa5f54c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlastimil=20Ov=C4=8D=C3=A1=C4=8D=C3=ADk?= Date: Fri, 15 Oct 2021 15:31:59 +0200 Subject: Add more tests of placeholder flags and simplify its logic (#2624) * [tests] Test fzf's placeholders and escaping on practical commands This tests some reasonable commands in fzf's templates (for commands, previews, rebinds etc.), how are those commands escaped (backslashes, double quotes), and documents if the output is executable in cmd.exe. Both on Unix and Windows. * [tests] Add testing of placeholder parsing and matching Adds tests and bit of docs for the curly brackets placeholders in fzf's template strings. Also tests the "placeholder" regex. * [tests] Add more test cases of replacing placeholders focused on flags Replacing placeholders in templates is already tested, this adds tests that focus more on the parameters of placeholders - e.g. flags, token ranges. There is at least one test for each flag, not all combinations are tested though. * [refactoring] Split OS-specific function quoteEntry() to corresponding source file This is minor refactoring, and also the function's test was made crossplatform. * [refactoring] Simplify replacePlaceholder function Should be equivalent to the original, but has simpler structure. --- src/terminal_windows.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/terminal_windows.go') diff --git a/src/terminal_windows.go b/src/terminal_windows.go index 9de7ae45..d4262b65 100644 --- a/src/terminal_windows.go +++ b/src/terminal_windows.go @@ -4,6 +4,8 @@ package fzf import ( "os" + "regexp" + "strings" ) func notifyOnResize(resizeChan chan<- os.Signal) { @@ -17,3 +19,12 @@ func notifyStop(p *os.Process) { func notifyOnCont(resizeChan chan<- os.Signal) { // NOOP } + +func quoteEntry(entry string) string { + escaped := strings.Replace(entry, `\`, `\\`, -1) + escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"` + r, _ := regexp.Compile(`[&|<>()@^%!"]`) + return r.ReplaceAllStringFunc(escaped, func(match string) string { + return "^" + match + }) +} -- cgit v1.2.3