From 82ad2fa6b19c542102c5c3b18b0b8b00ea63be3f Mon Sep 17 00:00:00 2001 From: b4mbus Date: Wed, 7 Aug 2024 22:54:30 +0200 Subject: [PATCH 1/3] fix(test-runner): find root by `testfiles` not .git `.git` is not be accessible in the Nix check. --- lua/cpp-tools/auto_test_runner.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/cpp-tools/auto_test_runner.lua b/lua/cpp-tools/auto_test_runner.lua index 129457f..cf2ace6 100644 --- a/lua/cpp-tools/auto_test_runner.lua +++ b/lua/cpp-tools/auto_test_runner.lua @@ -3,7 +3,7 @@ --- 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') local function project_lua_files(path, type) return type == 'file' and vim.endswith(path, 'lua') and not vim.endswith(path, current_filename) From a9cd05323a8770632f421f0699ff13ab6ba6f4fd Mon Sep 17 00:00:00 2001 From: b4mbus Date: Wed, 7 Aug 2024 22:56:04 +0200 Subject: [PATCH 2/3] ref(test-runner): don't map paths and require them, use dofile instead --- lua/cpp-tools/auto_test_runner.lua | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/lua/cpp-tools/auto_test_runner.lua b/lua/cpp-tools/auto_test_runner.lua index cf2ace6..c34d223 100644 --- a/lua/cpp-tools/auto_test_runner.lua +++ b/lua/cpp-tools/auto_test_runner.lua @@ -9,12 +9,8 @@ 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) From c64dbbf58ec5d222bb1664b96ef2d96e3ecfc731 Mon Sep 17 00:00:00 2001 From: b4mbus Date: Wed, 7 Aug 2024 23:19:05 +0200 Subject: [PATCH 3/3] fix(test-runner): add proper support for testfiles --- .../auto_test_runner.lua | 2 +- lua/cpp-tools/auto_test_runner/test.lua | 23 +++++++++++++++++++ testfiles/README.md | 2 ++ testfiles/auto_test_runner/test.txt | 1 + 4 files changed, 27 insertions(+), 1 deletion(-) rename lua/cpp-tools/{ => auto_test_runner}/auto_test_runner.lua (94%) create mode 100644 lua/cpp-tools/auto_test_runner/test.lua create mode 100644 testfiles/auto_test_runner/test.txt diff --git a/lua/cpp-tools/auto_test_runner.lua b/lua/cpp-tools/auto_test_runner/auto_test_runner.lua similarity index 94% rename from lua/cpp-tools/auto_test_runner.lua rename to lua/cpp-tools/auto_test_runner/auto_test_runner.lua index c34d223..dcf72c7 100644 --- a/lua/cpp-tools/auto_test_runner.lua +++ b/lua/cpp-tools/auto_test_runner/auto_test_runner.lua @@ -3,7 +3,7 @@ --- 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, '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) 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