Skip to content

Commit

Permalink
Add VUSTED_DISABLE_EXIT, VUSTED_DISABLE_CLEANUP to debug interactively
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Aug 29, 2024
1 parent e0c5e54 commit 5f30cee
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ See [vusted.helper](https://github.com/notomo/vusted/blob/master/lua/vusted/help
- `VUSTED_USE_LOCAL`
- Set this flag to true or 1 if vusted was installed locally, e.g., with `luarocks install --local vusted`.
- default: nil
- `VUSTED_DISABLE_EXIT`
- vusted does not exit if `VUSTED_DISABLE_EXIT` exists. Use to check neovim state interactively after test execution.
- example: `VUSTED_DISABLE_EXIT=1 VUSTED_DISABLE_CLEANUP=1 VUSTED_ARGS=--clean vusted --filter 'vusted can print'`
- default: nil
- `VUSTED_DISABLE_CLEANUP`
- `vusted.helper.cleanup()` and `vusted.helper.cleanup_loaded_modules()` do nothing if `VUSTED_DISABLE_CLEANUP` exists.
- default: nil
23 changes: 14 additions & 9 deletions lua/busted/outputHandlers/vusted/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ return function(options)
local busted = require("busted")
local handler = require("busted.outputHandlers.TAP")(options)

local write = io.write
if not vim.tbl_contains(vim.fn.argv(), "--headless") then
write = print
end

local suite_start = function(suite)
if suite.randomseed then
-- NOTE: can know random seed even if neovim crashes
io.write(("# Random seed: %s\n"):format(suite.randomseed))
write(("# Random seed: %s\n"):format(suite.randomseed))
end
return nil, true
end
Expand All @@ -24,7 +29,7 @@ return function(options)
for _, test in ipairs(tests) do
local path = test_path(test)
if path then
io.write(("# %s\n"):format(path))
write(("# %s\n"):format(path))
end
end
end
Expand All @@ -51,29 +56,29 @@ return function(options)
return a.duration > b.duration
end)

io.write(("# Slow: %d (threshold: %.2f ms)\n"):format(#slows, threshold))
write(("# Slow: %d (threshold: %.2f ms)\n"):format(#slows, threshold))
for _, slow in ipairs(slows) do
io.write(("# %s (%.2f ms)\n"):format(slow.path, slow.duration))
write(("# %s (%.2f ms)\n"):format(slow.path, slow.duration))
end
end

local suite_end = function()
io.write("\n\n")
write("\n\n")

io.write(("# Success: %d\n"):format(#handler.successes))
write(("# Success: %d\n"):format(#handler.successes))

if #handler.failures > 0 then
io.write(("# Failure: %d\n"):format(#handler.failures))
write(("# Failure: %d\n"):format(#handler.failures))
show_test_paths(handler.failures)
end

if #handler.pendings > 0 then
io.write(("# Pending: %d\n"):format(#handler.pendings))
write(("# Pending: %d\n"):format(#handler.pendings))
show_test_paths(handler.pendings)
end

if #handler.errors > 0 then
io.write(("# Error: %d\n"):format(#handler.errors))
write(("# Error: %d\n"):format(#handler.errors))
show_test_paths(handler.errors)
end

Expand Down
10 changes: 10 additions & 0 deletions lua/vusted/helper.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
local M = {}

--- Cleanup lua package.loaded for plugin.
--- This does nothing If VUSTED_DISABLE_CLEANUP environment variable exists.
--- @param plugin_name string: lua module name (`lua/{plugin_name}/*.lua`)
function M.cleanup_loaded_modules(plugin_name)
if vim.env.VUSTED_DISABLE_CLEANUP then
return
end

vim.validate({ plugin_name = { plugin_name, "string" } })

local dir = plugin_name:gsub("/", ".") .. "."
Expand Down Expand Up @@ -105,9 +110,14 @@ local default_targets = {
--- - `:syntax on`
--- - variable (g:, v:)
--- - register
--- This does nothing If VUSTED_DISABLE_CLEANUP environment variable exists.
--- @param targets table|nil: see above `default_targets`
--- - For example if disable 'autocmd' cleanup: `helper.cleanup({autocmd = {enabled = false}})`
function M.cleanup(targets)
if vim.env.VUSTED_DISABLE_CLEANUP then
return
end

targets = vim.tbl_deep_extend("force", default_targets, targets or {})
for _, name in ipairs({
"buffer",
Expand Down
8 changes: 7 additions & 1 deletion lua/vusted/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ return function()
os.exit(0)
end

local exit = os.exit
if vim.env.VUSTED_DISABLE_EXIT then
exit = function() end
require("busted.compatibility").exit = exit -- HACK
end

local runner = require("busted.runner")
local ok, result = pcall(runner, { standalone = false, output = "vusted.default" })

Expand All @@ -23,5 +29,5 @@ return function()
print(result .. "\n")
code = 1
end
os.exit(code)
exit(code)
end
22 changes: 22 additions & 0 deletions spec/lua/vusted/helper_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
describe("vusted.helper", function()
before_each(function()
vim.env.VUSTED_DISABLE_CLEANUP = nil
end)

describe("cleanup_loaded_modules()", function()
it("cleanups plugin's loaded modules", function()
package.loaded["vusted_test"] = {}
Expand All @@ -11,6 +15,15 @@ describe("vusted.helper", function()
assert.is_nil(package.loaded["vusted_test.test1"])
assert.is_nil(package.loaded["vusted_test/test2"])
end)

it("does nothing when VUSTED_DISABLE_CLEANUP exists", function()
package.loaded["vusted_test"] = {}
vim.env.VUSTED_DISABLE_CLEANUP = 1

require("vusted.helper").cleanup_loaded_modules("vusted_test")

assert.no.is_nil(package.loaded["vusted_test"])
end)
end)

describe("find_plugin_root()", function()
Expand Down Expand Up @@ -195,4 +208,13 @@ No abbreviation found]],
end)
end
end)

it("does nothing when VUSTED_DISABLE_CLEANUP exists", function()
vim.cmd.tabedit()

vim.env.VUSTED_DISABLE_CLEANUP = 1
require("vusted.helper").cleanup()

assert.equal(2, #vim.api.nvim_list_bufs())
end)
end)

0 comments on commit 5f30cee

Please sign in to comment.