robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
38.2 KiB1465 linesraw
1-- Black-box tests for the local Lua harness itself. These spawn a separate
2-- Nvim process instead of driving an embedded instance via testnvim.
3local t = require('test.testutil')
4local describe, it, pending, finally = t.describe, t.it, t.pending, t.finally
5local uv = vim.uv
6
7local eq = t.eq
8local matches = t.matches
9local not_matches = t.not_matches
10
11local root = t.paths.test_source_path
12local build_dir = t.paths.test_build_dir
13local nvim_prog = build_dir .. '/bin/nvim'
14local runner = root .. '/test/runner.lua'
15
16---@param path string
17local function mkdir(path)
18  assert(t.mkdir(path), ('failed to create directory: %s'):format(path))
19end
20
21-- Prepended to each *_spec.lua fixture so it can use the test framework.
22local spec_preamble = "local h = require('test.harness'); local eq = require('test.assert').eq; "
23  .. 'local describe, it, before_each, after_each, setup, teardown, pending, finally = '
24  .. 'h.describe, h.it, h.before_each, h.after_each, h.setup, h.teardown, h.pending, h.finally; '
25
26---@param suite_files table<string, string>
27---@return string
28local function write_suite(suite_files)
29  local dir = t.tmpname(false)
30  mkdir(dir)
31
32  for name, contents in pairs(suite_files) do
33    if name:match('_spec%.lua$') then
34      contents = spec_preamble .. contents
35    end
36    t.write_file(dir .. '/' .. name, contents)
37  end
38
39  return dir
40end
41
42---@param overrides table<string, string?>
43---@return string[]
44local function make_env(overrides)
45  local env = uv.os_environ()
46  for k, v in pairs(overrides) do
47    env[k] = v
48  end
49
50  local items = {}
51  for k, v in pairs(env) do
52    if v ~= nil then
53      items[#items + 1] = k .. '=' .. v
54    end
55  end
56
57  return items
58end
59
60---@param suite_dir string
61---@param extra_args? string[]
62---@return integer, string
63local function run_harness(suite_dir, extra_args)
64  local env_root = t.tmpname(false)
65  mkdir(env_root)
66  mkdir(env_root .. '/config')
67  mkdir(env_root .. '/share')
68  mkdir(env_root .. '/state')
69  mkdir(env_root .. '/tmp')
70
71  local stdout = assert(uv.new_pipe(false))
72  local stderr = assert(uv.new_pipe(false))
73  local out = {}
74  local err = {}
75  local exit_code --- @type integer?
76
77  local args = {
78    '-l',
79    runner,
80    '-v',
81    '--lpath=' .. build_dir .. '/?.lua',
82    '--lpath=' .. root .. '/src/?.lua',
83    '--lpath=' .. root .. '/runtime/lua/?.lua',
84    '--lpath=' .. suite_dir .. '/?.lua',
85    '--lpath=?.lua',
86  }
87  for _, arg in ipairs(extra_args or {}) do
88    args[#args + 1] = arg
89  end
90  args[#args + 1] = suite_dir
91
92  local handle = assert(uv.spawn(nvim_prog, {
93    args = args,
94    env = make_env({
95      NVIM_TEST = '1',
96      TEST_COLORS = '0',
97      VIMRUNTIME = root .. '/runtime',
98      XDG_CONFIG_HOME = env_root .. '/config',
99      XDG_DATA_HOME = env_root .. '/share',
100      XDG_STATE_HOME = env_root .. '/state',
101      NVIM_RPLUGIN_MANIFEST = env_root .. '/rplugin_manifest',
102      NVIM_LOG_FILE = env_root .. '/nvim.log',
103      TMPDIR = env_root .. '/tmp',
104      SYSTEM_NAME = os.getenv('SYSTEM_NAME') or uv.os_uname().sysname,
105      SHELL = os.getenv('SHELL') or 'sh',
106    }),
107    stdio = { nil, stdout, stderr },
108    hide = true,
109  }, function(code, _signal)
110    exit_code = code
111  end))
112
113  local function read_all(pipe, chunks)
114    pipe:read_start(function(read_err, chunk)
115      assert(not read_err, read_err)
116      if chunk == nil then
117        pipe:read_stop()
118        pipe:close()
119      else
120        chunks[#chunks + 1] = chunk
121      end
122    end)
123  end
124
125  read_all(stdout, out)
126  read_all(stderr, err)
127
128  while exit_code == nil or not stdout:is_closing() or not stderr:is_closing() do
129    uv.run('once')
130  end
131
132  handle:close()
133  while not handle:is_closing() do
134    uv.run('once')
135  end
136
137  return exit_code, table.concat(out) .. table.concat(err)
138end
139
140---@param suite_dir string
141---@param extra_args? string[]
142local function assert_harness_passes(suite_dir, extra_args)
143  local code, output = run_harness(suite_dir, extra_args)
144  eq(0, code)
145  matches('PASSED', output, true)
146end
147
148describe('test harness', function()
149  it('restores package.preload between files', function()
150    local suite_dir = write_suite({
151      ['one_spec.lua'] = [[
152        package.preload.leak_mod = function()
153          return 'leak'
154        end
155
156        describe('one', function()
157          it('defines a preload entry', function()
158            eq('leak', require('leak_mod'))
159          end)
160        end)
161      ]],
162      ['two_spec.lua'] = [[
163        describe('two', function()
164          it('does not see the preload entry from another file', function()
165            local ok = pcall(require, 'leak_mod')
166            assert(not ok)
167          end)
168        end)
169      ]],
170    })
171
172    assert_harness_passes(suite_dir)
173  end)
174
175  it('restores package.loaded between files', function()
176    local suite_dir = write_suite({
177      ['loaded_mod.lua'] = [[
178        return { source = 'file' }
179      ]],
180      ['one_spec.lua'] = [[
181        describe('one', function()
182          it('mutates package.loaded', function()
183            eq('file', require('loaded_mod').source)
184            package.loaded.loaded_mod = 'leak'
185            eq('leak', require('loaded_mod'))
186          end)
187        end)
188      ]],
189      ['two_spec.lua'] = [[
190        describe('two', function()
191          it('does not see package.loaded leaks from another file', function()
192            local mod = require('loaded_mod')
193            eq('file', mod.source)
194          end)
195        end)
196      ]],
197    })
198
199    assert_harness_passes(suite_dir)
200  end)
201
202  it('restores environment variables between files', function()
203    local env_name = '__NVIM_HARNESS_TEST_ENV__'
204    local suite_dir = write_suite({
205      ['one_spec.lua'] = string.format(
206        [[
207        describe('one', function()
208          it('mutates the environment', function()
209            vim.uv.os_setenv(%q, 'leak')
210            eq('leak', vim.uv.os_getenv(%q))
211          end)
212        end)
213      ]],
214        env_name,
215        env_name
216      ),
217      ['two_spec.lua'] = string.format(
218        [[
219        describe('two', function()
220          it('does not see environment leaks from another file', function()
221            eq(nil, vim.uv.os_getenv(%q))
222          end)
223        end)
224      ]],
225        env_name
226      ),
227    })
228
229    assert_harness_passes(suite_dir)
230  end)
231
232  it('restores globals between files', function()
233    local suite_dir = write_suite({
234      ['one_spec.lua'] = [[
235        describe('one', function()
236          it('mutates a global', function()
237            _G.__harness_leak = 'leak'
238            eq('leak', _G.__harness_leak)
239          end)
240        end)
241      ]],
242      ['two_spec.lua'] = [[
243        describe('two', function()
244          it('does not see global leaks from another file', function()
245            eq(nil, _G.__harness_leak)
246          end)
247        end)
248      ]],
249    })
250
251    assert_harness_passes(suite_dir)
252  end)
253
254  it('restores arg between files', function()
255    local suite_dir = write_suite({
256      ['one_spec.lua'] = [[
257        describe('one', function()
258          it('mutates arg', function()
259            _G.arg.__leak = 'leak'
260            eq('leak', _G.arg.__leak)
261          end)
262        end)
263      ]],
264      ['two_spec.lua'] = [[
265        describe('two', function()
266          it('does not see arg leaks from another file', function()
267            eq(nil, _G.arg.__leak)
268          end)
269        end)
270      ]],
271    })
272
273    assert_harness_passes(suite_dir)
274  end)
275
276  it('restores helper-provided globals to their baseline between files', function()
277    local suite_dir = write_suite({
278      ['helper.lua'] = [[
279        _G.helper_value = 'baseline'
280      ]],
281      ['one_spec.lua'] = [[
282        describe('one', function()
283          it('mutates a helper-provided global', function()
284            eq('baseline', _G.helper_value)
285            _G.helper_value = 'leak'
286            eq('leak', _G.helper_value)
287          end)
288        end)
289      ]],
290      ['two_spec.lua'] = [[
291        describe('two', function()
292          it('restores the helper baseline between files', function()
293            eq('baseline', _G.helper_value)
294          end)
295        end)
296      ]],
297    })
298
299    assert_harness_passes(suite_dir, {
300      '--helper=' .. suite_dir .. '/helper.lua',
301    })
302  end)
303
304  it('restores helper-provided globals shallowly between files', function()
305    local suite_dir = write_suite({
306      ['helper.lua'] = [[
307        _G.helper_value = { nested = 'baseline' }
308      ]],
309      ['one_spec.lua'] = [[
310        describe('one', function()
311          it('mutates nested helper state in place', function()
312            eq('baseline', _G.helper_value.nested)
313            _G.helper_value.nested = 'leak'
314            eq('leak', _G.helper_value.nested)
315          end)
316        end)
317      ]],
318      ['two_spec.lua'] = [[
319        describe('two', function()
320          it('keeps the same nested helper table', function()
321            eq('leak', _G.helper_value.nested)
322          end)
323        end)
324      ]],
325    })
326
327    assert_harness_passes(suite_dir, {
328      '--helper=' .. suite_dir .. '/helper.lua',
329    })
330  end)
331
332  it('keeps helper-loaded modules across files', function()
333    local suite_dir = write_suite({
334      ['helper_mod.lua'] = [[
335        _G.helper_module_loads = (_G.helper_module_loads or 0) + 1
336        return { loads = _G.helper_module_loads }
337      ]],
338      ['helper.lua'] = [[
339        require('helper_mod')
340      ]],
341      ['one_spec.lua'] = [[
342        describe('one', function()
343          it('uses the helper-loaded module', function()
344            eq(1, require('helper_mod').loads)
345          end)
346        end)
347      ]],
348      ['two_spec.lua'] = [[
349        describe('two', function()
350          it('does not reload the helper module', function()
351            eq(1, require('helper_mod').loads)
352          end)
353        end)
354      ]],
355    })
356
357    assert_harness_passes(suite_dir, {
358      '--helper=' .. suite_dir .. '/helper.lua',
359    })
360  end)
361
362  it('rejects helpers that register hooks', function()
363    local suite_dir = write_suite({
364      ['helper.lua'] = [[
365        before_each(function() end)
366      ]],
367      ['one_spec.lua'] = [[
368        describe('real', function()
369          it('passes', function() end)
370        end)
371      ]],
372    })
373
374    local code, output = run_harness(suite_dir, {
375      '--helper=' .. suite_dir .. '/helper.lua',
376    })
377
378    eq(1, code)
379    matches("attempt to call global 'before_each'", output, true)
380  end)
381
382  it('rejects helpers that define suites or tests', function()
383    local suite_dir = write_suite({
384      ['helper.lua'] = [[
385        describe('helper suite', function()
386          it('should fail', function() end)
387        end)
388      ]],
389      ['one_spec.lua'] = [[
390        describe('real', function()
391          it('passes', function() end)
392        end)
393      ]],
394    })
395
396    local code, output = run_harness(suite_dir, {
397      '--helper=' .. suite_dir .. '/helper.lua',
398    })
399
400    eq(1, code)
401    matches("attempt to call global 'describe'", output, true)
402  end)
403
404  it('deduplicates suite-end callbacks registered from the same module', function()
405    local marker = t.tmpname(false)
406    local suite_dir = write_suite({
407      ['cbmod.lua'] = string.format(
408        [[
409        local harness = require('test.harness')
410
411        harness.on_suite_end(function()
412          local file = assert(io.open(%q, 'ab'))
413          file:write('hit\n')
414          file:close()
415        end)
416
417        return true
418      ]],
419        marker
420      ),
421      ['one_spec.lua'] = [[
422        require('cbmod')
423
424        describe('one', function()
425          it('works', function()
426            assert(true)
427          end)
428        end)
429      ]],
430      ['two_spec.lua'] = [[
431        require('cbmod')
432
433        describe('two', function()
434          it('works', function()
435            assert(true)
436          end)
437        end)
438      ]],
439    })
440
441    assert_harness_passes(suite_dir)
442    eq('hit\n', t.read_file(marker))
443  end)
444
445  it(
446    'keeps distinct suite-end callbacks from long paths with the same truncated short_src tail',
447    function()
448      local marker = t.tmpname(false)
449      local suite_dir = t.tmpname(false)
450      local left = suite_dir
451        .. '/'
452        .. string.rep('L', 100)
453        .. '/common/common/common/common/common/same_suffix'
454      local right = suite_dir
455        .. '/'
456        .. string.rep('R', 100)
457        .. '/common/common/common/common/common/same_suffix'
458
459      mkdir(suite_dir)
460      for _, path in ipairs({ left, right }) do
461        local parents = {}
462        local dir = path
463        while dir ~= suite_dir and not uv.fs_stat(dir) do
464          table.insert(parents, 1, dir)
465          dir = vim.fs.dirname(dir)
466        end
467        for _, parent in ipairs(parents) do
468          if not uv.fs_stat(parent) then
469            mkdir(parent)
470          end
471        end
472      end
473
474      t.write_file(left .. '/same_spec.lua', spec_preamble .. string.format(
475        [[
476        local harness = require('test.harness')
477
478        harness.on_suite_end(function()
479          local file = assert(io.open(%q, 'ab'))
480          file:write('left\n')
481          file:close()
482        end)
483
484        describe('left', function()
485          it('works', function() end)
486        end)
487      ]],
488        marker
489      ))
490      t.write_file(right .. '/same_spec.lua', spec_preamble .. string.format(
491        [[
492        local harness = require('test.harness')
493
494        harness.on_suite_end(function()
495          local file = assert(io.open(%q, 'ab'))
496          file:write('right\n')
497          file:close()
498        end)
499
500        describe('right', function()
501          it('works', function() end)
502        end)
503      ]],
504        marker
505      ))
506
507      assert_harness_passes(suite_dir)
508      eq('left\nright\n', t.read_file(marker))
509    end
510  )
511
512  it('reports suite-end callback failures without crashing the runner', function()
513    local suite_dir = write_suite({
514      ['cbmod.lua'] = [[
515        local harness = require('test.harness')
516
517        harness.on_suite_end(function()
518          error('boom from suite_end')
519        end)
520
521        return true
522      ]],
523      ['one_spec.lua'] = [[
524        require('cbmod')
525
526        describe('one', function()
527          it('works', function()
528            assert(true)
529          end)
530        end)
531      ]],
532    })
533
534    local code, output = run_harness(suite_dir)
535
536    eq(1, code)
537    matches('[suite_end 1]', output, true)
538    matches('suite_end', output, true)
539    matches('boom from suite_end', output, true)
540    matches('ERROR   ', output, true)
541    not_matches('FAILED  ', output, true)
542    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
543    matches('Global test environment teardown.', output, true)
544  end)
545
546  it('reports wrapped suite-end callback failures at the callback definition line', function()
547    local suite_dir = write_suite({
548      ['wrapper.lua'] = [[
549        local M = {}
550
551        function M.fail()
552          error('boom from wrapper')
553        end
554
555        return M
556      ]],
557      ['cbmod.lua'] = [[
558        local harness = require('test.harness')
559        local wrapper = require('wrapper')
560
561        harness.on_suite_end(function()
562          wrapper.fail()
563        end)
564
565        return true
566      ]],
567      ['one_spec.lua'] = [[
568        require('cbmod')
569
570        describe('one', function()
571          it('works', function()
572            assert(true)
573          end)
574        end)
575      ]],
576    })
577
578    local code, output = run_harness(suite_dir)
579
580    eq(1, code)
581    matches('cbmod.lua @ 4: [suite_end 1]', output, true)
582    not_matches('wrapper.lua @ 4:', output, true)
583  end)
584
585  it('reloads helper suite-end callbacks between repeats', function()
586    local marker = t.tmpname(false)
587    local suite_dir = write_suite({
588      ['helper.lua'] = string.format(
589        [[
590        local harness = require('test.harness')
591        local hits = 0
592
593        harness.on_suite_end(function()
594          hits = hits + 1
595          local file = assert(io.open(%q, 'ab'))
596          file:write(hits .. '\n')
597          file:close()
598        end)
599      ]],
600        marker
601      ),
602      ['one_spec.lua'] = [[
603        describe('one', function()
604          it('works', function()
605            assert(true)
606          end)
607        end)
608      ]],
609    })
610
611    assert_harness_passes(suite_dir, {
612      '--helper=' .. suite_dir .. '/helper.lua',
613      '--repeat=2',
614    })
615    eq('1\n1\n', t.read_file(marker))
616  end)
617
618  it('restores process state before each repeat', function()
619    local suite_dir = write_suite({
620      ['one_spec.lua'] = [[
621        local harness = require('test.harness')
622
623        harness.on_suite_end(function()
624          _G.__repeat_leak = 'leak'
625        end)
626
627        describe('one', function()
628          it('starts clean', function()
629            eq(nil, _G.__repeat_leak)
630          end)
631        end)
632      ]],
633    })
634
635    assert_harness_passes(suite_dir, {
636      '--repeat=2',
637    })
638  end)
639
640  it('filters tests by tags across files', function()
641    local suite_dir = write_suite({
642      ['one_spec.lua'] = [[
643        describe('one', function()
644          it('fast #fast', function() end)
645        end)
646      ]],
647      ['two_spec.lua'] = [[
648        describe('two', function()
649          it('slow #slow', function() end)
650        end)
651      ]],
652    })
653
654    local code, output = run_harness(suite_dir, {
655      '--tags=fast',
656    })
657
658    eq(0, code)
659    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
660    matches('one fast #fast', output, true)
661    not_matches('two slow #slow', output, true)
662  end)
663
664  it('filters tests by suite tags across files', function()
665    local suite_dir = write_suite({
666      ['one_spec.lua'] = [[
667        describe('one #fast', function()
668          it('works', function() end)
669        end)
670      ]],
671      ['two_spec.lua'] = [[
672        describe('two #slow', function()
673          it('works', function() end)
674        end)
675      ]],
676    })
677
678    local code, output = run_harness(suite_dir, {
679      '--tags=fast',
680    })
681
682    eq(0, code)
683    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
684    matches('one #fast works', output, true)
685    not_matches('two #slow works', output, true)
686  end)
687
688  it('filters tests by name across files', function()
689    local suite_dir = write_suite({
690      ['one_spec.lua'] = [[
691        describe('one', function()
692          it('chosen', function() end)
693        end)
694      ]],
695      ['two_spec.lua'] = [[
696        describe('two', function()
697          it('skipped', function() end)
698        end)
699      ]],
700    })
701
702    local code, output = run_harness(suite_dir, {
703      '--filter=chosen',
704    })
705
706    eq(0, code)
707    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
708    matches('one chosen', output, true)
709    not_matches('two skipped', output, true)
710  end)
711
712  it('filters tests by suite name across files', function()
713    local suite_dir = write_suite({
714      ['one_spec.lua'] = [[
715        describe('chosen suite', function()
716          it('works', function() end)
717        end)
718      ]],
719      ['two_spec.lua'] = [[
720        describe('skipped suite', function()
721          it('works', function() end)
722        end)
723      ]],
724    })
725
726    local code, output = run_harness(suite_dir, {
727      '--filter',
728      'chosen suite',
729    })
730
731    eq(0, code)
732    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
733    matches('chosen suite works', output, true)
734    not_matches('skipped suite works', output, true)
735  end)
736
737  it('does not keep suite-end callbacks from filtered-out files', function()
738    local marker = t.tmpname(false)
739    local suite_dir = write_suite({
740      ['one_spec.lua'] = string.format(
741        [[
742        local harness = require('test.harness')
743
744        harness.on_suite_end(function()
745          local file = assert(io.open(%q, 'ab'))
746          file:write('filtered\n')
747          file:close()
748        end)
749
750        describe('one', function()
751          it('skipped', function()
752            assert(true)
753          end)
754        end)
755      ]],
756        marker
757      ),
758      ['two_spec.lua'] = [[
759        describe('two', function()
760          it('chosen', function()
761            assert(true)
762          end)
763        end)
764      ]],
765    })
766
767    local code, output = run_harness(suite_dir, {
768      '--filter=chosen',
769    })
770
771    eq(0, code)
772    eq(nil, uv.fs_stat(marker))
773    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
774  end)
775
776  it('filters tests out by name across files', function()
777    local suite_dir = write_suite({
778      ['one_spec.lua'] = [[
779        describe('one', function()
780          it('chosen', function() end)
781        end)
782      ]],
783      ['two_spec.lua'] = [[
784        describe('two', function()
785          it('skipped', function() end)
786        end)
787      ]],
788    })
789
790    local code, output = run_harness(suite_dir, {
791      '--filter-out=skipped',
792    })
793
794    eq(0, code)
795    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
796    matches('one chosen', output, true)
797    not_matches('two skipped', output, true)
798  end)
799
800  it('filters tests out by suite name across files', function()
801    local suite_dir = write_suite({
802      ['one_spec.lua'] = [[
803        describe('chosen suite', function()
804          it('works', function() end)
805        end)
806      ]],
807      ['two_spec.lua'] = [[
808        describe('skipped suite', function()
809          it('works', function() end)
810        end)
811      ]],
812    })
813
814    local code, output = run_harness(suite_dir, {
815      '--filter-out',
816      'skipped suite',
817    })
818
819    eq(0, code)
820    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
821    matches('chosen suite works', output, true)
822    not_matches('skipped suite works', output, true)
823  end)
824
825  it('filters tests out by multiple repeated option values', function()
826    local suite_dir = write_suite({
827      ['one_spec.lua'] = [[
828        describe('chosen suite', function()
829          it('works', function() end)
830        end)
831      ]],
832      ['two_spec.lua'] = [[
833        describe('skipped suite', function()
834          it('works', function() end)
835        end)
836      ]],
837      ['three_spec.lua'] = [[
838        describe('three', function()
839          it('skipped test', function() end)
840        end)
841      ]],
842    })
843
844    local code, output = run_harness(suite_dir, {
845      '--filter-out=skipped suite',
846      '--filter-out=skipped test',
847    })
848
849    eq(0, code)
850    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
851    matches('chosen suite works', output, true)
852    not_matches('skipped suite works', output, true)
853    not_matches('three skipped test', output, true)
854  end)
855
856  it('reports when filters exclude all tests', function()
857    local suite_dir = write_suite({
858      ['one_spec.lua'] = [[
859        describe('one', function()
860          it('skipped', function() end)
861        end)
862      ]],
863    })
864
865    local code, output = run_harness(suite_dir, {
866      '--filter-out=.',
867    })
868
869    eq(1, code)
870    matches('No tests matched the current selection.', output, true)
871    not_matches('Running tests from', output, true)
872  end)
873
874  it('reports malformed filter patterns clearly before selection runs', function()
875    local suite_dir = write_suite({
876      ['one_spec.lua'] = [[
877        describe('one', function()
878          it('works', function() end)
879        end)
880      ]],
881    })
882
883    for _, case in ipairs({
884      { '--filter=[', 'invalid value for --filter: malformed pattern' },
885      { '--filter-out=[', 'invalid value for --filter-out: malformed pattern' },
886    }) do
887      local code, output = run_harness(suite_dir, { case[1] })
888
889      eq(1, code)
890      matches(case[2], output, true)
891      not_matches('test_selected', output, true)
892      not_matches('Running tests from', output, true)
893      not_matches('test harness failed with exit code', output, true)
894    end
895  end)
896
897  it('skips unreadable test directories and keeps running readable files', function()
898    if t.is_os('win') then
899      pending('N/A: permission denied directory scan depends on POSIX chmod')
900    end
901
902    local suite_dir = t.tmpname(false)
903    mkdir(suite_dir)
904    t.write_file(suite_dir .. '/one_spec.lua', spec_preamble .. [[
905        describe('one', function()
906          it('works', function() end)
907        end)
908      ]])
909    local blocked = suite_dir .. '/blocked'
910
911    mkdir(blocked)
912    t.write_file(blocked .. '/two_spec.lua', spec_preamble .. [[
913        describe('two', function()
914          it('is hidden behind permissions', function() end)
915        end)
916      ]])
917    finally(function()
918      assert(uv.fs_chmod(blocked, 448))
919    end)
920    assert(uv.fs_chmod(blocked, 0))
921
922    local code, output = run_harness(suite_dir)
923
924    eq(0, code)
925    matches('Running tests from', output, true)
926    matches('one works', output, true)
927    not_matches('two is hidden behind permissions', output, true)
928    matches('1 test from 1 test file of ' .. suite_dir .. ' ran.', output, true)
929  end)
930
931  it('reports missing test paths clearly before loading suites', function()
932    local suite_dir = write_suite({
933      ['one_spec.lua'] = [[
934        describe('one', function()
935          it('works', function() end)
936        end)
937      ]],
938    })
939    local missing = suite_dir .. '/missing'
940
941    local code, output = run_harness(suite_dir, { missing })
942
943    eq(1, code)
944    matches('test path not found: ' .. missing, output, true)
945    not_matches('collect_test_files', output, true)
946    not_matches('Running tests from', output, true)
947    not_matches('test harness failed with exit code', output, true)
948  end)
949
950  it('reports missing helpers clearly before running suites', function()
951    local suite_dir = write_suite({
952      ['one_spec.lua'] = [[
953        describe('one', function()
954          it('works', function() end)
955        end)
956      ]],
957    })
958    local missing = suite_dir .. '/missing.lua'
959
960    local code, output = run_harness(suite_dir, {
961      '--helper=' .. missing,
962    })
963
964    eq(1, code)
965    matches('cannot open ' .. missing, output, true)
966    not_matches('load_helper', output, true)
967    not_matches('Running tests from', output, true)
968    not_matches('test harness failed with exit code', output, true)
969  end)
970
971  it('reports empty value options before running suites', function()
972    local suite_dir = write_suite({
973      ['one_spec.lua'] = [[
974        describe('one', function()
975          it('works', function() end)
976        end)
977      ]],
978    })
979
980    for _, case in ipairs({
981      { '--helper=', 'missing value for --helper' },
982      { '--summary-file=', 'missing value for --summary-file' },
983    }) do
984      local code, output = run_harness(suite_dir, {
985        case[1],
986      })
987
988      eq(1, code)
989      matches(case[2], output, true)
990      not_matches('open_summary_file', output, true)
991      not_matches('Running tests from', output, true)
992      not_matches('test harness failed with exit code', output, true)
993    end
994  end)
995
996  it('treats the next argv item as the value even when it starts with dashes', function()
997    local suite_dir = write_suite({
998      ['one_spec.lua'] = [[
999        describe('one', function()
1000          it('works', function() end)
1001        end)
1002      ]],
1003    })
1004
1005    local code, output = run_harness(suite_dir, { '--filter', '--verbose' })
1006
1007    eq(1, code)
1008    matches('No tests matched the current selection.', output, true)
1009    not_matches('missing value for --filter', output, true)
1010
1011    code, output = run_harness(suite_dir, { '--repeat', '-1' })
1012
1013    eq(1, code)
1014    matches('invalid value for --repeat: -1', output, true)
1015    not_matches('missing value for --repeat', output, true)
1016  end)
1017
1018  it('it() rejects a non-table, non-function second argument', function()
1019    eq(
1020      '.../harness.lua:0: it() arg 2 must be an opts table or a function',
1021      t.pcall_err(it, 'nope', 'not opts', function() end)
1022    )
1023  end)
1024
1025  it('{retries=n} reruns before_each/after_each/finally, only for the retried test', function()
1026    local log = t.tmpname(false)
1027    local suite_dir = write_suite({
1028      ['one_spec.lua'] = ([[
1029        local function log(s)
1030          vim.fn.writefile({ s }, '%s', 'a')
1031        end
1032        describe('retry', function()
1033          before_each(function() log('d-before') end)
1034          after_each(function() log('d-after') end)
1035          it('succeeds on the second attempt', { retries = 2 }, function(ctx)
1036            finally(function() log('t1-finally' .. ctx.retry) end)
1037            eq(true, ctx.retry >= 1)
1038          end)
1039          it('runs once', function(ctx)
1040            log('t2-next' .. ctx.retry)
1041          end)
1042        end)
1043      ]]):format(log),
1044    })
1045
1046    local code, output = run_harness(suite_dir)
1047
1048    eq(0, code)
1049    matches('PASSED  ', output, true)
1050    not_matches('FAILED  ', output, true)
1051    -- Attempt 2 succeeded so attempt 3 never ran, and the next test starts back at retry 0.
1052    eq(
1053      'd-before t1-finally0 d-after d-before t1-finally1 d-after d-before t2-next0 d-after',
1054      table.concat(vim.fn.readfile(log), ' ')
1055    )
1056  end)
1057
1058  it('{retries=n} reports one failure after all retries are exhausted', function()
1059    local suite_dir = write_suite({
1060      ['one_spec.lua'] = [[
1061        describe('retry', function()
1062          local attempts = 0
1063          before_each(function() attempts = attempts + 1 end)
1064          it('never succeeds', { retries = 2 }, function()
1065            error('boom on attempt ' .. attempts)
1066          end)
1067        end)
1068      ]],
1069    })
1070
1071    local code, output = run_harness(suite_dir)
1072
1073    eq(1, code)
1074    matches('boom on attempt 3', output, true)
1075    not_matches('boom on attempt 1', output, true)
1076    matches('1 test, listed below', output, true) -- One failure, not one per attempt.
1077  end)
1078
1079  it('reports test-body errors as failures', function()
1080    local suite_dir = write_suite({
1081      ['one_spec.lua'] = [[
1082        describe('one', function()
1083          it('fails', function()
1084            error('boom from test')
1085          end)
1086        end)
1087      ]],
1088    })
1089
1090    local code, output = run_harness(suite_dir)
1091
1092    eq(1, code)
1093    matches('boom from test', output, true)
1094    matches('FAILED  ', output, true)
1095    not_matches('ERROR   ', output, true)
1096  end)
1097
1098  it('reports test-body failures at the failing line', function()
1099    local suite_dir = write_suite({
1100      ['one_spec.lua'] = [[
1101        describe('one', function()
1102          it('fails', function()
1103            error('boom from test')
1104          end)
1105        end)
1106      ]],
1107    })
1108
1109    local code, output = run_harness(suite_dir)
1110
1111    eq(1, code)
1112    matches('one_spec.lua @ 3: one fails', output, true)
1113  end)
1114
1115  it('ignores fake trace lines embedded in multiline error messages', function()
1116    local suite_dir = write_suite({
1117      ['one_spec.lua'] = [[
1118        describe('one', function()
1119          it('fake trace', function()
1120            error('boom\n' .. debug.getinfo(1, 'S').source:sub(2) .. ':999: fake trace', 0)
1121          end)
1122        end)
1123      ]],
1124    })
1125
1126    local code, output = run_harness(suite_dir)
1127
1128    eq(1, code)
1129    matches('one_spec.lua @ 3: one fake trace', output, true)
1130    not_matches('one_spec.lua @ 999: one fake trace', output, true)
1131  end)
1132
1133  it('reports wrapped assertion failures at the test definition line', function()
1134    local suite_dir = write_suite({
1135      ['one_spec.lua'] = [[
1136        describe('one', function()
1137          it('equal fail', function()
1138            eq(1, 2)
1139          end)
1140        end)
1141      ]],
1142    })
1143
1144    local code, output = run_harness(suite_dir)
1145
1146    eq(1, code)
1147    matches('one_spec.lua @ 2: one equal fail', output, true)
1148    not_matches(root .. '/test/assert.lua @', output, true)
1149  end)
1150
1151  it('reports local wrapper failures at the test definition line', function()
1152    local suite_dir = write_suite({
1153      ['wrapper.lua'] = [[
1154        local M = {}
1155
1156        function M.fail()
1157          error('boom from wrapper')
1158        end
1159
1160        return M
1161      ]],
1162      ['one_spec.lua'] = [[
1163        local wrapper = require('wrapper')
1164
1165        describe('one', function()
1166          it('wrapper fail', function()
1167            wrapper.fail()
1168          end)
1169        end)
1170      ]],
1171    })
1172
1173    local code, output = run_harness(suite_dir)
1174
1175    eq(1, code)
1176    matches('one_spec.lua @ 4: one wrapper fail', output, true)
1177    not_matches('wrapper.lua @ 4:', output, true)
1178  end)
1179
1180  it('reports failing finally cleanup at the cleanup line', function()
1181    local suite_dir = write_suite({
1182      ['one_spec.lua'] = [[
1183        describe('one', function()
1184          it('finfail', function()
1185            finally(function()
1186              error('boom fin')
1187            end)
1188            pending('later')
1189          end)
1190        end)
1191      ]],
1192    })
1193
1194    local code, output = run_harness(suite_dir)
1195
1196    eq(1, code)
1197    matches('one_spec.lua @ 4: one finfail', output, true)
1198  end)
1199
1200  it('rejects finally in setup hooks', function()
1201    local suite_dir = write_suite({
1202      ['one_spec.lua'] = [[
1203        describe('one', function()
1204          setup(function()
1205            finally(function() end)
1206          end)
1207
1208          it('works', function() end)
1209        end)
1210      ]],
1211    })
1212
1213    local code, output = run_harness(suite_dir)
1214
1215    eq(1, code)
1216    matches('one_spec.lua @ 2: one [setup]', output, true)
1217    matches('finally() must be called while a test body is running', output, true)
1218  end)
1219
1220  it('rejects finally in teardown hooks', function()
1221    local suite_dir = write_suite({
1222      ['one_spec.lua'] = [[
1223        describe('one', function()
1224          teardown(function()
1225            finally(function() end)
1226          end)
1227
1228          it('works', function() end)
1229        end)
1230      ]],
1231    })
1232
1233    local code, output = run_harness(suite_dir)
1234
1235    eq(1, code)
1236    matches('one_spec.lua @ 2: one [teardown]', output, true)
1237    matches('finally() must be called while a test body is running', output, true)
1238  end)
1239
1240  it('reports failing after_each cleanup at the cleanup line', function()
1241    local suite_dir = write_suite({
1242      ['one_spec.lua'] = [[
1243        describe('one', function()
1244          after_each(function()
1245            error('boom after')
1246          end)
1247
1248          it('afterfail', function()
1249            pending('later')
1250          end)
1251        end)
1252      ]],
1253    })
1254
1255    local code, output = run_harness(suite_dir)
1256
1257    eq(1, code)
1258    matches('one_spec.lua @ 3: one afterfail', output, true)
1259  end)
1260
1261  it('reports cross-file after_each wrapper failures at the hook definition line', function()
1262    local suite_dir = write_suite({
1263      ['wrapper.lua'] = [[
1264        local M = {}
1265
1266        function M.fail()
1267          error('boom from wrapper')
1268        end
1269
1270        return M
1271      ]],
1272      ['one_spec.lua'] = [[
1273        local wrapper = require('wrapper')
1274
1275        describe('one', function()
1276          after_each(function()
1277            wrapper.fail()
1278          end)
1279
1280          it('afterwrap', function()
1281            assert(true)
1282          end)
1283        end)
1284      ]],
1285    })
1286
1287    local code, output = run_harness(suite_dir)
1288
1289    eq(1, code)
1290    matches('one_spec.lua @ 4: one afterwrap', output, true)
1291    not_matches('one_spec.lua @ 8: one afterwrap', output, true)
1292    not_matches('wrapper.lua @ 4:', output, true)
1293  end)
1294
1295  it(
1296    'reports pending cross-file after_each wrapper failures at the hook definition line',
1297    function()
1298      local suite_dir = write_suite({
1299        ['wrapper.lua'] = [[
1300        local M = {}
1301
1302        function M.fail()
1303          error('boom from wrapper')
1304        end
1305
1306        return M
1307      ]],
1308        ['one_spec.lua'] = [[
1309        local wrapper = require('wrapper')
1310
1311        describe('one', function()
1312          after_each(function()
1313            wrapper.fail()
1314          end)
1315
1316          it('afterwrap pending', function()
1317            pending('later')
1318          end)
1319        end)
1320      ]],
1321      })
1322
1323      local code, output = run_harness(suite_dir)
1324
1325      eq(1, code)
1326      matches('one_spec.lua @ 4: one afterwrap pending', output, true)
1327      not_matches('one_spec.lua @ 8: one afterwrap pending', output, true)
1328      not_matches('wrapper.lua @ 4:', output, true)
1329    end
1330  )
1331
1332  it('reports cross-file finally wrapper failures at the cleanup definition line', function()
1333    local suite_dir = write_suite({
1334      ['wrapper.lua'] = [[
1335        local M = {}
1336
1337        function M.fail()
1338          error('boom from wrapper')
1339        end
1340
1341        return M
1342      ]],
1343      ['one_spec.lua'] = [[
1344        local wrapper = require('wrapper')
1345
1346        describe('one', function()
1347          it('finwrap', function()
1348            finally(function()
1349              wrapper.fail()
1350            end)
1351          end)
1352        end)
1353      ]],
1354    })
1355
1356    local code, output = run_harness(suite_dir)
1357
1358    eq(1, code)
1359    matches('one_spec.lua @ 5: one finwrap', output, true)
1360    not_matches('one_spec.lua @ 4: one finwrap', output, true)
1361    not_matches('wrapper.lua @ 4:', output, true)
1362  end)
1363
1364  it('does not count synthetic hook failures as executed tests', function()
1365    local suite_dir = write_suite({
1366      ['one_spec.lua'] = [[
1367        describe('trace', function()
1368          setup(function()
1369            error('boom setup')
1370          end)
1371
1372          it('passes', function() end)
1373        end)
1374      ]],
1375    })
1376
1377    local code, output = run_harness(suite_dir)
1378
1379    eq(1, code)
1380    matches('0 tests from 1 test file of ' .. suite_dir .. ' ran.', output, true)
1381    matches('1 ERROR', output, true)
1382  end)
1383
1384  it('reports setup failures at the hook definition site', function()
1385    local suite_dir = write_suite({
1386      ['one_spec.lua'] = [[
1387        describe('trace', function()
1388          setup(function()
1389            error('boom setup')
1390          end)
1391
1392          it('passes', function() end)
1393        end)
1394      ]],
1395    })
1396
1397    local code, output = run_harness(suite_dir)
1398
1399    eq(1, code)
1400    matches('one_spec.lua @ 2: trace [setup]', output, true)
1401  end)
1402
1403  it('does not keep suite-end callbacks from load-error files', function()
1404    local marker = t.tmpname(false)
1405    local suite_dir = write_suite({
1406      ['one_spec.lua'] = string.format(
1407        [[
1408        local harness = require('test.harness')
1409
1410        harness.on_suite_end(function()
1411          local file = assert(io.open(%q, 'ab'))
1412          file:write('load\n')
1413          file:close()
1414        end)
1415
1416        error('boom during load')
1417      ]],
1418        marker
1419      ),
1420    })
1421
1422    local code, output = run_harness(suite_dir)
1423
1424    eq(1, code)
1425    eq(nil, uv.fs_stat(marker))
1426    matches('boom during load', output, true)
1427  end)
1428
1429  it('reports load failures at the failing line', function()
1430    local suite_dir = write_suite({
1431      ['one_spec.lua'] = [[
1432        local ok = true
1433
1434        local broken = )
1435      ]],
1436    })
1437
1438    local code, output = run_harness(suite_dir)
1439
1440    eq(1, code)
1441    matches('one_spec%.lua @ 3: .-one_spec%.lua %[load%]', output)
1442  end)
1443
1444  it('reports required-module syntax errors at the real module line', function()
1445    local suite_dir = write_suite({
1446      ['broken.lua'] = [[
1447        local ok = true
1448
1449        local broken = )
1450      ]],
1451      ['one_spec.lua'] = [[
1452        require('broken')
1453
1454        describe('one', function()
1455          it('works', function() end)
1456        end)
1457      ]],
1458    })
1459
1460    local code, output = run_harness(suite_dir)
1461
1462    eq(1, code)
1463    matches('broken%.lua @ 3: .-one_spec%.lua %[load%]', output)
1464  end)
1465end)