summaryrefslogtreecommitdiffstatshomepage
path: root/test/functional/plugin/health_spec.lua
blob: 7aad3cee03487ad6ba691e3ab660473d53f02e50 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')

local clear = n.clear
local curbuf_contents = n.curbuf_contents
local command = n.command
local eq, matches = t.eq, t.matches
local getcompletion = n.fn.getcompletion
local insert = n.insert
local exec_lua = n.exec_lua
local source = n.source
local assert_alive = n.assert_alive
local fn = n.fn
local api = n.api

describe(':checkhealth', function()
  it('detects invalid $VIMRUNTIME', function()
    clear({
      env = { VIMRUNTIME = 'bogus' },
    })
    local status, err = pcall(command, 'checkhealth')
    eq(false, status)
    eq('Invalid $VIMRUNTIME: bogus', string.match(err, 'Invalid.*'))
  end)

  it("detects invalid 'runtimepath'", function()
    clear()
    command('set runtimepath=bogus')
    local status, err = pcall(command, 'checkhealth')
    eq(false, status)
    eq("Invalid 'runtimepath'", string.match(err, 'Invalid.*'))
  end)

  it('detects invalid $VIM', function()
    clear()
    -- Do this after startup, otherwise it just breaks $VIMRUNTIME.
    command("let $VIM='zub'")
    command('checkhealth vim.health')
    matches('ERROR $VIM .* zub', curbuf_contents())
  end)

  it('getcompletion()', function()
    clear { args = { '-u', 'NORC', '+set runtimepath+=test/functional/fixtures' } }

    eq('vim.deprecated', getcompletion('vim', 'checkhealth')[1])
    eq('vim.provider', getcompletion('vim.prov', 'checkhealth')[1])
    eq('vim.lsp', getcompletion('vim.ls', 'checkhealth')[1])

    -- "test_plug/health/init.lua" should complete as "test_plug", not "test_plug.health". #30342
    eq({
      'test_plug',
      'test_plug.full_render',
      'test_plug.submodule',
      'test_plug.submodule_empty',
      'test_plug.success1',
      'test_plug.success2',
    }, getcompletion('test_plug', 'checkhealth'))
  end)

  it('completion checks for vim.health._complete() return type #28456', function()
    clear()
    exec_lua([[vim.health._complete = function() return 1 end]])
    eq({}, getcompletion('', 'checkhealth'))
    exec_lua([[vim.health._complete = function() return { 1 } end]])
    eq({}, getcompletion('', 'checkhealth'))
    assert_alive()
  end)

  it('cmdline completion works with multiple args #35054', function()
    clear()
    n.feed(':checkhealth vim.ls<Tab>')
    eq('checkhealth vim.lsp', fn.getcmdline())
    n.feed(' vim.prov<Tab>')
    eq('checkhealth vim.lsp vim.provider', fn.getcmdline())
  end)

  it('vim.g.health', function()
    clear {
      args_rm = { '-u' },
      args = { '--clean', '+set runtimepath+=test/functional/fixtures' },
    }
    command("let g:health = {'style':'float'}")
    command('checkhealth lsp')
    eq(
      'editor',
      exec_lua([[
      return vim.api.nvim_win_get_config(0).relative
    ]])
    )
    matches('health%.lua:%d+>$', fn.maparg('q', 'n', false, false))

    -- gO should not close the :checkhealth floating window. #34784
    command('checkhealth full_render')
    local win = api.nvim_get_current_win()
    api.nvim_win_set_cursor(win, { 5, 1 })
    n.feed('gO')
    eq(true, api.nvim_win_is_valid(win))
    eq('qf', api.nvim_get_option_value('filetype', { buf = 0 }))
  end)

  it("vim.provider works with a misconfigured 'shell'", function()
    clear()
    command([[set shell=echo\ WRONG!!!]])
    command('let g:loaded_perl_provider = 0')
    command('let g:loaded_python3_provider = 0')
    command('checkhealth vim.provider')
    eq(nil, string.match(curbuf_contents(), 'WRONG!!!'))
  end)

  it('vim.lsp warns about unknown filetypes', function()
    clear()
    exec_lua(function()
      vim.filetype.add({ extension = { mdx = 'mdx' } })
      vim.lsp.config('builtin_registry_ft', {
        cmd = { 'true' },
        filetypes = { 'beancount' },
      })
      vim.lsp.config('custom_ft', {
        cmd = { 'true' },
        filetypes = { 'mdx' },
      })
      vim.lsp.config('bad_ft', {
        cmd = { 'true' },
        filetypes = { 'hbs' },
      })
      vim.lsp.enable('builtin_registry_ft')
      vim.lsp.enable('custom_ft')
      vim.lsp.enable('bad_ft')
    end)
    command('checkhealth vim.lsp')
    local report = curbuf_contents()
    eq(nil, report:find("Unknown filetype 'beancount'", 1, true))
    eq(nil, report:find("Unknown filetype 'mdx'", 1, true))
    eq(
      true,
      report:find("Unknown filetype 'hbs' (Hint: filename extension != filetype).", 1, true) ~= nil
    )
    eq(true, report:find('- builtin_registry_ft:', 1, true) ~= nil)
    eq(true, report:find('- custom_ft:', 1, true) ~= nil)
    eq(true, report:find('- bad_ft:', 1, true) ~= nil)
  end)
end)

