-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhelper.lua
More file actions
110 lines (104 loc) · 3.18 KB
/
Copy pathhelper.lua
File metadata and controls
110 lines (104 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
local M = {}
local api = vim.api
function M.run_fmt(name, ft, input, opts)
opts = opts or {}
local config = require('guard-collection.formatter')[name]
assert(config, 'unknown formatter: ' .. name)
assert(not config.fn, name .. ' uses custom fn, not testable this way')
local cmd = vim.list_extend({ config.cmd }, config.args or {})
local dir = opts.tmpdir or '/tmp'
local tmpfile = dir .. '/guard-test.' .. ft
if config.fname then
vim.fn.writefile(input, tmpfile)
table.insert(cmd, tmpfile)
end
local result = vim
.system(cmd, {
stdin = config.stdin and (table.concat(input, '\n') .. '\n') or nil,
cwd = opts.cwd,
})
:wait()
assert(result.code == 0, name .. ' exited ' .. result.code .. ': ' .. (result.stderr or ''))
if config.stdin then
return vim.split(result.stdout, '\n', { trimempty = true })
else
return vim.fn.readfile(tmpfile)
end
end
function M.run_lint(name, ft, input, opts)
opts = opts or {}
local linter = require('guard-collection.linter')[name]
assert(linter, 'unknown linter: ' .. name)
assert(not linter.fn, name .. ' uses custom fn — test parse() directly instead')
local input_str = table.concat(input, '\n') .. '\n'
local dir = opts.tmpdir or '/tmp'
local tmpfile = dir .. '/guard-test.' .. ft
vim.fn.writefile(input, tmpfile)
local bufnr = api.nvim_create_buf(false, true)
local cmd = vim.list_extend({ linter.cmd }, linter.args or {})
if linter.fname then
table.insert(cmd, tmpfile)
end
local result = vim
.system(cmd, {
stdin = linter.stdin and input_str or nil,
cwd = opts.cwd,
})
:wait()
local output = result.stdout or ''
if output == '' then
output = result.stderr or ''
end
local diags = linter.parse(output, bufnr)
return bufnr, diags
end
function M.run_lint_fn(name, ft, input, assert_fn)
local linter = require('guard-collection.linter')[name]
assert(linter, 'unknown linter: ' .. name)
assert(linter.fn, name .. ' does not use custom fn')
local tmpfile = '/tmp/guard-test.' .. ft
vim.fn.writefile(input, tmpfile)
local bufnr = api.nvim_create_buf(false, true)
local done = false
local co = coroutine.create(function()
local output = linter.fn(nil, tmpfile)
local diags = linter.parse(output, bufnr)
assert_fn(bufnr, diags)
done = true
end)
coroutine.resume(co)
vim.wait(5000, function()
return done
end)
assert(done, name .. ' fn timed out')
end
function M.assert_diag(d, expect)
local a = require('luassert')
if expect.bufnr then
a.equal(expect.bufnr, d.bufnr)
end
if expect.source then
a.equal(expect.source, d.source)
end
if expect.severity then
a.equal(expect.severity, d.severity)
end
if expect.lnum then
a.equal(expect.lnum, d.lnum)
end
if expect.code then
a.equal(expect.code, d.code)
end
if expect.message_pat then
a.is_true(
d.message ~= nil and d.message:find(expect.message_pat, 1, true) ~= nil,
('expected message containing %q, got %q'):format(expect.message_pat, d.message or '')
)
end
end
function M.get_linter(name)
local linter = require('guard-collection.linter')[name]
assert(linter, 'unknown linter: ' .. name)
return linter
end
return M