Skip to content

Commit

Permalink
feat: detect go projects which are not in the repo root
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed May 12, 2024
1 parent ffe7826 commit ee27ed2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ return {

## ⚙️ Configuration

| Argument | Default value | Description |
| ---------------- | ----------------------------------------------- | ----------------------------------------------------------------------------------------- |
| `go_test_args` | `{ "-v", "-race", "-count=1", "-timeout=60s" }` | Arguments to pass into `go test`. |
| `dap_go_enabled` | `false` | Leverage [leoluz/nvim-dap-go](https://github.com/leoluz/nvim-dap-go) for debugging tests. |
| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. |
| Argument | Default value | Description |
| ---------------- | ----------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `go_test_args` | `{ "-v", "-race", "-count=1", "-timeout=60s" }` | Arguments to pass into `go test`. |
| `dap_go_enabled` | `false` | Leverage [leoluz/nvim-dap-go](https://github.com/leoluz/nvim-dap-go) for debugging tests. |
| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. |
| `search_depth` | `32` | Search depth used when searching for `go.mod` and `go.sum` in monorepos. Increase this value if you don't see your tests in monorepos. |

### Example configuration: custom `go test` arguments

Expand All @@ -111,8 +112,9 @@ require("neotest").setup({
})
```

Note that the example above writes a coverage file.
You can use [andythigpen/nvim-coverage](https://github.com/andythigpen/nvim-coverage) to show the coverage in Neovim.
Note that the example above writes a coverage file. You can use
[andythigpen/nvim-coverage](https://github.com/andythigpen/nvim-coverage) to
show the coverage in Neovim.

### Example configuration: debugging

Expand Down
58 changes: 44 additions & 14 deletions lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ M.Adapter = { name = "neotest-golang" }
---@param dir string @Directory to treat as cwd
---@return string | nil @Absolute root dir of test suite
function M.Adapter.root(dir)
---@type string | nil
local cwd = lib.files.match_root_pattern("go.mod", "go.sum")(dir)
if cwd == nil then
return
if M.find_go_files(vim.fn.getcwd(), M.Adapter._search_depth) then -- TODO: make depth configurable
return dir
end
end

Expand Down Expand Up @@ -322,6 +320,34 @@ function M.Adapter.results(spec, result, tree)
return results
end

---Find the project root directory given a current directory to work from
---@param root_path string @Root path of project
---@param search_depth number @Maximum depth to search for go.mod or go.sum
---@return boolean @True if go.mod or go.sum is found, false otherwise
function M.find_go_files(root_path, search_depth)
local stack = { { root_path, 0 } }
while #stack > 0 do
local top = table.remove(stack)
local dir = top[1]
local level = top[2]
if level > search_depth then
return false
end
local files = vim.fn.globpath(dir, "*", true, true)
for _, file in ipairs(files) do
if vim.fn.isdirectory(file) == 1 then
table.insert(stack, { file, level + 1 })
elseif
vim.fn.fnamemodify(file, ":t") == "go.mod"
or vim.fn.fnamemodify(file, ":t") == "go.sum"
then
return true
end
end
end
return false
end

--- Build runspec for a single test
---@param pos neotest.Position
---@param strategy string
Expand Down Expand Up @@ -446,24 +472,27 @@ function M.process_json(raw_output)
return jsonlines
end

-- Adapter options
---@type List
M.Adapter._go_test_args = {
"-v",
"-race",
"-count=1",
"-timeout=60s",
}

-- nvim-dap-go config
M.Adapter._dap_go_enabled = false
M.Adapter._dap_go_opts = {}
M.Adapter._search_depth = 32

setmetatable(M.Adapter, {
__call = function(_, opts)
return M.Adapter.setup(opts)
end,
})

--- Setup the adapter
---@param opts table
---@return table
M.Adapter.setup = function(opts)
opts = opts or {}
if opts.args or opts.dap_go_args then
Expand All @@ -474,16 +503,17 @@ M.Adapter.setup = function(opts)
)
end
if opts.go_test_args then
if opts.go_test_args then
M.Adapter._go_test_args = opts.go_test_args
end
if opts.dap_go_enabled then
M.Adapter._dap_go_enabled = opts.dap_go_enabled
if opts.dap_go_opts then
M.Adapter._dap_go_opts = opts.dap_go_opts
end
M.Adapter._go_test_args = opts.go_test_args
end
if opts.dap_go_enabled then
M.Adapter._dap_go_enabled = opts.dap_go_enabled
if opts.dap_go_opts then
M.Adapter._dap_go_opts = opts.dap_go_opts
end
end
if opts.search_depth then
M.Adapter._search_depth = opts.search_depth
end

return M.Adapter
end
Expand Down

0 comments on commit ee27ed2

Please sign in to comment.