test(runtime/health): cover lua healthchecks

- Add tests for lua healthchecks (failure, success and submodules).
- Reword some of the test naming for improved logs readability.
- Modify render test to accomodate the changes of the health autoload function.
- Add test for :checkhealth completion of Lua healtchecks.
This commit is contained in:
Javier López
2021-08-03 16:09:26 -05:00
parent 9249dcdda1
commit c65f956015
5 changed files with 159 additions and 39 deletions

View File

@@ -0,0 +1,8 @@
function! health#full_render#check()
call health#report_start("report 1")
call health#report_ok("life is fine")
call health#report_warn("no what installed", ["pip what", "make what"])
call health#report_start("report 2")
call health#report_info("stuff is stable")
call health#report_error("why no hardcopy", [":h :hardcopy", ":h :TOhtml"])
endfunction

View File

@@ -0,0 +1,11 @@
local M = {}
local health = require("health")
M.check = function()
health.report_start("report 1")
health.report_ok("everything is fine")
health.report_start("report 2")
health.report_ok("nothing to see here")
end
return M

View File

@@ -0,0 +1,11 @@
local M = {}
local health = require("health")
M.check = function()
health.report_start("report 1")
health.report_ok("everything is fine")
health.report_start("report 2")
health.report_ok("nothing to see here")
end
return M

View File

@@ -0,0 +1,12 @@
local M = {}
local health = require("health")
M.check = function()
health.report_start("report 1")
health.report_ok("everything is fine")
health.report_warn("About to add a number to nil")
local a = nil + 2
return a
end
return M

View File

