Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): tidy up #824

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 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)
helpers.env()

--- @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
2 changes: 1 addition & 1 deletion test/busted/output_handler.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local pretty = require 'pl.pretty'
local global_helpers = require('test.helpers')()
local global_helpers = require('test.helpers')

-- Colors are disabled by default. #15610
local colors = require 'term.colors'
Expand Down
12 changes: 0 additions & 12 deletions test/compat.lua

This file was deleted.

2 changes: 1 addition & 1 deletion test/gitdir_watcher_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local setup_gitsigns = helpers.setup_gitsigns
local test_file = helpers.test_file
local git = helpers.git

local it = helpers.it(it)
helpers.env()

local function get_bufs()
local bufs = {}
Expand Down
76 changes: 25 additions & 51 deletions test/gitsigns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ local test_config = helpers.test_config
local check = helpers.check
local eq = helpers.eq

local it = helpers.it(it)
helpers.env()

describe('gitsigns', function()
local screen
Expand Down 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
36 changes: 12 additions & 24 deletions test/gs_helpers.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
local helpers = require('test.helpers')()

local system = helpers.funcs.system
local exec_lua = helpers.exec_lua
local matches = helpers.matches
local eq = helpers.eq
local fn = helpers.funcs
local get_buf_var = helpers.curbufmeths.get_var
local helpers = require'test.helpers'

local timeout = 8000

local M = helpers

M.inspect = require('vim.inspect')
local exec_lua = helpers.exec_lua
local matches = helpers.matches
local eq = helpers.eq
local get_buf_var = helpers.curbufmeths.get_var
local system = helpers.funcs.system

M.scratch = os.getenv('PJ_ROOT')..'/scratch'
M.gitdir = M.scratch..'/.git'
Expand Down Expand Up @@ -106,12 +103,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 +120,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 +183,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 +194,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 All @@ -227,16 +221,10 @@ function M.setup_gitsigns(config, extra)
end)
end

local id = 0
M.it = function(it)
return function(name, test)
id = id+1
return it(name..' #'..id..'#', test)
end
end

function M.check(attrs, interval)
attrs = attrs or {}
local fn = helpers.funcs

M.expectf(function()
local status = attrs.status
local signs = attrs.signs
Expand Down Expand Up @@ -293,7 +281,7 @@ function M.check(attrs, interval)
end
end

eq(signs, act, M.inspect(buf_signs))
eq(signs, act, vim.inspect(buf_signs))
end
end, interval)
end
Expand Down
Loading