Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if test was not executed #42

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
58 changes: 38 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 no_tests_to_run = 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
no_tests_to_run = 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,30 @@ function M.results(spec, result, tree)
end
end

if no_tests_to_run then
if options.get().warn_test_not_executed == true then
vim.notify(
"Could not execute test: "
.. convert.to_gotest_test_name(spec.context.id),
vim.log.levels.WARN
)
end
else
-- 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
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
Loading