aboutsummaryrefslogtreecommitdiff
path: root/test/functional/plugin/health_spec.lua
blob: ba66117fb1aa57e102a7309c7e17eee62c92bb39 (plain) (blame)
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
local helpers = require('test.functional.helpers')(after_each)
local global_helpers = require('test.helpers')
local Screen = require('test.functional.ui.screen')

local clear = helpers.clear
local curbuf_contents = helpers.curbuf_contents
local command = helpers.command
local eq, neq = helpers.eq, helpers.neq
local getcompletion = helpers.funcs.getcompletion

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 nvim")
    eq("ERROR: $VIM is invalid: zub",
       string.match(curbuf_contents(), "ERROR: $VIM .* zub"))
  end)
  it('completions can be listed via getcompletion()', function()
    clear()
    eq('nvim', getcompletion('nvim', 'checkhealth')[1])
    eq('provider', getcompletion('prov', 'checkhealth')[1])
    eq('vim.lsp', getcompletion('vim.ls', 'checkhealth')[1])
    neq('vim', getcompletion('^vim', 'checkhealth')[1])  -- should not complete vim.health
  end)
end)

describe('health.vim', function()
  before_each(function()
    clear{args={'-u', 'NORC'}}
    -- Provides functions:
    --    health#broken#check()
    --    health#success1#check()
    --    health#success2#check()
    command("set runtimepath+=test/functional/fixtures")
  end)

  describe(":checkhealth", function()
    it("functions health#report_*() render correctly", function()
      command("checkhealth full_render")
      helpers.expect([[

      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
          - OK: everything is fine

        ## report 2
          - OK: nothing to see here

        success2: health#success2#check
        ========================================================================
        ## another 1
          - OK: ok

        test_plug: require("test_plug.health").check()
        ========================================================================
        ## report 1
          - OK: everything is fine

        ## report 2
          - OK: nothing to see here
        ]])
    end)

    it("lua plugins, skips vimscript healthchecks with the same name", function()
      command("checkhealth test_plug")
      -- Existing file in test/functional/fixtures/lua/test_plug/autoload/health/test_plug.vim
      -- and the Lua healthcheck is used instead.
      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 - 5)
      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_empty: require("test_plug.submodule_empty.health").check()
        ========================================================================
          - ERROR: The healthcheck report for "test_plug.submodule_empty" plugin is empty.

        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 20]])
      eq(expected, received)
    end)

    it("gracefully handles broken healthcheck", function()
      command("checkhealth broken")
      helpers.expect([[

        broken: health#broken#check
        ========================================================================
          - ERROR: Failed to run healthcheck for "broken" plugin. Exception:
            function health#check[20]..health#broken#check, line 1
            caused an error
        ]])
    end)

    it("... including empty reports", function()
      command("checkhealth test_plug.submodule_empty")
      helpers.expect([[

      test_plug.submodule_empty: require("test_plug.submodule_empty.health").check()
      ========================================================================
        - ERROR: The healthcheck report for "test_plug.submodule_empty" plugin is empty.
      ]])
    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 - 5)
      -- avoid dealing with path separators
      local lua_err = "attempt to perform arithmetic on a nil value"
      local last_line = buf_lines[#buf_lines - 4]
      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 20]])
      eq(expected, received)
    end)

    it("highlights OK, ERROR", function()
      local screen = Screen.new(72, 10)
      screen:attach()
      screen:set_default_attr_ids({
        Ok = { foreground = Screen.colors.Grey3, background = 6291200 },
        Error = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
        Heading = { bold=true, foreground=Screen.colors.Magenta },
        Heading2 = { foreground = Screen.colors.SlateBlue },
        Bar = { foreground = 0x6a0dad },
        Bullet = { bold=true, foreground=Screen.colors.Brown },
      })
      command("checkhealth foo success1")
      command("1tabclose")
      command("set laststatus=0")
      screen:expect{grid=[[
        ^                                                                        |
        {Heading:foo: }                                                                   |
        {Bar:========================================================================}|
        {Bullet:  -} {Error:ERROR}: No healthcheck found for "foo" plugin.                       |
                                                                                |
        {Heading:success1: health#success1#check}                                         |
        {Bar:========================================================================}|
        {Heading2:##}{Heading: report 1}                                                             |
        {Bullet:  -} {Ok:OK}: everything is fine                                              |
                                                                                |
      ]]}
    end)

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

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

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

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