robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
20.3 KiB583 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear = n.clear
7local curbuf_contents = n.curbuf_contents
8local command = n.command
9local eq, matches = t.eq, t.matches
10local getcompletion = n.fn.getcompletion
11local insert = n.insert
12local exec_lua = n.exec_lua
13local source = n.source
14local assert_alive = n.assert_alive
15local fn = n.fn
16local api = n.api
17local retry = t.retry
18
19describe(':checkhealth', function()
20  it('detects invalid $VIMRUNTIME', function()
21    clear({
22      env = { VIMRUNTIME = 'bogus' },
23    })
24    local status, err = pcall(command, 'checkhealth')
25    eq(false, status)
26    eq('Invalid $VIMRUNTIME: bogus', string.match(err, 'Invalid.*'))
27  end)
28
29  it("detects invalid 'runtimepath'", function()
30    clear()
31    command('set runtimepath=bogus')
32    local status, err = pcall(command, 'checkhealth')
33    eq(false, status)
34    eq("Invalid 'runtimepath'", string.match(err, 'Invalid.*'))
35  end)
36
37  it('detects invalid $VIM', function()
38    clear()
39    -- Do this after startup, otherwise it just breaks $VIMRUNTIME.
40    command("let $VIM='zub'")
41    command('checkhealth vim.health')
42    retry(nil, 10000, function()
43      matches('ERROR $VIM .* zub', curbuf_contents())
44    end)
45  end)
46
47  it('getcompletion()', function()
48    clear { args = { '-u', 'NORC', '+set runtimepath+=test/functional/fixtures' } }
49
50    eq('nvim.zip', getcompletion('vim', 'checkhealth')[1])
51    eq('vim.provider', getcompletion('vim.prov', 'checkhealth')[1])
52    eq('vim.lsp', getcompletion('vim.ls', 'checkhealth')[1])
53
54    -- "test_plug/health/init.lua" should complete as "test_plug", not "test_plug.health". #30342
55    eq({
56      'test_plug',
57      'test_plug.full_render',
58      'test_plug.submodule',
59      'test_plug.submodule_empty',
60      'test_plug.success1',
61      'test_plug.success2',
62    }, getcompletion('test_plug', 'checkhealth'))
63  end)
64
65  it('completion checks for vim.health._complete() return type #28456', function()
66    clear()
67    exec_lua([[vim.health._complete = function() return 1 end]])
68    eq({}, getcompletion('', 'checkhealth'))
69    exec_lua([[vim.health._complete = function() return { 1 } end]])
70    eq({}, getcompletion('', 'checkhealth'))
71    assert_alive()
72  end)
73
74  it('cmdline completion works with multiple args #35054', function()
75    clear()
76    n.feed(':checkhealth vim.ls<Tab>')
77    eq('checkhealth vim.lsp', fn.getcmdline())
78    n.feed(' vim.prov<Tab>')
79    eq('checkhealth vim.lsp vim.provider', fn.getcmdline())
80  end)
81
82  it('vim.g.health', function()
83    clear {
84      args_rm = { '-u' },
85      args = { '--clean', '+set runtimepath+=test/functional/fixtures' },
86    }
87    command("let g:health = {'style':'float'}")
88    command('checkhealth lsp')
89    eq(
90      'editor',
91      exec_lua([[
92      return vim.api.nvim_win_get_config(0).relative
93    ]])
94    )
95    matches('health%.lua:%d+>$', fn.maparg('q', 'n', false, false))
96
97    -- gO should not close the :checkhealth floating window. #34784
98    command('checkhealth full_render')
99    local win = api.nvim_get_current_win()
100    api.nvim_win_set_cursor(win, { 5, 1 })
101    n.feed('gO')
102    eq(true, api.nvim_win_is_valid(win))
103    eq('qf', api.nvim_get_option_value('filetype', { buf = 0 }))
104  end)
105
106  it("vim.provider works with a misconfigured 'shell'", function()
107    clear()
108    command([[set shell=echo\ WRONG!!!]])
109    command('let g:loaded_perl_provider = 0')
110    command('let g:loaded_python3_provider = 0')
111    command('checkhealth vim.provider')
112    eq(nil, string.match(curbuf_contents(), 'WRONG!!!'))
113  end)
114
115  it('vim.lsp warns about unknown filetypes', function()
116    clear()
117    exec_lua(function()
118      vim.filetype.add({ extension = { mdx = 'mdx' } })
119      vim.lsp.config('builtin_registry_ft', {
120        cmd = { 'true' },
121        filetypes = { 'beancount' },
122      })
123      vim.lsp.config('custom_ft', {
124        cmd = { 'true' },
125        filetypes = { 'mdx' },
126      })
127      vim.lsp.config('bad_ft', {
128        cmd = { 'true' },
129        filetypes = { 'hbs' },
130      })
131      vim.lsp.enable('builtin_registry_ft')
132      vim.lsp.enable('custom_ft')
133      vim.lsp.enable('bad_ft')
134    end)
135    command('checkhealth vim.lsp')
136    local report = curbuf_contents()
137    eq(nil, report:find("Unknown filetype 'beancount'", 1, true))
138    eq(nil, report:find("Unknown filetype 'mdx'", 1, true))
139    eq(
140      true,
141      report:find("Unknown filetype 'hbs' (Hint: filename extension != filetype).", 1, true) ~= nil
142    )
143    eq(true, report:find('- builtin_registry_ft:', 1, true) ~= nil)
144    eq(true, report:find('- custom_ft:', 1, true) ~= nil)
145    eq(true, report:find('- bad_ft:', 1, true) ~= nil)
146  end)
147
148  it('renders report when Nvim starts with -M', function()
149    clear { args = { '-M' } }
150    command('checkhealth lsp')
151    eq(true, curbuf_contents() ~= '')
152  end)
153end)
154
155describe('vim.health', function()
156  before_each(function()
157    clear { args = { '-u', 'NORC', '+set runtimepath+=test/functional/fixtures' } }
158  end)
159
160  describe(':checkhealth', function()
161    it('does not block on vim.health subprocesses', function()
162      exec_lua(function()
163        local system = vim.system
164        vim.system = function(cmd, opts, on_exit)
165          if cmd[1] == 'git' and cmd[2] == 'ls-remote' then
166            _G.resolve_health_system = function()
167              vim.system = system
168              on_exit({ code = 1, signal = 0, stdout = '', stderr = 'failed' })
169            end
170            return
171          end
172          return system(cmd, opts, on_exit)
173        end
174      end)
175
176      command('checkhealth vim.health')
177      eq('function', exec_lua('return type(_G.resolve_health_system)'))
178      eq(true, api.nvim_get_option_value('modifiable', { buf = 0 }))
179
180      command('let g:healthcheck_responsive = v:true')
181      eq(true, exec_lua('return vim.g.healthcheck_responsive'))
182      exec_lua('_G.resolve_health_system()')
183
184      retry(nil, 5000, function()
185        eq(false, api.nvim_get_option_value('modifiable', { buf = 0 }))
186      end)
187      eq('checkhealth', api.nvim_get_option_value('filetype', { buf = 0 }))
188      matches('vim%.health:', curbuf_contents())
189    end)
190
191    it('report_xx() renders correctly', function()
192      command('checkhealth full_render')
193      n.expect([[
194      ==============================================================================
195      test_plug.full_render:                                              1 ⚠️  1 ❌
196
197      report 1 ~
198      - ✅ OK life is fine
199      - ⚠️ WARNING no what installed
200        - ADVICE:
201          - pip what
202          - make what
203
204      report 2 ~
205      - stuff is stable
206      - ❌ ERROR why no hardcopy
207        - ADVICE:
208          - :help |:hardcopy|
209          - :help |:TOhtml|
210      ]])
211    end)
212
213    it('user FileType handler can modify report', function()
214      -- Define a FileType autocmd that removes emoji chars.
215      source [[
216        autocmd FileType checkhealth :set modifiable | silent! %s/\v( ?[^\x00-\x7F])//g
217        checkhealth full_render
218      ]]
219      n.expect([[
220      ==============================================================================
221      test_plug.full_render:                                              1  1
222
223      report 1 ~
224      - OK life is fine
225      - WARNING no what installed
226        - ADVICE:
227          - pip what
228          - make what
229
230      report 2 ~
231      - stuff is stable
232      - ERROR why no hardcopy
233        - ADVICE:
234          - :help |:hardcopy|
235          - :help |:TOhtml|
236      ]])
237    end)
238
239    it('concatenates multiple reports', function()
240      command('checkhealth success1 success2 test_plug')
241      retry(nil, 5000, function()
242        eq(false, api.nvim_get_option_value('modifiable', { buf = 0 }))
243      end)
244      n.expect([[
245        ==============================================================================
246        test_plug:                                                                  ✅
247
248        report 1 ~
249        - ✅ OK everything is fine
250
251        report 2 ~
252        - ✅ OK nothing to see here
253
254        ==============================================================================
255        test_plug.success1:                                                         ✅
256
257        report 1 ~
258        - ✅ OK everything is fine
259
260        report 2 ~
261        - ✅ OK nothing to see here
262
263        ==============================================================================
264        test_plug.success2:                                                         ✅
265
266        another 1 ~
267        - ✅ OK ok
268        ]])
269    end)
270
271    it('lua plugins submodules', function()
272      command('checkhealth test_plug.submodule')
273      n.expect([[
274        ==============================================================================
275        test_plug.submodule:                                                        ✅
276
277        report 1 ~
278        - ✅ OK everything is fine
279
280        report 2 ~
281        - ✅ OK nothing to see here
282        ]])
283    end)
284
285    it('... including empty reports', function()
286      command('checkhealth test_plug.submodule_empty')
287      n.expect([[
288      ==============================================================================
289      test_plug.submodule_empty:                                                1 ❌
290
291      - ❌ ERROR The healthcheck report for "test_plug.submodule_empty" plugin is empty.
292      ]])
293    end)
294
295    it('highlights OK, ERROR', function()
296      local screen = Screen.new(50, 11)
297      screen:set_default_attr_ids({
298        h1 = { reverse = true },
299        h2 = { foreground = tonumber('0x6a0dad') },
300        Ok = { foreground = Screen.colors.LightGreen },
301        Error = { foreground = Screen.colors.Red },
302        Done = { foreground = Screen.colors.NvimDarkGreen },
303        Bar = { foreground = Screen.colors.LightGrey, background = Screen.colors.DarkGrey },
304      })
305      command('checkhealth foo success1')
306      -- Wait for the filetype plugin before changing window options.
307      retry(nil, 5000, function()
308        eq('checkhealth', api.nvim_get_option_value('filetype', { buf = 0 }))
309      end)
310      command('set nofoldenable nowrap laststatus=0')
311      screen:expect {
312        grid = [[
313        {Bar:^                                                  }|
314        {h1:foo:                                              }|
315                                                          |
316        - ❌ {Error:ERROR} No healthcheck found for "foo" plugin. |
317                                                          |
318        {Bar:                                                  }|
319        {h1:test_plug.success1:                               }|
320                                                          |
321        {h2:report 1}                                          |
322        - ✅ {Ok:OK} everything is fine                        |
323        {Done:checkhealth}: checks done                          |
324      ]],
325      }
326    end)
327
328    it('gracefully handles invalid healthcheck', function()
329      command('checkhealth non_existent_healthcheck')
330      -- luacheck: ignore 613
331      n.expect([[
332        ==============================================================================
333        non_existent_healthcheck:                                                 1 ❌
334
335        - ❌ ERROR No healthcheck found for "non_existent_healthcheck" plugin.
336        ]])
337    end)
338
339    it('does not use vim.health as a healthcheck', function()
340      -- vim.health is not a healthcheck
341      command('checkhealth vim')
342      n.expect([[
343      ERROR: No healthchecks found.]])
344    end)
345
346    it('nested lua/ directory', function()
347      command('checkhealth lua')
348      n.expect([[
349      ==============================================================================
350      test_plug.lua:                                                              ✅
351
352      nested lua/ directory ~
353      - ✅ OK everything is ok
354      ]])
355    end)
356
357    it('&rtp can contain nested path (by packadd)', function()
358      -- re-add to ensure this appears before new nested rtp
359      command([[set runtimepath-=test/functional/fixtures]])
360      command([[set runtimepath+=test/functional/fixtures]])
361      command('set packpath+=test/functional/fixtures')
362      -- set rtp+=test/functional/fixtures/pack/foo/opt/healthy
363      command('packadd healthy')
364      command('checkhealth nest')
365      n.expect([[
366      ==============================================================================
367      nest:                                                                       ✅
368
369      healthy pack ~
370      - ✅ OK healthy ok
371      ]])
372    end)
373  end)
374end)
375
376describe(':checkhealth window', function()
377  before_each(function()
378    clear { args = { '-u', 'NORC', '+set runtimepath+=test/functional/fixtures' } }
379    command('set nofoldenable nowrap laststatus=0')
380  end)
381
382  it('opens directly if no buffer created', function()
383    local screen = Screen.new(50, 11, { ext_multigrid = true })
384    screen:set_default_attr_ids {
385      h1 = { reverse = true },
386      h2 = { foreground = tonumber('0x6a0dad') },
387      Done = { foreground = Screen.colors.NvimDarkGreen },
388      [1] = { foreground = Screen.colors.Blue, bold = true },
389      [14] = { foreground = Screen.colors.LightGrey, background = Screen.colors.DarkGray },
390      [32] = { foreground = Screen.colors.PaleGreen2 },
391    }
392    command('checkhealth success1')
393    screen:expect {
394      grid = [[
395    ## grid 1
396      [2:--------------------------------------------------]|*10
397      [3:--------------------------------------------------]|
398    ## grid 2
399      {14:^                                                  }|
400      {14:                            }                      |
401      {h1:test_plug.                                        }|
402      {h1:success1:                                         }|
403      {h1:                ✅}                                |
404                                                        |
405      {h2:report 1}                                          |
406      - ✅ {32:OK} everything is fine                        |
407                                                        |
408      {h2:report 2}                                          |
409    ## grid 3
410      {Done:checkhealth}: checks done                          |
411    ]],
412    }
413  end)
414
415  local function test_health_vsplit(left, emptybuf, mods)
416    local screen = Screen.new(50, 19, { ext_multigrid = true })
417    screen:set_default_attr_ids {
418      h1 = { reverse = true },
419      h2 = { foreground = tonumber('0x6a0dad') },
420      Done = { foreground = Screen.colors.NvimDarkGreen },
421      [1] = { foreground = Screen.colors.Blue, bold = true },
422      [14] = { foreground = Screen.colors.LightGrey, background = Screen.colors.DarkGray },
423      [32] = { foreground = Screen.colors.PaleGreen2 },
424    }
425    if not emptybuf then
426      insert('hello')
427    end
428    command(mods .. ' checkhealth success1')
429    screen:expect(
430      ([[
431    ## grid 1
432      %s
433      [3:--------------------------------------------------]|
434    ## grid 2
435      %s                   |
436      {1:~                       }|*17
437    ## grid 3
438      {Done:checkhealth}: checks done                          |
439    ## grid 4
440      {14:^                         }|
441      {14:                         }|*2
442      {14:   }                      |
443      {h1:test_plug.               }|
444      {h1:success1:                }|
445      {h1:                         }|
446      {h1:                ✅}       |
447                               |
448      {h2:report 1}                 |
449      - ✅ {32:OK} everything is    |
450      fine                     |
451                               |
452      {h2:report 2}                 |
453      - ✅ {32:OK} nothing to see   |
454      here                     |
455                               |
456      {1:~                        }|
457    ]]):format(
458        left and '[4:-------------------------]│[2:------------------------]|*18'
459          or '[2:------------------------]│[4:-------------------------]|*18',
460        emptybuf and '     ' or 'hello'
461      )
462    )
463  end
464
465  for _, mods in ipairs({ 'vertical', 'leftabove vertical', 'topleft vertical' }) do
466    it(('opens in left vsplit window with :%s and no buffer created'):format(mods), function()
467      test_health_vsplit(true, true, mods)
468    end)
469    it(('opens in left vsplit window with :%s and non-empty buffer'):format(mods), function()
470      test_health_vsplit(true, false, mods)
471    end)
472  end
473
474  for _, mods in ipairs({ 'rightbelow vertical', 'botright vertical' }) do
475    it(('opens in right vsplit window with :%s and no buffer created'):format(mods), function()
476      test_health_vsplit(false, true, mods)
477    end)
478    it(('opens in right vsplit window with :%s and non-empty buffer'):format(mods), function()
479      test_health_vsplit(false, false, mods)
480    end)
481  end
482
483  local function test_health_split(top, emptybuf, mods)
484    local screen = Screen.new(50, 25, { ext_multigrid = true })
485    screen._default_attr_ids = nil
486    if not emptybuf then
487      insert('hello')
488    end
489    command(mods .. ' checkhealth success1')
490    screen:expect(
491      ([[
492    ## grid 1
493%s
494      [3:--------------------------------------------------]|
495    ## grid 2
496      %s                                             |
497      ~                                                 |*10
498    ## grid 3
499      checkhealth: checks done                          |
500    ## grid 4
501      ^                                                  |
502                                                        |
503      test_plug.                                        |
504      success1:                                         |
505                      ✅                                |
506                                                        |
507      report 1                                          |
508      - ✅ OK everything is fine                        |
509                                                        |
510      report 2                                          |
511      - ✅ OK nothing to see here                       |
512                                                        |
513    ]]):format(
514        top
515            and [[
516      [4:--------------------------------------------------]|*12
517      health:// [-]                                     |
518      [2:--------------------------------------------------]|*11]]
519          or ([[
520      [2:--------------------------------------------------]|*11
521      [No Name] %s                                     |
522      [4:--------------------------------------------------]|*12]]):format(
523            emptybuf and '   ' or '[+]'
524          ),
525        emptybuf and '     ' or 'hello'
526      )
527    )
528  end
529
530  for _, mods in ipairs({ 'horizontal', 'leftabove', 'topleft' }) do
531    it(('opens in top split window with :%s and no buffer created'):format(mods), function()
532      test_health_split(true, true, mods)
533    end)
534    it(('opens in top split window with :%s and non-empty buffer'):format(mods), function()
535      test_health_split(true, false, mods)
536    end)
537  end
538
539  for _, mods in ipairs({ 'rightbelow', 'botright' }) do
540    it(('opens in bottom split window with :%s and no buffer created'):format(mods), function()
541      test_health_split(false, true, mods)
542    end)
543    it(('opens in bottom split window with :%s and non-empty buffer'):format(mods), function()
544      test_health_split(false, false, mods)
545    end)
546  end
547
548  it('opens in new tabpage after the current one', function()
549    -- create an empty buffer called "my_buff"
550    api.nvim_create_buf(false, true)
551    command('file my_buff')
552    command('checkhealth success1')
553    -- define a function that collects all buffers in each tab
554    -- returns a dict like {tab1 = ["buf1", "buf2"], tab2 = ["buf3"]}
555    source([[
556        function CollectBuffersPerTab()
557                let buffs = {}
558                for i in range(tabpagenr('$'))
559                  let key = 'tab' . (i + 1)
560                  let value = []
561                  for j in tabpagebuflist(i + 1)
562                    call add(value, bufname(j))
563                  endfor
564                  let buffs[key] = value
565                endfor
566                return buffs
567        endfunction
568    ]])
569    local buffers_per_tab = fn.CollectBuffersPerTab()
570    eq(buffers_per_tab, { tab1 = { 'my_buff' }, tab2 = { 'health://' } })
571    command('bwipe')
572    command('tab split | tab split | tab split | tabprevious')
573    command('checkhealth success1')
574    buffers_per_tab = fn.CollectBuffersPerTab()
575    eq(buffers_per_tab, {
576      tab1 = { 'my_buff' },
577      tab2 = { 'my_buff' },
578      tab3 = { 'my_buff' },
579      tab4 = { 'health://' },
580      tab5 = { 'my_buff' },
581    })
582  end)
583end)