@@ -226,13 +226,12 @@ def base_patches(test_paths, mock_truediv, debug_helper):
226226 "pathlib.Path.parent" , return_value = debug_helper .config_file .parent .parent
227227 ),
228228 patch ("pathlib.Path.__truediv__" , side_effect = mock_truediv ),
229- patch ("pathlib.Path.iterdir" , return_value = []), # Mock empty directory listing
229+ patch ("pathlib.Path.iterdir" , return_value = []),
230230 patch ("json.dump" ),
231231 patch ("pathlib.Path.touch" ),
232232 patch ("vec_inf.client._utils.Path" , return_value = test_paths ["weights_dir" ]),
233- patch (
234- "pathlib.Path.home" , return_value = Path ("/home/user" )
235- ), # Mock home directory
233+ patch ("pathlib.Path.home" , return_value = Path ("/home/user" )),
234+ patch ("pathlib.Path.rename" ),
236235 ]
237236
238237
@@ -246,25 +245,25 @@ def apply_base_patches(base_patches):
246245 yield
247246
248247
249- def test_launch_command_success (runner , mock_launch_output , path_exists , debug_helper ):
248+ def test_launch_command_success (
249+ runner ,
250+ mock_launch_output ,
251+ path_exists ,
252+ debug_helper ,
253+ mock_truediv ,
254+ test_paths ,
255+ base_patches ,
256+ ):
250257 """Test successful model launch with minimal required arguments."""
251- test_log_dir = Path ("/tmp/test_vec_inf_logs" )
258+ with ExitStack () as stack :
259+ # Apply all base patches
260+ for patch_obj in base_patches :
261+ stack .enter_context (patch_obj )
262+
263+ # Apply specific patches for this test
264+ mock_run = stack .enter_context (patch ("vec_inf.client._utils.run_bash_command" ))
265+ stack .enter_context (patch ("pathlib.Path.exists" , new = path_exists ))
252266
253- with (
254- patch ("vec_inf.client._utils.run_bash_command" ) as mock_run ,
255- patch ("pathlib.Path.mkdir" ),
256- patch ("builtins.open" , debug_helper .tracked_mock_open ),
257- patch ("pathlib.Path.open" , debug_helper .tracked_mock_open ),
258- patch ("pathlib.Path.exists" , new = path_exists ),
259- patch ("pathlib.Path.expanduser" , return_value = test_log_dir ),
260- patch ("pathlib.Path.resolve" , return_value = debug_helper .config_file .parent ),
261- patch (
262- "pathlib.Path.parent" , return_value = debug_helper .config_file .parent .parent
263- ),
264- patch ("json.dump" ),
265- patch ("pathlib.Path.touch" ),
266- patch ("pathlib.Path.__truediv__" , return_value = test_log_dir ),
267- ):
268267 expected_job_id = "14933053"
269268 mock_run .return_value = mock_launch_output (expected_job_id )
270269
@@ -277,25 +276,24 @@ def test_launch_command_success(runner, mock_launch_output, path_exists, debug_h
277276
278277
279278def test_launch_command_with_json_output (
280- runner , mock_launch_output , path_exists , debug_helper
279+ runner ,
280+ mock_launch_output ,
281+ path_exists ,
282+ debug_helper ,
283+ mock_truediv ,
284+ test_paths ,
285+ base_patches ,
281286):
282287 """Test JSON output format for launch command."""
283- test_log_dir = Path ("/tmp/test_vec_inf_logs" )
284- with (
285- patch ("vec_inf.client._utils.run_bash_command" ) as mock_run ,
286- patch ("pathlib.Path.mkdir" ),
287- patch ("builtins.open" , debug_helper .tracked_mock_open ),
288- patch ("pathlib.Path.open" , debug_helper .tracked_mock_open ),
289- patch ("pathlib.Path.exists" , new = path_exists ),
290- patch ("pathlib.Path.expanduser" , return_value = test_log_dir ),
291- patch ("pathlib.Path.resolve" , return_value = debug_helper .config_file .parent ),
292- patch (
293- "pathlib.Path.parent" , return_value = debug_helper .config_file .parent .parent
294- ),
295- patch ("json.dump" ),
296- patch ("pathlib.Path.touch" ),
297- patch ("pathlib.Path.__truediv__" , return_value = test_log_dir ),
298- ):
288+ with ExitStack () as stack :
289+ # Apply all base patches
290+ for patch_obj in base_patches :
291+ stack .enter_context (patch_obj )
292+
293+ # Apply specific patches for this test
294+ mock_run = stack .enter_context (patch ("vec_inf.client._utils.run_bash_command" ))
295+ stack .enter_context (patch ("pathlib.Path.exists" , new = path_exists ))
296+
299297 expected_job_id = "14933051"
300298 mock_run .return_value = mock_launch_output (expected_job_id )
301299
@@ -319,7 +317,7 @@ def test_launch_command_with_json_output(
319317 assert output .get ("slurm_job_id" ) == expected_job_id
320318 assert output .get ("model_name" ) == "Meta-Llama-3.1-8B"
321319 assert output .get ("model_type" ) == "LLM"
322- assert str (test_log_dir ) in output .get ("log_dir" , "" )
320+ assert str (test_paths [ "log_dir" ] ) in output .get ("log_dir" , "" )
323321
324322
325323def test_launch_command_no_model_weights_parent_dir (runner , debug_helper , base_patches ):
0 commit comments