diff --git a/README.md b/README.md index e6ce033d..96ad1955 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,7 @@ consider setting up neotest and its adapters in a | `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. | +| `colorize_test_output` | `true` | Enable output color for `SUCCESS`, `FAIL`, and `SKIP` tests. | | `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. | diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index 0d0f7346..8caa54b2 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -13,6 +13,7 @@ local opts = { go_list_args = {}, -- NOTE: can also be a function dap_go_opts = {}, -- NOTE: can also be a function testify_enabled = false, + colorize_test_output = true, warn_test_name_dupes = true, warn_test_not_executed = true, diff --git a/lua/neotest-golang/process.lua b/lua/neotest-golang/process.lua index e09f96ff..3f14cc1b 100644 --- a/lua/neotest-golang/process.lua +++ b/lua/neotest-golang/process.lua @@ -302,6 +302,8 @@ function M.decorate_with_go_test_results(res, gotest_output) elseif line.Action == "fail" then test_data.status = "failed" elseif line.Action == "output" then + line.Output = M.colorizer(line.Output) + test_data.gotest_data.output = vim.list_extend(test_data.gotest_data.output, { line.Output }) @@ -334,6 +336,26 @@ function M.decorate_with_go_test_results(res, gotest_output) return res end +--- Colorize the test output based on the test result. +--- +--- It will colorize the test output line based on the test result (PASS - green, FAIL - red, SKIP - yellow). +--- @param output string +--- @return string +function M.colorizer(output) + if not options.get().colorize_test_output == true or not output then + return output + end + + if string.find(output, "FAIL") then + output = output:gsub("^", "[31m"):gsub("$", "[0m") + elseif string.find(output, "PASS") then + output = output:gsub("^", "[32m"):gsub("$", "[0m") + elseif string.find(output, "SKIP") then + output = output:gsub("^", "[33m"):gsub("$", "[0m") + end + return output +end + --- Show warnings. --- @param d table<string, TestData> --- @return nil diff --git a/tests/unit/options_spec.lua b/tests/unit/options_spec.lua index 199c6a26..e7859406 100644 --- a/tests/unit/options_spec.lua +++ b/tests/unit/options_spec.lua @@ -14,6 +14,7 @@ describe("Options are set up", function() gotestsum_args = { "--format=standard-verbose" }, dap_go_opts = {}, testify_enabled = false, + colorize_test_output = true, warn_test_name_dupes = true, warn_test_not_executed = true, @@ -37,6 +38,7 @@ describe("Options are set up", function() gotestsum_args = { "--format=standard-verbose" }, dap_go_opts = {}, testify_enabled = false, + colorize_test_output = false, warn_test_name_dupes = true, warn_test_not_executed = true, @@ -64,6 +66,7 @@ describe("Options are set up", function() return {} end, testify_enabled = false, + colorize_test_output = true, warn_test_name_dupes = true, warn_test_not_executed = true,