diff --git a/README.md b/README.md index 7cd4358..ef8bccf 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ adapters = { transform_spec_path = function(path) return path end, + engine_support = true, results_path = function() return async.fn.tempname() end, @@ -191,6 +192,18 @@ require("neotest-rspec")({ }) ``` +### Running engine tests + +By default, the adapter will run specs from the directory that contains the `spec/` directory. This means that nested engines' specs will be run in an isolated way from within the engine. + +You can opt out of this behaviour and always run specs from the project root: + +```lua +require("neotest-rspec")({ + engine_support = false +}) +``` + ### Running tests in a Docker container The following configuration overrides `rspec_cmd` to run a Docker container (using `docker-compose`) and overrides `transform_spec_path` to pass the spec file as a relative path instead of an absolute path to RSpec. The `results_path` needs to be set to a location which is available to both the container and the host. Additionally, to avoid using a custom formatter, you need to specify `formatter = "json"`. diff --git a/engine/spec/engine_spec.rb b/engine/spec/engine_spec.rb new file mode 100644 index 0000000..3b571ee --- /dev/null +++ b/engine/spec/engine_spec.rb @@ -0,0 +1,5 @@ +RSpec.describe 'Engine support' do + it 'runs specs from the engine root' do + expect(Dir.pwd).to end_with('/engine') + end +end diff --git a/engine/spec/engine_spec_01.commands b/engine/spec/engine_spec_01.commands new file mode 100644 index 0000000..4d9cafd --- /dev/null +++ b/engine/spec/engine_spec_01.commands @@ -0,0 +1,2 @@ +:1 +:lua require("neotest").run.run() diff --git a/engine/spec/engine_spec_01.expected b/engine/spec/engine_spec_01.expected new file mode 100644 index 0000000..9d47fb0 --- /dev/null +++ b/engine/spec/engine_spec_01.expected @@ -0,0 +1 @@ +./SPEC/ENGINE_SPEC.RB@@PASSED@@runs specs from the engine root diff --git a/engine/spec/engine_spec_02.commands b/engine/spec/engine_spec_02.commands new file mode 100644 index 0000000..b039081 --- /dev/null +++ b/engine/spec/engine_spec_02.commands @@ -0,0 +1,3 @@ +:lua require("neotest-rspec")({engine_support=false}) +:1 +:lua require("neotest").run.run() diff --git a/engine/spec/engine_spec_02.expected b/engine/spec/engine_spec_02.expected new file mode 100644 index 0000000..49a9435 --- /dev/null +++ b/engine/spec/engine_spec_02.expected @@ -0,0 +1 @@ +./ENGINE/SPEC/ENGINE_SPEC.RB@@FAILED@@runs specs from the engine root diff --git a/lua/neotest-rspec/config.lua b/lua/neotest-rspec/config.lua index e2cc4d1..69e55b3 100644 --- a/lua/neotest-rspec/config.lua +++ b/lua/neotest-rspec/config.lua @@ -20,6 +20,8 @@ M.transform_spec_path = function(path) return path end +M.engine_support = true + M.results_path = function() return require("neotest.async").fn.tempname() end diff --git a/lua/neotest-rspec/init.lua b/lua/neotest-rspec/init.lua index f42187e..2cef4e8 100644 --- a/lua/neotest-rspec/init.lua +++ b/lua/neotest-rspec/init.lua @@ -93,7 +93,7 @@ local function get_formatter_path() local formatter_path = plugin_root .. "/neotest_formatter.rb" -- Return the absolute path - return vim.fn.resolve(formatter_path) + return vim.fn.fnamemodify(formatter_path, ":p") end ---@param args neotest.RunArgs @@ -103,11 +103,14 @@ function NeotestAdapter.build_spec(args) local engine_name = nil local spec_path = config.transform_spec_path(position.path) - local path = async.fn.expand("%") + local path = vim.fn.fnamemodify(async.fn.expand("%"), ":.") + + if config.engine_support then + -- if the path starts with spec, it's a normal test. Otherwise, it's an engine test + local match = vim.regex("spec/"):match_str(path) + if match and match ~= 0 then engine_name = string.sub(path, 0, match - 1) end + end - -- if the path starts with spec, it's a normal test. Otherwise, it's an engine test - local match = vim.regex("^spec/"):match_str(path) - if match and match ~= 0 then engine_name = string.sub(path, 0, match - 1) end local results_path = config.results_path() local formatter_path = get_formatter_path() @@ -257,6 +260,9 @@ setmetatable(NeotestAdapter, { return opts.results_path end end + if opts.engine_support ~= nil then + config.engine_support = opts.engine_support + end if is_callable(opts.formatter) then config.formatter = opts.formatter elseif opts.formatter then diff --git a/tests/template.lua b/tests/template.lua index 8ce671b..c13763f 100644 --- a/tests/template.lua +++ b/tests/template.lua @@ -9,6 +9,12 @@ local function assert_skipped(reason) end local function for_each_test_file(test, callback) + -- If given a full relative path, use it directly + local test_path = Path:new(cwd, test):absolute() + if vim.loop.fs_stat(test_path) then + callback(test_path) + return + end local files = scandir.scan_dir(Path:new(cwd, "spec"):absolute()) for _, file in pairs(files) do if string.match(file, test) then diff --git a/tests/unit/rspec/engine_spec.lua b/tests/unit/rspec/engine_spec.lua new file mode 100644 index 0000000..1ca7776 --- /dev/null +++ b/tests/unit/rspec/engine_spec.lua @@ -0,0 +1,3 @@ +local test = require("tests.template") + +test.describe("Testing engine specs", "engine/spec/engine_spec.rb")