@@ -5,6 +5,26 @@ local T = MiniTest.new_set()
55
66local 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+
828local 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 )
169252end
@@ -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 )
183265end
@@ -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 )
276371end
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+
278405T [" 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 )
287415end
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 )
421553end
422554
0 commit comments