Skip to content

Commit 7435108

Browse files
committed
Add APIs to run failed tests and open last reports
1 parent eaf7773 commit 7435108

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

lua/clojure-test/api/init.lua

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ local M = {}
99

1010
M.state = {
1111
previous = nil,
12+
last_run = nil,
1213
}
1314

15+
local function run_tests_and_update_state(tests)
16+
M.state.previous = tests
17+
M.state.last_run = run_api.run_tests(tests)
18+
end
19+
1420
function M.run_all_tests()
1521
nio.run(function()
1622
local tests = tests_api.get_all_tests()
1723
if #tests == 0 then
1824
return
1925
end
20-
M.state.previous = tests
21-
run_api.run_tests(tests)
26+
run_tests_and_update_state(tests)
2227
end)
2328
end
2429

@@ -37,8 +42,7 @@ function M.run_tests()
3742
return
3843
end
3944

40-
M.state.previous = tests
41-
run_api.run_tests(tests)
45+
run_tests_and_update_state(tests)
4246
end)
4347
end
4448

@@ -65,8 +69,7 @@ function M.run_tests_in_ns()
6569
return
6670
end
6771

68-
M.state.previous = tests
69-
run_api.run_tests(tests)
72+
run_tests_and_update_state(tests)
7073
end)
7174
end
7275

@@ -75,10 +78,35 @@ function M.rerun_previous()
7578
if not M.state.previous then
7679
return
7780
end
78-
run_api.run_tests(M.state.previous)
81+
run_tests_and_update_state(M.state.previous)
82+
end)
83+
end
84+
85+
function M.rerun_failed()
86+
nio.run(function()
87+
local failed = {}
88+
for test, report in pairs(M.state.last_run) do
89+
if report.status ~= "passed" then
90+
table.insert(failed, test)
91+
end
92+
end
93+
94+
if #failed == 0 then
95+
vim.notify("No failed tests to run", vim.log.levels.WARN)
96+
return
97+
end
98+
99+
run_tests_and_update_state(failed)
79100
end)
80101
end
81102

103+
function M.open_last_report()
104+
if not M.state.last_run then
105+
return
106+
end
107+
run_api.open_reports(M.state.last_run)
108+
end
109+
82110
function M.load_tests()
83111
nio.run(function()
84112
tests_api.load_tests()

lua/clojure-test/api/run.lua

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ local M = {}
4747

4848
local active_ui = nil
4949

50+
function M.open_reports(reports)
51+
local last_active_window = vim.api.nvim_get_current_win()
52+
53+
local ui = active_ui
54+
if not ui then
55+
ui = interface_api.create(function(event)
56+
if event.type == "go-to" then
57+
return handle_go_to_event(last_active_window, event)
58+
end
59+
end)
60+
active_ui = ui
61+
end
62+
63+
ui:mount()
64+
65+
ui:render_reports(reports)
66+
end
67+
5068
function M.run_tests(tests)
5169
if config.hooks.before_run then
5270
config.hooks.before_run(tests)
@@ -79,17 +97,18 @@ function M.run_tests(tests)
7997

8098
ui:render_reports(reports)
8199

82-
local semaphore = nio.control.semaphore(1)
83-
for _, test in ipairs(tests) do
84-
nio.run(function()
100+
nio.run(function()
101+
local semaphore = nio.control.semaphore(1)
102+
for _, test in ipairs(tests) do
85103
semaphore.with(function()
86104
local report = config.backend:run_test(test)
87105
if report then
88106
queue.put(report)
89107
end
90108
end)
91-
end)
92-
end
109+
end
110+
queue.put(nil)
111+
end)
93112

94113
while true do
95114
local report = queue.get()
@@ -100,6 +119,8 @@ function M.run_tests(tests)
100119
reports[report.test] = report
101120
ui:render_reports(reports)
102121
end
122+
123+
return reports
103124
end
104125

105126
return M

0 commit comments

Comments
 (0)