<commit_before>-- 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 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', nil, processOption)
  cli:add_option('--seed=SEED', 'random seed value to use for shuffling test order', defaultSeed, processOption)
  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('--no-keep-going', 'quit after first error or failure', processOption)
  cli:add_flag('--no-recurse', 'do not recurse into subdirectories', processOption)
  cli:add_flag('--list', 'list the names of all tests instead of running them', 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(err)
    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<commit_msg>Exit if error loading configuration<commit_after>-- 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 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', nil, processOption)
  cli:add_option('--seed=SEED', 'random seed value to use for shuffling test order', defaultSeed, processOption)
  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('--no-keep-going', 'quit after first error or failure', processOption)
  cli:add_flag('--no-recurse', 'do not recurse into subdirectories', processOption)
  cli:add_flag('--list', 'list the names of all tests instead of running them', 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(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
{'fraction_non_alphanumeric': 0.1348900476082521, 'fraction_numerical': 0.001738079044812212, 'mean_word_length': 4.314859437751004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5170272', 'n_tokens_mistral': 4264, 'n_tokens_neox': 4109, 'n_words': 1297}