Skip to content

Commit

Permalink
feat: filters by one directory when DEFAULT_PATH is file
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasia committed Nov 12, 2023
1 parent 0164c77 commit d9a1db1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
23 changes: 22 additions & 1 deletion lua/neotest-bash/core/dir_filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ local read_dot_env = require("neotest-bash.core.util.read_dot_env")

DirFilter = {}

local function is_file(path)
-- check if it's a file with regex
-- example: .../some_test.sh
-- should end with _test.sh
return string.match(path, "_test.sh$")
end

---Filter directories when searching for test files
---@async
---@param name string Name of directory
Expand All @@ -17,7 +24,21 @@ function DirFilter.filter_dir(name, rel_path, root)
return true -- allow all directories
end

if string.match(rel_path, test_dir) then
if is_file(test_dir) then
-- get rid of the file name
-- example: tests/some_test.sh -> tests/
test_dir = string.match(test_dir, "^(.*/)")
return test_dir == rel_path
else
-- make sure it ends with a slash
-- check if it has a slash at the end
-- else add it
if string.match(test_dir, "/$") == nil then
test_dir = test_dir .. "/"
end
end

if string.find(rel_path, test_dir) ~= nil then
return true -- allow specified directories
end

Expand Down
60 changes: 51 additions & 9 deletions tests/core/dir_filter_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,36 @@ local async = require("nio").tests

local root = vim.fn.fnamemodify("tests/fixtures/demo-project/", ":p")

local function create_temporary_dir()
-- create a temporary directory
local tmp_dir = "/tmp/neotest-bash/tests/"
os.execute("mkdir -p " .. tmp_dir)
return tmp_dir
end

local function remove_dir()
-- delete the temporary directory
os.execute("rm -rf /tmp/neotest-bash/tests/")
end

--- @param dir string
--- @param env table
local function create_env_file(dir, env)
local env_file = dir .. ".env"
os.execute("touch " .. env_file)
for k, v in pairs(env) do
os.execute("echo " .. k .. "=" .. v .. " >> " .. env_file)
end
end

describe("dir_filter", function()
after_each(function()
remove_dir()
end)

local allowed_dirs = {
"tests/",
"tests/core",
"tests/core/",
}
for _, dir in ipairs(allowed_dirs) do
async.it("should allow with .env file => (DEFAULT_PATH=tests): " .. dir, function()
Expand All @@ -35,20 +61,36 @@ describe("dir_filter", function()

-- local all_dirs = table.insert(not_allowed_dirs, allowed_dirs)
-- merge tables
local all_dirs = { vim.tbl_extend("force", allowed_dirs, not_allowed_dirs) }
local all_dirs = vim.tbl_extend("force", allowed_dirs, not_allowed_dirs)
for _, dir in ipairs(all_dirs) do
async.it("should allow all directories when there is no .env file", function()
async.it("should allow all directories when there is no .env file: " .. dir, function()
-- create a temporary directory
local tmp_dir = vim.fn.tempname()
local tmp_root = create_temporary_dir()

local is_allowed = plugin.filter_dir("name1", dir, tmp_root)

vim.fn.mkdir(tmp_dir, "p")
assert.is_true(is_allowed)
end)
end

local is_allowed = plugin.filter_dir("name1", dir, tmp_dir)
local dirs = {
["tests/core/"] = true,
["tests/core/deeper/"] = false,
}
for dir, expected in pairs(dirs) do
async.it("should allow only one directory when DEFAULT_PATH is a test file: " .. dir, function()
local name = "name1" -- doesn't matter
-- create a temporary directory
local tmp_root = create_temporary_dir()
create_env_file(tmp_root, { ["DEFAULT_PATH"] = "tests/core/logic_test.sh" })

--delete the temporary directory
vim.fn.delete(tmp_dir, "rf")
local is_allowed = plugin.filter_dir(name, dir, tmp_root)

assert.is_true(is_allowed)
assert.is_equal(expected, is_allowed)
end)
end

it("playing around", function()
print(string.find("core/tests/", "core") ~= nil)
end)
end)

0 comments on commit d9a1db1

Please sign in to comment.