Skip to content

Commit

Permalink
feat: generate lookup using debouncer
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 13, 2024
1 parent 51ffe50 commit ce935ef
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 21 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ 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. |
| `testify_generate_lookup` | `true` | Automatically re-generate testify lookup when recalculating Neotest tree. |
| `testify_debounce_delay` | `500` | The time to debounce/delay lookup re-generation (in milliseconds). |
| `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
48 changes: 41 additions & 7 deletions lua/neotest-golang/features/testify/lookup.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--- Lookup table for renaming Neotest namespaces (receiver type to testify suite function).

local options = require("neotest-golang.options")
local lib = require("neotest-golang.lib")
local query = require("neotest-golang.features.testify.query")

Expand Down Expand Up @@ -63,26 +64,49 @@ M.query = [[
(identifier))))))
]]

--- The lookup table.
--- The lookup table store.
--- @type table<string, table>
local lookup_table = {}

--- Get the current lookup table, generating it if empty.
--- @return table<string, table> The lookup table containing testify suite information
function M.get()
--- Debouncer for generating the lookup table.
--- @type function
local debounce = lib.debounce.create_debouncer()
local debounced_generate = debounce(function()
return M._generate()
end, options.get().testify_debounce_delay)

--- Generate the lookup table for testify suites.
--- @return table<string, table> The generated lookup table
function M.generate()
if options.get().testify_generate_lookup then
local get_result = debounced_generate()

-- Wait for up to 5 seconds for the result, polling every 100ms.
local max_wait_time = 5000
local interval_time = 100
vim.wait(max_wait_time, function()
lookup_table = get_result()
return not vim.tbl_isempty(lookup_table)
end, interval_time)
end

if vim.tbl_isempty(lookup_table) then
lookup_table = M.generate()
vim.notify(
"Warning: generating the lookup timed out.",
vim.log.levels.ERROR
)
end

return lookup_table
end

--- Generate the lookup table for testify suites.
--- @return table<string, table> The generated lookup table
function M.generate()
function M._generate()
vim.notify("Generating testify lookup...", vim.log.levels.INFO)
local cwd = vim.fn.getcwd()
local filepaths = lib.find.go_test_filepaths(cwd)
local lookup = {}
-- local global_suites = {}

-- First pass: collect all data for the lookup table.
for _, filepath in ipairs(filepaths) do
Expand All @@ -109,6 +133,16 @@ function M.generate()
return lookup
end

--- Get the lookup table for testify suites.
--- @param opts table<string, boolean> Options for getting the lookup table
--- @return table<string, table> The generated lookup table
function M.get(opts)
if options.get().testify_generate_lookup and opts.generate then
return M.generate()
end
return lookup_table
end

--- Clear the lookup table.
function M.clear()
lookup_table = {}
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-golang/features/testify/tree_modification.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local M = {}
--- @param tree neotest.Tree The original neotest tree
--- @return neotest.Tree The modified tree.
function M.modify_neotest_tree(tree)
local lookup_map = lookup.get()
local lookup_map = lookup.get({ generate = true })

if not lookup_map then
vim.notify(
Expand Down
6 changes: 1 addition & 5 deletions lua/neotest-golang/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ local M = {}
--- @field name string
M.Adapter = {
name = "neotest-golang",
init = function()
if options.get().testify_enabled == true then
testify.lookup.generate()
end
end,
init = function() end,
}

--- Find the project root directory given a current directory to work from.
Expand Down
65 changes: 65 additions & 0 deletions lua/neotest-golang/lib/debounce.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
local M = {}

function M.create_debouncer()
local timers = {}
local results = {}
local funcs = {} -- Store the original functions

return function(func, delay)
if type(func) ~= "function" then
error("First argument to debounce must be a function")
end

local key = tostring(func)
funcs[key] = func -- Store the function

return function(...)
local args = { ... }
local callback = nil

-- Check if the last argument is a callback function
if #args > 0 and type(args[#args]) == "function" then
callback = table.remove(args)
end

if timers[key] then
vim.fn.timer_stop(timers[key])
end

timers[key] = vim.fn.timer_start(delay, function()
if not funcs[key] then
print("Error: Function not found for key: " .. key)
return
end

local result
local success, err = pcall(function()
if #args > 0 then
result = funcs[key](unpack(args))
else
result = funcs[key]()
end
end)

if not success then
print("Error executing function: " .. tostring(err))
return
end

results[key] = result
timers[key] = nil

if callback then
callback(result)
end
end)

-- Return a function to get the result
return function()
return results[key]
end
end
end
end

return M
1 change: 1 addition & 0 deletions lua/neotest-golang/lib/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local M = {}

M.convert = require("neotest-golang.lib.convert")
M.cmd = require("neotest-golang.lib.cmd")
M.debounce = require("neotest-golang.lib.debounce")
M.find = require("neotest-golang.lib.find")
M.json = require("neotest-golang.lib.json")

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 @@ -9,6 +9,8 @@ local opts = {
dap_go_enabled = false,
dap_go_opts = {},
testify_enabled = false,
testify_generate_lookup = true,
testify_debounce_delay = 500,
warn_test_name_dupes = true,
warn_test_not_executed = true,

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe("Options are set up", function()
dap_go_enabled = false,
dap_go_opts = {},
testify_enabled = false,
testify_generate_lookup = true,
testify_debounce_delay = 500,
warn_test_name_dupes = true,
warn_test_not_executed = true,

Expand All @@ -35,6 +37,8 @@ describe("Options are set up", function()
dap_go_enabled = false,
dap_go_opts = {},
testify_enabled = false,
testify_generate_lookup = true,
testify_debounce_delay = 500,
warn_test_name_dupes = true,
warn_test_not_executed = true,

Expand Down

0 comments on commit ce935ef

Please sign in to comment.