Skip to content

Commit 4152845

Browse files
committed
Fix: Improve linting configuration and test runner
- Fix passing unused arguments with underscores - Remove redefining of global print function - Use sandboxed environments for test files - Add missing Lua functions to globals list - Update luacheck configuration for test files
1 parent 1b6f4b2 commit 4152845

File tree

2 files changed

+68
-16
lines changed

2 files changed

+68
-16
lines changed

.luacheckrc

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ std = {
2121
"error",
2222
"assert",
2323
"_VERSION",
24+
"loadfile",
25+
"setfenv",
26+
"setmetatable",
27+
"getmetatable",
2428
},
2529
}
2630

@@ -31,19 +35,53 @@ exclude_files = {
3135
"tests/plenary/*",
3236
}
3337

38+
-- Special configuration for test files
39+
files["tests/**/*.lua"] = {
40+
-- Allow common globals used in testing
41+
globals = {
42+
-- Common testing globals
43+
"describe", "it", "before_each", "after_each", "teardown", "pending", "spy", "stub", "mock",
44+
-- Lua standard utilities used in tests
45+
"print", "dofile",
46+
-- Test helpers
47+
"test", "expect",
48+
-- Global test state (allow modification)
49+
"_G",
50+
},
51+
52+
-- Allow modification of globals in test files
53+
allow_defined_top = true,
54+
55+
-- Allow mutation of underscore globals in test files
56+
module = true,
57+
58+
-- In tests, we're not concerned about these
59+
max_cyclomatic_complexity = false,
60+
61+
-- Don't report accessing/mutating globals that are needed for test frameworks
62+
read_globals = {
63+
"_G", -- Global environment
64+
"package", -- Package management
65+
},
66+
67+
-- Ignore unused variables in test code
68+
unused = false,
69+
unused_args = false,
70+
}
71+
3472
-- Allow unused self arguments of methods
3573
self = false
3674

3775
-- Don't report unused arguments/locals
3876
unused_args = false
3977
unused = false
4078

41-
-- Ignore warnings related to whitespace
79+
-- Ignore certain warnings that don't affect functionality
4280
ignore = {
4381
"611", -- Line contains trailing whitespace
4482
"612", -- Line contains trailing whitespace in a comment
4583
"613", -- Line contains trailing whitespace in a string
46-
"614", -- Line contains only whitespace
84+
"614", -- Line contains only whitespace (formatting style preference)
4785
}
4886

4987
-- Maximum line length

tests/run_tests.lua

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Test runner for Plenary-based tests
2-
local ok, plenary = pcall(require, "plenary")
2+
local ok, _ = pcall(require, "plenary")
33
if not ok then
44
print("ERROR: Could not load plenary")
55
vim.cmd("qa!")
@@ -26,7 +26,7 @@ _G.TEST_RESULTS = {
2626

2727
-- Silence vim.notify during tests to prevent output pollution
2828
local original_notify = vim.notify
29-
vim.notify = function(msg, level, opts)
29+
vim.notify = function(msg, level, _)
3030
-- Capture the message for debugging but don't display it
3131
if level == vim.log.levels.ERROR then
3232
_G.TEST_RESULTS.last_error = msg
@@ -148,11 +148,10 @@ local function run_tests()
148148
end
149149

150150
-- 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)
154153
-- Forward to original print
155-
old_print(msg)
154+
print(msg)
156155

157156
-- Count tests by looking for success/failure indicators
158157
if type(msg) == "string" then
@@ -171,19 +170,34 @@ local function run_tests()
171170
end
172171
end
173172

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
175177
for _, file in ipairs(test_files) do
176-
print("\nRunning 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("\nRunning 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)
180189
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
181198
end
182199
end
183200

184-
-- Restore print function
185-
print = old_print
186-
187201
-- Count the actual number of tests based on output
188202
-- The test counts will be used for hardcoding since the dynamic counting isn't working
189203
local test_count = 0

0 commit comments

Comments
 (0)