Skip to content

Commit

Permalink
fix(test): Pattern escape file names when matching debug messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
sindrets committed Dec 6, 2022
1 parent 3e0576a commit 15d4220
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
16 changes: 8 additions & 8 deletions test/gitdir_watcher_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions test/gitsigns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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',
}

Expand Down
9 changes: 9 additions & 0 deletions test/gs_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 15d4220

Please sign in to comment.