4141)
4242
4343
44+ def with_ansi_style (style ):
45+ def arg_decorator (func ):
46+ import functools
47+
48+ @functools .wraps (func )
49+ def cmd_wrapper (* args , ** kwargs ):
50+ old = ansi .allow_style
51+ ansi .allow_style = style
52+ try :
53+ retval = func (* args , ** kwargs )
54+ finally :
55+ ansi .allow_style = old
56+ return retval
57+
58+ return cmd_wrapper
59+
60+ return arg_decorator
61+
62+
4463def CreateOutsimApp ():
4564 c = cmd2 .Cmd ()
4665 c .stdout = utils .StdSim (c .stdout )
@@ -1798,9 +1817,9 @@ def test_poutput_none(outsim_app):
17981817 assert out == expected
17991818
18001819
1820+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
18011821def test_poutput_ansi_always (outsim_app ):
18021822 msg = 'Hello World'
1803- ansi .allow_style = ansi .AllowStyle .ALWAYS
18041823 colored_msg = ansi .style (msg , fg = ansi .Fg .CYAN )
18051824 outsim_app .poutput (colored_msg )
18061825 out = outsim_app .stdout .getvalue ()
@@ -1809,9 +1828,9 @@ def test_poutput_ansi_always(outsim_app):
18091828 assert out == expected
18101829
18111830
1831+ @with_ansi_style (ansi .AllowStyle .NEVER )
18121832def test_poutput_ansi_never (outsim_app ):
18131833 msg = 'Hello World'
1814- ansi .allow_style = ansi .AllowStyle .NEVER
18151834 colored_msg = ansi .style (msg , fg = ansi .Fg .CYAN )
18161835 outsim_app .poutput (colored_msg )
18171836 out = outsim_app .stdout .getvalue ()
@@ -2236,64 +2255,64 @@ def test_nonexistent_macro(base_app):
22362255 assert exception is not None
22372256
22382257
2258+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
22392259def test_perror_style (base_app , capsys ):
22402260 msg = 'testing...'
22412261 end = '\n '
2242- ansi .allow_style = ansi .AllowStyle .ALWAYS
22432262 base_app .perror (msg )
22442263 out , err = capsys .readouterr ()
22452264 assert err == ansi .style_error (msg ) + end
22462265
22472266
2267+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
22482268def test_perror_no_style (base_app , capsys ):
22492269 msg = 'testing...'
22502270 end = '\n '
2251- ansi .allow_style = ansi .AllowStyle .ALWAYS
22522271 base_app .perror (msg , apply_style = False )
22532272 out , err = capsys .readouterr ()
22542273 assert err == msg + end
22552274
22562275
2276+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
22572277def test_pwarning_style (base_app , capsys ):
22582278 msg = 'testing...'
22592279 end = '\n '
2260- ansi .allow_style = ansi .AllowStyle .ALWAYS
22612280 base_app .pwarning (msg )
22622281 out , err = capsys .readouterr ()
22632282 assert err == ansi .style_warning (msg ) + end
22642283
22652284
2285+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
22662286def test_pwarning_no_style (base_app , capsys ):
22672287 msg = 'testing...'
22682288 end = '\n '
2269- ansi .allow_style = ansi .AllowStyle .ALWAYS
22702289 base_app .pwarning (msg , apply_style = False )
22712290 out , err = capsys .readouterr ()
22722291 assert err == msg + end
22732292
22742293
2294+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
22752295def test_pexcept_style (base_app , capsys ):
22762296 msg = Exception ('testing...' )
2277- ansi .allow_style = ansi .AllowStyle .ALWAYS
22782297
22792298 base_app .pexcept (msg )
22802299 out , err = capsys .readouterr ()
22812300 assert err .startswith (ansi .style_error ("EXCEPTION of type 'Exception' occurred with message: testing..." ))
22822301
22832302
2303+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
22842304def test_pexcept_no_style (base_app , capsys ):
22852305 msg = Exception ('testing...' )
2286- ansi .allow_style = ansi .AllowStyle .ALWAYS
22872306
22882307 base_app .pexcept (msg , apply_style = False )
22892308 out , err = capsys .readouterr ()
22902309 assert err .startswith ("EXCEPTION of type 'Exception' occurred with message: testing..." )
22912310
22922311
2312+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
22932313def test_pexcept_not_exception (base_app , capsys ):
22942314 # Pass in a msg that is not an Exception object
22952315 msg = False
2296- ansi .allow_style = ansi .AllowStyle .ALWAYS
22972316
22982317 base_app .pexcept (msg )
22992318 out , err = capsys .readouterr ()
@@ -2322,20 +2341,20 @@ def test_ppaged_none(outsim_app):
23222341 assert not out
23232342
23242343
2344+ @with_ansi_style (ansi .AllowStyle .TERMINAL )
23252345def test_ppaged_strips_ansi_when_redirecting (outsim_app ):
23262346 msg = 'testing...'
23272347 end = '\n '
2328- ansi .allow_style = ansi .AllowStyle .TERMINAL
23292348 outsim_app ._redirecting = True
23302349 outsim_app .ppaged (ansi .style (msg , fg = ansi .Fg .RED ))
23312350 out = outsim_app .stdout .getvalue ()
23322351 assert out == msg + end
23332352
23342353
2354+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
23352355def test_ppaged_strips_ansi_when_redirecting_if_always (outsim_app ):
23362356 msg = 'testing...'
23372357 end = '\n '
2338- ansi .allow_style = ansi .AllowStyle .ALWAYS
23392358 outsim_app ._redirecting = True
23402359 colored_msg = ansi .style (msg , fg = ansi .Fg .RED )
23412360 outsim_app .ppaged (colored_msg )
@@ -2526,9 +2545,9 @@ def do_echo_error(self, args):
25262545 self .perror (args )
25272546
25282547
2548+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
25292549def test_ansi_pouterr_always_tty (mocker , capsys ):
25302550 app = AnsiApp ()
2531- ansi .allow_style = ansi .AllowStyle .ALWAYS
25322551 mocker .patch .object (app .stdout , 'isatty' , return_value = True )
25332552 mocker .patch .object (sys .stderr , 'isatty' , return_value = True )
25342553
@@ -2549,9 +2568,9 @@ def test_ansi_pouterr_always_tty(mocker, capsys):
25492568 assert 'oopsie' in err
25502569
25512570
2571+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
25522572def test_ansi_pouterr_always_notty (mocker , capsys ):
25532573 app = AnsiApp ()
2554- ansi .allow_style = ansi .AllowStyle .ALWAYS
25552574 mocker .patch .object (app .stdout , 'isatty' , return_value = False )
25562575 mocker .patch .object (sys .stderr , 'isatty' , return_value = False )
25572576
@@ -2572,9 +2591,9 @@ def test_ansi_pouterr_always_notty(mocker, capsys):
25722591 assert 'oopsie' in err
25732592
25742593
2594+ @with_ansi_style (ansi .AllowStyle .TERMINAL )
25752595def test_ansi_terminal_tty (mocker , capsys ):
25762596 app = AnsiApp ()
2577- ansi .allow_style = ansi .AllowStyle .TERMINAL
25782597 mocker .patch .object (app .stdout , 'isatty' , return_value = True )
25792598 mocker .patch .object (sys .stderr , 'isatty' , return_value = True )
25802599
@@ -2594,9 +2613,9 @@ def test_ansi_terminal_tty(mocker, capsys):
25942613 assert 'oopsie' in err
25952614
25962615
2616+ @with_ansi_style (ansi .AllowStyle .TERMINAL )
25972617def test_ansi_terminal_notty (mocker , capsys ):
25982618 app = AnsiApp ()
2599- ansi .allow_style = ansi .AllowStyle .TERMINAL
26002619 mocker .patch .object (app .stdout , 'isatty' , return_value = False )
26012620 mocker .patch .object (sys .stderr , 'isatty' , return_value = False )
26022621
@@ -2609,9 +2628,9 @@ def test_ansi_terminal_notty(mocker, capsys):
26092628 assert out == err == 'oopsie\n '
26102629
26112630
2631+ @with_ansi_style (ansi .AllowStyle .NEVER )
26122632def test_ansi_never_tty (mocker , capsys ):
26132633 app = AnsiApp ()
2614- ansi .allow_style = ansi .AllowStyle .NEVER
26152634 mocker .patch .object (app .stdout , 'isatty' , return_value = True )
26162635 mocker .patch .object (sys .stderr , 'isatty' , return_value = True )
26172636
@@ -2624,9 +2643,9 @@ def test_ansi_never_tty(mocker, capsys):
26242643 assert out == err == 'oopsie\n '
26252644
26262645
2646+ @with_ansi_style (ansi .AllowStyle .NEVER )
26272647def test_ansi_never_notty (mocker , capsys ):
26282648 app = AnsiApp ()
2629- ansi .allow_style = ansi .AllowStyle .NEVER
26302649 mocker .patch .object (app .stdout , 'isatty' , return_value = False )
26312650 mocker .patch .object (sys .stderr , 'isatty' , return_value = False )
26322651
0 commit comments