Skip to content

Commit

Permalink
feat: warn if test was not executed
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jun 24, 2024
1 parent 7d0c752 commit 8df6c93
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
57 changes: 37 additions & 20 deletions lua/neotest-golang/results_test.lua
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand All @@ -10,23 +12,19 @@ local M = {}
--- @param tree neotest.Tree
--- @return table<string, neotest.Result>
function M.results(spec, result, tree)
---@type table<string, neotest.Result>
local results = {}
results[spec.context.id] = {
---@type neotest.ResultStatus
status = "skipped", -- default value
}

if spec.context.skip then
---@type table<string, neotest.Result>
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)
Expand All @@ -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+):")
Expand All @@ -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<string, neotest.Result>
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
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit 8df6c93

Please sign in to comment.