robin/laufey

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

Justin M. Keyestest: mark non-actionable tests as N/A #40207baabb7ab6f

master
12.2 KiB478 linesraw
1--- @alias test.reporter.SummaryStatus 'skipped' | 'failure' | 'error'
2
3--- @class test.reporter.ColorMap
4--- @field bright fun(s: any): string
5--- @field green fun(s: any): string
6--- @field yellow fun(s: any): string
7--- @field magenta fun(s: any): string
8--- @field red fun(s: any): string
9--- @field cyan fun(s: any): string
10--- @field dim fun(s: any): string
11
12--- @class test.reporter.FileElement
13--- @field name string
14--- @field duration? number
15
16--- @class test.reporter.Options
17--- @field paths string[]
18--- @field verbose boolean
19--- @field summary_file string
20
21--- @class test.base_reporter
22--- @field new fun(opts: test.reporter.Options): test.base_reporter
23--- @field suite_start fun(self: test.base_reporter, repeat_index?: integer, repeat_count?: integer)
24--- @field file_start fun(self: test.base_reporter, file: test.reporter.FileElement)
25--- @field test_start fun(self: test.base_reporter, name: string)
26--- @field test_end fun(self: test.base_reporter, record: test.harness.Record)
27--- @field file_end fun(self: test.base_reporter, file: test.reporter.FileElement, test_count: integer)
28--- @field suite_end fun(self: test.base_reporter, duration: number, run_summary: test.harness.RunSummary, failure_output?: string)
29
30--- @class test.reporter : test.base_reporter
31--- @field opts test.reporter.Options
32--- @field colors test.reporter.ColorMap
33local M = {}
34M.__index = M
35
36--- @return test.reporter.ColorMap
37local function identity_colors()
38  return setmetatable({}, {
39    __index = function()
40      return function(s)
41        return s == nil and '' or tostring(s)
42      end
43    end,
44  })
45end
46
47--- @param code string
48--- @return fun(s: any): string
49local function ansi_color(code)
50  return function(s)
51    return ('\27[%sm%s\27[0m'):format(code, s == nil and '' or tostring(s))
52  end
53end
54
55local ansi_colors = {
56  bright = ansi_color('1'),
57  green = ansi_color('32'),
58  yellow = ansi_color('33'),
59  magenta = ansi_color('35'),
60  red = ansi_color('31'),
61  cyan = ansi_color('36'),
62  dim = ansi_color('2'),
63}
64
65--- @return boolean
66local function use_colors()
67  local test_colors = os.getenv('TEST_COLORS')
68  if not test_colors then
69    return true
70  end
71
72  local value = test_colors:lower()
73  return not (value == 'false' or value == '0' or value == 'no' or value == 'off')
74end
75
76--- @param path? string
77--- @return file
78local function open_summary_file(path)
79  if type(path) ~= 'string' or path == '-' then
80    return io.stdout
81  end
82
83  return (assert(io.open(path, 'w')))
84end
85
86--- @param opts test.reporter.Options
87--- @return test.reporter
88function M.new(opts)
89  local colors = identity_colors()
90  if use_colors() then
91    colors = ansi_colors
92  end
93
94  --- @type test.reporter
95  local self = setmetatable({
96    opts = opts,
97    colors = colors,
98  }, M)
99
100  return self
101end
102
103--- @param element? { duration?: number }
104--- @return number
105local function get_elapsed_time_ms(element)
106  if element and element.duration then
107    return element.duration * 1000
108  end
109
110  return tonumber('nan')
111end
112
113--- @private
114--- @param s any
115--- @return string
116function M:succ(s)
117  return self.colors.bright(self.colors.green(s))
118end
119
120--- @private
121--- @param s any
122--- @return string
123function M:skip(s)
124  return self.colors.bright(self.colors.yellow(s))
125end
126
127--- @private
128--- @param s any
129--- @return string
130function M:fail(s)
131  return self.colors.bright(self.colors.magenta(s))
132end
133
134--- @private
135--- @param s any
136--- @return string
137function M:errr(s)
138  return self.colors.bright(self.colors.red(s))
139end
140
141--- @private
142--- @param s any
143--- @return string
144function M:fpath(s)
145  return self.colors.cyan(s)
146end
147
148--- @private
149--- @param s any
150--- @return string
151function M:time(s)
152  return self.colors.dim(s)
153end
154
155--- @private
156--- @param s any
157--- @return string
158function M:sect(s)
159  return self.colors.green(self.colors.dim(s))
160end
161
162--- @private
163--- @param s any
164--- @return string
165function M:nmbr(s)
166  return self.colors.bright(s)
167end
168
169--- @private
170--- @param status test.harness.ResultStatus
171--- @return string
172function M:result_text(status)
173  if status == 'success' then
174    return ('%s\n'):format(self:succ('OK'))
175  elseif status == 'pending' then
176    return ('%s\n'):format(self:skip('SKIP'))
177  elseif status == 'failure' then
178    return ('%s\n'):format(self:fail('FAIL'))
179  end
180
181  return ('%s\n'):format(self:errr('ERR'))
182end
183
184--- @private
185--- @param status test.reporter.SummaryStatus
186--- @return string
187function M:summary_label(status)
188  if status == 'skipped' then
189    return self:skip('SKIPPED ')
190  elseif status == 'failure' then
191    return self:fail('FAILED  ')
192  end
193
194  return self:errr('ERROR   ')
195end
196
197--- @private
198--- @param status test.reporter.SummaryStatus
199--- @param count integer
200--- @return string
201function M:summary_header_noun(status, count)
202  if status == 'error' then
203    return count == 1 and 'error' or 'errors'
204  end
205
206  return count == 1 and 'test' or 'tests'
207end
208
209--- @private
210--- @param status test.reporter.SummaryStatus
211--- @param count integer
212--- @return string
213function M:summary_footer_noun(status, count)
214  if status == 'skipped' then
215    return count == 1 and 'SKIPPED TEST' or 'SKIPPED TESTS'
216  elseif status == 'failure' then
217    return count == 1 and 'FAILED TEST' or 'FAILED TESTS'
218  elseif status == 'error' then
219    return count == 1 and 'ERROR' or 'ERRORS'
220  end
221
222  return count == 1 and 'TEST' or 'TESTS'
223end
224
225--- @param message any
226--- @return string
227local function stringify_message(message)
228  if type(message) == 'string' then
229    return message
230  elseif message == nil then
231    return ''
232  end
233
234  return vim.inspect(message)
235end
236
237--- True if `pending` starts with "<token>:". Used to bucket skips:
238--- - 'N/A' for non-actionable skips.
239--- - 'NVIM_TEST_INTEG' for integration tests.
240---
241--- @private
242--- @param pending test.harness.Record
243--- @param token string
244--- @return boolean
245function M:has_skip_token(pending, token)
246  if pending.name and vim.list_contains(vim.split(pending.name, '[ :]'), token) then
247    return true
248  end
249
250  if type(pending.message) == 'string' then
251    return vim.list_contains(vim.split(pending.message, '[ :]'), token)
252  end
253
254  return false
255end
256
257--- @private
258--- @param pending test.harness.Record
259--- @return string
260function M:pending_description(pending)
261  local message = stringify_message(pending.message)
262  if message == '' then
263    return ''
264  end
265
266  return table.concat({ message, '\n' })
267end
268
269--- @private
270--- @param failure test.harness.Record
271--- @return string
272function M:failure_description(failure)
273  local message = stringify_message(failure.message)
274  if message == '' then
275    message = 'Nil error'
276  end
277
278  local parts = { message, '\n' }
279  if self.opts.verbose and failure.traceback then
280    parts[#parts + 1] = failure.traceback
281    parts[#parts + 1] = '\n'
282  end
283
284  return table.concat(parts)
285end
286
287--- @private
288--- @param trace? test.harness.Trace
289--- @return string
290function M:get_file_line(trace)
291  if not trace or not trace.short_src then
292    return ''
293  end
294
295  local source = vim.fs.normalize(trace.short_src)
296  local line = trace.currentline or 0
297  return self:fpath(source) .. ' @ ' .. self:fpath(line) .. ': '
298end
299
300--- @private
301--- @param status test.reporter.SummaryStatus
302--- @param count integer
303--- @param list test.harness.Record[]
304--- @param describe fun(self: test.reporter, item: test.harness.Record): string
305--- @return string
306function M:get_test_list(status, count, list, describe)
307  if count == 0 then
308    return ''
309  end
310
311  local label = self:summary_label(status)
312  local parts = {
313    ('%s %s %s, listed below:\n'):format(
314      label,
315      self:nmbr(count),
316      self:summary_header_noun(status, count)
317    ),
318  }
319  local na_count = 0
320  local integ_count = 0
321
322  for _, item in ipairs(list) do
323    if status == 'skipped' and self:has_skip_token(item, 'NVIM_TEST_INTEG') then
324      integ_count = integ_count + 1
325    elseif status == 'skipped' and self:has_skip_token(item, 'N/A') then
326      na_count = na_count + 1
327    else
328      local fullname = self:get_file_line(item.trace) .. self:nmbr(item.name)
329      parts[#parts + 1] = ('%s %s\n'):format(label, fullname)
330      parts[#parts + 1] = describe(self, item)
331    end
332  end
333
334  if na_count > 0 then
335    parts[#parts + 1] = self:nmbr(('Skipped %d N/A test(s) (not listed)\n'):format(na_count))
336  end
337  if integ_count > 0 then
338    parts[#parts + 1] =
339      self:nmbr(('Skipped %d NVIM_TEST_INTEG test(s) (not listed)\n'):format(integ_count))
340  end
341
342  return table.concat(parts)
343end
344
345--- @private
346--- @param status test.reporter.SummaryStatus
347--- @param count integer
348--- @return string
349function M:get_summary(status, count)
350  if count == 0 then
351    return ''
352  end
353
354  return (' %s %s\n'):format(self:nmbr(count), self:summary_footer_noun(status, count))
355end
356
357--- @private
358--- @return string
359--- @param summary test.harness.RunSummary
360function M:get_summary_string(summary)
361  local tests = summary.success_count == 1 and 'test' or 'tests'
362  local parts = {
363    ('%s %s %s.\n'):format(self:succ('PASSED  '), self:nmbr(summary.success_count), tests),
364    self:get_test_list('skipped', summary.skipped_count, summary.pendings, M.pending_description),
365    self:get_test_list('failure', summary.failure_count, summary.failures, M.failure_description),
366    self:get_test_list('error', summary.error_count, summary.errors, M.failure_description),
367  }
368
369  if (summary.skipped_count + summary.failure_count + summary.error_count) > 0 then
370    parts[#parts + 1] = '\n'
371  end
372
373  parts[#parts + 1] = self:get_summary('skipped', summary.skipped_count)
374  parts[#parts + 1] = self:get_summary('failure', summary.failure_count)
375  parts[#parts + 1] = self:get_summary('error', summary.error_count)
376  return table.concat(parts)
377end
378
379--- @return nil
380--- @param repeat_index? integer
381--- @param repeat_count? integer
382function M:suite_start(repeat_index, repeat_count)
383  if repeat_count and repeat_count > 1 and repeat_index then
384    io.write(('\nRepeating all tests (run %d of %d) . . .\n\n'):format(repeat_index, repeat_count))
385  end
386  io.write(('%s Global test environment setup.\n'):format(self:sect('--------')))
387  io.flush()
388end
389
390--- @param file test.reporter.FileElement
391function M:file_start(file)
392  io.write(
393    ('%s Running tests from %s\n'):format(
394      self:sect('--------'),
395      self:fpath(vim.fs.normalize(file.name))
396    )
397  )
398  io.flush()
399end
400
401--- @param name string
402function M:test_start(name)
403  local desc = ('%s %s'):format(_G._nvim_test_id or '', name)
404  io.write(('%s %s: '):format(self:sect('RUN     '), desc))
405  io.flush()
406end
407
408--- @private
409--- @param record { duration?: number }
410--- @param text string
411function M:write_status(record, text)
412  io.write(('%s %s'):format(self:time(('%.2f ms'):format(get_elapsed_time_ms(record))), text))
413  io.flush()
414end
415
416--- @param record test.harness.Record
417function M:test_end(record)
418  local text = self:result_text(record.status)
419  if record.status == 'failure' or record.status == 'error' then
420    text = text .. self:failure_description(record)
421  end
422
423  self:write_status(record, text)
424end
425
426--- @param file test.reporter.FileElement
427--- @param test_count integer
428function M:file_end(file, test_count)
429  io.write(
430    ('%s %s %s from %s %s\n\n'):format(
431      self:sect('--------'),
432      self:nmbr(test_count),
433      test_count == 1 and 'test' or 'tests',
434      self:fpath(vim.fs.normalize(file.name)),
435      self:time(('(%.2f ms total)'):format(get_elapsed_time_ms(file)))
436    )
437  )
438  io.flush()
439end
440
441--- @param duration number
442--- @param run_summary test.harness.RunSummary
443--- @param failure_output? string
444function M:suite_end(duration, run_summary, failure_output)
445  local tests = run_summary.test_count == 1 and 'test' or 'tests'
446  local files = run_summary.file_count == 1 and 'file' or 'files'
447
448  io.write(('%s Global test environment teardown.\n'):format(self:sect('--------')))
449  io.flush()
450
451  local fpath = function(s)
452    return self:fpath(s)
453  end
454
455  local summary_file = open_summary_file(self.opts.summary_file)
456  summary_file:write('\n')
457  summary_file:write(
458    ('%s %s %s from %s test %s of %s ran. %s\n'):format(
459      self:sect('========'),
460      self:nmbr(run_summary.test_count),
461      tests,
462      self:nmbr(run_summary.file_count),
463      files,
464      vim.iter(self.opts.paths):map(fpath):join(';'),
465      self:time(('(%.2f ms total)'):format(duration * 1000))
466    )
467  )
468  summary_file:write(self:get_summary_string(run_summary))
469  if failure_output then
470    summary_file:write(failure_output)
471  end
472  summary_file:flush()
473  if summary_file ~= io.stdout then
474    summary_file:close()
475  end
476end
477
478return M