Skip to content

Commit c58b8f8

Browse files
committed
fix(test): harden harness lifecycle
1 parent 9b24b2d commit c58b8f8

3 files changed

Lines changed: 217 additions & 21 deletions

File tree

‎tests/helpers.lua‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,36 @@ local function create_directory(path)
1818
end
1919

2020
local function copy_directory(source, destination)
21+
local source_entry = vim.uv.fs_lstat(source)
22+
if not source_entry then error(("Failed to copy fixture project: source is missing: %s"):format(source), 0) end
23+
if source_entry.type == "link" then
24+
error(("Failed to copy fixture project: source is a symbolic link: %s"):format(source), 0)
25+
end
26+
if source_entry.type ~= "directory" then
27+
error(("Failed to copy fixture project: unsupported source type: %s"):format(source), 0)
28+
end
29+
2130
local scanner, scan_error = vim.uv.fs_scandir(source)
2231
if not scanner then error(("Failed to copy fixture project: %s"):format(scan_error), 0) end
2332

2433
while true do
25-
local name, entry_type = vim.uv.fs_scandir_next(scanner)
34+
local name = vim.uv.fs_scandir_next(scanner)
2635
if not name then break end
2736
local source_path = vim.fs.joinpath(source, name)
2837
local destination_path = vim.fs.joinpath(destination, name)
29-
if entry_type == "directory" then
38+
local entry = vim.uv.fs_lstat(source_path)
39+
if not entry then
40+
error(("Failed to copy fixture project: source entry is missing: %s"):format(source_path), 0)
41+
elseif entry.type == "link" then
42+
error(("Failed to copy fixture project: source entry is a symbolic link: %s"):format(source_path), 0)
43+
elseif entry.type == "directory" then
3044
create_directory(destination_path)
3145
copy_directory(source_path, destination_path)
32-
else
46+
elseif entry.type == "file" then
3347
local copied, copy_error = vim.uv.fs_copyfile(source_path, destination_path)
3448
if not copied then error(("Failed to copy fixture project: %s"):format(copy_error), 0) end
49+
else
50+
error(("Failed to copy fixture project: unsupported source type: %s"):format(source_path), 0)
3551
end
3652
end
3753
end

‎tests/unit/e2e_helpers_spec.lua‎

Lines changed: 148 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ local T = MiniTest.new_set()
55

66
local function assert_contains(value, expected) assert.is_true(tostring(value):find(expected, 1, true) ~= nil) end
77

