diff --git a/lua/cpp-tools/auto_test_runner.lua b/lua/cpp-tools/auto_test_runner/auto_test_runner.lua similarity index 71% rename from lua/cpp-tools/auto_test_runner.lua rename to lua/cpp-tools/auto_test_runner/auto_test_runner.lua index 129457f..dcf72c7 100644 --- a/lua/cpp-tools/auto_test_runner.lua +++ b/lua/cpp-tools/auto_test_runner/auto_test_runner.lua @@ -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() @@ -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) diff --git a/lua/cpp-tools/auto_test_runner/test.lua b/lua/cpp-tools/auto_test_runner/test.lua new file mode 100644 index 0000000..29ff3eb --- /dev/null +++ b/lua/cpp-tools/auto_test_runner/test.lua @@ -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 diff --git a/testfiles/README.md b/testfiles/README.md index 83fcfc3..1c02bc0 100644 --- a/testfiles/README.md +++ b/testfiles/README.md @@ -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')` diff --git a/testfiles/auto_test_runner/test.txt b/testfiles/auto_test_runner/test.txt new file mode 100644 index 0000000..153d194 --- /dev/null +++ b/testfiles/auto_test_runner/test.txt @@ -0,0 +1 @@ +works