Skip to content

Commit

Permalink
feat: filter directories during discovery
Browse files Browse the repository at this point in the history
See #13
  • Loading branch information
rcarriga committed Sep 11, 2022
1 parent af13a93 commit d20507c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
11 changes: 9 additions & 2 deletions doc/neotest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Default values:
jump = {
enabled = true
},
log_level = 3,
output = {
enabled = true,
open_on_run = "short"
Expand Down Expand Up @@ -173,6 +174,7 @@ Fields~
Class~
{neotest.Config:} neotest.CoreConfig
Fields~
{log_level} `(number)` Minimum log levels, one of vim.log.levels
{consumers} `(table<string, neotest.Consumer>)`
{icons} `(table<string, string>)`
{highlights} `(table<string, string>)`
Expand All @@ -181,13 +183,18 @@ Fields~
{summary} neotest.Config.summary
{output} neotest.Config.output
{status} neotest.Config.status
{projects} `(table<string, neotest.CoreConfig>)` Project specific settings, keys are project root directories (e.g "~/Dev/my_project")
{projects} `(table<string, neotest.CoreConfig>)` Project specific settings, keys
are project root directories (e.g "~/Dev/my_project")

Class~
{neotest.Config.discovery}
Fields~
{enabled} `(boolean)`
{concurrent} `(integer)` Number of workers to parse files concurrently. 0 automatically assigns number based on CPU. Set to 1 if experiencing lag.
{concurrent} `(integer)` Number of workers to parse files concurrently. 0
automatically assigns number based on CPU. Set to 1 if experiencing lag.
{filter_dir} `(nil)` | fun(name: string, rel_path: string, root: string): boolean
A function to filter directories when searching for test files. Receives the name,
path relative to project root and project root path

Class~
{neotest.Config.running}
Expand Down
6 changes: 5 additions & 1 deletion lua/neotest/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ function NeotestClient:_update_positions(path, args)
end
end
logger.info("Searching", path, "for test files")
local files = lib.func_util.filter_list(adapter.is_test_file, lib.files.find(path))
local root_path = existing_root and existing_root:data().path or path
local files = lib.func_util.filter_list(
adapter.is_test_file,
lib.files.find(path, { filter_dir = config.projects[root_path].discovery.filter_dir })
)
local positions = lib.files.parse_dir_from_files(path, files)
logger.debug("Found", positions)
self._state:update_positions(adapter_id, positions)
Expand Down
10 changes: 8 additions & 2 deletions lua/neotest/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ define_highlights()
---@field summary neotest.Config.summary
---@field output neotest.Config.output
---@field status neotest.Config.status
---@field projects table<string, neotest.CoreConfig> Project specific settings, keys are project root directories (e.g "~/Dev/my_project")
---@field projects table<string, neotest.CoreConfig> Project specific settings, keys
--- are project root directories (e.g "~/Dev/my_project")

---@class neotest.Config.discovery
---@field enabled boolean
---@field concurrent integer Number of workers to parse files concurrently. 0 automatically assigns number based on CPU. Set to 1 if experiencing lag.
---@field concurrent integer Number of workers to parse files concurrently. 0
--- automatically assigns number based on CPU. Set to 1 if experiencing lag.
---@field filter_dir nil | fun(name: string, rel_path: string, root: string): boolean
--- A function to filter directories when searching for test files. Receives the name,
--- path relative to project root and project root path

---@class neotest.Config.running
---@field concurrent boolean Run tests concurrently when an adapter provides multiple commands to run
Expand Down Expand Up @@ -104,6 +109,7 @@ local default_config = {
discovery = {
enabled = true,
concurrent = 0,
filter_dir = nil,
},
running = {
concurrent = true,
Expand Down
26 changes: 16 additions & 10 deletions lua/neotest/lib/file/find.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@ local M = {}
--- Find all files under the given directory.
--- Does not search hidden directories.
---@async
---@param dir_path string
---@param root string
---@return string[] @Absolute paths of all files within directories to search
function M.find(dir_path)
function M.find(root, opts)
opts = opts or {}
local filter_dir = opts.filter_dir
local sep = require("neotest.lib").files.sep
local dirs_to_scan = { dir_path }
local dirs_to_scan = {}

local paths = {}
local dir, dir_handle
local dir, dir_handle = "", uv.fs_scandir(root)
while dir_handle or #dirs_to_scan > 0 do
if not dir_handle then
dir = table.remove(dirs_to_scan, 1)
dir_handle = uv.fs_scandir(dir)
end

local next_path, path_type = uv.fs_scandir_next(dir_handle)
local name, path_type = uv.fs_scandir_next(dir_handle)
local rel_path = name and (dir == "" and name or (dir .. sep .. name))

if not next_path then
if not name then
dir_handle = nil
elseif path_type == "directory" and next_path:sub(1, 1) ~= "." then
local i = #dirs_to_scan + 1
dirs_to_scan[i] = dir .. sep .. next_path
elseif
path_type == "directory"
and name:sub(1, 1) ~= "."
and (not filter_dir or filter_dir(name, rel_path, root))
then
dirs_to_scan[#dirs_to_scan + 1] = rel_path
elseif path_type == "file" then
paths[#paths + 1] = dir .. sep .. next_path
paths[#paths + 1] = (root .. sep .. rel_path)
end
end
return paths
Expand Down

3 comments on commit d20507c

@daliusd
Copy link

Choose a reason for hiding this comment

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

@rcarriga could it be that with this change you have introduced a problem with node_modules. It looks like neotest is looking for tests in node_modules folder now.

@rcarriga
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It wasn't with this change but yes a recent change would have that effect (switched from using rg or fd to custom file finding. I've added in some logic to filter out node_modules by default.

@daliusd
Copy link

Choose a reason for hiding this comment

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

Thanks

Please sign in to comment.