Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(*): add --name, --exclude-names-file and --log-success options #721

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions busted/modules/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ return function(options)
cli:option('-t, --tags=TAGS', 'only run tests with these #tags', {}, processList)
cli:option('--exclude-tags=TAGS', 'do not run tests with these #tags, takes precedence over --tags', {}, processList)
cli:option('--filter=PATTERN', 'only run test names matching the Lua pattern', {}, processMultiOption)
cli:option('--name=NAME', 'run test with the given full name', {}, processMultiOption)
cli:option('--filter-out=PATTERN', 'do not run test names matching the Lua pattern, takes precedence over --filter', {}, processMultiOption)
cli:option('--exclude-names-file=FILE', 'do not run the tests with names listed in the given file, takes precedence over --filter', nil, processOption)
cli:option('--log-success=FILE', 'append the name of each successful test to the given file', nil, processOption)
cli:option('-m, --lpath=PATH', 'optional path to be prefixed to the Lua module search path', lpathprefix, processPath)
cli:option('--cpath=PATH', 'optional path to be prefixed to the Lua C module search path', cpathprefix, processPath)
cli:option('-r, --run=RUN', 'config to run from .busted file', nil, processOption)
Expand Down
35 changes: 31 additions & 4 deletions busted/modules/filter_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ return function()
return nil, true
end

local excludeNames = {}
if options.excludeNamesFile then
for name in io.lines(options.excludeNamesFile) do
table.insert(excludeNames, name)
end
end

local excludeNamesFile = function(name)
for _, filter in ipairs(excludeNames) do
if getFullName(name) == filter then
return nil, false
end
end
return nil, true
end

local name = function(name)
for _, candidate in pairs(options.name) do
if string.find(candidate, getFullName(name), 1, true) then
return nil, true
end
end
return nil, (#options.name == 0)
end

local filterNames = function(name)
for _, filter in pairs(options.filter) do
if getFullName(name):find(filter) ~= nil then
Expand Down Expand Up @@ -122,10 +147,12 @@ return function()
applyFilter({ 'file', 'describe', 'it', 'pending' }, 'nokeepgoing', skipOnError)

-- The following filters are applied in reverse order
applyFilter({ 'it', 'pending' } , 'filter' , filterNames )
applyFilter({ 'describe', 'it', 'pending' }, 'filterOut' , filterOutNames )
applyFilter({ 'it', 'pending' } , 'tags' , filterTags )
applyFilter({ 'describe', 'it', 'pending' }, 'excludeTags', filterExcludeTags)
applyFilter({ 'it', 'pending' } , 'filter' , filterNames )
applyFilter({ 'describe', 'it', 'pending' }, 'name' , name )
applyFilter({ 'describe', 'it', 'pending' }, 'filterOut' , filterOutNames )
applyFilter({ 'describe', 'it', 'pending' }, 'excludeNamesFile', excludeNamesFile )
applyFilter({ 'it', 'pending' } , 'tags' , filterTags )
applyFilter({ 'describe', 'it', 'pending' }, 'excludeTags' , filterExcludeTags )
end

return filter
Expand Down
24 changes: 24 additions & 0 deletions busted/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,36 @@ return function(options)
end
end

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

if cliArgs['log-success'] then
local logFile = assert(io.open(cliArgs['log-success'], 'a'))
busted.subscribe({ 'test', 'end' }, function (test, parent, status)
if status == "success" then
logFile:write(getFullName() .. "\n")
end
end)
end

-- Load tag and test filters
filterLoader(busted, {
tags = cliArgs.tags,
excludeTags = cliArgs['exclude-tags'],
filter = cliArgs.filter,
name = cliArgs.name,
filterOut = cliArgs['filter-out'],
excludeNamesFile = cliArgs['exclude-names-file'],
list = cliArgs.list,
nokeepgoing = not cliArgs['keep-going'],
suppressPending = cliArgs['suppress-pending'],
Expand Down
9 changes: 8 additions & 1 deletion completions/bash/busted.bash
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,14 @@ _busted() {
# no completion available
return 0
;;
--filter|--filter-out)
--filter|--filter-out|--name)
# no completion available
return 0
;;
--exclude-names-file|--log-success)
_filedir
return 0
;;
-m|--lpath|--cpath)
_filedir -d
return 0
Expand Down Expand Up @@ -175,10 +179,13 @@ _busted() {
--lua=
--ignore-lua
--filter= --filter-out=
--name=
--exclude-names-file=
hanshuebner marked this conversation as resolved.
Show resolved Hide resolved
--repeat=
--seed=
--lang=
--loaders=
--log-success
--helper=
-c --coverage --no-coverage
-s --enable-sound --no-enable-sound
Expand Down
3 changes: 3 additions & 0 deletions completions/zsh/_busted
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ _busted_args=(
"--exclude-tags=[Do not run tests with these #tags, takes precedence over --tags]: :"
"--filter=[Only run test names matching the Lua pattern]: :"
"--filter-out=[Do not run test names matching the Lua pattern, takes precedence over --filter]: :"
"--exclude-names-file=[Do not run the tests with names listed in the given file, takes precedence over --filter]:files:_files"
"--name=[Run test with the given full name]:files:_files"
"--log-success=[Append the name of each successful test to the given file]:file:_files"
"-e[Execute Lua statement]: :"
"(-v --verbose --no-verbose)"{-v,--verbose}"[Verbose output of errors]"
"(-v --verbose --no-verbose)--no-verbose[Disable verbose output of errors]"
Expand Down
22 changes: 22 additions & 0 deletions spec/cl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ describe('Tests the busted command-line options', function()
assert.is_true(success)
end)

local function count_successes(out)
local count = 0
for successes in out:gmatch('(%d+) success') do
count = count + successes
end
return count
end

it('tests running with --log-success and --exclude-names-file specified', function ()
local logfile = os.tmpname()
local success, errcnt, out = executeBusted('--pattern=_filter.lua$ --log-success=' .. logfile .. ' --exclude-names-file=' .. logfile)
assert.is_false(success)
assert.equals(8, errcnt)
assert.equals(2, count_successes(out))
-- re-run tests with previously successful tests skipped
success, errcnt, out = executeBusted('--pattern=_filter.lua$ --log-success=' .. logfile .. ' --exclude-names-file=' .. logfile)
assert.is_false(success)
assert.equals(8, errcnt)
assert.equals(0, count_successes(out))
os.remove(logfile)
end)

it('tests running with --filter specified in describe', function ()
local success, errcnt = executeBusted('--pattern=_filter.lua$ --filter="patt1"')
assert.is_false(success)
Expand Down
2 changes: 1 addition & 1 deletion try
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
luarocks remove busted --force
luarocks make
LUA_PATH="./?.lua;./?/init.lua;./src/?/?.lua;./src/?/init.lua;$LUA_PATH" busted $@
LUA_PATH="./?.lua;./?/init.lua;$LUA_PATH" busted "$@"
hanshuebner marked this conversation as resolved.
Show resolved Hide resolved
Loading