Skip to content

Commit

Permalink
refactor(tests): tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Jun 19, 2023
1 parent f137883 commit b435a59
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 71 deletions.
5 changes: 3 additions & 2 deletions test/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ local exec_lua = helpers.exec_lua
local fn = helpers.funcs
local system = fn.system
local test_config = helpers.test_config
local cleanup = helpers.cleanup
local clear = helpers.clear
local setup_test_repo = helpers.setup_test_repo
local eq = helpers.eq
local expectf = helpers.expectf

local it = helpers.it(it)

--- @param exp_hunks string[]
local function expect_hunks(exp_hunks)
expectf(function()
--- @type table[]
local hunks = exec_lua"return require('gitsigns').get_hunks()"
if #exp_hunks~= #hunks then
local msg = {}
local msg = {} --- @type string[]
msg[#msg+1] = ''
msg[#msg+1] = string.format(
'Number of hunks do not match. Expected: %d, passed in: %d',
Expand Down
12 changes: 0 additions & 12 deletions test/compat.lua

This file was deleted.

74 changes: 24 additions & 50 deletions test/gitsigns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,13 @@ describe('gitsigns', function()
it('gitdir watcher works on a fresh repo', function()
local nvim_ver = exec_lua('return vim.version().minor')
if nvim_ver == 8 then
-- v0.8.0 has some regression that's fixed it v0.9.0 dev
pending()
pending("v0.8.0 has some regression that's fixed it v0.9.0 dev")
end
screen:try_resize(20,6)
setup_test_repo{no_add=true}
-- Don't set this too low, or else the test will lock up
config.watch_gitdir = {interval = 100}
setup_gitsigns(config)
command('Gitsigns clear_debug')
edit(test_file)

match_dag {
Expand Down Expand Up @@ -110,12 +108,11 @@ describe('gitsigns', function()

it('can open files not in a git repo', function()
setup_gitsigns(config)
command('Gitsigns clear_debug')
local tmpfile = os.tmpname()
edit(tmpfile)

match_debug_messages {
n'attach(1): Attaching (trigger=BufReadPost)',
'attach(1): Attaching (trigger=BufReadPost)',
np'run_job: git .* config user.name',
np'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD',
n'new: Not in git repo',
Expand Down Expand Up @@ -160,18 +157,16 @@ describe('gitsigns', function()
end)

it('does not attach inside .git', function()
command("Gitsigns clear_debug")
edit(scratch..'/.git/index')

match_debug_messages {
n'attach(1): Attaching (trigger=BufReadPost)',
'attach(1): Attaching (trigger=BufReadPost)',
n'new: In git dir',
n'attach(1): Empty git obj'
}
end)

it('doesn\'t attach to ignored files', function()
command("Gitsigns clear_debug")
write_to_file(scratch..'/.gitignore', {'dummy_ignored.txt'})

local ignored_file = scratch.."/dummy_ignored.txt"
Expand All @@ -180,7 +175,7 @@ describe('gitsigns', function()
edit(ignored_file)

match_debug_messages {
n'attach(1): Attaching (trigger=BufReadPost)',
'attach(1): Attaching (trigger=BufReadPost)',
np'run_job: git .* config user.name',
np'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD',
np'run_job: git .* ls%-files .*/dummy_ignored.txt',
Expand All @@ -191,11 +186,10 @@ describe('gitsigns', function()
end)

it('doesn\'t attach to non-existent files', function()
command("Gitsigns clear_debug")
edit(newfile)

match_debug_messages {
n'attach(1): Attaching (trigger=BufNewFile)',
'attach(1): Attaching (trigger=BufNewFile)',
np'run_job: git .* config user.name',
np'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD',
np('run_job: git .* ls%-files %-%-stage %-%-others %-%-exclude%-standard %-%-eol '..vim.pesc(newfile)),
Expand All @@ -206,11 +200,10 @@ describe('gitsigns', function()
end)

it('doesn\'t attach to non-existent files with non-existent sub-dirs', function()
command("Gitsigns clear_debug")
edit(scratch..'/does/not/exist')

match_debug_messages {
n'attach(1): Attaching (trigger=BufNewFile)',
'attach(1): Attaching (trigger=BufNewFile)',
n'attach(1): Not a path',
}

Expand All @@ -219,10 +212,9 @@ describe('gitsigns', function()
end)

it('can run copen', function()
command("Gitsigns clear_debug")
command("copen")
match_debug_messages {
n'attach(2): Attaching (trigger=BufReadPost)',
'attach(2): Attaching (trigger=BufReadPost)',
n'attach(2): Non-normal buffer',
}
end)
Expand Down Expand Up @@ -267,7 +259,6 @@ describe('gitsigns', function()

edit(newfile)
feed('gg')
command("Gitsigns clear_debug")
check { signs = {} }

-- Wait until the virtual blame line appears
Expand All @@ -293,19 +284,6 @@ describe('gitsigns', function()
]]}
end

it('doesn\'t error on untracked files', function()
local nvim_ver = exec_lua('return vim.version().minor')
if nvim_ver >= 8 then
pending()
end

setup_test_repo{no_add=true}
edit(newfile)
insert("line")
command("write")
screen:expect{messages = { { content = { { "<" } }, kind = "" } } }
end)
--
it('does handle dos fileformats', function()
-- Add a file with windows line ending into the repo
-- Disable autocrlf, so that the file keeps the \r\n file endings.
Expand All @@ -321,15 +299,15 @@ describe('gitsigns', function()
end)
end)

describe('configuration', function()
it('handled deprecated fields', function()
-- TODO(lewis6991): All deprecated fields removed. Re-add when we have another deprecated field
pending()
-- config.current_line_blame_delay = 100
-- setup_gitsigns(config)
-- eq(100, exec_lua([[return package.loaded['gitsigns.config'].config.current_line_blame_opts.delay]]))
end)
end)
-- TODO(lewis6991): All deprecated fields removed. Re-add when we have another deprecated field
-- describe('configuration', function()
-- it('handled deprecated fields', function()
-- pending()
-- -- config.current_line_blame_delay = 100
-- -- setup_gitsigns(config)
-- -- eq(100, exec_lua([[return package.loaded['gitsigns.config'].config.current_line_blame_opts.delay]]))
-- end)
-- end)

describe('on_attach()', function()
it('can prevent attaching to a buffer', function()
Expand All @@ -342,11 +320,10 @@ describe('gitsigns', function()
return false
end
]])
command("Gitsigns clear_debug")

edit(test_file)
match_debug_messages {
n'attach(1): Attaching (trigger=BufReadPost)',
'attach(1): Attaching (trigger=BufReadPost)',
np'run_job: git .* config user.name',
np'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD',
np'run_job: git .* rev%-parse %-%-short HEAD',
Expand Down Expand Up @@ -456,20 +433,18 @@ describe('gitsigns', function()

it('attaches to newly created files', function()
setup_gitsigns(config)
command('Gitsigns clear_debug')
edit(newfile)
match_debug_messages{
n'attach(1): Attaching (trigger=BufNewFile)',
'attach(1): Attaching (trigger=BufNewFile)',
np'run_job: git .* config user.name',
np'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD',
np'run_job: git .* ls%-files .*',
n'attach(1): Not a file',
}
command('Gitsigns clear_debug')
command("write")

local messages = {
n'attach(1): Attaching (trigger=BufWritePost)',
'attach(1): Attaching (trigger=BufWritePost)',
np"run_job: git .* config user.name",
np'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD',
np'run_job: git .* ls%-files .*',
Expand Down Expand Up @@ -656,20 +631,19 @@ describe('gitsigns', function()
write_to_file(scratch..'/t3.txt', {'hello lewis'})

setup_gitsigns(config)
command('Gitsigns clear_debug')

helpers.exc_exec("vimgrep ben "..scratch..'/*')

screen:expect{messages = {{
kind = "quickfix", content = { { "(1 of 2): hello ben" } },
}}}

eq({
match_debug_messages {
'attach(2): attaching is disabled',
'attach(3): attaching is disabled',
'attach(4): attaching is disabled',
'attach(5): attaching is disabled',
}, exec_lua[[return require'gitsigns'.debug_messages(true)]])
n'attach(3): attaching is disabled',
n'attach(4): attaching is disabled',
n'attach(5): attaching is disabled',
}

end)

Expand Down
9 changes: 3 additions & 6 deletions test/gs_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ function M.expectf(cond, interval)
cond()
end

function M.command_fmt(str, ...)
helpers.command(str:format(...))
end

function M.edit(path)
M.command_fmt("edit %s", path)
helpers.command("edit " .. path)
end

function M.write_to_file(path, text)
Expand All @@ -127,7 +123,6 @@ end
--- @param spec string|{next:boolean, pattern:boolean, text:string}
--- @return boolean
local function match_spec_elem(line, spec)

if spec.pattern then
if line:match(spec.text) then
return true
Expand Down Expand Up @@ -191,6 +186,7 @@ function M.debug_messages()
return exec_lua("return require'gitsigns.debug.log'.messages")
end

--- Like match_debug_messages but elements in spec are unordered
--- @param spec table<integer, (string|{next:boolean, pattern:boolean, text:string})?>
function M.match_dag(spec)
M.expectf(function()
Expand All @@ -201,6 +197,7 @@ function M.match_dag(spec)
end)
end

--- @param spec table<integer, (string|{next:boolean, pattern:boolean, text:string})?>
function M.match_debug_messages(spec)
M.expectf(function()
M.match_lines(M.debug_messages(), spec)
Expand Down
3 changes: 2 additions & 1 deletion vim.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ globals:
- type: function
required: true
pending:
args: []
args:
- type: string

0 comments on commit b435a59

Please sign in to comment.