Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ adapters = {
transform_spec_path = function(path)
return path
end,
engine_support = true,
results_path = function()
return async.fn.tempname()
end,
Expand Down Expand Up @@ -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"`.
Expand Down
5 changes: 5 additions & 0 deletions engine/spec/engine_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions engine/spec/engine_spec_01.commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:1
:lua require("neotest").run.run()
1 change: 1 addition & 0 deletions engine/spec/engine_spec_01.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./SPEC/ENGINE_SPEC.RB@@PASSED@@runs specs from the engine root
3 changes: 3 additions & 0 deletions engine/spec/engine_spec_02.commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:lua require("neotest-rspec")({engine_support=false})
:1
:lua require("neotest").run.run()
1 change: 1 addition & 0 deletions engine/spec/engine_spec_02.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./ENGINE/SPEC/ENGINE_SPEC.RB@@FAILED@@runs specs from the engine root
2 changes: 2 additions & 0 deletions lua/neotest-rspec/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions lua/neotest-rspec/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/rspec/engine_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local test = require("tests.template")

test.describe("Testing engine specs", "engine/spec/engine_spec.rb")
Loading