@@ -1,4 +1,5 @@
local helpers = require('test.functional.helpers')(after_each) local helpers = require('test.functional.helpers')(after_each)
local global_helpers = require('test.helpers')
local Screen = require('test.functional.ui.screen') local Screen = require('test.functional.ui.screen')
local clear = helpers.clear local clear = helpers.clear
@@ -48,42 +49,34 @@ describe('health.vim', function()
command("set runtimepath+=test/functional/fixtures") command("set runtimepath+=test/functional/fixtures")
end) end)
it("health#report_*()", function()
helpers.source([[
let g:health_report = execute([
\ "call health#report_start('Check Bar')",
\ "call health#report_ok('Bar status')",
\ "call health#report_ok('Other Bar status')",
\ "call health#report_warn('Zub')",
\ "call health#report_start('Baz')",
\ "call health#report_warn('Zim', ['suggestion 1', 'suggestion 2'])"
\ ])
]])
local result = helpers.eval("g:health_report")
helpers.eq(helpers.dedent([[
## Check Bar
- OK: Bar status
- OK: Other Bar status
- WARNING: Zub
## Baz
- WARNING: Zim
- ADVICE:
- suggestion 1
- suggestion 2]]),
result)
end)
describe(":checkhealth", function() describe(":checkhealth", function()
it("concatenates multiple reports", function() it("functions health#report_*() render correctly", function()
command("checkhealth success1 success2") command("checkhealth full_render")
helpers.expect([[ helpers.expect([[
health#success1#check full_render: health#full_render#check
========================================================================
## report 1
- OK: life is fine
- WARNING: no what installed
- ADVICE:
- pip what
- make what
## report 2
- INFO: stuff is stable
- ERROR: why no hardcopy
- ADVICE:
- :help |:hardcopy|
- :help |:TOhtml|
]])
end)
it("concatenates multiple reports", function()
command("checkhealth success1 success2 test_plug")
helpers.expect([[
success1: health#success1#check
======================================================================== ========================================================================
## report 1 ## report 1
- OK: everything is fine - OK: everything is fine
@@ -91,25 +84,109 @@ describe('health.vim', function()
## report 2 ## report 2
- OK: nothing to see here - OK: nothing to see here
health#success2#check success2: health#success2#check
======================================================================== ========================================================================
## another 1 ## another 1
- OK: ok - OK: ok
test_plug: require("test_plug.health").check()
========================================================================
## report 1
- OK: everything is fine
## report 2
- OK: nothing to see here
]]) ]])
end) end)
it("lua plugins", function()
command("checkhealth test_plug")
helpers.expect([[
test_plug: require("test_plug.health").check()
========================================================================
## report 1
- OK: everything is fine
## report 2
- OK: nothing to see here
]])
end)
it("lua plugins submodules", function()
command("checkhealth test_plug.submodule")
helpers.expect([[
test_plug.submodule: require("test_plug.submodule.health").check()
========================================================================
## report 1
- OK: everything is fine
## report 2
- OK: nothing to see here
]])
end)
it("lua plugins submodules with expression '*'", function()
command("checkhealth test_plug*")
local buf_lines = helpers.curbuf('get_lines', 0, -1, true)
-- avoid dealing with path separators
local received = table.concat(buf_lines, '\n', 1, #buf_lines - 2)
local expected = helpers.dedent([[
test_plug: require("test_plug.health").check()
========================================================================
## report 1
- OK: everything is fine
## report 2
- OK: nothing to see here
test_plug.submodule: require("test_plug.submodule.health").check()
========================================================================
## report 1
- OK: everything is fine
## report 2
- OK: nothing to see here
test_plug.submodule_failed: require("test_plug.submodule_failed.health").check()
========================================================================
- ERROR: Failed to run healthcheck for "test_plug.submodule_failed" plugin. Exception:
function health#check, line 24]])
eq(expected, received)
end)
it("gracefully handles broken healthcheck", function() it("gracefully handles broken healthcheck", function()
command("checkhealth broken") command("checkhealth broken")
helpers.expect([[ helpers.expect([[
health#broken#check broken: health#broken#check
======================================================================== ========================================================================
- ERROR: Failed to run healthcheck for "broken" plugin. Exception: - ERROR: Failed to run healthcheck for "broken" plugin. Exception:
function health#check[21]..health#broken#check, line 1 function health#check[24]..health#broken#check, line 1
caused an error caused an error
]]) ]])
end) end)
it("gracefully handles broken lua healthcheck", function()
command("checkhealth test_plug.submodule_failed")
local buf_lines = helpers.curbuf('get_lines', 0, -1, true)
local received = table.concat(buf_lines, '\n', 1, #buf_lines - 2)
-- avoid dealing with path separators
local lua_err = "attempt to perform arithmetic on a nil value"
local last_line = buf_lines[#buf_lines - 1]
assert(string.find(last_line, lua_err) ~= nil, "Lua error not present")
local expected = global_helpers.dedent([[
test_plug.submodule_failed: require("test_plug.submodule_failed.health").check()
========================================================================
- ERROR: Failed to run healthcheck for "test_plug.submodule_failed" plugin. Exception:
function health#check, line 24]])
eq(expected, received)
end)
it("highlights OK, ERROR", function() it("highlights OK, ERROR", function()
local screen = Screen.new(72, 10) local screen = Screen.new(72, 10)
screen:attach() screen:attach()
@@ -126,11 +203,11 @@ describe('health.vim', function()
command("set laststatus=0") command("set laststatus=0")
screen:expect{grid=[[ screen:expect{grid=[[
^ | ^ |
{Heading:health#foo#check} | {Heading:foo: } |
{Bar:========================================================================}| {Bar:========================================================================}|
{Bullet: -} {Error:ERROR:} No healthcheck found for "foo" plugin. | {Bullet: -} {Error:ERROR:} No healthcheck found for "foo" plugin. |
| |
{Heading:health#success1#check} | {Heading:success1: health#success1#check} |
{Bar:========================================================================}| {Bar:========================================================================}|
{Heading2:##}{Heading: report 1} | {Heading2:##}{Heading: report 1} |
{Bullet: -} {Ok:OK:} everything is fine | {Bullet: -} {Ok:OK:} everything is fine |
@@ -140,9 +217,10 @@ describe('health.vim', function()
it("gracefully handles invalid healthcheck", function() it("gracefully handles invalid healthcheck", function()
command("checkhealth non_existent_healthcheck") command("checkhealth non_existent_healthcheck")
-- luacheck: ignore 613
helpers.expect([[ helpers.expect([[
health#non_existent_healthcheck#check non_existent_healthcheck:
======================================================================== ========================================================================
- ERROR: No healthcheck found for "non_existent_healthcheck" plugin. - ERROR: No healthcheck found for "non_existent_healthcheck" plugin.
]]) ]])