Skip to content

Commit dbe6cf1

Browse files
committed
feat: generic test runner
1 parent 3e7bda5 commit dbe6cf1

File tree

1 file changed

+93
-72
lines changed

1 file changed

+93
-72
lines changed

lua/vial/init.lua

Lines changed: 93 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,157 @@
11
local M = {}
2-
local last_test = nil
3-
local vial_path = "vial"
4-
local test_cmd = "cargo test -- %s --nocapture --color=always"
5-
local toggle_state = {
6-
enabled = false,
7-
extensions = {},
8-
}
2+
local vial_path = nil
3+
local file_types = nil
4+
local enabled = false
95

106
function M.setup(opts)
117
opts = opts or {}
128
vial_path = opts.vial_path or vial_path
13-
test_cmd = opts.command or test_cmd
14-
toggle_state.extensions = opts.extensions or {}
9+
file_types = opts.file_types
10+
enabled = true
11+
12+
vim.cmd([[autocmd BufWritePost * lua require'vial'.on_buf_write_post()]])
13+
end
14+
15+
-- Function to get the current context and run command
16+
local function run_command(command)
17+
local handle = io.popen(string.format("%s send '%s' > /dev/null 2>&1", vial_path, command))
18+
19+
-- we don't handle responses yet
20+
-- local result = handle:read("*a")
21+
-- handle:close()
1522
end
1623

17-
local function current_test_name()
18-
-- Get the current buffer and cursor position
19-
local bufnr = vim.api.nvim_get_current_buf()
20-
---@diagnostic disable-next-line: deprecated
21-
local row, _ = unpack(vim.api.nvim_win_get_cursor(0))
22-
23-
local function_declaration_line = 0
24-
local function_name = ""
25-
26-
-- Check lines above the cursor for the #[test] attribute and function definition
27-
for i = row, 1, -1 do
28-
local line = vim.api.nvim_buf_get_lines(bufnr, i - 1, i, false)[1]
29-
30-
-- Check for function start and capture the function name
31-
if line:match("^%s*fn%s") then
32-
function_declaration_line = i
33-
function_name = line:match("^%s*fn%s([%w_]+)%s*%(")
34-
break
35-
end
24+
local function current_settings()
25+
if file_types == nil then
26+
print("No file types defined")
27+
return
3628
end
3729

38-
for i = function_declaration_line, 1, -1 do
39-
local line = vim.api.nvim_buf_get_lines(bufnr, i - 1, i, false)[1]
40-
if line:match("^%s*#%[test%]") then
41-
return function_name
42-
end
30+
local filetype = vim.bo.filetype
31+
return file_types[filetype]
32+
end
33+
34+
local function get(setting)
35+
local settings = current_settings()
36+
if settings == nil then
37+
return nil
4338
end
4439

45-
return nil
40+
return settings[setting]
4641
end
4742

48-
-- Function to get the current context and run command
49-
local function run_command(command)
50-
-- local file = vim.api.nvim_buf_get_name(0)
51-
-- local line = vim.fn.line(".")
52-
-- local context_command = string.format("%s -file=%s -line=%s", command, file, line)
43+
local function set(setting, value)
44+
local settings = current_settings()
45+
if settings == nil then
46+
return
47+
end
5348

54-
-- Execute the Rust binary and pass the command
55-
local handle = io.popen(string.format("%s send '%s' > /dev/null 2>&1", vial_path, command))
56-
-- local result = handle:read("*a")
57-
-- handle:close()
49+
settings[setting] = value
5850
end
5951

6052
local function run_test(test_name)
61-
local cmd = string.format(test_cmd, test_name)
53+
local raw_cmd = get("command")
54+
if raw_cmd == nil then
55+
print("No command defined")
56+
return
57+
end
58+
59+
local cmd = string.format(raw_cmd, test_name)
6260
run_command(cmd)
6361
end
6462

6563
function M.run_test()
66-
local test_name = current_test_name()
64+
local test_name = get("extract")()
6765

6866
if test_name == nil then
6967
print("No test found")
7068
return
7169
end
7270

73-
last_test = test_name
71+
set("last_test", test_name)
7472
run_test(test_name)
7573
end
7674

7775
function M.run_last_test()
76+
local last_test = get("last_test")
77+
7878
if last_test == nil then
79-
print("No last test found")
8079
return
8180
end
8281

8382
run_test(last_test)
8483
end
8584

86-
local function is_valid_extension(extension)
87-
for _, ext in ipairs(toggle_state.extensions) do
88-
if ext == extension then
89-
return true
90-
end
91-
end
92-
return false
93-
end
94-
95-
local function get_extension(filename)
96-
return filename:match("^.+(%..+)$")
97-
end
85+
local function is_inside_test()
86+
local extract_fn = get("extract")
9887

99-
local function on_buf_write_post()
100-
if not toggle_state.enabled then
101-
return
88+
if extract_fn == nil then
89+
return false
10290
end
10391

104-
local filename = vim.api.nvim_buf_get_name(0)
105-
local extension = get_extension(filename)
92+
return get("extract")() ~= nil
93+
end
10694

107-
if is_valid_extension(extension) then
95+
local function run_last_or_current_test()
96+
if is_inside_test() then
10897
M.run_test()
98+
else
99+
M.run_last_test()
109100
end
110101
end
111102

112-
function M.toggle_automated_tests()
113-
toggle_state.enabled = not toggle_state.enabled
114-
if toggle_state.enabled then
103+
local function on_buf_write_post()
104+
run_last_or_current_test()
105+
end
106+
107+
function M.toggle_active()
108+
if enabled then
115109
vim.cmd([[autocmd BufWritePost * lua require'vial'.on_buf_write_post()]])
116-
print("Automated tests enabled")
110+
enabled = false
117111
else
118-
vim.cmd([[autocmd! BufWritePost * lua require'vial'.on_buf_write_post()]])
119-
print("Automated tests disabled")
112+
vim.cmd([[autocmd BufWritePost * lua require'vial'.on_buf_write_post()]])
113+
enabled = true
120114
end
121115
end
122116

117+
function M.clear_last_test()
118+
set("last_test", nil)
119+
end
120+
121+
function M.show_last_test()
122+
local last_test = get("last_test")
123+
124+
if last_test == nil then
125+
print("No last test found")
126+
return
127+
end
128+
129+
print(last_test)
130+
end
131+
123132
vim.api.nvim_set_keymap("n", "<leader>tt", "<cmd>lua require'vial'.run_test()<cr>", { noremap = true, silent = true })
124133
vim.api.nvim_set_keymap(
125134
"n",
126135
"<leader>tl",
127136
"<cmd>lua require'vial'.run_last_test()<cr>",
128137
{ noremap = true, silent = true }
129138
)
139+
vim.api.nvim_set_keymap(
140+
"n",
141+
"<leader>tc",
142+
"<cmd>lua require'vial'.clear_last_test()<cr>",
143+
{ noremap = true, silent = true }
144+
)
130145
vim.api.nvim_set_keymap(
131146
"n",
132147
"<leader>ta",
133-
"<cmd>lua require'vial'.toggle_automated_tests()<cr>",
148+
"<cmd>lua require'vial'.toggle_active()<cr>",
149+
{ noremap = true, silent = true }
150+
)
151+
vim.api.nvim_set_keymap(
152+
"n",
153+
"<leader>ts",
154+
"<cmd>lua require'vial'.show_last_test()<cr>",
134155
{ noremap = true, silent = true }
135156
)
136157

0 commit comments

Comments
 (0)