Skip to content

Commit

Permalink
feat: test output color (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymorelli96 authored Oct 28, 2024
1 parent 11db9a2 commit 47ba93e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand Down
1 change: 1 addition & 0 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
22 changes: 22 additions & 0 deletions lua/neotest-golang/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down Expand Up @@ -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("^", ""):gsub("$", "")
elseif string.find(output, "PASS") then
output = output:gsub("^", ""):gsub("$", "")
elseif string.find(output, "SKIP") then
output = output:gsub("^", ""):gsub("$", "")
end
return output
end

--- Show warnings.
--- @param d table<string, TestData>
--- @return nil
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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,

Expand Down Expand Up @@ -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,

Expand Down

0 comments on commit 47ba93e

Please sign in to comment.