Skip to content

Commit

Permalink
feat: Allow configuration of test directories and test file pattern (#40
Browse files Browse the repository at this point in the history
)

* feat: Allow configuration of test directories and test file pattern

* Simplify conditions

---------

Co-authored-by: Jhon Pedroza <[email protected]>
  • Loading branch information
oskar1233 and jfpedroza authored Jan 19, 2025
1 parent baede6c commit a242aeb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ require("neotest").setup({
-- Can be a function to return a dynamic value.
-- Default: 1000
write_delay = 1000,
-- The pattern to match test files
-- Default: "_test.exs$"
test_file_pattern = ".test.exs$",
-- Function to determine whether a directory should be ignored
-- By default includes root test directory and umbrella apps' test directories
-- Params:
-- - name (string) - Name of directory
-- - rel_path (string) - Path to directory, relative to root
-- - root (string) - Root directory of project
filter_dir = function(name, rel_path, root)
return rel_path == "test"
or rel_path == "lib"
or vim.startswith(rel_path, 'test/')
or vim.startswith(rel_path, 'lib/')
end,
}),
}
})
Expand Down
4 changes: 2 additions & 2 deletions lua/neotest-elixir/base.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}

function M.is_test_file(file_path)
return vim.endswith(file_path, "_test.exs")
function M.is_test_file(file_path, pattern)
return file_path:match(pattern)
end

return M
33 changes: 25 additions & 8 deletions lua/neotest-elixir/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ local function get_iex_shell_direction()
return "horizontal"
end

local function get_test_file_pattern()
return "_test.exs$"
end

local function filter_dir(_, rel_path, _)
return rel_path == "test"
or vim.startswith(rel_path, "test/")
or rel_path == "apps"
or rel_path:match("^apps/[^/]+$")
or rel_path:match("^apps/[^/]+/test")
end

local function post_process_command(cmd)
return cmd
end
Expand Down Expand Up @@ -71,16 +83,12 @@ end

ElixirNeotestAdapter.root = core.mix_root

function ElixirNeotestAdapter.filter_dir(_, rel_path, _)
return rel_path == "test"
or vim.startswith(rel_path, "test/")
or rel_path == "apps"
or rel_path:match("^apps/[^/]+$")
or rel_path:match("^apps/[^/]+/test")
function ElixirNeotestAdapter.filter_dir(name, rel_path, root)
return filter_dir(name, rel_path, root)
end

function ElixirNeotestAdapter.is_test_file(file_path)
return base.is_test_file(file_path)
return base.is_test_file(file_path, get_test_file_pattern())
end

local function get_match_type(captured_nodes)
Expand Down Expand Up @@ -343,7 +351,7 @@ end

setmetatable(ElixirNeotestAdapter, {
__call = function(_, opts)
if opts.post_process_command and type(opts.post_process_command) == "function" then
if is_callable(opts.post_process_command) then
post_process_command = opts.post_process_command
end

Expand Down Expand Up @@ -377,6 +385,15 @@ setmetatable(ElixirNeotestAdapter, {
get_write_delay = write_delay
end

local test_file_pattern = callable_opt(opts.test_file_pattern)
if test_file_pattern then
get_test_file_pattern = test_file_pattern
end

if is_callable(opts.filter_dir) then
filter_dir = opts.filter_dir
end

return ElixirNeotestAdapter
end,
})
Expand Down

0 comments on commit a242aeb

Please sign in to comment.