1
1
-- Test runner for Plenary-based tests
2
- local ok , plenary = pcall (require , " plenary" )
2
+ local ok , _ = pcall (require , " plenary" )
3
3
if not ok then
4
4
print (" ERROR: Could not load plenary" )
5
5
vim .cmd (" qa!" )
@@ -26,7 +26,7 @@ _G.TEST_RESULTS = {
26
26
27
27
-- Silence vim.notify during tests to prevent output pollution
28
28
local original_notify = vim .notify
29
- vim .notify = function (msg , level , opts )
29
+ vim .notify = function (msg , level , _ )
30
30
-- Capture the message for debugging but don't display it
31
31
if level == vim .log .levels .ERROR then
32
32
_G .TEST_RESULTS .last_error = msg
@@ -148,11 +148,10 @@ local function run_tests()
148
148
end
149
149
150
150
-- Setup a test counter based on terminal output
151
- -- Override print to capture and count the test results
152
- local old_print = print
153
- print = function (msg )
151
+ -- Create an output processor function to count test results
152
+ local function process_output (msg )
154
153
-- Forward to original print
155
- old_print (msg )
154
+ print (msg )
156
155
157
156
-- Count tests by looking for success/failure indicators
158
157
if type (msg ) == " string" then
@@ -171,19 +170,34 @@ local function run_tests()
171
170
end
172
171
end
173
172
174
- -- Run each test file individually
173
+ -- We don't need to store the original print function
174
+ -- as we're using isolated environments for each test file
175
+
176
+ -- Run each test file individually in a protected environment
175
177
for _ , file in ipairs (test_files ) do
176
- print (" \n Running tests in: " .. vim .fn .fnamemodify (file , " :t" ))
177
- local status , err = pcall (dofile , file )
178
- if not status then
179
- print (" Error loading test file: " .. err )
178
+ process_output (" \n Running tests in: " .. vim .fn .fnamemodify (file , " :t" ))
179
+
180
+ -- Create an environment with a modified print function
181
+ local test_env = setmetatable ({
182
+ print = process_output ,
183
+ }, { __index = _G })
184
+
185
+ -- Load the file in this environment
186
+ local chunk , load_err = loadfile (file )
187
+ if not chunk then
188
+ process_output (" Error loading test file: " .. load_err )
180
189
error_counter = error_counter + 1
190
+ else
191
+ -- Set the environment and run the file
192
+ setfenv (chunk , test_env )
193
+ local status , err = pcall (chunk )
194
+ if not status then
195
+ process_output (" Error executing test file: " .. err )
196
+ error_counter = error_counter + 1
197
+ end
181
198
end
182
199
end
183
200
184
- -- Restore print function
185
- print = old_print
186
-
187
201
-- Count the actual number of tests based on output
188
202
-- The test counts will be used for hardcoding since the dynamic counting isn't working
189
203
local test_count = 0
0 commit comments