Repository navigation
Expand file tree
/
Copy pathhealth_test.lua
More file actions
130 lines (110 loc) · 4.02 KB
/
Copy pathhealth_test.lua
File metadata and controls
130 lines (110 loc) · 4.02 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
---@diagnostic disable: duplicate-set-field
local helpers = require('helpers')
---Replace vim.health with a capturing stub. Returns the report and a restore fn.
local function capture_health()
local report = { sections = {} }
local current
local original = vim.health
vim.health = {
start = function(name)
current = { name = name, items = {} }
table.insert(report.sections, current)
end,
ok = function(msg) table.insert(current.items, { kind = 'ok', msg = msg }) end,
warn = function(msg, advice)
table.insert(current.items, { kind = 'warn', msg = msg, advice = advice })
end,
error = function(msg, advice)
table.insert(current.items, { kind = 'error', msg = msg, advice = advice })
end,
info = function(msg) table.insert(current.items, { kind = 'info', msg = msg }) end,
}
return report, function() vim.health = original end
end
local function section(report, name)
for _, sec in ipairs(report.sections) do
if sec.name == name then
return sec
end
end
return nil
end
---True when `sec` has an item of `kind` whose message contains every needle.
local function has_item(sec, kind, needles)
for _, item in ipairs(sec.items) do
if item.kind == kind then
local matched = true
for _, needle in ipairs(needles) do
if not item.msg:find(needle, 1, true) then
matched = false
break
end
end
if matched then
return true
end
end
end
return false
end
---Run health.check() against a freshly-required health module.
local function run_check()
package.loaded['zpack.health'] = nil
require('zpack.health').check()
end
describe("Health Check", function()
before_each(helpers.setup_test_env)
after_each(helpers.cleanup_test_env)
it("check() produces every report section without error", function()
local report, restore = capture_health()
local ok, err = pcall(run_check)
restore()
assert.is_truthy(ok, "health.check() should not throw: " .. tostring(err))
for _, name in ipairs({ 'Environment', 'Setup', 'Configuration', 'Plugins', 'Reporting a bug' }) do
assert.is_not_nil(section(report, name), "missing section: " .. name)
end
end)
it("environment section reports Neovim and vim.pack as OK", function()
local report, restore = capture_health()
run_check()
restore()
local env = section(report, 'Environment')
assert.is_truthy(has_item(env, 'ok', { 'Neovim' }), "Neovim version should be OK")
assert.is_truthy(has_item(env, 'ok', { 'vim.pack' }), "vim.pack should be OK")
end)
it("warns when setup() has not been called", function()
local report, restore = capture_health()
run_check()
restore()
local setup = section(report, 'Setup')
assert.is_truthy(has_item(setup, 'warn', { 'setup()' }),
"Setup section should warn that setup() was not called")
end)
it("after a valid setup, config is OK and plugins are counted", function()
require('zpack').setup({
spec = { { 'test/plugin' } },
defaults = { confirm = false },
})
helpers.flush_pending()
local report, restore = capture_health()
run_check()
restore()
local setup = section(report, 'Setup')
assert.is_truthy(has_item(setup, 'ok', { 'setup()' }), "Setup should be OK")
local config = section(report, 'Configuration')
assert.is_truthy(has_item(config, 'ok', { 'valid' }), "Configuration should be valid")
local plugins = section(report, 'Plugins')
assert.is_truthy(has_item(plugins, 'ok', { 'plugin(s) registered' }),
"Plugins section should report a registered count")
end)
it("surfaces an invalid merged config", function()
require('zpack').setup({ spec = {}, defaults = { confirm = 'yes' } })
helpers.flush_pending()
local report, restore = capture_health()
run_check()
restore()
local config = section(report, 'Configuration')
assert.is_truthy(has_item(config, 'error', { 'defaults.confirm' }),
"Configuration section should report the bad defaults.confirm field")
end)
end)