Skip to content

Commit

Permalink
Log and notify
Browse files Browse the repository at this point in the history
  • Loading branch information
MisanthropicBit committed Jun 22, 2024
1 parent 38f7b8a commit b68324a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
47 changes: 34 additions & 13 deletions lua/neotest-busted/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ local lib = require("neotest.lib")
local logger = require("neotest.logging")
local types = require("neotest.types")

local log_methods = {
"debug",
"info",
"warn",
"error",
}

---@param message string
---@param level 1 | 2 | 3 | 4
local function log_and_notify(message, level)
local log_method = log_methods[level]

if not log_method then
return
end

logger[log_method](message)
vim.notify(message, level)
end

---@type neotest.Adapter
local BustedNeotestAdapter = { name = "neotest-busted" }

Expand Down Expand Up @@ -151,7 +171,7 @@ function BustedNeotestAdapter.create_test_command(results_path, paths, filters,
local busted = BustedNeotestAdapter.find_busted_command()

if not busted then
logger.debug("Could not find a busted command")
log_and_notify("Could not find a busted command", vim.log.levels.ERROR)
return
end

Expand Down Expand Up @@ -282,7 +302,7 @@ local function get_strategy_config(strategy, results_path, paths, filters)
)

if not test_command_info then
logger.debug("Failed to construct test command for debugging")
log_and_notify("Failed to construct test command for debugging", vim.log.levels.ERROR)
return nil
end

Expand Down Expand Up @@ -448,10 +468,7 @@ function BustedNeotestAdapter.build_spec(args)
local test_command = BustedNeotestAdapter.create_test_command(results_path, paths, filters)

if not test_command then
local message = "Could not find a busted executable"
logger.error(message)
vim.notify(message, vim.log.levels.ERROR)

log_and_notify("Could not find a busted executable", vim.log.levels.ERROR)
return
end

Expand Down Expand Up @@ -537,19 +554,20 @@ function BustedNeotestAdapter.results(spec, strategy_result, tree)
local ok, data = pcall(lib.files.read, results_path)

if not ok then
logger.error("Failed to read json test output file ", results_path, " with error: ", data)
log_and_notify(
("Failed to read json test output file %s with error: %s"):format(results_path, data),
vim.log.levels.ERROR
)
return {}
end

---@diagnostic disable-next-line: cast-local-type
local json_ok, parsed = pcall(vim.json.decode, data, { luanil = { object = true } })

if not json_ok then
logger.error(
"Failed to parse json test output file ",
results_path,
" with error: ",
parsed
log_and_notify(
("Failed to parse json test output file %s with error: %s"):format(results_path, parsed),
vim.log.levels.ERROR
)
return {}
end
Expand Down Expand Up @@ -581,7 +599,10 @@ function BustedNeotestAdapter.results(spec, strategy_result, tree)
local pos_id = position_ids[pos_id_key]

if not pos_id then
logger.error("Failed to find matching position id for key ", pos_id_key)
log_and_notify(
("Failed to find matching position id for key %s"):format(pos_id_key),
vim.log.levels.ERROR
)
else
results[position_ids[pos_id_key]] = result
end
Expand Down
19 changes: 3 additions & 16 deletions tests/adapter_results_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,7 @@ describe("adapter.results", function()
assert.are.same(neotest_results, {})

assert.stub(lib.files.read).was.called_with(spec.context.results_path)
assert.stub(logger.error).was.called_with(
"Failed to read json test output file ",
"test_output.json",
" with error: ",
"Could not read file"
)
assert.stub(logger.error).was.called_with("Failed to read json test output file test_output.json with error: Could not read file")
end)

it("handles failure to decode json", function()
Expand All @@ -174,12 +169,7 @@ describe("adapter.results", function()
assert.are.same(neotest_results, {})

assert.stub(lib.files.read).was.called_with(spec.context.results_path)
assert.stub(logger.error).was.called_with(
"Failed to parse json test output file ",
"test_output.json",
" with error: ",
"Expected value but found invalid token at character 1"
)
assert.stub(logger.error).was.called_with("Failed to parse json test output file test_output.json with error: Expected value but found invalid token at character 1")

vim.json.decode:revert()
end)
Expand Down Expand Up @@ -214,9 +204,6 @@ describe("adapter.results", function()
})

assert.stub(lib.files.read).was.called_with(spec.context.results_path)
assert.stub(logger.error).was.called_with(
"Failed to find matching position id for key ",
test_path .. "::namespace tests a failing test::7"
)
assert.stub(logger.error).was.called_with("Failed to find matching position id for key " .. test_path .. "::namespace tests a failing test::7")
end)
end)

0 comments on commit b68324a

Please sign in to comment.