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(busted): option to filter tests to run by pattern [wip] #378

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 22 additions & 1 deletion lua/plenary/busted.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local function get_trace(element, level, msg)
info.traceback = info.traceback:sub(1, index)
return info
end

level = level or 3

local thisdir = dirname(debug.getinfo(1, "Sl").source, ":h")
Expand Down Expand Up @@ -170,7 +171,20 @@ local run_each = function(tbl)
end
end

local matches_filter = function(desc)
if not _PlenaryBustedOpts.filter then
return true
end

local desc_stack = table.concat(current_description, " ") .. desc
return desc_stack:match(_PlenaryBustedOpts.filter)
end

mod.it = function(desc, func)
if not matches_filter(desc) then
return
end

run_each(current_before_each)
local ok, msg, desc_stack = call_inner(desc, func)
run_each(current_after_each)
Expand Down Expand Up @@ -199,12 +213,17 @@ mod.it = function(desc, func)
end

mod.pending = function(desc, func)
if not matches_filter(desc) then
return
end

local curr_stack = vim.deepcopy(current_description)
table.insert(curr_stack, desc)
print(PENDING, "||", table.concat(curr_stack, " "))
end

_PlenaryBustedOldAssert = _PlenaryBustedOldAssert or assert
_PlenaryBustedOpts = {} -- TODO: check if this should be here?

describe = mod.describe
it = mod.it
Expand All @@ -214,7 +233,9 @@ after_each = mod.after_each
clear = mod.clear
assert = require "luassert"

mod.run = function(file)
mod.run = function(file, opts)
_PlenaryBustedOpts = vim.tbl_deep_extend("force", {}, opts or {})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing a global is technically fine because busted is already run in another process. But can't we do

-- top of the file
local busted_opts

--- ......

mod.run = function(file, opts)
  busted_opts = vim.F.if_nil(opts, {})
-- ...
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks! Although I did a vim.F.if_nil(opts, {}, opts) (3rd arg) 👍


print("\n" .. HEADER)
print("Testing: ", file)

Expand Down
13 changes: 12 additions & 1 deletion lua/plenary/test_harness.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ local get_nvim_output = function(job_id)
end

function harness.test_directory_command(command)
-- TODO: this is broken if we pass mutliple args!
-- figure out if there is a way to call vim commands with lua tables as args.
local split_string = vim.split(command, " ")
local directory = table.remove(split_string, 1)

Expand Down Expand Up @@ -78,11 +80,20 @@ function harness.test_directory(directory, opts)

local failure = false

local busted_opts = {}
if opts.filter then
busted_opts.filter = opts.filter
end

local jobs = vim.tbl_map(function(p)
local args = {
"--headless",
"-c",
string.format('lua require("plenary.busted").run("%s")', p:absolute()),
string.format(
'lua require("plenary.busted").run("%s", %s)',
p:absolute(),
vim.inspect(busted_opts) -- TODO: find better way to do this!
),
}

if opts.minimal ~= nil then
Expand Down