Skip to content

Commit

Permalink
Ref/auto test runner (#11)
Browse files Browse the repository at this point in the history
This PR:
- fixes the testfiles (previously it tried to find the root of the
project by `.git`, which isn't available under nix
- simplifies the implementation a bit (before we mapped each path to a
requirable and requried them, now we just `dofile`)
- adds auto test runner tests as well
  • Loading branch information
Dich0tomy authored Aug 7, 2024
2 parents a5e8fb7 + c64dbbf commit d69a482
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
--- and runs their __test functions with busted test context.

local current_filename = debug.getinfo(1).source:match('([%w_%.]+)$')
local testfiles_dir = vim.fs.root(0, '.git') .. '/testfiles'
local testfiles_dir = vim.fs.root(0, 'testfiles') .. '/testfiles'

local function project_lua_files(path, type)
return type == 'file' and vim.endswith(path, 'lua') and not vim.endswith(path, current_filename)
end

local function path_to_requirable(path)
return path:match('lua/(.*)'):gsub('/init%.lua', ''):gsub('%.lua', ''):gsub('/', '.')
end

local function run_module_test(name)
local ok, module = pcall(require, name)
local ok, module = pcall(dofile, name)
if not ok then
describe(name, function()
it('Has an error!', function()
Expand All @@ -39,8 +35,4 @@ local function is_lua_dir(dir)
return vim.startswith(dir, 'lua')
end

vim
.iter(vim.fs.dir('.', { depth = 10, skip = is_lua_dir }))
:filter(project_lua_files)
:map(path_to_requirable)
:each(run_module_test)
vim.iter(vim.fs.dir('.', { depth = 10, skip = is_lua_dir })):filter(project_lua_files):each(run_module_test)
23 changes: 23 additions & 0 deletions lua/cpp-tools/auto_test_runner/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--- For some reason when I define the tests in the main runner the busted context cannot be found,
--- so I just created another file for that.

local M = {}

---@package
function M.__test(testfiles_dir)
describe('auto test runner', function()
it('returns a proper testfiles dir', function()
local file = testfiles_dir .. '/auto_test_runner/test.txt'
print(file)
local f, msg = io.open(file)

if not f then
assert.message(('Kurwa nie dziala: %s'):format(msg)).falsy(true)
end

assert.are.equal(vim.trim(f:read('*a')), 'works')
end)
end)
end

return M
2 changes: 2 additions & 0 deletions testfiles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
A special directory with all kinds of test files categorized by modules that use them in their tests.

Tests use these files by requiring them like so: `require('testfiles.lib.modules')`, `require('testfiles.modules.foo')`, etc.

Or simply opened by doing `io.open(testfile_dir .. 'lib/modules/foo.txt')`
1 change: 1 addition & 0 deletions testfiles/auto_test_runner/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
works

0 comments on commit d69a482

Please sign in to comment.