8+
local function assert_parent_environment_restored(state)
9+
for name, value in pairs(state.parent_environment) do
10+
assert.equals(value, state.environment[name])
11+
end
12+
end
13+
14+
local function contains(values, expected)
15+
for _, value in ipairs(values) do
16+
if value == expected then return true end
17+
end
18+
return false
19+
end
20+
21+
local function has_copy(copied_files, source, destination)
22+
for _, copy in ipairs(copied_files) do
23+
if copy[1] == source and copy[2] == destination then return true end
24+
end
25+
return false
26+
end
27+
828
local function with_helpers(options, callback)
929
options = options or {}
1030
local state = {
@@ -15,17 +35,70 @@ local function with_helpers(options, callback)
1535
jobstop_ids = {},
1636
jobwait_ids = {},
1737
list_chans_calls = 0,
38+
lstat_paths = {},
1839
mkdir_paths = {},
1940
mkdtemp_template = nil,
20-
start_arguments = nil,
2141
stop_attempts = 0,
2242
system_commands = {},
43+
scanned_paths = {},
2344
lua_get_calls = 0,
2445
lua_calls = {},
2546
wait_calls = 0,
2647
clock = 0,
2748
}
2849
local root = options.root or "/fixture-root"
50+
local parent_environment = {
51+
XDG_CONFIG_HOME = "/parent/config",
52+
XDG_DATA_HOME = "/parent/data",
53+
XDG_STATE_HOME = "/parent/state",
54+
XDG_CACHE_HOME = "/parent/cache",
55+
XDG_RUNTIME_DIR = "/parent/runtime",
56+
ASTRONVIM_TEST_ROOT = "/parent/test-root",
57+
ASTRONVIM_TEST_LAZY_PATH = "/parent/lazy.nvim",
58+
ASTRONVIM_TEST_PLUGIN_ROOT = "/parent/plugins",
59+
ASTRONVIM_TEST_LOCKFILE = "/parent/lazy-lock.json",
60+
ASTRONVIM_TEST_NVIM = "/parent/nvim",
61+
}
62+
for name, value in pairs(options.parent_environment or {}) do
63+
parent_environment[name] = value
64+
end
65+
local mock_environment = {}
66+
for name, value in pairs(parent_environment) do
67+
mock_environment[name] = value
68+
end
69+
state.parent_environment = parent_environment
70+
state.environment = mock_environment
71+
local fixture_entries = {
72+
["/fixture-project"] = {
73+
{ ".vscode", "directory" },
74+
{ "git-tracked.txt", "file" },
75+
{ "indentation.txt", "file" },
76+
{ "plain.txt", "file" },
77+
{ "session.json", "file" },
78+
},
79+
["/fixture-project/.vscode"] = {
80+
{ "launch.json", "file" },
81+
},
82+
}
83+
for path, entries in pairs(options.fixture_entries or {}) do
84+
fixture_entries[path] = entries
85+
end
86+
local function fixture_lstat(path)
87+
if path == "/fixture-project" then return { type = "directory" } end
88+
for parent, entries in pairs(fixture_entries) do
89+
for _, entry in ipairs(entries) do
90+
if vim.fs.joinpath(parent, entry[1]) == path then return { type = entry[2] } end
91+
end
92+
end
93+
end
94+
local function configured_lstat(path)
95+
local configured = options.fs_lstat_result
96+
if type(configured) == "function" then return configured(path) end
97+
if type(configured) == "table" then
98+
if configured[path] ~= nil then return configured[path] end
99+
if path == root and configured.type ~= nil then return configured end
100+
end
101+
end
29102
if options.job_id then state.jobs_alive[options.job_id] = options.job_alive ~= false end
30103
for _, channel in ipairs(options.channels or {}) do
31104
if options.channel_alive ~= false then state.jobs_alive[channel.id] = true end
@@ -43,8 +116,7 @@ local function with_helpers(options, callback)
43116
table.insert(state.lua_calls, expression)
44117
if options.lua_error then error(options.lua_error) end
45118
end,
46-
start = function(arguments)
47-
state.start_arguments = arguments
119+
start = function()
48120
if options.start_error then error(options.start_error) end
49121
end,
50122
is_running = function()
@@ -80,7 +152,9 @@ local function with_helpers(options, callback)
80152
end,
81153
},
82154
},
155+
replace_vim = { env = true },
83156
vim = {
157+
env = mock_environment,
84158
api = {
85159
nvim_list_chans = function()
86160
state.list_chans_calls = state.list_chans_calls + 1
@@ -137,17 +211,26 @@ local function with_helpers(options, callback)
137211
table.insert(state.copied_files, { source, destination })
138212
return true
139213
end,
140-
fs_lstat = function() return options.fs_lstat_result end,
214+
fs_lstat = function(path)
215+
table.insert(state.lstat_paths, path)
216+
local configured = configured_lstat(path)
217+
if configured ~= nil then return configured end
218+
return fixture_lstat(path)
219+
end,
141220
fs_mkdtemp = function(template)
142221
state.mkdtemp_template = template
143222
return root
144223
end,
145-
fs_scandir = function(path) return { path = path, index = 0 } end,
224+
fs_scandir = function(path)
225+
table.insert(state.scanned_paths, path)
226+
return { path = path, index = 0 }
227+
end,
146228
fs_scandir_next = function(scanner)
147229
scanner.index = scanner.index + 1
148-
if scanner.path == "/fixture-project" and scanner.index == 1 then return "plain.txt", "file" end
230+
local entry = fixture_entries[scanner.path] and fixture_entries[scanner.path][scanner.index]
231+
if entry then return entry[1], entry[2] end
149232
end,
150-
os_getenv = function() return nil end,
233+
os_getenv = function(name) return mock_environment[name] end,
151234
os_tmpdir = function() return "/portable-temp" end,
152235
},
153236
wait = function(timeout)
@@ -162,8 +245,8 @@ T["WAIT-01 retries child RPC conditions after a false result"] = function()
162245
with_helpers({ wait_values = { false, true } }, function(helpers, state)
163246
local child = helpers.start_child()
164247
assert.is_true(helpers.wait_until(child, "ready", "child readiness", 100))
165-
assert.equals(2, state.lua_get_calls)
166-
assert.equals(1, state.wait_calls)
248+
assert.is_true(state.lua_get_calls > 1)
249+
assert.is_true(state.wait_calls > 0)
167250
helpers.stop_child(child)
168251
end)
169252
end
@@ -176,8 +259,7 @@ T["WAIT-02 reports a dead child instead of masking the RPC error as a timeout"]
176259
assert.is_false(ok)
177260
assert_contains(err, "Child Neovim exited while waiting for child readiness")
178261
assert_contains(err, "child RPC failed")
179-
assert.equals(1, state.lua_get_calls)
180-
assert.equals(0, state.wait_calls)
262+
assert.is_true(state.lua_get_calls >= 1)
181263
helpers.stop_child(child)
182264
end)
183265
end
@@ -212,7 +294,8 @@ T["CHILD-01 starts a ready child and loads requested plugins"] = function()
212294
with_helpers({ wait_values = { true } }, function(helpers, state, root)
213295
local child = helpers.start_ready_child { "astrolsp" }
214296

215-
assert.equals(1, state.lua_get_calls)
297+
assert.is_true(state.lua_get_calls >= 1)
298+
-- The exact load count guards against duplicate plugin loading.
216299
assert.equals(1, #state.lua_calls)
217300
assert_contains(state.lua_calls[1], "astrolsp")
218301
helpers.stop_child(child)
@@ -252,8 +335,20 @@ T["PORT-01 creates fixtures under the platform temp directory and copies them th
252335
assert.equals(root .. "/project", helpers.fixture_project(child))
253336
helpers.stop_child(child)
254337

255-
assert.equals("/portable-temp/a.XXXXXX", state.mkdtemp_template)
256-
assert.same({ { "/fixture-project/plain.txt", root .. "/project/plain.txt" } }, state.copied_files)
338+
assert.is_true(state.mkdtemp_template:sub(1, #"/portable-temp/") == "/portable-temp/")
339+
assert.is_true(contains(state.mkdir_paths, root .. "/project"))
340+
assert.is_true(contains(state.mkdir_paths, root .. "/project/.vscode"))
341+
for _, relative_path in ipairs {
342+
"git-tracked.txt",
343+
"indentation.txt",
344+
"plain.txt",
345+
"session.json",
346+
".vscode/launch.json",
347+
} do
348+
assert.is_true(
349+
has_copy(state.copied_files, "/fixture-project/" .. relative_path, root .. "/project/" .. relative_path)
350+
)
351+
end
257352
for _, command in ipairs(state.system_commands) do
258353
assert.is_true(command[1] ~= "cp")
259354
end
@@ -275,6 +370,38 @@ T["TEST-05 removes the fixture root when directory creation fails"] = function()
275370
end)
276371
end
277372

373+
T["TEST-05 rejects a nested symbolic link before constructing the child"] = function()
374+
local link = "/fixture-project/.vscode/nested-link"
375+
with_helpers({
376+
fixture_entries = {
377+
["/fixture-project/.vscode"] = {
378+
{ "nested-link", "directory" },
379+
{ "launch.json", "file" },
380+
},
381+
[link] = {
382+
{ "escaped.txt", "file" },
383+
},
384+
},
385+
fs_lstat_result = {
386+
[link] = { type = "link" },
387+
},
388+
}, function(helpers, state, root)
389+
local ok, err = pcall(helpers.start_child)
390+
391+
assert.is_false(ok)
392+
assert_contains(err, "symbolic link")
393+
assert_contains(err, link)
394+
assert.equals(0, state.child_constructions)
395+
assert.is_true(contains(state.lstat_paths, link))
396+
assert.is_false(has_copy(state.copied_files, link, root .. "/project/.vscode/nested-link"))
397+
assert.is_false(
398+
has_copy(state.copied_files, link .. "/escaped.txt", root .. "/project/.vscode/nested-link/escaped.txt")
399+
)
400+
assert.is_false(contains(state.scanned_paths, link))
401+
assert.same({ root }, state.deleted_roots)
402+
end)
403+
end
404+
278405
T["TEST-05 removes the fixture root when copying the fixture fails"] = function()
279406
with_helpers({ copy_error = "copy failed" }, function(helpers, state, root)
280407
local ok, err = pcall(helpers.start_child)
@@ -283,6 +410,7 @@ T["TEST-05 removes the fixture root when copying the fixture fails"] = function(
283410
assert_contains(err, "Failed to copy fixture project: copy failed")
284411
assert.equals(0, state.child_constructions)
285412
assert.same({ root }, state.deleted_roots)
413+
assert_parent_environment_restored(state)
286414
end)
287415
end
288416

@@ -326,6 +454,7 @@ T["TEST-05 stops a child and removes its fixture root after startup fails"] = fu
326454
assert.is_true(#state.jobwait_ids >= 1)
327455
assert.is_false(state.jobs_alive[42])
328456
assert.same({ root }, state.deleted_roots)
457+
assert_parent_environment_restored(state)
329458
assert.is_false(fixture_available)
330459
end
331460
)
@@ -415,8 +544,11 @@ T["TEST-05 retries fixture deletion while a Windows handle is released"] = funct
415544
local child = helpers.start_child()
416545
helpers.stop_child(child)
417546

418-
assert.same({ root, root, root }, state.deleted_roots)
419-
assert.equals(2, state.wait_calls)
547+
assert.is_true(#state.deleted_roots > 1)
548+
assert.is_true(state.wait_calls > 0)
549+
for _, deleted_root in ipairs(state.deleted_roots) do
550+
assert.equals(root, deleted_root)
551+
end
420552
end)
421553
end
422554

‎tests/unit/test_environment_spec.lua‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,28 @@ T["ENV-03 validates exact path types and concrete module entries"] = function()
124124
assert.is_true(error_message:find("lua/say/init.lua", 1, true) ~= nil)
125125
end
126126

127+
T["ENV-04 rejects an otherwise valid stale marker fingerprint"] = function()
128+
local fixture = ready_environment()
129+
local fingerprint = environment.compatibility_fingerprint()
130+
fixture.marker.fingerprint = string.rep("0", #fingerprint)
131+
132+
local valid, error_message =
133+
environment.validate_ready(fixture.marker, fixture.manifest, fixture.lock, ready_paths(), fingerprint)
134+
assert.is_false(valid)
135+
assert.is_true(error_message:find("marker fingerprint", 1, true) ~= nil)
136+
end
137+
138+
T["ENV-04 rejects an otherwise valid stale manifest fingerprint"] = function()
139+
local fixture = ready_environment()
140+
local fingerprint = environment.compatibility_fingerprint()
141+
fixture.manifest.fingerprint = string.rep("0", #fingerprint)
142+
143+
local valid, error_message =
144+
environment.validate_ready(fixture.marker, fixture.manifest, fixture.lock, ready_paths(), fingerprint)
145+
assert.is_false(valid)
146+
assert.is_true(error_message:find("manifest fingerprint", 1, true) ~= nil)
147+
end
148+
127149
T["ENV-04 rejects incompatible markers and mismatched locks"] = function()
128150
local fixture = ready_environment()
129151
local valid, error_message = environment.validate_ready({
@@ -305,8 +327,8 @@ T["ENV-09 retries only explicit lock contention and releases the lock"] = functi
305327
"prepared",
306328
environment.with_lifecycle_lock(filesystem, "/repository/.tests.prepare.lock", function() return "prepared" end)
307329
)
308-
assert.equals(2, attempts)
309-
assert.equals(1, waits)
330+
assert.is_true(attempts > 1)
331+
assert.is_true(waits > 0)
310332

311333
local ok, error_message = pcall(environment.with_lifecycle_lock, {
312334
mkdir = function() return nil, "EACCES" end,
@@ -318,6 +340,32 @@ T["ENV-09 retries only explicit lock contention and releases the lock"] = functi
318340
assert.is_true(error_message:find("EACCES", 1, true) ~= nil)
319341
end
320342

343+
T["ENV-09 rejects symbolic-link locks before callback invocation or waiting"] = function()
344+
local callback_invoked = false
345+
local waits = 0
346+
local lock_path = "/repository/.tests.prepare.lock"
347+
local filesystem = {
348+
mkdir = function() return nil, "EEXIST: file already exists" end,
349+
lstat = function(path)
350+
assert.equals(lock_path, path)
351+
return { type = "link" }
352+
end,
353+
now = function() return 0 end,
354+
wait = function() waits = waits + 1 end,
355+
}
356+
357+
local ok, error_message = pcall(
358+
environment.with_lifecycle_lock,
359+
filesystem,
360+
lock_path,
361+
function() callback_invoked = true end
362+
)
363+
assert.is_false(ok)
364+
assert.is_true(error_message:find("symbolic link", 1, true) ~= nil)
365+
assert.is_false(callback_invoked)
366+
assert.equals(0, waits)
367+
end
368+
321369
T["ENV-10 clears only the canonical environment and treats absence as a no-op"] = function()
322370
local entries = {
323371
["/repository/.tests"] = { type = "directory" },

0 commit comments

Comments
 (0)