1616from test .support import is_emscripten , requires_remote_subprocess_debugging
1717
1818from profiling .sampling .cli import main
19+ from profiling .sampling .errors import SamplingScriptNotFoundError , SamplingModuleNotFoundError , SamplingUnknownProcessError
1920
2021
2122class TestSampleProfilerCLI (unittest .TestCase ):
@@ -203,12 +204,12 @@ def test_cli_mutually_exclusive_pid_script(self):
203204 with (
204205 mock .patch ("sys.argv" , test_args ),
205206 mock .patch ("sys.stderr" , io .StringIO ()) as mock_stderr ,
206- self .assertRaises (SystemExit ) as cm ,
207+ self .assertRaises (SamplingScriptNotFoundError ) as cm ,
207208 ):
208209 main ()
209210
210211 # Verify the error is about the non-existent script
211- self .assertIn ("12345" , str (cm .exception . code ))
212+ self .assertIn ("12345" , str (cm .exception ))
212213
213214 def test_cli_no_target_specified (self ):
214215 # In new CLI, must specify a subcommand
@@ -436,6 +437,7 @@ def test_cli_default_collapsed_filename(self):
436437
437438 with (
438439 mock .patch ("sys.argv" , test_args ),
440+ mock .patch ("profiling.sampling.cli._is_process_running" , return_value = True ),
439441 mock .patch ("profiling.sampling.cli.sample" ) as mock_sample ,
440442 ):
441443 main ()
@@ -475,6 +477,7 @@ def test_cli_custom_output_filenames(self):
475477 for test_args , expected_filename , expected_format in test_cases :
476478 with (
477479 mock .patch ("sys.argv" , test_args ),
480+ mock .patch ("profiling.sampling.cli._is_process_running" , return_value = True ),
478481 mock .patch ("profiling.sampling.cli.sample" ) as mock_sample ,
479482 ):
480483 main ()
@@ -513,6 +516,7 @@ def test_argument_parsing_basic(self):
513516
514517 with (
515518 mock .patch ("sys.argv" , test_args ),
519+ mock .patch ("profiling.sampling.cli._is_process_running" , return_value = True ),
516520 mock .patch ("profiling.sampling.cli.sample" ) as mock_sample ,
517521 ):
518522 main ()
@@ -534,6 +538,7 @@ def test_sort_options(self):
534538
535539 with (
536540 mock .patch ("sys.argv" , test_args ),
541+ mock .patch ("profiling.sampling.cli._is_process_running" , return_value = True ),
537542 mock .patch ("profiling.sampling.cli.sample" ) as mock_sample ,
538543 ):
539544 main ()
@@ -547,6 +552,7 @@ def test_async_aware_flag_defaults_to_running(self):
547552
548553 with (
549554 mock .patch ("sys.argv" , test_args ),
555+ mock .patch ("profiling.sampling.cli._is_process_running" , return_value = True ),
550556 mock .patch ("profiling.sampling.cli.sample" ) as mock_sample ,
551557 ):
552558 main ()
@@ -562,6 +568,7 @@ def test_async_aware_with_async_mode_all(self):
562568
563569 with (
564570 mock .patch ("sys.argv" , test_args ),
571+ mock .patch ("profiling.sampling.cli._is_process_running" , return_value = True ),
565572 mock .patch ("profiling.sampling.cli.sample" ) as mock_sample ,
566573 ):
567574 main ()
@@ -576,6 +583,7 @@ def test_async_aware_default_is_none(self):
576583
577584 with (
578585 mock .patch ("sys.argv" , test_args ),
586+ mock .patch ("profiling.sampling.cli._is_process_running" , return_value = True ),
579587 mock .patch ("profiling.sampling.cli.sample" ) as mock_sample ,
580588 ):
581589 main ()
@@ -697,14 +705,20 @@ def test_async_aware_incompatible_with_all_threads(self):
697705 def test_run_nonexistent_script_exits_cleanly (self ):
698706 """Test that running a non-existent script exits with a clean error."""
699707 with mock .patch ("sys.argv" , ["profiling.sampling.cli" , "run" , "/nonexistent/script.py" ]):
700- with self .assertRaises ( SystemExit ) as cm :
708+ with self .assertRaisesRegex ( SamplingScriptNotFoundError , "Script '[ \\ w/.]+' not found." ) :
701709 main ()
702- self .assertIn ("Script not found" , str (cm .exception .code ))
703710
704711 @unittest .skipIf (is_emscripten , "subprocess not available" )
705712 def test_run_nonexistent_module_exits_cleanly (self ):
706713 """Test that running a non-existent module exits with a clean error."""
707714 with mock .patch ("sys.argv" , ["profiling.sampling.cli" , "run" , "-m" , "nonexistent_module_xyz" ]):
708- with self .assertRaises (SystemExit ) as cm :
715+ with self .assertRaisesRegex (SamplingModuleNotFoundError , "Module '[\\ w/.]+' not found." ):
716+ main ()
717+
718+ def test_cli_attach_nonexistent_pid (self ):
719+ fake_pid = "99999"
720+ with mock .patch ("sys.argv" , ["profiling.sampling.cli" , "attach" , fake_pid ]):
721+ with self .assertRaises (SamplingUnknownProcessError ) as cm :
709722 main ()
710- self .assertIn ("Module not found" , str (cm .exception .code ))
723+
724+ self .assertIn (fake_pid , str (cm .exception ))
0 commit comments