Skip to content

Commit

Permalink
ref: improve test runner to use nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-souza committed Sep 26, 2024
1 parent 381d2a3 commit 17672e1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
lua_test_files = $(shell find lua -name "*_test.lua")

all: pr-ready t

t: tests
test: tests
tests:
@echo "===> Running tests \n" \
$(foreach t_file,$(lua_test_files), $(shell lua $(t_file) > /dev/null && echo '.' || echo 'F');)
@echo "===> Running tests \n" && \
nvim --headless +"lua require('lua.shared.tests').test()" +"qall"

fmt:
echo "===> Formatting"
Expand Down
2 changes: 1 addition & 1 deletion lua/di/init_test.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- create test in lua
local di = require("lua.di")
local di = require("di")
local t = require("lua.shared.tests")

t.run("test module setup", function()
Expand Down
1 change: 0 additions & 1 deletion lua/ollero/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ local default_options = {
local M = {
---@param opts OlleroOptions
setup = function(opts)
local logger = require("shared.logger")
opts = vim.tbl_deep_extend("force", default_options, opts or {})

local registration_list = {
Expand Down
29 changes: 27 additions & 2 deletions lua/shared/tests.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
local M = {}

local log = require("shared.logger").create_logger({ log_level = "debug" })

--- Run a test
---@param name string
---@param cb function
function M.run(name, cb)
print("Running test: " .. name)
log.debug("Running test: " .. name)
cb()
print("Test passed: " .. name)
log.debug("Test passed: " .. name)
end

---Check if a file is a test file
---@param file string
---@return boolean
local function is_test_file(file)
return file:match(".*_test.lua$")
end

--- Find and run all tests
function M.test()
local files = vim.fs.find(
is_test_file,
{ type = "file", path = vim.fn.getcwd() .. "/lua" }
)

for _, file in ipairs(files) do
local relative_path = file:gsub(".*/ollero.nvim/lua/", "")
local require_path = relative_path:gsub(".lua", ""):gsub("/", ".")
require(require_path)
print(".")
end
print("\n")
end

return M

0 comments on commit 17672e1

Please sign in to comment.