diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2016-08-20 19:37:18 -0400 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2016-08-21 22:03:28 -0400 |
commit | ed49d9d866f8260842ea177fa9ce31dbc398701d (patch) | |
tree | 55073aa69bc14ff0df1acecad432b66d6ff20c23 | |
parent | 545e7a416310c9ff700b2afed9eef834c8948c8b (diff) | |
download | rneovim-ed49d9d866f8260842ea177fa9ce31dbc398701d.tar.gz rneovim-ed49d9d866f8260842ea177fa9ce31dbc398701d.tar.bz2 rneovim-ed49d9d866f8260842ea177fa9ce31dbc398701d.zip |
CheckHealth: Accept [plugin1 [, plugin2 [, ...]]] args.
To healthcheck the "foo" plugin:
:CheckHealth foo
To healthcheck the "foo" and "bar" plugins:
:CheckHealth foo bar
To run all auto-discovered healthchecks:
:CheckHealth
-rw-r--r-- | runtime/autoload/health.vim | 108 | ||||
-rw-r--r-- | runtime/doc/vim_diff.txt | 1 | ||||
-rw-r--r-- | runtime/plugin/health.vim | 2 | ||||
-rw-r--r-- | test/functional/fixtures/autoload/health/broken.vim | 3 | ||||
-rw-r--r-- | test/functional/fixtures/autoload/health/success1.vim | 6 | ||||
-rw-r--r-- | test/functional/fixtures/autoload/health/success2.vim | 4 | ||||
-rw-r--r-- | test/functional/plugin/health_spec.lua | 100 |
7 files changed, 138 insertions, 86 deletions
diff --git a/runtime/autoload/health.vim b/runtime/autoload/health.vim index a94688cbf9..d2f36b2f65 100644 --- a/runtime/autoload/health.vim +++ b/runtime/autoload/health.vim @@ -20,46 +20,49 @@ function! s:enhance_syntax() abort highlight link healthSuggestion String endfunction -" Runs the health checkers. Manages the output and buffer-local settings. -function! health#check(bang) abort - let l:report = '' +" Runs the specified healthchecks. +" Runs all discovered healthchecks if a:plugin_names is empty. +function! health#check(plugin_names) abort + let report = '' - if empty(g:health_checkers) - call health#add_checker(s:_default_checkers()) - endif - - for l:checker in items(g:health_checkers) - " Disabled checkers will not run their registered check functions - if l:checker[1] - let s:current_checker = l:checker[0] - let l:report .= printf("\n%s\n================================================================================", - \ s:current_checker) - - let l:report .= execute('call ' . l:checker[0] . '()') - endif - endfor + let healthchecks = empty(a:plugin_names) + \ ? s:discover_health_checks() + \ : s:to_fn_names(a:plugin_names) - if a:bang - new - setlocal bufhidden=wipe - set filetype=markdown - call s:enhance_syntax() - call setline(1, split(report, "\n")) - setlocal nomodified + if empty(healthchecks) + let report = "ERROR: No healthchecks found." else - echo report - echo "\nTip: Use " - echohl Identifier - echon ':CheckHealth!' - echohl None - echon ' to open this in a new buffer.' + for c in healthchecks + let report .= printf("\n%s\n%s", c, repeat('=',80)) + try + let report .= execute('call '.c.'()') + catch /^Vim\%((\a\+)\)\=:E117/ + let report .= execute( + \ 'call health#report_error(''No healthcheck found for "' + \ .s:to_plugin_name(c) + \ .'" plugin.'')') + catch + let report .= execute( + \ 'call health#report_error(''Failed to run healthcheck for "' + \ .s:to_plugin_name(c) + \ .'" plugin. Exception:''."\n".v:exception)') + endtry + let report .= "\n" + endfor endif + + new + setlocal bufhidden=wipe + set filetype=markdown + call s:enhance_syntax() + call setline(1, split(report, "\n")) + setlocal nomodified endfunction " Starts a new report. -function! health#report_start(name) abort " {{{ +function! health#report_start(name) abort echo "\n## " . a:name -endfunction " }}} +endfunction " Indents lines *except* line 1 of a string if it contains newlines. function! s:indent_after_line1(s, columns) abort @@ -102,12 +105,12 @@ function! health#report_info(msg) abort " {{{ echo s:format_report_message('INFO', a:msg) endfunction " }}} -" Use {msg} to represent the check that has passed +" Reports a successful healthcheck. function! health#report_ok(msg) abort " {{{ echo s:format_report_message('SUCCESS', a:msg) endfunction " }}} -" Use {msg} to represent a failed health check and optionally a list of suggestions on how to fix it. +" Reports a health warning. function! health#report_warn(msg, ...) abort " {{{ if a:0 > 0 echo s:format_report_message('WARNING', a:msg, a:1) @@ -116,7 +119,7 @@ function! health#report_warn(msg, ...) abort " {{{ endif endfunction " }}} -" Use {msg} to represent a critically failed health check and optionally a list of suggestions on how to fix it. +" Reports a failed healthcheck. function! health#report_error(msg, ...) abort " {{{ if a:0 > 0 echo s:format_report_message('ERROR', a:msg, a:1) @@ -181,19 +184,28 @@ function! health#disable_checker(checker_name) abort " {{{ endif endfunction " }}} -function! s:change_file_name_to_health_checker(name) abort " {{{ - return substitute(substitute(substitute(a:name, ".*autoload/", "", ""), "\\.vim", "#check", ""), "/", "#", "g") -endfunction " }}} +function! s:filepath_to_function(name) abort + return substitute(substitute(substitute(a:name, ".*autoload/", "", ""), + \ "\\.vim", "#check", ""), "/", "#", "g") +endfunction -function! s:_default_checkers() abort " {{{ - " Get all of the files that are in autoload/health/ folders with a vim - " suffix - let checker_files = globpath(&runtimepath, 'autoload/health/*.vim', 1, 1) - let temp = checker_files[0] +function! s:discover_health_checks() abort + let healthchecks = globpath(&runtimepath, 'autoload/health/*.vim', 1, 1) + let healthchecks = map(healthchecks, '<SID>filepath_to_function(v:val)') + return healthchecks +endfunction - let checkers_to_source = [] - for file_name in checker_files - call add(checkers_to_source, s:change_file_name_to_health_checker(file_name)) +" Translates a list of plugin names to healthcheck function names. +function! s:to_fn_names(plugin_names) abort + let healthchecks = [] + for p in a:plugin_names + call add(healthchecks, 'health#'.p.'#check') endfor - return checkers_to_source -endfunction " }}} + return healthchecks +endfunction + +" Extracts 'foo' from 'health#foo#check'. +function! s:to_plugin_name(fn_name) abort + return substitute(a:fn_name, + \ '\v.*health\#(.+)\#check.*', '\1', '') +endfunction diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index 2b1c66d0c1..8ed8a7f64c 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -107,6 +107,7 @@ Events: |TabClosed| |TermOpen| |TermClose| + |TextYankPost| Highlight groups: |hl-EndOfBuffer| diff --git a/runtime/plugin/health.vim b/runtime/plugin/health.vim index db094a03a4..3c8e509acd 100644 --- a/runtime/plugin/health.vim +++ b/runtime/plugin/health.vim @@ -1 +1 @@ -command! -bang CheckHealth call health#check(<bang>0) +command! -nargs=* CheckHealth call health#check([<f-args>]) diff --git a/test/functional/fixtures/autoload/health/broken.vim b/test/functional/fixtures/autoload/health/broken.vim new file mode 100644 index 0000000000..a2a595b96f --- /dev/null +++ b/test/functional/fixtures/autoload/health/broken.vim @@ -0,0 +1,3 @@ +function! health#broken#check() + throw 'caused an error' +endfunction diff --git a/test/functional/fixtures/autoload/health/success1.vim b/test/functional/fixtures/autoload/health/success1.vim new file mode 100644 index 0000000000..a360347455 --- /dev/null +++ b/test/functional/fixtures/autoload/health/success1.vim @@ -0,0 +1,6 @@ +function! health#success1#check() + call health#report_start("report 1") + call health#report_ok("everything is fine") + call health#report_start("report 2") + call health#report_ok("nothing to see here") +endfunction diff --git a/test/functional/fixtures/autoload/health/success2.vim b/test/functional/fixtures/autoload/health/success2.vim new file mode 100644 index 0000000000..b742b4879d --- /dev/null +++ b/test/functional/fixtures/autoload/health/success2.vim @@ -0,0 +1,4 @@ +function! health#success2#check() + call health#report_start("another 1") + call health#report_ok("ok") +endfunction diff --git a/test/functional/plugin/health_spec.lua b/test/functional/plugin/health_spec.lua index 50fbfd58ee..a9665cd751 100644 --- a/test/functional/plugin/health_spec.lua +++ b/test/functional/plugin/health_spec.lua @@ -4,52 +4,78 @@ local plugin_helpers = require('test.functional.plugin.helpers') describe('health.vim', function() before_each(function() plugin_helpers.reset() + -- Provides functions: + -- health#broken#check() + -- health#success1#check() + -- health#success2#check() + helpers.execute("set runtimepath+=test/functional/fixtures") end) - it('reports results', function() - helpers.execute("call health#report_start('Foo')") - local report = helpers.redir_exec([[call health#report_start('Check Bar')]]) - .. helpers.redir_exec([[call health#report_ok('Bar status')]]) - .. helpers.redir_exec([[call health#report_ok('Other Bar status')]]) - .. helpers.redir_exec([[call health#report_warn('Zub')]]) - .. helpers.redir_exec([[call health#report_start('Baz')]]) - .. helpers.redir_exec([[call health#report_warn('Zim', ['suggestion 1', 'suggestion 2'])]]) - - local expected_contents = { - 'Checking: Check Bar', - 'SUCCESS: Bar status', - 'WARNING: Zub', - 'SUGGESTIONS:', - '- suggestion 1', - '- suggestion 2' - } - - for _, content in ipairs(expected_contents) do - assert(string.find(report, content)) - end + it("reports", 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 + - SUCCESS: Bar status + - SUCCESS: Other Bar status + - WARNING: Zub + + ## Baz + - WARNING: Zim + - SUGGESTIONS: + - suggestion 1 + - suggestion 2]]), + result) end) - describe(':CheckHealth', function() - -- Run it here because it may be slow, depending on the system. - helpers.execute([[CheckHealth!]]) - local report = helpers.curbuf_contents() - local health_checkers = helpers.redir_exec("echo g:health_checkers") + describe(":CheckHealth", function() + it("concatenates multiple reports", function() + helpers.execute("CheckHealth success1 success2") + helpers.expect([[ + health#success1#check + ================================================================================ - it('finds the default checker', function() - assert(string.find(health_checkers, "'health#nvim#check': v:true")) - end) + ## report 1 + - SUCCESS: everything is fine - it('prints a header with the name of the checker', function() - assert(string.find(report, 'health#nvim#check')) + ## report 2 + - SUCCESS: nothing to see here + + health#success2#check + ================================================================================ + + ## another 1 + - SUCCESS: ok]]) end) - end) - it('allows users to disable checkers', function() - helpers.execute("call health#disable_checker('health#nvim#check')") - helpers.execute("CheckHealth!") - local health_checkers = helpers.redir_exec("echo g:health_checkers") + it("gracefully handles broken healthcheck", function() + helpers.execute("CheckHealth broken") + helpers.expect([[ + health#broken#check + ================================================================================ + - ERROR: Failed to run healthcheck for "broken" plugin. Exception: + caused an error]]) + end) - assert(string.find(health_checkers, "'health#nvim#check': v:false")) + it("gracefully handles invalid healthcheck", function() + helpers.execute("CheckHealth non_existent_healthcheck") + helpers.expect([[ + health#non_existent_healthcheck#check + ================================================================================ + - ERROR: No healthcheck found for "non_existent_healthcheck" plugin.]]) + end) end) end) |