Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gotestsum #150

Merged
merged 1 commit into from
Oct 13, 2024
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
55 changes: 47 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,16 @@ consider setting up neotest and its adapters in a

## ⚙️ Configuration

| Argument | Default value | Description |
| ------------------------ | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `go_test_args` | `{ "-v", "-race", "-count=1" }` | Arguments to pass into `go test`. Note: [`-tags` usage](https://github.com/fredrikaverpil/neotest-golang#using-build-tags), [pass args as function](https://github.com/fredrikaverpil/neotest-golang#pass-arguments-as-function-instead-of-table). |
| `go_list_args` | `{}` | Arguments to pass into `go list`. Note: [`-tags` usage](https://github.com/fredrikaverpil/neotest-golang#using-build-tags), [pass args as function](https://github.com/fredrikaverpil/neotest-golang#pass-arguments-as-function-instead-of-table). |
| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. Note: [`-tags` usage](https://github.com/fredrikaverpil/neotest-golang#using-build-tags), [pass args as function](https://github.com/fredrikaverpil/neotest-golang#pass-arguments-as-function-instead-of-table). |
| `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`. Note: [`-tags` usage](https://github.com/fredrikaverpil/neotest-golang#using-build-tags), [pass args as function](https://github.com/fredrikaverpil/neotest-golang#pass-arguments-as-function-instead-of-table). |
| `gotestsum_args` | `{ "--format=standard-verbose" }` | Arguments to pass into `gotestsum`. Notes: [`-tags` usage](https://github.com/fredrikaverpil/neotest-golang#using-build-tags), [pass args as function](https://github.com/fredrikaverpil/neotest-golang#pass-arguments-as-function-instead-of-table). Will only be used if `runner = "gotestsum"`. The `go_test_args` still applies. |
| `go_list_args` | `{}` | Arguments to pass into `go list`. Note: [`-tags` usage](https://github.com/fredrikaverpil/neotest-golang#using-build-tags), [pass args as function](https://github.com/fredrikaverpil/neotest-golang#pass-arguments-as-function-instead-of-table). |
| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. Note: [`-tags` usage](https://github.com/fredrikaverpil/neotest-golang#using-build-tags), [pass args as function](https://github.com/fredrikaverpil/neotest-golang#pass-arguments-as-function-instead-of-table). |
| `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 @@ -257,6 +259,43 @@ return {

For a more verbose example, see the "extra everything" example config.

### 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.

Make the `gotestsum` command availalbe via
[mason.nvim](https://github.com/williamboman/mason.nvim) or by running the
following in your shell:

```bash
go install gotest.tools/gotestsum@latest
fredrikaverpil marked this conversation as resolved.
Show resolved Hide resolved
```

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
3 changes: 3 additions & 0 deletions lua/neotest-golang/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ function M.check()
M.is_plugin_available("dap")
M.is_plugin_available("dapui")
M.is_plugin_available("dap-go")

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

function M.binary_found_on_path(executable)
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,16 +7,16 @@ local logger = require("neotest-golang.logging")
local M = {}

local opts = {
runner = "go", -- or "gotestsum"
go_test_args = { "-v", "-race", "-count=1" }, -- NOTE: can also be a function
gotestsum_args = { "--format=standard-verbose" }, -- NOTE: can also be a function
go_list_args = {}, -- NOTE: can also be a function
dap_go_opts = {}, -- NOTE: can also be a function
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" }, -- NOTE: can also be a function
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,20 +4,20 @@ 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",
},
go_list_args = {},
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 @@ -26,21 +26,21 @@ 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
},
go_list_args = {},
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
Loading