Skip to content

Commit

Permalink
feat: add support for running rspec in a docker container. (#52)
Browse files Browse the repository at this point in the history
* feat: Add support for running rspec in a docker container.

- Adds `transform_spec_path` config option to allow the spec path to be
  modified before being passed to rspec.
- Adds `results_path` config option to allow the results file to be
  modified (required so that a location which is available to both the
  docker container and host can be used).

* fix: Fix call to async in default results_path function.
  • Loading branch information
ramblex authored Jul 29, 2023
1 parent 754431f commit 7f03e1e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ adapters = {
})
end,
root_files = { "Gemfile", ".rspec", ".gitignore" },
filter_dirs = { ".git", "node_modules" }
filter_dirs = { ".git", "node_modules" },
transform_spec_path = function(path)
return path
end,
results_path = function()
return async.fn.tempname()
end
}),
}
```
Expand Down Expand Up @@ -131,7 +137,43 @@ require("neotest-rspec")({

### Running tests in a docker container

Whilst not directly supported by neotest, but you can accomplish this using a shell script as your Rspec command. See [this comment](https://github.com/nvim-neotest/neotest/issues/89#issuecomment-1338141432) for an example.
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.

```lua
require("neotest").setup({
adapters = {
require("neotest-rspec")({
rspec_cmd = function()
return vim.tbl_flatten({
"docker",
"compose",
"exec",
"-i",
"-w", "/app",
"-e", "RAILS_ENV=test",
"app",
"bundle",
"exec",
"rspec"
})
end,

transform_spec_path = function(path)
local prefix = require('neotest-rspec').root(path)
return string.sub(path, string.len(prefix) + 2, -1)
end,

results_path = "tmp/rspec.output"
})
}
})
```

Alternatively, you can accomplish this using a shell script as your Rspec command. See [this comment](https://github.com/nvim-neotest/neotest/issues/89#issuecomment-1338141432) for an example.

## :rocket: Usage

Expand Down
8 changes: 8 additions & 0 deletions lua/neotest-rspec/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ M.get_filter_dirs = function()
return { ".git", "node_modules" }
end

M.transform_spec_path = function(path)
return path
end

M.results_path = function()
return require("neotest.async").fn.tempname()
end

return M
21 changes: 18 additions & 3 deletions lua/neotest-rspec/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ function NeotestAdapter.build_spec(args)
local position = args.tree:data()
local engine_name = nil

local spec_path = config.transform_spec_path(position.path)
local path = async.fn.expand("%")

-- 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 = async.fn.tempname()
local results_path = config.results_path()

local script_args = vim.tbl_flatten({
"-f",
Expand All @@ -107,14 +108,14 @@ function NeotestAdapter.build_spec(args)
})

local function run_by_filename()
table.insert(script_args, position.path)
table.insert(script_args, spec_path)
end

local function run_by_line_number()
table.insert(
script_args,
vim.tbl_flatten({
position.path .. ":" .. tonumber(position.range[1] + 1),
spec_path .. ":" .. tonumber(position.range[1] + 1),
})
)
end
Expand Down Expand Up @@ -199,6 +200,20 @@ setmetatable(NeotestAdapter, {
return opts.filter_dirs
end
end
if is_callable(opts.transform_spec_path) then
config.transform_spec_path = opts.transform_spec_path
elseif opts.transform_spec_path then
config.transform_spec_path = function()
return opts.transform_spec_path
end
end
if is_callable(opts.results_path) then
config.results_path = opts.results_path
elseif opts.results_path then
config.results_path = function()
return opts.results_path
end
end
return NeotestAdapter
end,
})
Expand Down

0 comments on commit 7f03e1e

Please sign in to comment.