Skip to content

Commit 801ab95

Browse files
committed
refactor: reduce duplication in scanner benchmarks
More duplication in the stubbing (which is fine), and less duplication in the scanner set-up (which is good to reduce, because we don't want this drifting out of sync).
1 parent 07d31e6 commit 801ab95

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

bin/benchmarks/scanner.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ benchmark({
2121
setup = function(config)
2222
collectgarbage()
2323
local scanner = nil
24+
if config.stub then
25+
-- For scanners that otherwise depend on Neovim for a list of candidates.
26+
config.stub()
27+
end
2428
if type(config.source) == 'string' then
2529
scanner = require(config.source)
2630
elseif type(config.source) == 'function' then
@@ -37,10 +41,6 @@ benchmark({
3741
local name = output:match('"sockname":%s*"([^"]+)"') or fallback
3842
scanner.set_sockname(name)
3943
end
40-
if config.stub then
41-
-- For scanners that otherwise depend on Neovim for a list of candidates.
42-
config.stub(scanner)
43-
end
4444
return scanner
4545
end,
4646

data/wincent/commandt/benchmark/configs/scanner.lua

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,11 @@ return {
55
{
66
name = 'buffer',
77
source = function()
8+
local command = require('wincent.commandt').default_options().finders.buffer.candidates()
89
local scanner = require('wincent.commandt.private.scanners.list').scanner
910
return {
1011
scanner = function()
11-
local handles = vim.api.nvim_list_bufs()
12-
local paths = {}
13-
for _, handle in ipairs(handles) do
14-
if vim.api.nvim_buf_is_loaded(handle) then
15-
local name = vim.api.nvim_buf_get_name(handle)
16-
if name ~= '' then
17-
local relative = vim.fn.fnamemodify(name, ':~:.')
18-
table.insert(paths, relative)
19-
end
20-
end
21-
end
22-
return scanner(paths)
12+
return scanner(command)
2313
end,
2414
}
2515
end,
@@ -222,22 +212,32 @@ return {
222212
{
223213
name = 'find',
224214
source = function()
225-
local command = 'find -L . -type f -print0 2> /dev/null'
226-
local drop = 2
215+
local command = require('wincent.commandt').default_options().finders.find.command('')
227216
local scanner = require('wincent.commandt.private.scanners.command').scanner
228217
return {
229218
scanner = function()
230-
return scanner(command, drop)
219+
return scanner(command)
231220
end,
232221
}
233222
end,
223+
stub = function()
224+
assert(_G.vim == nil)
225+
_G.vim = {
226+
startswith = function()
227+
return false
228+
end,
229+
}
230+
end,
231+
unstub = function()
232+
_G.vim = nil
233+
end,
234234
times = times,
235235
skip_in_ci = false,
236236
},
237237
{
238238
name = 'git',
239239
source = function()
240-
local command = 'git ls-files --exclude-standard --cached -z 2> /dev/null'
240+
local command = require('wincent.commandt').default_options().finders.git.command('', {})
241241
local scanner = require('wincent.commandt.private.scanners.command').scanner
242242
return {
243243
scanner = function()
@@ -251,14 +251,25 @@ return {
251251
{
252252
name = 'rg',
253253
source = function()
254-
local command = 'rg --files --null 2> /dev/null'
254+
local command = require('wincent.commandt').default_options().finders.rg.command('')
255255
local scanner = require('wincent.commandt.private.scanners.command').scanner
256256
return {
257257
scanner = function()
258258
return scanner(command)
259259
end,
260260
}
261261
end,
262+
stub = function()
263+
assert(_G.vim == nil)
264+
_G.vim = {
265+
startswith = function()
266+
return false
267+
end,
268+
}
269+
end,
270+
unstub = function()
271+
_G.vim = nil
272+
end,
262273
times = times,
263274
skip_in_ci = true,
264275
},

0 commit comments

Comments
 (0)