Skip to content

Commit

Permalink
feat: add logger (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil authored Jul 15, 2024
1 parent 3965d7b commit d7cf086
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .github/DISCUSSION_TEMPLATE/configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ body:
required: true
- label: I have updated to the latest version of neotest-golang.
required: true
- label: I have checked the Neotest log for errors (see README for instructions on enabling it).
required: false
- type: input
attributes:
label: "Neovim version (nvim -v)"
Expand Down
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ You can run `:checkhealth neotest-golang` to review common issues.

## ⚙️ Configuration

| Argument | Default value | Description |
| ------------------------ | ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `go_test_args` | `{ "-v", "-race", "-count=1" }` | 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()`. |
| `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. |
| `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. |
| Argument | Default value | Description |
| ---------------------- | ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `go_test_args` | `{ "-v", "-race", "-count=1" }` | 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()`. |
| `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. |
| `warn_test_name_dupes` | `true` | Warn about duplicate test names within the same Go package. |

### Example configuration: custom `go test` arguments

Expand Down Expand Up @@ -303,12 +302,25 @@ return {

## ⛑️ Tips & troubleshooting

### Issues with setting up the adapter
### Issues with setting up or using the adapter

You can run `:checkhealth neotest-golang` to review common issues. If you need
help, please open a discussion
[here](https://github.com/fredrikaverpil/neotest-golang/discussions/new?category=configuration).

You can also enable logging to further inspect what's going on under the hood.
Neotest-golang piggybacks on the Neotest logger. You can enable it like so:

```lua
require("neotest.logging"):set_level(vim.log.levels.INFO)
```

Lower the log level further to `DEBUG` or `TRACE` to get even more information.

You can get ahold of the log file's path using
`require("neotest.logging"):get_filename()`, which usually points to your
`~/.local/state/nvim/neotest.log`.

### Neotest is slowing down Neovim

Neotest, out of the box with default settings, can appear very slow in large
Expand Down
7 changes: 3 additions & 4 deletions lua/neotest-golang/features/testify/tree_modification.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- Functions to modify the Neotest tree, for testify suite support.

local options = require("neotest-golang.options")
local logger = require("neotest-golang.logging")
local lib = require("neotest-golang.lib")
local lookup = require("neotest-golang.features.testify.lookup")

Expand Down Expand Up @@ -40,9 +40,8 @@ function M.modify_neotest_tree(file_path, tree)
end

if not lookup_table then
vim.notify(
"No lookup found. Could not modify Neotest tree for testify suite support",
vim.log.levels.WARN
logger.warn(
"No lookup found. Could not modify Neotest tree for testify suite support"
)
return tree
end
Expand Down
17 changes: 6 additions & 11 deletions lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
--- This is the main entry point for the neotest-golang adapter. It follows the
--- Neotest interface: https://github.com/nvim-neotest/neotest/blob/master/lua/neotest/adapters/interface.lua

local logger = require("neotest-golang.logging")
local options = require("neotest-golang.options")
local query = require("neotest-golang.query")
local runspec = require("neotest-golang.runspec")
local process = require("neotest-golang.process")
local testify = require("neotest-golang.features.testify")

local M = {}

Expand Down Expand Up @@ -72,10 +72,7 @@ function M.Adapter.build_spec(args)
local pos = args.tree:data() -- NOTE: causes <file> is not accessible by the current user!

if not tree then
vim.notify(
"Unexpectedly did not receive a neotest.Tree.",
vim.log.levels.ERROR
)
logger.error("Unexpectedly did not receive a neotest.Tree.")
return
end

Expand Down Expand Up @@ -123,11 +120,10 @@ function M.Adapter.build_spec(args)
return runspec.test.build(pos, args.strategy)
end

vim.notify(
logger.error(
"Unknown Neotest position type, "
.. "cannot build runspec with position type: "
.. pos.type,
vim.log.levels.ERROR
.. pos.type
)
end

Expand Down Expand Up @@ -165,10 +161,9 @@ function M.Adapter.results(spec, result, tree)
return results
end

vim.notify(
logger.error(
"Cannot process test results due to unknown Neotest position type:"
.. spec.context.pos_type,
vim.log.levels.ERROR
.. spec.context.pos_type
)
end

Expand Down
5 changes: 4 additions & 1 deletion lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

local async = require("neotest.async")

local logger = require("neotest-golang.logging")
local options = require("neotest-golang.options")
local json = require("neotest-golang.lib.json")

Expand Down Expand Up @@ -52,6 +53,8 @@ function M.test_command(go_test_required_args)
cmd = M.gotestsum(go_test_required_args, json_filepath)
end

logger.info("Test command: " .. table.concat(cmd, " "))

return cmd, json_filepath
end

Expand Down Expand Up @@ -81,7 +84,7 @@ end

function M.system_has(executable)
if vim.fn.executable(executable) == 0 then
vim.notify("Executable not found: " .. executable, vim.log.levels.WARN)
logger.warn("Executable not found: " .. executable)
return false
end
return true
Expand Down
4 changes: 3 additions & 1 deletion lua/neotest-golang/lib/json.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--- JSON processing helpers.

local logger = require("neotest-golang.logging")

local M = {}

--- Decode JSON from a table of strings into a table of objects.
Expand All @@ -14,7 +16,7 @@ function M.decode_from_table(tbl)
table.insert(jsonlines, json_data)
else
-- NOTE: this can be hit because of "Vim:E474: Unidentified byte: ..."
vim.notify("Failed to decode JSON line: " .. line, vim.log.levels.WARN)
logger.warn("Failed to decode JSON line: " .. line)
end
else
-- vim.notify("Not valid JSON: " .. line, vim.log.levels.DEBUG)
Expand Down
35 changes: 35 additions & 0 deletions lua/neotest-golang/logging.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local M = {}

---@type neotest.Logger
local logger = require("neotest.logging")

local prefix = "[neotest-golang] "

---@param msg string
function M.trace(msg)
return logger.trace(prefix .. msg)
end

---@param msg string
function M.debug(msg)
logger.debug(prefix .. msg)
end

---@param msg string
function M.info(msg)
logger.info(prefix .. msg)
end

---@param msg string
function M.warn(msg)
vim.notify(msg, vim.log.levels.WARN)
logger.warn(prefix .. msg)
end

---@param msg string
function M.error(msg)
vim.notify(msg, vim.log.levels.ERROR)
logger.error(prefix .. msg)
end

return M
1 change: 0 additions & 1 deletion lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ local opts = {
dap_go_opts = {},
testify_enabled = false,
warn_test_name_dupes = true,
warn_test_not_executed = true,

-- experimental, for now undocumented, options
runner = "go", -- or "gotestsum"
Expand Down
24 changes: 11 additions & 13 deletions lua/neotest-golang/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

local async = require("neotest.async")

local logger = require("neotest-golang.logging")
local options = require("neotest-golang.options")
local lib = require("neotest-golang.lib")

Expand Down Expand Up @@ -59,9 +60,8 @@ function M.test_results(spec, result, tree)
-- Sanity check
if options.get().dev_notifications == true then
if pos.id ~= context.pos_id then
vim.notify(
"Neotest position id mismatch: " .. pos.id .. " vs " .. context.pos_id,
vim.log.levels.ERROR
logger.error(
"Neotest position id mismatch: " .. pos.id .. " vs " .. context.pos_id
)
end
end
Expand Down Expand Up @@ -118,7 +118,7 @@ function M.test_results(spec, result, tree)
local res = M.aggregate_data(tree, gotest_output, golist_output)

-- DEBUG: enable the following to see the internal test result data.
-- vim.notify(vim.inspect(res), vim.log.levels.DEBUG)
-- logger.debug(vim.inspect(res))

-- show various warnings
M.show_warnings(res)
Expand All @@ -130,7 +130,7 @@ function M.test_results(spec, result, tree)
end

-- DEBUG: enable the following to see the final Neotest result.
-- vim.notify(vim.inspect(neotest_results), vim.log.levels.DEBUG)
-- logger.debug(vim.inspect(neotest_results))

return neotest_result
end
Expand Down Expand Up @@ -322,11 +322,10 @@ function M.show_warnings(d)
end
end
if #position_ids > 0 then
vim.notify(
"Test(s) not associated (not found/executed):\n"
.. table.concat(position_ids, "\n"),
vim.log.levels.DEBUG
)
local msg = "Test(s) not associated (not found/executed):\n"
.. table.concat(position_ids, "\n")
vim.notify(msg, vim.log.levels.DEBUG)
logger.debug(msg)
end
end

Expand All @@ -342,9 +341,8 @@ function M.show_warnings(d)
end
end
if #test_dupes > 0 then
vim.notify(
"Duplicate test name(s) detected:\n" .. table.concat(test_dupes, "\n"),
vim.log.levels.WARN
logger.warn(
"Duplicate test name(s) detected:\n" .. table.concat(test_dupes, "\n")
)
end
end
Expand Down
3 changes: 2 additions & 1 deletion lua/neotest-golang/runspec/dir.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--- Helpers to build the command and context around running all tests of
--- a Go package.

local logger = require("neotest-golang.logging")
local lib = require("neotest-golang.lib")

local M = {}
Expand Down Expand Up @@ -61,7 +62,7 @@ end
function M.fail_fast(pos)
local msg = "The selected folder must contain a go.mod file "
.. "or be a subdirectory of a Go package."
vim.notify(msg, vim.log.levels.ERROR)
logger.error(msg)

--- @type RunspecContext
local context = {
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe("Options are set up", function()
dap_go_opts = {},
testify_enabled = false,
warn_test_name_dupes = true,
warn_test_not_executed = true,

-- experimental
runner = "go",
Expand All @@ -36,7 +35,6 @@ describe("Options are set up", function()
dap_go_opts = {},
testify_enabled = false,
warn_test_name_dupes = true,
warn_test_not_executed = true,

-- experimental
runner = "go",
Expand Down

0 comments on commit d7cf086

Please sign in to comment.