Skip to content

Commit

Permalink
Add support for running minitest in docker container (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
epair authored Dec 3, 2024
1 parent a211cd6 commit 7ff057d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@ require("neotest-minitest")({
})
```

### Running tests in a Docker container

The following configuration overrides `test_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 Minitest. 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-minitest")({
test_cmd = function()
return vim.tbl_flatten({
"docker",
"compose",
"exec",
"-i",
"-w", "/app",
"-e", "RAILS_ENV=test",
"app",
"bundle",
"exec",
"test"
})
end,

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

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

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

## :rocket: Usage

_NOTE_: All usages of `require('neotest').run.run` can be mapped to a command in your config (this is not included and should be done by yourself).
Expand Down
8 changes: 8 additions & 0 deletions lua/neotest-minitest/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ M.get_test_cmd = function()
})
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-minitest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,18 @@ end
function NeotestAdapter.build_spec(args)
local script_args = {}
local position = args.tree:data()
local results_path = async.fn.tempname()
local results_path = config.results_path()
local spec_path = config.transform_spec_path(position.path)

local name_mappings = utils.get_mappings(args.tree)

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

local function run_by_name()
local full_name = utils.escaped_full_test_name(args.tree, position.name)
table.insert(script_args, position.path)
table.insert(script_args, spec_path)
table.insert(script_args, "--name")
-- https://chriskottom.com/articles/command-line-flags-for-minitest-in-the-raw/
table.insert(script_args, "/^" .. full_name .. "$/")
Expand Down Expand Up @@ -292,6 +293,20 @@ setmetatable(NeotestAdapter, {
return opts.test_cmd
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 7ff057d

Please sign in to comment.