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

chore: log more #144

Merged
merged 7 commits into from
Jul 16, 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
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ 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. |
| 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. |

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

Expand Down Expand Up @@ -315,11 +316,26 @@ Neotest-golang piggybacks on the Neotest logger. You can enable it like so:
require("neotest.logging"):set_level(vim.log.levels.INFO)
```

Lower the log level further to `DEBUG` or `TRACE` to get even more information.
⚠️ Please note that this could cause tests to run slower, so don't forget to
remove this setting once you have resolved your issue!

Lower the log level further to `DEBUG` to get even more information. The lowest
level is `TRACE`, but is not used by this adapter and is only useful when
debugging issues with Neotest.

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`.
`~/.local/state/nvim/neotest.Log`.

The logfile tends to be ginormous and if you are only looking for neotest-golang
related entries, you can either search for the `[neotest-golang]` prefix, or
open the log in a Neovim buffer and then filter out only the adapter-related
entries:

```lua
:edit ~/.local/state/nvim/neotest.log
:lua require("neotest-golang.utils.buffer").filter("[neotest-golang]")
```

### Neotest is slowing down Neovim

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 @@ -16,8 +16,11 @@ function M.golist_data(cwd)
"-json",
"./...",
}
local go_list_command_concat = table.concat(go_list_command, " ")
logger.debug("Running Go list: " .. go_list_command_concat .. " in " .. cwd)
local output =
vim.fn.system("cd " .. cwd .. " && " .. table.concat(go_list_command, " "))
vim.fn.system("cd " .. cwd .. " && " .. go_list_command_concat, " ")
logger.info({ "Go list output: ", output })
return json.decode_from_string(output)
end

Expand Down
73 changes: 66 additions & 7 deletions lua/neotest-golang/logging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,90 @@ local logger = require("neotest.logging")

local prefix = "[neotest-golang] "

---@param msg string
function M.trace(msg)
return logger.trace(prefix .. msg)
local function clean_output(str)
-- Replace escaped newlines and tabs with spaces
str = str:gsub("\\n", " "):gsub("\\t", " ")
-- Remove any remaining actual newlines and tabs
str = str:gsub("[\n\t]", " ")
-- Collapse multiple spaces into one
str = str:gsub("%s+", " ")
return str
end

---@param msg string
local function table_to_string(tbl)
local result = vim.inspect(tbl)
return clean_output(result)
end

---@param input any
---@return string
local function handle_input(input)
if type(input) == "string" then
return clean_output(input)
elseif type(input) == "table" then
local result = ""
for _, v in ipairs(input) do
if type(v) == "table" then
result = result .. table_to_string(v) .. " "
elseif type(v) == "string" then
result = result .. clean_output(v) .. " "
else
result = result .. tostring(v) .. " "
end
end
return result:sub(1, -2) -- Remove trailing space
else
return tostring(input)
end
end

---Log the debug information.
---@param msg string|table
function M.debug(msg)
if M.get_level() > vim.log.levels.DEBUG then
return
end
if type(msg) ~= "string" then
msg = handle_input(msg)
end
logger.debug(prefix .. msg)
end

---@param msg string
---Log the information.
---@param msg string|table
function M.info(msg)
if M.get_level() > vim.log.levels.INFO then
return
end
if type(msg) ~= "string" then
msg = handle_input(msg)
end
logger.info(prefix .. msg)
end

---@param msg string
---Notify and log the warning.
---@param msg string|table
function M.warn(msg)
if type(msg) ~= "string" then
msg = handle_input(msg)
end
vim.notify(msg, vim.log.levels.WARN)
logger.warn(prefix .. msg)
end

---@param msg string
---Notify, log and throw error.
---@param msg string|table
function M.error(msg)
if type(msg) ~= "string" then
msg = handle_input(msg)
end
vim.notify(msg, vim.log.levels.ERROR)
logger.error(prefix .. msg)
error(msg)
end

function M.get_level()
return logger._level
end

return M
4 changes: 4 additions & 0 deletions lua/neotest-golang/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
--- providing them as arguments to the Adapter function. See the README for mode
--- details and examples.

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

local M = {}

local opts = {
Expand All @@ -10,6 +12,7 @@ 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 All @@ -24,6 +27,7 @@ function M.setup(user_opts)
end
else
end
logger.debug("Loaded with options: " .. vim.inspect(opts))
end

function M.get()
Expand Down
17 changes: 8 additions & 9 deletions lua/neotest-golang/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function M.test_results(spec, result, tree)
local pos = tree:data()

-- Sanity check
-- TODO: refactor so that we use pos.type and pos.id instead of passing them separately on the context
if options.get().dev_notifications == true then
if pos.id ~= context.pos_id then
logger.error(
Expand Down Expand Up @@ -310,8 +311,8 @@ end
--- @param d table<string, TestData>
--- @return nil
function M.show_warnings(d)
if options.get().dev_notifications == true then
-- warn if Go package/test is missing for given Neotest position id (Neotest tree node).
-- warn if Go package/test is missing for given Neotest position id (Neotest tree node).
if options.get().warn_test_not_executed == true then
--- @type table<string>
local position_ids = {}
for pos_id, test_data in pairs(d) do
Expand All @@ -322,10 +323,10 @@ function M.show_warnings(d)
end
end
if #position_ids > 0 then
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)
logger.warn({
"Test(s) not associated (not found/executed): ",
position_ids,
})
end
end

Expand All @@ -341,9 +342,7 @@ function M.show_warnings(d)
end
end
if #test_dupes > 0 then
logger.warn(
"Duplicate test name(s) detected:\n" .. table.concat(test_dupes, "\n")
)
logger.warn({ "Duplicate test name(s) detected: ", test_dupes })
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lua/neotest-golang/runspec/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ local M = {}
function M.build(pos)
local go_mod_filepath = lib.find.file_upwards("go.mod", pos.path)
if go_mod_filepath == nil then
local msg =
logger.error(
"The selected directory does not contain a go.mod file or is not part of a Go module."
logger.error(msg)
error(msg)
)
return nil -- NOTE: logger.error will throw an error, but the LSP doesn't see it.
end

local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
Expand Down
6 changes: 3 additions & 3 deletions lua/neotest-golang/runspec/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ function M.build(pos, tree)

local go_mod_filepath = lib.find.file_upwards("go.mod", pos.path)
if go_mod_filepath == nil then
local msg =
logger.error(
"The selected file does not appear to be part of a valid Go module (no go.mod file found)."
logger.error(msg)
error(msg)
)
return nil -- NOTE: logger.error will throw an error, but the LSP doesn't see it.
end

local go_mod_folderpath = vim.fn.fnamemodify(go_mod_filepath, ":h")
Expand Down
29 changes: 29 additions & 0 deletions lua/neotest-golang/utils/buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local lib = require("neotest-golang.lib")

local M = {}

function M.filter(word)
-- Get the current buffer
local bufnr = vim.api.nvim_get_current_buf()

-- Get all lines in the buffer
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

-- Create a new table to store lines containing the word
local new_lines = {}

-- Iterate through all lines
for _, line in ipairs(lines) do
-- If the line contains "neotest-golang", add it to new_lines
if line:match(lib.convert.to_lua_pattern(word)) then
table.insert(new_lines, line)
end
end

-- Replace the buffer contents with the new lines
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines)

vim.notify("Removed lines not containing '" .. word .. "'")
end

return M
5 changes: 5 additions & 0 deletions lua/neotest-golang/utils/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local M = {}

M.buffer = require("neotest-golang.utils.buffer")

return M
2 changes: 2 additions & 0 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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 @@ -35,6 +36,7 @@ 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
Loading