Skip to content

Commit

Permalink
feat: re-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 fa8d6e2
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 17 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
14 changes: 10 additions & 4 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 @@ -67,22 +68,23 @@ M.query = [[
--- @type table<string, table>
local lookup_table = {}

--- Debouncer for generating the lookup table.
--- @type function
local debounce = lib.debounce.create_debouncer()

--- Get the current lookup table, generating it if empty.
--- @return table<string, table> The lookup table containing testify suite information
function M.get()
if vim.tbl_isempty(lookup_table) then
lookup_table = M.generate()
end
return lookup_table
end

--- Generate the lookup table for testify suites.
--- @return table<string, table> The generated lookup table
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 +111,10 @@ function M.generate()
return lookup
end

--- Generate the lookup table for testify suites, debounced.
M.generate_debounced =
debounce(M.generate, options.get().testify_debounce_delay)

--- Clear the lookup table.
function M.clear()
lookup_table = {}
Expand Down
12 changes: 7 additions & 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 Expand Up @@ -60,6 +56,12 @@ end
--- @param file_path string Absolute file path
--- @return neotest.Tree | nil
function M.Adapter.discover_positions(file_path)
if
options.get().testify_enabled and options.get().testify_generate_lookup
then
testify.lookup.generate_debounced()
end

return query.detect_tests(file_path)
end

Expand Down
31 changes: 31 additions & 0 deletions lua/neotest-golang/lib/debounce.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local M = {}

-- Usage:
-- local debounce = create_debouncer()
--
-- local function1 = function() print("Function 1 executed!") end
-- local function2 = function() print("Function 2 executed!") end
--
-- local debounced1 = debounce(function1, 300)
-- local debounced2 = debounce(function2, 500)
--
-- -- Now you can use debounced1 and debounced2
function M.create_debouncer()
local timers = {}

return function(func, delay)
local key = tostring(func)
return function(...)
local args = { ... }
if timers[key] then
vim.fn.timer_stop(timers[key])
end
timers[key] = vim.fn.timer_start(delay, function()
func(unpack(args))
timers[key] = nil
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 fa8d6e2

Please sign in to comment.