describe('vim.health', function()
  before_each(function()
    clear { args = { '-u', 'NORC', '+set runtimepath+=test/functional/fixtures' } }
  end)

  describe(':checkhealth', function()
    it('report_xx() renders correctly', function()
      command('checkhealth full_render')
      n.expect([[

      ==============================================================================
      test_plug.full_render:                                              1 ⚠️  1 ❌

      report 1 ~
      - ✅ OK life is fine
      - ⚠️ WARNING no what installed
        - ADVICE:
          - pip what
          - make what

      report 2 ~
      - stuff is stable
      - ❌ ERROR why no hardcopy
        - ADVICE:
          - :help |:hardcopy|
          - :help |:TOhtml|
      ]])
    end)

    it('user FileType handler can modify report', function()
      -- Define a FileType autocmd that removes emoji chars.
      source [[
        autocmd FileType checkhealth :set modifiable | silent! %s/\v( ?[^\x00-\x7F])//g
        checkhealth full_render
      ]]
      n.expect([[

      ==============================================================================
      test_plug.full_render:                                              1  1

      report 1 ~
      - OK life is fine
      - WARNING no what installed
        - ADVICE:
          - pip what
          - make what

      report 2 ~
      - stuff is stable
      - ERROR why no hardcopy
        - ADVICE:
          - :help |:hardcopy|
          - :help |:TOhtml|
      ]])
    end)

    it('concatenates multiple reports', function()
      command('checkhealth success1 success2 test_plug')
      n.expect([[

        ==============================================================================
        test_plug:                                                                  ✅

        report 1 ~
        - ✅ OK everything is fine

        report 2 ~
        - ✅ OK nothing to see here

        ==============================================================================
        test_plug.success1:                                                         ✅

        report 1 ~
        - ✅ OK everything is fine

        report 2 ~
        - ✅ OK nothing to see here

        ==============================================================================
        test_plug.success2:                                                         ✅

        another 1 ~
        - ✅ OK ok
        ]])
    end)

    it('lua plugins submodules', function()
      command('checkhealth test_plug.submodule')
      n.expect([[

        ==============================================================================
        test_plug.submodule:                                                        ✅

        report 1 ~
        - ✅ OK everything is fine

        report 2 ~
        - ✅ OK nothing to see here
        ]])
    end)

    it('... including empty reports', function()
      command('checkhealth test_plug.submodule_empty')
      n.expect([[

      ==============================================================================
      test_plug.submodule_empty:                                                1 ❌

      - ❌ ERROR The healthcheck report for "test_plug.submodule_empty" plugin is empty.
      ]])
    end)

    it('highlights OK, ERROR', function()
      local screen = Screen.new(50, 12)
      screen:set_default_attr_ids({
        h1 = { reverse = true },
        h2 = { foreground = tonumber('0x6a0dad') },
        Ok = { foreground = Screen.colors.LightGreen },
        Error = { foreground = Screen.colors.Red },
        Done = { foreground = Screen.colors.NvimDarkGreen },
        Bar = { foreground = Screen.colors.LightGrey, background = Screen.colors.DarkGrey },
      })
      command('checkhealth foo success1')
      command('set nofoldenable nowrap laststatus=0')
      screen:expect {
        grid = [[
        ^                                                  |
        {Bar:                                                  }|
        {h1:foo:                                              }|
                                                          |
        - ❌ {Error:ERROR} No healthcheck found for "foo" plugin. |
                                                          |
        {Bar:                                                  }|
        {h1:test_plug.success1:                               }|
                                                          |
        {h2:report 1}                                          |
        - ✅ {Ok:OK} everything is fine                        |
        {Done:checkhealth}: checks done                          |
      ]],
      }
    end)

    it('gracefully handles invalid healthcheck', function()
      command('checkhealth non_existent_healthcheck')
      -- luacheck: ignore 613
      n.expect([[

        ==============================================================================
        non_existent_healthcheck:                                                 1 ❌

        - ❌ ERROR No healthcheck found for "non_existent_healthcheck" plugin.
        ]])
    end)

    it('does not use vim.health as a healthcheck', function()
      -- vim.health is not a healthcheck
      command('checkhealth vim')
      n.expect([[
      ERROR: No healthchecks found.]])
    end)

    it('nested lua/ directory', function()
      command('checkhealth lua')
      n.expect([[

      ==============================================================================
      test_plug.lua:                                                              ✅

      nested lua/ directory ~
      - ✅ OK everything is ok
      ]])
    end)

    it('&rtp can contain nested path (by packadd)', function()
      -- re-add to ensure this appears before new nested rtp
      command([[set runtimepath-=test/functional/fixtures]])
      command([[set runtimepath+=test/functional/fixtures]])
      command('set packpath+=test/functional/fixtures')
      -- set rtp+=test/functional/fixtures/pack/foo/opt/healthy
      command('packadd healthy')
      command('checkhealth nest')
      n.expect([[

      ==============================================================================
      nest:                                                                       ✅

      healthy pack ~
      - ✅ OK healthy ok
      ]])
    end)
  end)
end)

