summaryrefslogtreecommitdiffstats
path: root/test/lua/busted/runner.lua
blob: 91ce94e501c478edbb6878ecfcfe7879272087c2 (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
-- Busted command-line runner

local path = require 'pl.path'
local term = require 'term'
local utils = require 'busted.utils'
local loaded = false

return function(options)
  if loaded then return else loaded = true end

  local opt = options or {}
  local isBatch = opt.batch
  local cli = require 'cliargs'
  local busted = require 'busted.core'()

  local configLoader = require 'busted.modules.configuration_loader'()
  local helperLoader = require 'busted.modules.helper_loader'()
  local outputHandlerLoader = require 'busted.modules.output_handler_loader'()

  local luacov = require 'busted.modules.luacov'()

  local osexit = require 'busted.compatibility'.osexit

  require 'busted'(busted)

  -- Default cli arg values
  local defaultOutput = term.isatty(io.stdout) and 'utfTerminal' or 'plainTerminal'
  local defaultLoaders = 'lua,moonscript'
  local defaultPattern = '_spec'
  local defaultSeed = 'os.time()'
  local lpathprefix = './src/?.lua;./src/?/?.lua;./src/?/init.lua'
  local cpathprefix = path.is_windows and './csrc/?.dll;./csrc/?/?.dll;' or './csrc/?.so;./csrc/?/?.so;'

  local level = 2
  local info = debug.getinfo(level, 'Sf')
  local source = info.source
  local fileName = source:sub(1,1) == '@' and source:sub(2) or source

  local cliArgsParsed = {}

  local function processOption(key, value, altkey, opt)
    if altkey then cliArgsParsed[altkey] = value end
    cliArgsParsed[key] = value
    return true
  end

  local function processNumber(key, value, altkey, opt)
    local number = tonumber(value)
    if not number then
      return nil, 'argument to ' .. opt:gsub('=.*', '') .. ' must be a number'
    end
    if altkey then cliArgsParsed[altkey] = number end
    cliArgsParsed[key] = number
    return true
  end

  local function processVersion()
    -- Return early if asked for the version
    print(busted.version)
    osexit(0, true)
  end

  -- Load up the command-line interface options
  cli:set_name(path.basename(fileName))
  cli:add_flag('--version', 'prints the program version and exits', processVersion)

  if isBatch then
    cli:optarg('ROOT', 'test script file/folder. Folders will be traversed for any file that matches the --pattern option.', 'spec', 1)

    cli:add_option('-p, --pattern=PATTERN', 'only run test files matching the Lua pattern', defaultPattern, processOption)
  end

  cli:add_option('-o, --output=LIBRARY', 'output library to load', defaultOutput, processOption)
  cli:add_option('-d, --cwd=cwd', 'path to current working directory', './', processOption)
  cli:add_option('-t, --tags=TAGS', 'only run tests with these #tags', nil, processOption)
  cli:add_option('--exclude-tags=TAGS', 'do not run tests with these #tags, takes precedence over --tags', nil, processOption)
  cli:add_option('--filter=PATTERN', 'only run test names matching the Lua pattern', nil, processOption)
  cli:add_option('--filter-out=PATTERN', 'do not run test names matching the Lua pattern, takes precedence over --filter', nil, processOption)
  cli:add_option('-m, --lpath=PATH', 'optional path to be prefixed to the Lua module search path', lpathprefix, processOption)
  cli:add_option('--cpath=PATH', 'optional path to be prefixed to the Lua C module search path', cpathprefix, processOption)
  cli:add_option('-r, --run=RUN', 'config to run from .busted file', nil, processOption)
  cli:add_option('--repeat=COUNT', 'run the tests repeatedly', '1', processNumber)
  cli:add_option('--seed=SEED', 'random seed value to use for shuffling test order', defaultSeed, processNumber)
  cli:add_option('--lang=LANG', 'language for error messages', 'en', processOption)
  cli:add_option('--loaders=NAME', 'test file loaders', defaultLoaders, processOption)
  cli:add_option('--helper=PATH', 'A helper script that is run before tests', nil, processOption)

  cli:add_option('-Xoutput OPTION', 'pass `OPTION` as an option to the output handler. If `OPTION` contains commas, it is split into multiple options at the commas.', nil, processOption)
  cli:add_option('-Xhelper OPTION', 'pass `OPTION` as an option to the helper script. If `OPTION` contains commas, it is split into multiple options at the commas.', nil, processOption)

  cli:add_flag('-c, --coverage', 'do code coverage analysis (requires `LuaCov` to be installed)', processOption)
  cli:add_flag('-v, --verbose', 'verbose output of errors', processOption)
  cli:add_flag('-s, --enable-sound', 'executes `say` command if available', processOption)
  cli:add_flag('-l, --list', 'list the names of all tests instead of running them', processOption)
  cli:add_flag('--no-keep-going', 'quit after first error or failure', processOption)
  cli:add_flag('--no-recursive', 'do not recurse into subdirectories', processOption)
  cli:add_flag('--shuffle', 'randomize file and test order, takes precedence over --sort (--shuffle-test and --shuffle-files)', processOption)
  cli:add_flag('--shuffle-files', 'randomize file execution order, takes precedence over --sort-files', processOption)
  cli:add_flag('--shuffle-tests', 'randomize test order within a file, takes precedence over --sort-tests', processOption)
  cli:add_flag('--sort', 'sort file and test order (--sort-tests and --sort-files)', processOption)
  cli:add_flag('--sort-files', 'sort file execution order', processOption)
  cli:add_flag('--sort-tests', 'sort test order within a file', processOption)
  cli:add_flag('--suppress-pending', 'suppress `pending` test output', processOption)
  cli:add_flag('--defer-print', 'defer print to when test suite is complete', processOption)

  -- Parse the cli arguments
  local cliArgs = cli:parse(arg)
  if not cliArgs then
    osexit(1, true)
  end

  -- Load current working directory
  local fpath = utils.normpath(cliArgs.cwd)

  -- Load busted config file if available
  local configFile = { }
  local bustedConfigFilePath = utils.normpath(path.join(fpath, '.busted'))
  local bustedConfigFile = pcall(function() configFile = loadfile(bustedConfigFilePath)() end)
  if bustedConfigFile then
    local config, err = configLoader(configFile, cliArgsParsed, cliArgs)
    if err then
      print('Error: ' .. err)
      osexit(1, true)
    else
      cliArgs = config
    end
  end

  local tags = {}
  local excludeTags = {}

  if cliArgs.tags and cliArgs.tags ~= '' then
    tags = utils.split(cliArgs.tags, ',')
  end

  if cliArgs['exclude-tags'] and cliArgs['exclude-tags'] ~= '' then
    excludeTags = utils.split(cliArgs['exclude-tags'], ',')
  end

  -- If coverage arg is passed in, load LuaCovsupport
  if cliArgs.coverage then
    luacov()
  end

  -- Add additional package paths based on lpath and cpath cliArgs
  if #cliArgs.lpath > 0 then
    lpathprefix = cliArgs.lpath
    lpathprefix = lpathprefix:gsub('^%.([/%\\])', fpath .. '%1')
    lpathprefix = lpathprefix:gsub(';%.([/%\\])', ';' .. fpath .. '%1')
    package.path = (lpathprefix .. ';' .. package.path):gsub(';;',';')
  end

  if #cliArgs.cpath > 0 then
    cpathprefix = cliArgs.cpath
    cpathprefix = cpathprefix:gsub('^%.([/%\\])', fpath .. '%1')
    cpathprefix = cpathprefix:gsub(';%.([/%\\])', ';' .. fpath .. '%1')
    package.cpath = (cpathprefix .. ';' .. package.cpath):gsub(';;',';')
  end

  local loaders = {}
  if #cliArgs.loaders > 0 then
    string.gsub(cliArgs.loaders, '([^,]+)', function(c) loaders[#loaders+1] = c end)
  end

  -- We report an error if the same tag appears in both `options.tags`
  -- and `options.excluded_tags` because it does not make sense for the
  -- user to tell Busted to include and exclude the same tests at the
  -- same time.
  for _, excluded in pairs(excludeTags) do
    for _, included in pairs(tags) do
      if excluded == included then
        print('Error: Cannot use --tags and --exclude-tags for the same tags')
        osexit(1, true)
      end
    end
  end

  -- watch for test errors
  local failures = 0
  local errors = 0
  local quitOnError = cliArgs['no-keep-going']

  busted.subscribe({ 'error', 'output' }, function(element, parent, message)
    print('Error: Cannot load output library: ' .. element.name .. '\n' .. message)
    return nil, true
  end)

  busted.subscribe({ 'error', 'helper' }, function(element, parent, message)
    print('Error: Cannot load helper script: ' .. element.name .. '\n' .. message)
    return nil, true
  end)

  busted.subscribe({ 'error' }, function(element, parent, message)
    errors = errors + 1
    busted.skipAll = quitOnError
    return nil, true
  end)

  busted.subscribe({ 'failure' }, function(element, parent, message)
    if element.descriptor == 'it' then
      failures = failures + 1
    else
      errors = errors + 1
    end
    busted.skipAll = quitOnError
    return nil, true
  end)

  -- Set up output handler to listen to events
  local outputHandlerOptions = {
    verbose = cliArgs.verbose,
    suppressPending = cliArgs['suppress-pending'],
    language = cliArgs.lang,
    deferPrint = cliArgs['defer-print'],
    arguments = utils.split(cliArgs.Xoutput or '', ',') or {}
  }

  local opath = utils.normpath(path.join(fpath, cliArgs.output))
  local outputHandler = outputHandlerLoader(cliArgs.output, opath, outputHandlerOptions, busted, defaultOutput)
  outputHandler:subscribe(outputHandlerOptions)

  if cliArgs['enable-sound'] then
    require 'busted.outputHandlers.sound'(outputHandlerOptions, busted)
  end

  -- Set up randomization options
  busted.sort = cliArgs['sort-tests'] or cliArgs.sort
  busted.randomize = cliArgs['shuffle-tests'] or cliArgs.shuffle
  busted.randomseed = tonumber(cliArgs.seed) or os.time()

  local getFullName = function(name)
    local parent = busted.context.get()
    local names = { name }

    while parent and (parent.name or parent.descriptor) and
          parent.descriptor ~= 'file' do
      table.insert(names, 1, parent.name or parent.descriptor)
      parent = busted.context.parent(parent)
    end

    return table.concat(names, ' ')
  end

  local hasTag = function(name, tag)
    local found = name:find('#' .. tag)
    return (found ~= nil)
  end

  local filterExcludeTags = function(name)
    for i, tag in pairs(excludeTags) do
      if hasTag(name, tag) then
        return nil, false
      end
    end
    return nil, true
  end

  local filterTags = function(name)
    local fullname = getFullName(name)
    for i, tag in pairs(tags) do
      if hasTag(fullname, tag) then
        return nil, true
      end
    end
    return nil, (#tags == 0)
  end

  local filterOutNames = function(name)
    local found = (getFullName(name):find(cliArgs['filter-out']) ~= nil)
    return nil, not found
  end

  local filterNames = function(name)
    local found = (getFullName(name):find(cliArgs.filter) ~= nil)
    return nil, found
  end

  local printNameOnly = function(name, fn, trace)
    local fullname = getFullName(name)
    if trace and trace.what == 'Lua' then
      print(trace.short_src .. ':' .. trace.currentline .. ': ' .. fullname)
    else
      print(fullname)
    end
    return nil, false
  end

  local ignoreAll = function()
    return nil, false
  end

  local skipOnError = function()
    return nil, (failures == 0 and errors == 0)
  end

  local applyFilter = function(descriptors, name, fn)
    if cliArgs[name] and cliArgs[name] ~= '' then
      for _, descriptor in ipairs(descriptors) do
        busted.subscribe({ 'register', descriptor }, fn, { priority = 1 })
      end
    end
  end

  if cliArgs.list then
    busted.subscribe({ 'suite', 'start' }, ignoreAll, { priority = 1 })
    busted.subscribe({ 'suite', 'end' }, ignoreAll, { priority = 1 })
    applyFilter({ 'setup', 'teardown', 'before_each', 'after_each' }, 'list', ignoreAll)
    applyFilter({ 'it', 'pending' }, 'list', printNameOnly)
  end

  applyFilter({ 'setup', 'teardown', 'before_each', 'after_each' }, 'no-keep-going', skipOnError)
  applyFilter({ 'file', 'describe', 'it', 'pending' }, 'no-keep-going', skipOnError)

  -- The following filters are applied in reverse order
  applyFilter({ 'it', 'pending' }            , 'filter'      , filterNames      )
  applyFilter({ 'describe', 'it', 'pending' }, 'filter-out'  , filterOutNames   )
  applyFilter({ 'it', 'pending' }            , 'tags'        , filterTags       )
  applyFilter({ 'describe', 'it', 'pending' }, 'exclude-tags', filterExcludeTags)

  -- Set up helper script
  if cliArgs.helper and cliArgs.helper ~= '' then
    local helperOptions = {
      verbose = cliArgs.verbose,
      language = cliArgs.lang,
      arguments = utils.split(cliArgs.Xhelper or '', ',') or {}
    }

    local hpath = utils.normpath(path.join(fpath, cliArgs.helper))
    helperLoader(cliArgs.helper, hpath, helperOptions, busted)
  end

  -- Set up test loader options
  local testFileLoaderOptions = {
    verbose = cliArgs.verbose,
    sort = cliArgs['sort-files'] or cliArgs.sort,
    shuffle = cliArgs['shuffle-files'] or cliArgs.shuffle,
    recursive = not cliArgs['no-recursive'],
    seed = busted.randomseed
  }

  -- Load test directory
  local rootFile = cliArgs.ROOT and utils.normpath(path.join(fpath, cliArgs.ROOT)) or fileName
  local pattern = cliArgs.pattern
  local testFileLoader = require 'busted.modules.test_file_loader'(busted, loaders, testFileLoaderOptions)
  local fileList = testFileLoader(rootFile, pattern)

  if not cliArgs.ROOT then
    local ctx = busted.context.get()
    local file = busted.context.children(ctx)[1]
    getmetatable(file.run).__call = info.func
  end

  busted.subscribe({'suite', 'reinitialize'}, function()
    local oldctx = busted.context.get()
    local children = busted.context.children(oldctx)

    busted.context.clear()
    local ctx = busted.context.get()
    for k, v in pairs(oldctx) do
      ctx[k] = v
    end

    for _, child in pairs(children) do
      for descriptor, _ in pairs(busted.executors) do
        child[descriptor] = nil
      end
      busted.context.attach(child)
    end

    busted.randomseed = tonumber(cliArgs.seed) or os.time()

    return nil, true
  end)

  local runs = tonumber(cliArgs['repeat']) or 1
  for i = 1, runs do
    if i > 1 then
      busted.publish({ 'suite', 'reinitialize' })
    end

    busted.publish({ 'suite', 'start' }, i, runs)
    busted.execute()
    busted.publish({ 'suite', 'end' }, i, runs)

    if quitOnError and (failures > 0 or errors > 0) then
      break
    end
  end

  busted.publish({ 'exit' })

  local exit = 0
  if failures > 0 or errors > 0 then
    exit = failures + errors
    if exit > 255 then
      exit = 255
    end
  end
  osexit(exit, true)
end