Tidy up tests

This commit is contained in:
Lewis Russell 2021-08-03 22:28:35 +01:00
parent 605166b6f6
commit fecff8b866
4 changed files with 136 additions and 155 deletions

View File

@ -1,35 +1,36 @@
-- vim: foldnestmax=5:
-- vim: foldnestmax=5 foldminlines=1
local Screen = require('test.functional.ui.screen')
local helpers = require('test.gs_helpers')
local clear = helpers.clear
local command = helpers.command
local exec_capture = helpers.exec_capture
local feed = helpers.feed
local exec_lua = helpers.exec_lua
local split = helpers.split
local get_buf_var = helpers.curbufmeths.get_var
local fn = helpers.funcs
local system = fn.system
local init = helpers.init
local wait = helpers.wait
local write_to_file = helpers.write_to_file
local edit = helpers.edit
local cleanup = helpers.cleanup
local test_file = helpers.test_file
local git = helpers.git
local scratch = helpers.scratch
local newfile = helpers.newfile
local debug_messages = helpers.debug_messages
local match_dag = helpers.match_dag
local clear = helpers.clear
local command = helpers.command
local exec_capture = helpers.exec_capture
local feed = helpers.feed
local insert = helpers.insert
local exec_lua = helpers.exec_lua
local split = helpers.split
local get_buf_var = helpers.curbufmeths.get_var
local fn = helpers.funcs
local system = fn.system
local expectf = helpers.expectf
local write_to_file = helpers.write_to_file
local edit = helpers.edit
local cleanup = helpers.cleanup
local test_file = helpers.test_file
local git = helpers.git
local scratch = helpers.scratch
local newfile = helpers.newfile
local debug_messages = helpers.debug_messages
local match_dag = helpers.match_dag
local match_lines = helpers.match_lines
local p = helpers.p
local match_debug_messages = helpers.match_debug_messages
local match_lines = helpers.match_lines
local p = helpers.p
local setup = helpers.setup
local test_config = helpers.test_config
local check = helpers.check
local eq = helpers.eq
local setup_gitsigns = helpers.setup_gitsigns
local setup_test_repo = helpers.setup_test_repo
local test_config = helpers.test_config
local check = helpers.check
local eq = helpers.eq
local it = helpers.it(it)
@ -65,21 +66,20 @@ describe('gitsigns', function()
end)
it('can run basic setup', function()
screen:try_resize(60,6)
setup()
setup_gitsigns()
check { status = {}, signs = {} }
end)
it('index watcher works on a fresh repo', function()
screen:try_resize(20,6)
init(true)
setup_test_repo(true)
config.watch_index = {interval = 5}
setup(config)
setup_gitsigns(config)
edit(test_file)
wait(function()
expectf(function()
match_dag(debug_messages(), {
"run_job: git --no-pager --version",
'run_job: git --no-pager --version',
'attach(1): Attaching (trigger=BufRead)',
p'run_job: git .* config user.name',
'run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD',
@ -105,21 +105,23 @@ describe('gitsigns', function()
end)
it('can open files not in a git repo', function()
screen:try_resize(60,6)
setup(config)
setup_gitsigns(config)
local tmpfile = os.tmpname()
edit(tmpfile)
feed('iline<esc>')
helpers.sleep(10)
match_debug_messages {
'run_job: git --no-pager --version',
'attach(1): Attaching (trigger=BufRead)',
p'run_job: git .* config user.name',
'run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD',
'attach(1): Not in git repo',
}
command('Gitsigns clear_debug')
insert('line')
command("write")
match_debug_messages {
"run_job: git --no-pager --version",
'attach(1): Attaching (trigger=BufRead)',
p"run_job: git .* config user.name",
"run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD",
'attach(1): Not in git repo',
'attach(1): Attaching (trigger=BufWritePost)',
'run_job: git --no-pager config user.name',
'run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD',
@ -129,13 +131,13 @@ describe('gitsigns', function()
describe('when attaching', function()
before_each(function()
init()
setup(config)
setup_test_repo()
setup_gitsigns(config)
end)
it('can setup mappings', function()
edit(test_file)
wait(function()
expectf(function()
local res = split(exec_capture('nmap <buffer>'), '\n')
table.sort(res)
@ -155,7 +157,7 @@ describe('gitsigns', function()
edit(scratch..'/.git/index')
match_debug_messages {
"run_job: git --no-pager --version",
'run_job: git --no-pager --version',
'attach(1): Attaching (trigger=BufRead)',
'attach(1): In git dir'
}
@ -170,12 +172,12 @@ describe('gitsigns', function()
edit(ignored_file)
match_debug_messages {
"run_job: git --no-pager --version",
'run_job: git --no-pager --version',
'attach(1): Attaching (trigger=BufRead)',
p"run_job: git .* config user.name",
"run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD",
p"run_job: git .* ls%-files .*/dummy_ignored.txt",
"attach(1): Cannot resolve file in repo",
p'run_job: git .* config user.name',
'run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD',
p'run_job: git .* ls%-files .*/dummy_ignored.txt',
'attach(1): Cannot resolve file in repo',
}
check {status = {head='master'}}
@ -185,47 +187,45 @@ describe('gitsigns', function()
edit(newfile)
match_debug_messages {
"run_job: git --no-pager --version",
'run_job: git --no-pager --version',
'attach(1): Attaching (trigger=BufNewFile)',
p"run_job: git .* config user.name",
"run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD",
p("run_job: git .* ls%-files %-%-stage %-%-others %-%-exclude%-standard "..newfile),
"attach(1): Not a file",
p'run_job: git .* config user.name',
'run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD',
p('run_job: git .* ls%-files %-%-stage %-%-others %-%-exclude%-standard '..newfile),
'attach(1): Not a file',
}
check {status = {head='master'}}
end)
it('doesn\'t attach to non-existent files with non-existent sub-dirs', function()
edit(scratch..'/does/not/exist')
match_debug_messages {
"run_job: git --no-pager --version",
'run_job: git --no-pager --version',
'attach(1): Attaching (trigger=BufNewFile)',
"attach(1): Not a path",
'attach(1): Not a path',
}
helpers.pcall_err(get_buf_var, 'gitsigns_head')
helpers.pcall_err(get_buf_var, 'gitsigns_status_dict')
end)
it('can run copen', function()
command("copen")
match_debug_messages {
"run_job: git --no-pager --version",
'run_job: git --no-pager --version',
'attach(2): Attaching (trigger=BufRead)',
"attach(2): Non-normal buffer",
'attach(2): Non-normal buffer',
}
end)
it('can run get_hunks()', function()
edit(test_file)
feed("iline1<esc>")
insert("line1")
feed("oline2<esc>")
wait(function()
expectf(function()
eq({{
head = '@@ -1,1 +1,2 @@',
type = 'change',
@ -241,12 +241,11 @@ describe('gitsigns', function()
describe('current line blame', function()
it('doesn\'t error on untracked files', function()
command("set updatetime=1")
init(true)
setup_test_repo(true)
config.current_line_blame = true
setup(config)
setup_gitsigns(config)
edit(newfile)
feed("iline<esc>")
insert("line")
command("write")
screen:expect{messages = { { content = { { "<" } }, kind = "" } } }
end)
@ -254,35 +253,31 @@ describe('gitsigns', function()
describe('on_attach()', function()
it('can prevent attaching to a buffer', function()
init(true)
setup_test_repo(true)
-- Functions can't be serialized over rpc so need to setup config
-- remotely
exec_lua('config = ...', config)
exec_lua[[config.on_attach = function()
return false
end]]
exec_lua('require("gitsigns").setup(config)')
wait(function()
exec_capture('au gitsigns')
end)
setup_gitsigns(config, [[
config.on_attach = function()
return false
end
]])
edit(test_file)
match_debug_messages {
"run_job: git --no-pager --version",
'run_job: git --no-pager --version',
'attach(1): Attaching (trigger=BufRead)',
p"run_job: git .* config user.name",
"run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD",
p'run_job: git .* config user.name',
'run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD',
p'run_job: git %-%-no%-pager %-%-git%-dir=.* %-%-stage %-%-others %-%-exclude%-standard .*',
"attach(1): User on_attach() returned false",
'attach(1): User on_attach() returned false',
}
end)
end)
describe('change_base()', function()
it('works', function()
screen:try_resize(20, 4)
init()
setup_test_repo()
edit(test_file)
feed('oEDIT<esc>')
@ -292,7 +287,7 @@ describe('gitsigns', function()
git{"commit", "-m", "commit on main"}
-- Don't setup gitsigns until the repo has two commits
setup(config)
setup_gitsigns(config)
check {
status = {head='master', added=0, changed=0, removed=0},
@ -313,11 +308,11 @@ describe('gitsigns', function()
before_each(function()
config.use_decoration_api = advanced_features
config.use_internal_diff = advanced_features
init()
setup_test_repo()
end)
it('apply basic signs', function()
setup(config)
setup_gitsigns(config)
edit(test_file)
command("set signcolumn=yes")
@ -339,8 +334,7 @@ describe('gitsigns', function()
end)
it('perform actions', function()
screen:try_resize(20,6)
setup(config)
setup_gitsigns(config)
edit(test_file)
command("set signcolumn=yes")
@ -406,7 +400,7 @@ describe('gitsigns', function()
it('can enable numhl', function()
config.numhl = true
setup(config)
setup_gitsigns(config)
edit(test_file)
command("set signcolumn=no")
command("set number")
@ -444,8 +438,7 @@ describe('gitsigns', function()
end)
it('attaches to newly created files', function()
screen:try_resize(4, 4)
setup(config)
setup_gitsigns(config)
edit(newfile)
match_debug_messages{
'run_job: git --no-pager --version',
@ -461,10 +454,10 @@ describe('gitsigns', function()
local messages = {
'attach(1): Attaching (trigger=BufWritePost)',
p"run_job: git .* config user.name",
"run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD",
p"run_job: git .* ls%-files .*",
"watch_index(1): Watching index",
p"run_job: git .* show :0:newfile.txt"
'run_job: git --no-pager rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD',
p'run_job: git .* ls%-files .*',
'watch_index(1): Watching index',
p'run_job: git .* show :0:newfile.txt'
}
if not advanced_features then
@ -484,8 +477,7 @@ describe('gitsigns', function()
end)
it('can add untracked files to the index', function()
screen:try_resize(10, 4)
setup(config)
setup_gitsigns(config)
edit(newfile)
feed("iline<esc>")
@ -508,8 +500,7 @@ describe('gitsigns', function()
end)
it('tracks files in new repos', function()
screen:try_resize(10, 4)
setup(config)
setup_gitsigns(config)
system{"touch", newfile}
edit(newfile)
@ -538,7 +529,7 @@ describe('gitsigns', function()
end)
it('can detach from buffers', function()
setup(config)
setup_gitsigns(config)
edit(test_file)
command("set signcolumn=yes")
@ -563,8 +554,7 @@ describe('gitsigns', function()
end)
it('can stages file with merge conflicts', function()
screen:try_resize(40, 8)
setup(config)
setup_gitsigns(config)
command("set signcolumn=yes")
-- Edit a file and commit it on main branch
@ -607,8 +597,7 @@ describe('gitsigns', function()
end)
it('handle files with spaces', function()
screen:try_resize(20,6)
setup(config)
setup_gitsigns(config)
command("set signcolumn=yes")
local spacefile = scratch..'/a b c d'
@ -643,13 +632,13 @@ describe('gitsigns', function()
describe('diff-int', testsuite(true))
it('can handle vimgrep', function()
init()
setup_test_repo()
write_to_file(scratch..'/t1.txt', {'hello ben'})
write_to_file(scratch..'/t2.txt', {'hello ben'})
write_to_file(scratch..'/t3.txt', {'hello lewis'})
setup(config)
setup_gitsigns(config)
helpers.exc_exec("vimgrep ben "..scratch..'/*')
@ -667,17 +656,4 @@ describe('gitsigns', function()
end)
-- TODO Add test for current_line_blame
-- TODO Add test for toggle_current_line_blame
-- TODO Add test for #163: signcolumn=number with signs disabled shouldn't
-- overwrite number column
-- TODO Test config.attach_to_untracked
-- TODO Add test for #194: Should not attach if file is in a custom git dir
-- TODO Add test diffthis
end)
-- vim: foldminlines=1

View File

@ -1,12 +1,12 @@
local helpers = require('test.functional.helpers')(nil)
local helpers = require('test.functional.helpers')()
local system = helpers.funcs.system
local exec_lua = helpers.exec_lua
local matches = helpers.matches
local system = helpers.funcs.system
local exec_lua = helpers.exec_lua
local matches = helpers.matches
local exec_capture = helpers.exec_capture
local eq = helpers.eq
local fn = helpers.funcs
local get_buf_var = helpers.curbufmeths.get_var
local eq = helpers.eq
local fn = helpers.funcs
local get_buf_var = helpers.curbufmeths.get_var
local timeout = 4000
@ -76,7 +76,7 @@ function M.setup_git()
M.git{'config', 'init.defaultBranch', 'master'}
end
function M.init(no_add)
function M.setup_test_repo(no_add)
M.cleanup()
system{"mkdir", M.scratch}
M.setup_git()
@ -88,7 +88,7 @@ function M.init(no_add)
end
end
function M.wait(cond)
function M.expectf(cond)
local duration = 0
local interval = 5
while duration < timeout do
@ -200,14 +200,19 @@ function M.match_dag(lines, spec)
end
function M.match_debug_messages(spec)
M.wait(function()
M.expectf(function()
M.match_lines(M.debug_messages(), spec)
end)
end
function M.setup(config)
exec_lua([[require('gitsigns').setup(...)]], config)
M.wait(function()
function M.setup_gitsigns(config, extra)
extra = extra or ''
exec_lua([[
local config = ...
]]..extra..[[
require('gitsigns').setup(...)
]], config)
M.expectf(function()
exec_capture('au gitsigns')
end)
end
@ -222,7 +227,7 @@ end
function M.check(attrs)
attrs = attrs or {}
M.wait(function()
M.expectf(function()
local status = attrs.status
local signs = attrs.signs

View File

@ -9,11 +9,11 @@ local exec_capture = helpers.exec_capture
local cleanup = helpers.cleanup
local test_config = helpers.test_config
local wait = helpers.wait
local expectf = helpers.expectf
local match_dag = helpers.match_dag
local debug_messages = helpers.debug_messages
local p = helpers.p
local setup = helpers.setup
local setup_gitsigns = helpers.setup_gitsigns
local it = helpers.it(it)
@ -61,7 +61,7 @@ describe('highlights', function()
exec_lua('gs.setup(...)', config)
wait(function()
expectf(function()
match_dag(debug_messages(), {
p'Deriving GitSignsAdd from DiffAdd',
p'Deriving GitSignsAddLn from DiffAdd',
@ -94,9 +94,9 @@ describe('highlights', function()
config.signs.topdelete.hl = nil
config.linehl = true
setup(config)
setup_gitsigns(config)
wait(function()
expectf(function()
eq('GitSignsChange xxx links to DiffChange',
exec_capture('hi GitSignsChange'))
@ -112,7 +112,7 @@ describe('highlights', function()
command('colorscheme blue')
wait(function()
expectf(function()
eq('GitSignsChange xxx links to DiffChange',
exec_capture('hi GitSignsChange'))

View File

@ -1,19 +1,19 @@
local helpers = require('test.gs_helpers')
local clear = helpers.clear
local exec_lua = helpers.exec_lua
local edit = helpers.edit
local eq = helpers.eq
local init = helpers.init
local cleanup = helpers.cleanup
local command = helpers.command
local test_config = helpers.test_config
local clear = helpers.clear
local exec_lua = helpers.exec_lua
local edit = helpers.edit
local eq = helpers.eq
local setup_test_repo = helpers.setup_test_repo
local cleanup = helpers.cleanup
local command = helpers.command
local test_config = helpers.test_config
local match_debug_messages = helpers.match_debug_messages
local p = helpers.p
local setup = helpers.setup
local test_file = helpers.test_file
local git = helpers.git
local get_buf_name = helpers.curbufmeths.get_name
local p = helpers.p
local setup_gitsigns = helpers.setup_gitsigns
local test_file = helpers.test_file
local git = helpers.git
local get_buf_name = helpers.curbufmeths.get_name
local it = helpers.it(it)
@ -30,8 +30,8 @@ describe('index_watcher', function()
end)
it('can follow moved files', function()
init()
setup(test_config)
setup_test_repo()
setup_gitsigns(test_config)
edit(test_file)
match_debug_messages {