describe(':checkhealth window', function()
  before_each(function()
    clear { args = { '-u', 'NORC', '+set runtimepath+=test/functional/fixtures' } }
    command('set nofoldenable nowrap laststatus=0')
  end)

  it('opens directly if no buffer created', function()
    local screen = Screen.new(50, 12, { ext_multigrid = true })
    screen:set_default_attr_ids {
      h1 = { reverse = true },
      h2 = { foreground = tonumber('0x6a0dad') },
      Done = { foreground = Screen.colors.NvimDarkGreen },
      [1] = { foreground = Screen.colors.Blue, bold = true },
      [14] = { foreground = Screen.colors.LightGrey, background = Screen.colors.DarkGray },
      [32] = { foreground = Screen.colors.PaleGreen2 },
    }
    command('checkhealth success1')
    screen:expect {
      grid = [[
    ## grid 1
      [2:--------------------------------------------------]|*11
      [3:--------------------------------------------------]|
    ## grid 2
      ^                                                  |
      {14:                                                  }|
      {14:                            }                      |
      {h1:test_plug.                                        }|
      {h1:success1:                                         }|
      {h1:                ✅}                                |
                                                        |
      {h2:report 1}                                          |
      - ✅ {32:OK} everything is fine                        |
                                                        |
      {h2:report 2}                                          |
    ## grid 3
      {Done:checkhealth}: checks done                          |
    ]],
    }
  end)

  local function test_health_vsplit(left, emptybuf, mods)
    local screen = Screen.new(50, 20, { ext_multigrid = true })
    screen:set_default_attr_ids {
      h1 = { reverse = true },
      h2 = { foreground = tonumber('0x6a0dad') },
      Done = { foreground = Screen.colors.NvimDarkGreen },
      [1] = { foreground = Screen.colors.Blue, bold = true },
      [14] = { foreground = Screen.colors.LightGrey, background = Screen.colors.DarkGray },
      [32] = { foreground = Screen.colors.PaleGreen2 },
    }
    if not emptybuf then
      insert('hello')
    end
    command(mods .. ' checkhealth success1')
    screen:expect(
      ([[
    ## grid 1
      %s
      [3:--------------------------------------------------]|
    ## grid 2
      %s                   |
      {1:~                       }|*18
    ## grid 3
      {Done:checkhealth}: checks done                          |
    ## grid 4
      ^                         |
      {14:                         }|*3
      {14:   }                      |
      {h1:test_plug.               }|
      {h1:success1:                }|
      {h1:                         }|
      {h1:                ✅}       |
                               |
      {h2:report 1}                 |
      - ✅ {32:OK} everything is    |
      fine                     |
                               |
      {h2:report 2}                 |
      - ✅ {32:OK} nothing to see   |
      here                     |
                               |
      {1:~                        }|
    ]]):format(
        left and '[4:-------------------------]│[2:------------------------]|*19'
          or '[2:------------------------]│[4:-------------------------]|*19',
        emptybuf and '     ' or 'hello'
      )
    )
  end

  for _, mods in ipairs({ 'vertical', 'leftabove vertical', 'topleft vertical' }) do
    it(('opens in left vsplit window with :%s and no buffer created'):format(mods), function()
      test_health_vsplit(true, true, mods)
    end)
    it(('opens in left vsplit window with :%s and non-empty buffer'):format(mods), function()
      test_health_vsplit(true, false, mods)
    end)
  end

  for _, mods in ipairs({ 'rightbelow vertical', 'botright vertical' }) do
    it(('opens in right vsplit window with :%s and no buffer created'):format(mods), function()
      test_health_vsplit(false, true, mods)
    end)
    it(('opens in right vsplit window with :%s and non-empty buffer'):format(mods), function()
      test_health_vsplit(false, false, mods)
    end)
  end

  local function test_health_split(top, emptybuf, mods)
    local screen = Screen.new(50, 25, { ext_multigrid = true })
    screen._default_attr_ids = nil
    if not emptybuf then
      insert('hello')
    end
    command(mods .. ' checkhealth success1')
    screen:expect(
      ([[
    ## grid 1
%s
      [3:--------------------------------------------------]|
    ## grid 2
      %s                                             |
      ~                                                 |*10
    ## grid 3
      checkhealth: checks done                          |
    ## grid 4
      ^                                                  |
                                                        |*2
      test_plug.                                        |
      success1:                                         |
                      ✅                                |
                                                        |
      report 1                                          |
      - ✅ OK everything is fine                        |
                                                        |
      report 2                                          |
      - ✅ OK nothing to see here                       |
    ]]):format(
        top
            and [[
      [4:--------------------------------------------------]|*12
      health:// [-]                                     |
      [2:--------------------------------------------------]|*11]]
          or ([[
      [2:--------------------------------------------------]|*11
      [No Name] %s                                     |
      [4:--------------------------------------------------]|*12]]):format(
            emptybuf and '   ' or '[+]'
          ),
        emptybuf and '     ' or 'hello'
      )
    )
  end

  for _, mods in ipairs({ 'horizontal', 'leftabove', 'topleft' }) do
    it(('opens in top split window with :%s and no buffer created'):format(mods), function()
      test_health_split(true, true, mods)
    end)
    it(('opens in top split window with :%s and non-empty buffer'):format(mods), function()
      test_health_split(true, false, mods)
    end)
  end

  for _, mods in ipairs({ 'rightbelow', 'botright' }) do
    it(('opens in bottom split window with :%s and no buffer created'):format(mods), function()
      test_health_split(false, true, mods)
    end)
    it(('opens in bottom split window with :%s and non-empty buffer'):format(mods), function()
      test_health_split(false, false, mods)
    end)
  end

  it('opens in tab', function()
    -- create an empty buffer called "my_buff"
    api.nvim_create_buf(false, true)
    command('file my_buff')
    command('checkhealth success1')
    -- define a function that collects all buffers in each tab
    -- returns a dict like {tab1 = ["buf1", "buf2"], tab2 = ["buf3"]}
    source([[
        function CollectBuffersPerTab()
                let buffs = {}
                for i in range(tabpagenr('$'))
                  let key = 'tab' . (i + 1)
                  let value = []
                  for j in tabpagebuflist(i + 1)
                    call add(value, bufname(j))
                  endfor
                  let buffs[key] = value
                endfor
                return buffs
        endfunction
    ]])
    local buffers_per_tab = fn.CollectBuffersPerTab()
    eq(buffers_per_tab, { tab1 = { 'my_buff' }, tab2 = { 'health://' } })
  end)
end)