Skip to content

Commit

Permalink
feat: re-generate lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 13, 2024
1 parent 51ffe50 commit 797f1b0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 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
6 changes: 6 additions & 0 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,6 +64,10 @@ M.query = [[
(identifier))))))
]]

M.debounced_generate = lib.debounce.debounce(function()
M.generate()
end, options.get().testify_debunce_delay)

--- The lookup table.
--- @type table<string, table>
local lookup_table = {}
Expand All @@ -79,6 +84,7 @@ 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 = {}
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.debounced_generate()
end

return query.detect_tests(file_path)
end

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

-- Table to store timers for each debounced function
local timers = {}

-- Debounce function
-- @param func The function to debounce
-- @param delay The delay in milliseconds
-- @return The debounced function
function M.debounce(func, delay)
return function(...)
local args = { ... }
local key = tostring(func)

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

timers[key] = vim.loop.new_timer()
timers[key]:start(
delay,
0,
vim.schedule_wrap(function()
func(unpack(args))
timers[key]:close()
timers[key] = nil
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_debunce_delay = 500,
warn_test_name_dupes = true,
warn_test_not_executed = true,

Expand Down

0 comments on commit 797f1b0

Please sign in to comment.