From 71e0e102e2ac83c6685c6a92fa4f714eef17778a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sindre=20T=2E=20Str=C3=B8m?= Date: Tue, 6 Dec 2022 13:17:08 +0100 Subject: [PATCH] fix(test): Pattern escape file names when matching debug messages. --- test/gitdir_watcher_spec.lua | 16 ++++++++-------- test/gitsigns_spec.lua | 4 ++-- test/gs_helpers.lua | 9 +++++++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/test/gitdir_watcher_spec.lua b/test/gitdir_watcher_spec.lua index 93f93ec4..7bf9ffb5 100644 --- a/test/gitdir_watcher_spec.lua +++ b/test/gitdir_watcher_spec.lua @@ -55,7 +55,7 @@ describe('gitdir_watcher', function() 'attach(1): Attaching (trigger=BufRead)', p"run_job: git .* config user.name", p"run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD", - p('run_job: git .* ls%-files .* '..test_file), + p('run_job: git .* ls%-files .* '..helpers.pesc(test_file)), 'watch_gitdir(1): Watching git dir', p'run_job: git .* show :0:dummy.txt', 'update(1): updates: 1, jobs: 6', @@ -70,10 +70,10 @@ describe('gitdir_watcher', function() match_debug_messages { 'watcher_cb(1): Git dir update', p'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD', - p('run_job: git .* ls%-files .* '..test_file), + p('run_job: git .* ls%-files .* '..helpers.pesc(test_file)), p'run_job: git .* diff %-%-name%-status %-C %-%-cached', 'handle_moved(1): File moved to dummy.txt2', - p('run_job: git .* ls%-files .* '..test_file2), + p('run_job: git .* ls%-files .* '..helpers.pesc(test_file2)), p'handle_moved%(1%): Renamed buffer 1 from .*/dummy.txt to .*/dummy.txt2', p'run_job: git .* show :0:dummy.txt2', 'update(1): updates: 2, jobs: 11' @@ -88,10 +88,10 @@ describe('gitdir_watcher', function() match_debug_messages { 'watcher_cb(1): Git dir update', p'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD', - p('run_job: git .* ls%-files .* '..test_file2), + p('run_job: git .* ls%-files .* '..helpers.pesc(test_file2)), p'run_job: git .* diff %-%-name%-status %-C %-%-cached', 'handle_moved(1): File moved to dummy.txt3', - p('run_job: git .* ls%-files .* '..test_file3), + p('run_job: git .* ls%-files .* '..helpers.pesc(test_file3)), p'handle_moved%(1%): Renamed buffer 1 from .*/dummy.txt2 to .*/dummy.txt3', p'run_job: git .* show :0:dummy.txt3', 'update(1): updates: 3, jobs: 16' @@ -106,11 +106,11 @@ describe('gitdir_watcher', function() match_debug_messages { 'watcher_cb(1): Git dir update', p'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD', - p('run_job: git .* ls%-files .* '..test_file3), + p('run_job: git .* ls%-files .* '..helpers.pesc(test_file3)), p'run_job: git .* diff %-%-name%-status %-C %-%-cached', - p('run_job: git .* ls%-files .* '..test_file), + p('run_job: git .* ls%-files .* '..helpers.pesc(test_file)), 'handle_moved(1): Moved file reset', - p('run_job: git .* ls%-files .* '..test_file), + p('run_job: git .* ls%-files .* '..helpers.pesc(test_file)), p'handle_moved%(1%): Renamed buffer 1 from .*/dummy.txt3 to .*/dummy.txt', p'run_job: git .* show :0:dummy.txt', 'update(1): updates: 4, jobs: 22' diff --git a/test/gitsigns_spec.lua b/test/gitsigns_spec.lua index b5f22377..20f78ca8 100644 --- a/test/gitsigns_spec.lua +++ b/test/gitsigns_spec.lua @@ -91,7 +91,7 @@ describe('gitsigns', function() 'attach(1): Attaching (trigger=BufRead)', p'run_job: git .* config user.name', p'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD', - p('run_job: git .* ls%-files %-%-stage %-%-others %-%-exclude%-standard %-%-eol '..test_file), + p('run_job: git .* ls%-files %-%-stage %-%-others %-%-exclude%-standard %-%-eol '..helpers.pesc(test_file)), 'watch_gitdir(1): Watching git dir', p'run_job: git .* show :0:dummy.txt', 'update(1): updates: 1, jobs: 7' @@ -201,7 +201,7 @@ describe('gitsigns', function() 'attach(1): Attaching (trigger=BufNewFile)', p'run_job: git .* config user.name', p'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD', - p('run_job: git .* ls%-files %-%-stage %-%-others %-%-exclude%-standard %-%-eol '..newfile), + p('run_job: git .* ls%-files %-%-stage %-%-others %-%-exclude%-standard %-%-eol '..helpers.pesc(newfile)), 'attach(1): Not a file', } diff --git a/test/gs_helpers.lua b/test/gs_helpers.lua index 127f836d..42ad4d65 100644 --- a/test/gs_helpers.lua +++ b/test/gs_helpers.lua @@ -47,6 +47,15 @@ local test_file_text = { 'content', 'doesn\'t', 'matter,', 'it', 'just', 'needs', 'to', 'be', 'static.' } +--- Escapes magic chars in |lua-patterns|. +--- +---@see https://github.com/rxi/lume +---@param s string String to escape +---@return string %-escaped pattern string +function M.pesc(s) + return (s:gsub('[%(%)%.%%%+%-%*%?%[%]%^%$]', '%%%1')) +end + function M.git(args) system{"git", "-C", M.scratch, unpack(args)} end