Skip to content

Commit

Permalink
feat: gotestsum
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 22, 2024
1 parent f6657d8 commit 289ef5d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 21 deletions.
51 changes: 44 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ You can run `:checkhealth neotest-golang` to review common issues.

## ⚙️ Configuration

| Argument | Default value | Description |
| ------------------------ | ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `go_test_args` | `{ "-v", "-race", "-count=1" }` | Arguments to pass into `go test`. |
| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. |
| `testify_enabled` | `false` | Enable support for [testify](https://github.com/stretchr/testify) suites. See [here](https://github.com/fredrikaverpil/neotest-golang#testify-suites) for more info. |
| `warn_test_name_dupes` | `true` | Warn about duplicate test names within the same Go package. |
| `warn_test_not_executed` | `true` | Warn if test was not executed. |
| Argument | Default value | Description |
| ------------------------ | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `runner` | `go` | Defines the test runner. Valid values: `go` or `gotestsum`. |
| `go_test_args` | `{ "-v", "-race", "-count=1" }` | Arguments to pass into `go test`. |
| `gotestsum_args` | `{ "--format=standard-verbose" }` | Arguments to pass into `gotestsum`. Will only be used if `runner = "gotestsum"`. Note that `go_test_args` still applies. |
| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. |
| `testify_enabled` | `false` | Enable support for [testify](https://github.com/stretchr/testify) suites. See [here](https://github.com/fredrikaverpil/neotest-golang#testify-suites) for more info. |
| `warn_test_name_dupes` | `true` | Warn about duplicate test names within the same Go package. |
| `warn_test_not_executed` | `true` | Warn if test was not executed. |

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

Expand Down Expand Up @@ -170,6 +172,41 @@ return {
}
```

### Use `gotestsum` as test runner

To improve reliability, you can choose to run tests using
[`gotestsum`](https://github.com/gotestyourself/gotestsum) as the test runner.
This tool allows you to use one format for stdout while simultaneously writing
test output to a JSON file. `gotestsum` actually calls go test behind the
scenes, so your `go_test_args` configuration remains valid and will still apply.
Using `gotestsum` offers the following benefits:

- When you "attach" to a running test, you'll see clean `go test` output instead
of having to navigate through difficult-to-read JSON.
- On certain platforms or terminals, there's a risk of ANSI codes or other
characters being seemingly randomly inserted into the JSON test output. This
can corrupt the data and cause problems with JSON decoding. Enabling
`gotestsum` eliminates these issues.

First install it:

```bash
go install gotest.tools/gotestsum@latest
```

Then add the required configuration:

```lua
local config = { -- Specify configuration
runner = "gotestsum"
}
require("neotest").setup({
adapters = {
require("neotest-golang")(config), -- Apply configuration
},
})
```

### Example configuration: extra everything

<details>
Expand Down
30 changes: 22 additions & 8 deletions lua/neotest-golang/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,37 @@ local lib = require("neotest-golang.lib")
local M = {}

function M.check()
M.go_binary_on_path()
start("Requirements")
M.binary_found_on_path("go")
M.go_mod_found()

M.is_plugin_available("neotest")
M.is_plugin_available("nvim-treesitter")
M.is_plugin_available("nio")
M.is_plugin_available("dap-go")
M.is_plugin_available("plenary")

start("Gotestsum (optional)")
M.binary_found_on_path("gotestsum")

start("DAP (optional)")
M.is_plugin_available("dap")
M.is_plugin_available("dapui")
M.is_plugin_available("dap-go")
end

function M.go_binary_on_path()
local go = vim.fn.executable("go")
if go == 1 then
ok("Go binary found on PATH: " .. vim.fn.exepath("go"))
function M.binary_found_on_path(executable)
local found = vim.fn.executable(executable)
if found == 1 then
ok(
"Binary '"
.. executable
.. "' found on PATH: "
.. vim.fn.exepath(executable)
)
return true
else
warn("Go binary not found on PATH")
warn("Binary '" .. executable .. "' not found on PATH")
end
return false
end

function M.go_mod_found()
Expand Down
4 changes: 2 additions & 2 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ local logger = require("neotest-golang.logging")
local M = {}

local opts = {
runner = "go", -- or "gotestsum"
go_test_args = { "-v", "-race", "-count=1" },
gotestsum_args = { "--format=standard-verbose" },
dap_go_opts = {},
testify_enabled = false,
warn_test_name_dupes = true,
warn_test_not_executed = true,

-- experimental, for now undocumented, options
runner = "go", -- or "gotestsum"
gotestsum_args = { "--format=standard-verbose" },
dev_notifications = false,
}

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ local _ = require("plenary")
describe("Options are set up", function()
it("With defaults", function()
local expected_options = {
runner = "go",
go_test_args = {
"-v",
"-race",
"-count=1",
},
gotestsum_args = { "--format=standard-verbose" },
dap_go_opts = {},
testify_enabled = false,
warn_test_name_dupes = true,
warn_test_not_executed = true,

-- experimental
runner = "go",
gotestsum_args = { "--format=standard-verbose" },
dev_notifications = false,
}
options.setup()
Expand All @@ -25,20 +25,20 @@ describe("Options are set up", function()

it("With non-defaults", function()
local expected_options = {
runner = "go",
go_test_args = {
"-v",
"-race",
"-count=1",
"-parallel=1", -- non-default
},
gotestsum_args = { "--format=standard-verbose" },
dap_go_opts = {},
testify_enabled = false,
warn_test_name_dupes = true,
warn_test_not_executed = true,

-- experimental
runner = "go",
gotestsum_args = { "--format=standard-verbose" },
dev_notifications = false,
}
options.setup(expected_options)
Expand Down

0 comments on commit 289ef5d

Please sign in to comment.