@@ -553,8 +553,8 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent
553553 # This boolean flag determines whether or not the cmd2 application can interact with the clipboard
554554 self .can_clip = can_clip
555555
556- # This determines if a non-zero exit code should be used when exiting the application
557- self .exit_code = None
556+ # This determines the value returned by cmdloop() when exiting the application
557+ self .exit_code = 0
558558
559559 # This lock should be acquired before doing any asynchronous changes to the terminal to
560560 # ensure the updates to the terminal don't interfere with the input being typed or output
@@ -3684,7 +3684,12 @@ class TestMyAppCase(Cmd2TestCase):
36843684 sys .argv = [sys .argv [0 ]] # the --test argument upsets unittest.main()
36853685 testcase = TestMyAppCase ()
36863686 runner = unittest .TextTestRunner ()
3687- runner .run (testcase )
3687+ test_results = runner .run (testcase )
3688+ if test_results .wasSuccessful ():
3689+ self .poutput ('Tests passed' , color = Fore .LIGHTGREEN_EX )
3690+ else :
3691+ self .perror ('Tests failed' , traceback_war = False )
3692+ self .exit_code = - 1
36883693
36893694 def async_alert (self , alert_msg : str , new_prompt : Optional [str ] = None ) -> None : # pragma: no cover
36903695 """
@@ -3932,14 +3937,15 @@ def _report_disabled_command_usage(self, *args, message_to_print: str, **kwargs)
39323937 """
39333938 self .decolorized_write (sys .stderr , "{}\n " .format (message_to_print ))
39343939
3935- def cmdloop (self , intro : Optional [str ] = None ) -> None :
3940+ def cmdloop (self , intro : Optional [str ] = None ) -> int :
39363941 """This is an outer wrapper around _cmdloop() which deals with extra features provided by cmd2.
39373942
39383943 _cmdloop() provides the main loop equivalent to cmd.cmdloop(). This is a wrapper around that which deals with
39393944 the following extra features provided by cmd2:
39403945 - commands at invocation
39413946 - transcript testing
39423947 - intro banner
3948+ - exit code
39433949
39443950 :param intro: if provided this overrides self.intro and serves as the intro banner printed once at start
39453951 """
@@ -4002,8 +4008,7 @@ def cmdloop(self, intro: Optional[str] = None) -> None:
40024008 # Restore the original signal handler
40034009 signal .signal (signal .SIGINT , original_sigint_handler )
40044010
4005- if self .exit_code is not None :
4006- sys .exit (self .exit_code )
4011+ return self .exit_code
40074012
40084013 ###
40094014 #
0 commit comments