From 81d19d4551b4701307d70900ad5e6345eb2b7e1a Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Mon, 22 Jul 2024 21:37:26 +0200 Subject: [PATCH] feat: gotestsum --- README.md | 53 +++++++++++++++++++++++++++++----- lua/neotest-golang/health.lua | 30 ++++++++++++++----- lua/neotest-golang/options.lua | 4 +-- tests/unit/options_spec.lua | 8 ++--- 4 files changed, 74 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index df50a6eb..64421f5c 100644 --- a/README.md +++ b/README.md @@ -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"`. | +| `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 @@ -170,6 +172,43 @@ return { } ``` +### Use `gotestsum` as test runner + +For increased robustness, you can opt in to run tests with +[`gotestsum`](https://github.com/gotestyourself/gotestsum) as test runner. It +brings the capability of using one format for stdout/sterr and at the same time +write the test output to JSON file. Technically, it will call `go test` under +the hood, and therefore the `go_test_args` configuration is still valid and will +apply. + +Using `gotestsum` will bring the following: + +- When attaching to a running test, you will now be able to view clean `go test` + output instead of wading through hard-to-read JSON. +- On some platforms/terminals, there's a risk of having characters such as ANSI + codes injected seemingly randomly into the JSON test output, garbling it up + and resulting in issues with JSON decoding. In such cases, enable `gotestsum` + to rid such 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
diff --git a/lua/neotest-golang/health.lua b/lua/neotest-golang/health.lua index d49aad72..093d8e60 100644 --- a/lua/neotest-golang/health.lua +++ b/lua/neotest-golang/health.lua @@ -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") + M.binary_found_on_path("gotestsum") + + start("DAP (debugging)") + 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() diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index 8482a765..5017cc8f 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -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, } diff --git a/tests/unit/options_spec.lua b/tests/unit/options_spec.lua index 8b669990..02dc277b 100644 --- a/tests/unit/options_spec.lua +++ b/tests/unit/options_spec.lua @@ -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() @@ -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)