@@ -77,17 +77,17 @@ def run_command(command, *args, parse_json=True) -> Tuple[str, str]:
77
77
78
78
# Check the return code to ensure the script ran successfully
79
79
if result .returncode != 0 :
80
- return result .stdout , result .stderr
80
+ return result .stdout , result .stderr , result . returncode
81
81
82
82
# Parse the output
83
83
if result .stdout :
84
84
if parse_json :
85
85
try :
86
- return json .loads (result .stdout ), result .stderr
86
+ return json .loads (result .stdout ), result .stderr , result . returncode
87
87
except json .JSONDecodeError :
88
- return result .stdout , result .stderr
89
- return result .stdout , result .stderr
90
- return result .stdout , result .stderr
88
+ return result .stdout , result .stderr , result . returncode
89
+ return result .stdout , result .stderr , result . returncode
90
+ return result .stdout , result .stderr , result . returncode
91
91
finally :
92
92
os .chdir (cwd )
93
93
@@ -1271,7 +1271,8 @@ def test_command_line(self, settings=None):
1271
1271
("hey! " * 5 ).strip (),
1272
1272
)
1273
1273
else :
1274
- self .assertIn ("UsageError" , result [1 ])
1274
+ self .assertIn ("Usage: ./manage.py groups echo [OPTIONS] MESSAGE" , result [0 ])
1275
+ self .assertIn ("Got unexpected extra argument (5)" , result [1 ])
1275
1276
with self .assertRaises (TypeError ):
1276
1277
call_command ("groups" , "echo" , "hey!" , echoes = 5 )
1277
1278
with self .assertRaises (TypeError ):
@@ -1415,13 +1416,16 @@ def test_command_line(self, settings=None):
1415
1416
"groups" , * settings , "string" , "annamontes" , "case" , "upper" , "4" , "9"
1416
1417
)
1417
1418
if override :
1418
- result = result [1 ].strip ()
1419
- self .assertIn ("UsageError" , result )
1419
+ self .assertIn (
1420
+ "Usage: ./manage.py groups string STRING case upper [OPTIONS]" ,
1421
+ result [0 ],
1422
+ )
1423
+ self .assertIn ("Got unexpected extra arguments (4 9)" , result [1 ].strip ())
1420
1424
grp_cmd .string ("annamontes" )
1421
1425
with self .assertRaises (TypeError ):
1422
1426
self .assertEqual (grp_cmd .upper (4 , 9 ), "annaMONTEs" )
1423
1427
1424
- with self .assertRaises (UsageError ):
1428
+ with self .assertRaises (CommandError ):
1425
1429
self .assertEqual (
1426
1430
call_command (
1427
1431
"groups" , "string" , "annamontes" , "case" , "upper" , "4" , "9"
@@ -1451,8 +1455,12 @@ def test_command_line(self, settings=None):
1451
1455
grp_cmd .string (" emmatc " )
1452
1456
self .assertEqual (grp_cmd .strip (), "emmatc" )
1453
1457
else :
1454
- self .assertIn ("UsageError" , result [1 ])
1455
- with self .assertRaises (UsageError ):
1458
+ self .assertIn (
1459
+ "Usage: ./manage.py groups string [OPTIONS] STRING COMMAND [ARGS]" ,
1460
+ result [0 ],
1461
+ )
1462
+ self .assertIn ("No such command 'strip'." , result [1 ])
1463
+ with self .assertRaises (CommandError ):
1456
1464
self .assertEqual (
1457
1465
call_command ("groups" , "string" , " emmatc " , "strip" ), "emmatc"
1458
1466
)
@@ -2615,3 +2623,85 @@ def test_custom_fallback(self):
2615
2623
"shell " ,
2616
2624
)[0 ]
2617
2625
self .assertTrue ("shell " in result )
2626
+
2627
+
2628
+ class TracebackTests (TestCase ):
2629
+ """
2630
+ Tests that show CommandErrors and UsageErrors do not result in tracebacks unless --traceback is set.
2631
+
2632
+ Also make sure that sys.exit is not called when not run from the terminal
2633
+ (i.e. in get_command invocation or call_command).
2634
+ """
2635
+
2636
+ def test_usage_error_no_tb (self ):
2637
+ stdout , stderr , retcode = run_command ("tb" , "wrong" )
2638
+ self .assertTrue ("Usage: ./manage.py tb [OPTIONS] COMMAND [ARGS]" in stdout )
2639
+ self .assertTrue ("No such command" in stderr )
2640
+ self .assertTrue (retcode > 0 )
2641
+
2642
+ stdout , stderr , retcode = run_command ("tb" , "error" , "wrong" )
2643
+ self .assertTrue ("Usage: ./manage.py tb error [OPTIONS]" in stdout )
2644
+ self .assertTrue ("Got unexpected extra argument" in stderr )
2645
+ self .assertTrue (retcode > 0 )
2646
+
2647
+ with self .assertRaises (CommandError ):
2648
+ call_command ("tb" , "wrong" )
2649
+
2650
+ with self .assertRaises (CommandError ):
2651
+ call_command ("tb" , "error" , "wrong" )
2652
+
2653
+ def test_usage_error_with_tb_if_requested (self ):
2654
+
2655
+ stdout , stderr , retcode = run_command ("tb" , "--traceback" , "wrong" )
2656
+ self .assertFalse (stdout .strip ())
2657
+ self .assertTrue ("Traceback" in stderr )
2658
+ if rich_installed :
2659
+ self .assertTrue ("───── locals ─────" in stderr )
2660
+ else :
2661
+ self .assertFalse ("───── locals ─────" in stderr )
2662
+ self .assertTrue ("No such command 'wrong'" in stderr )
2663
+ self .assertTrue (retcode > 0 )
2664
+
2665
+ stdout , stderr , retcode = run_command ("tb" , "--traceback" , "error" , "wrong" )
2666
+ self .assertFalse (stdout .strip ())
2667
+ self .assertTrue ("Traceback" in stderr )
2668
+ if rich_installed :
2669
+ self .assertTrue ("───── locals ─────" in stderr )
2670
+ else :
2671
+ self .assertFalse ("───── locals ─────" in stderr )
2672
+ self .assertFalse (stdout .strip ())
2673
+ self .assertTrue ("Got unexpected extra argument" in stderr )
2674
+ self .assertTrue (retcode > 0 )
2675
+
2676
+ with self .assertRaises (CommandError ):
2677
+ call_command ("tb" , "--traceback" , "wrong" )
2678
+
2679
+ with self .assertRaises (CommandError ):
2680
+ call_command ("tb" , "--traceback" , "error" , "wrong" )
2681
+
2682
+ def test_click_exception_retcodes_honored (self ):
2683
+
2684
+ self .assertEqual (run_command ("vanilla" )[2 ], 0 )
2685
+ self .assertEqual (run_command ("vanilla" , "--exit-code=2" )[2 ], 2 )
2686
+
2687
+ self .assertEqual (run_command ("tb" , "exit" )[2 ], 0 )
2688
+ self .assertEqual (run_command ("tb" , "exit" , "--code=2" )[2 ], 2 )
2689
+
2690
+ def test_exit_on_call (self ):
2691
+ with self .assertRaises (SystemExit ):
2692
+ call_command ("vanilla" , "--help" )
2693
+
2694
+ with self .assertRaises (SystemExit ):
2695
+ call_command ("vanilla" , "--exit-code" , "0" )
2696
+
2697
+ with self .assertRaises (SystemExit ):
2698
+ call_command ("vanilla" , "--exit-code" , "1" )
2699
+
2700
+ with self .assertRaises (SystemExit ):
2701
+ call_command ("tb" , "--help" )
2702
+
2703
+ with self .assertRaises (SystemExit ):
2704
+ call_command ("tb" , "exit" )
2705
+
2706
+ with self .assertRaises (SystemExit ):
2707
+ call_command ("tb" , "exit" , "--code=1" )
0 commit comments