From 4047cdee68cce8e530be7413fc4d9ffd1c3e9b6c Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Mon, 24 Jun 2024 12:34:50 +0200 Subject: [PATCH] feat: warn if test was not executed --- README.md | 13 ++++--- lua/neotest-golang/options.lua | 2 + lua/neotest-golang/results_test.lua | 57 +++++++++++++++++++---------- tests/unit/options_spec.lua | 4 +- 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 0cf8e22..67c507b 100644 --- a/README.md +++ b/README.md @@ -96,12 +96,13 @@ return { ## ⚙️ Configuration -| Argument | Default value | Description | -| ---------------------- | ----------------------------------------------- | ----------------------------------------------------------------------------------------- | -| `go_test_args` | `{ "-v", "-race", "-count=1", "-timeout=60s" }` | Arguments to pass into `go test`. | -| `dap_go_enabled` | `false` | Leverage [leoluz/nvim-dap-go](https://github.com/leoluz/nvim-dap-go) for debugging tests. | -| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. | -| `warn_test_name_dupes` | `true` | Warn about duplicate test names within the same Go package. | +| Argument | Default value | Description | +| ------------------------ | ----------------------------------------------- | ----------------------------------------------------------------------------------------- | +| `go_test_args` | `{ "-v", "-race", "-count=1", "-timeout=60s" }` | Arguments to pass into `go test`. | +| `dap_go_enabled` | `false` | Leverage [leoluz/nvim-dap-go](https://github.com/leoluz/nvim-dap-go) for debugging tests. | +| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. | +| `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 diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index d6af482..475c4e8 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -16,6 +16,7 @@ function Opts:new(opts) self.dap_go_enabled = opts.dap_go_enabled or false self.dap_go_opts = opts.dap_go_opts or {} self.warn_test_name_dupes = opts.warn_test_name_dupes or true + self.warn_test_not_executed = opts.warn_test_not_executed or true end --- A convenience function to get the current options. @@ -25,6 +26,7 @@ function Opts:get() dap_go_enabled = self.dap_go_enabled, dap_go_opts = self.dap_go_opts, warn_test_name_dupes = self.warn_test_name_dupes, + warn_test_not_executed = self.warn_test_not_executed, } end diff --git a/lua/neotest-golang/results_test.lua b/lua/neotest-golang/results_test.lua index d459c71..f863b1a 100644 --- a/lua/neotest-golang/results_test.lua +++ b/lua/neotest-golang/results_test.lua @@ -1,5 +1,7 @@ local async = require("neotest.async") +local options = require("neotest-golang.options") +local convert = require("neotest-golang.convert") local json = require("neotest-golang.json") local M = {} @@ -10,23 +12,19 @@ local M = {} --- @param tree neotest.Tree --- @return table function M.results(spec, result, tree) + ---@type table + local results = {} + results[spec.context.id] = { + ---@type neotest.ResultStatus + status = "skipped", -- default value + } + if spec.context.skip then - ---@type table - local results = {} - results[spec.context.id] = { - ---@type neotest.ResultStatus - status = "skipped", - } return results end - --- @type neotest.ResultStatus - local result_status = "skipped" - if result.code == 0 then - result_status = "passed" - else - result_status = "failed" - end + --- @type boolean + local test_not_found = false --- @type table local raw_output = async.fn.readfile(result.output) @@ -45,10 +43,17 @@ function M.results(spec, result, tree) if line.Action == "output" and line.Output ~= nil then -- record output, prints to output panel table.insert(test_result, line.Output) + + -- if test was not run, mark it as skipped + + -- if line contains "no test files" or "no tests to run", mark as skipped + if string.match(line.Output, "no tests to run") then + test_not_found = true + end end + -- record an error if result.code ~= 0 and line.Output ~= nil then - -- record an error ---@type string local matched_line_number = string.match(line.Output, test_filename .. ":(%d+):") @@ -75,17 +80,29 @@ function M.results(spec, result, tree) end end + if not test_not_found then + -- assign status code, as long as the test was found + if result.code == 0 then + results[spec.context.id].status = "passed" + else + results[spec.context.id].status = "failed" + end + else + if options.get().warn_test_not_executed == true then + vim.notify( + "Could not find test " .. convert.to_gotest_test_name(spec.context.id), + vim.log.levels.WARN + ) + end + end + -- write json_decoded to file local parsed_output_path = vim.fs.normalize(async.fn.tempname()) async.fn.writefile(test_result, parsed_output_path) ---@type table - local results = {} - results[spec.context.id] = { - status = result_status, - output = parsed_output_path, - errors = errors, - } + results[spec.context.id].output = parsed_output_path + results[spec.context.id].errors = errors return results end diff --git a/tests/unit/options_spec.lua b/tests/unit/options_spec.lua index 195d5ab..37e137f 100644 --- a/tests/unit/options_spec.lua +++ b/tests/unit/options_spec.lua @@ -12,6 +12,7 @@ describe("Options are set up", function() "-timeout=60s", }, warn_test_name_dupes = true, + warn_test_not_executed = true, } options.setup() assert.are_same(expected_options, options.get()) @@ -25,10 +26,11 @@ describe("Options are set up", function() "-v", "-race", "-count=1", - "-parallel=1", + "-parallel=1", -- non-default "-timeout=60s", }, warn_test_name_dupes = true, + warn_test_not_executed = true, } options.setup(expected_options) assert.are_same(expected_options, options.get())