41
41
)
42
42
43
43
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
+
44
63
def CreateOutsimApp ():
45
64
c = cmd2 .Cmd ()
46
65
c .stdout = utils .StdSim (c .stdout )
@@ -1798,9 +1817,9 @@ def test_poutput_none(outsim_app):
1798
1817
assert out == expected
1799
1818
1800
1819
1820
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
1801
1821
def test_poutput_ansi_always (outsim_app ):
1802
1822
msg = 'Hello World'
1803
- ansi .allow_style = ansi .AllowStyle .ALWAYS
1804
1823
colored_msg = ansi .style (msg , fg = ansi .Fg .CYAN )
1805
1824
outsim_app .poutput (colored_msg )
1806
1825
out = outsim_app .stdout .getvalue ()
@@ -1809,9 +1828,9 @@ def test_poutput_ansi_always(outsim_app):
1809
1828
assert out == expected
1810
1829
1811
1830
1831
+ @with_ansi_style (ansi .AllowStyle .NEVER )
1812
1832
def test_poutput_ansi_never (outsim_app ):
1813
1833
msg = 'Hello World'
1814
- ansi .allow_style = ansi .AllowStyle .NEVER
1815
1834
colored_msg = ansi .style (msg , fg = ansi .Fg .CYAN )
1816
1835
outsim_app .poutput (colored_msg )
1817
1836
out = outsim_app .stdout .getvalue ()
@@ -2236,64 +2255,64 @@ def test_nonexistent_macro(base_app):
2236
2255
assert exception is not None
2237
2256
2238
2257
2258
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2239
2259
def test_perror_style (base_app , capsys ):
2240
2260
msg = 'testing...'
2241
2261
end = '\n '
2242
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2243
2262
base_app .perror (msg )
2244
2263
out , err = capsys .readouterr ()
2245
2264
assert err == ansi .style_error (msg ) + end
2246
2265
2247
2266
2267
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2248
2268
def test_perror_no_style (base_app , capsys ):
2249
2269
msg = 'testing...'
2250
2270
end = '\n '
2251
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2252
2271
base_app .perror (msg , apply_style = False )
2253
2272
out , err = capsys .readouterr ()
2254
2273
assert err == msg + end
2255
2274
2256
2275
2276
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2257
2277
def test_pwarning_style (base_app , capsys ):
2258
2278
msg = 'testing...'
2259
2279
end = '\n '
2260
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2261
2280
base_app .pwarning (msg )
2262
2281
out , err = capsys .readouterr ()
2263
2282
assert err == ansi .style_warning (msg ) + end
2264
2283
2265
2284
2285
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2266
2286
def test_pwarning_no_style (base_app , capsys ):
2267
2287
msg = 'testing...'
2268
2288
end = '\n '
2269
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2270
2289
base_app .pwarning (msg , apply_style = False )
2271
2290
out , err = capsys .readouterr ()
2272
2291
assert err == msg + end
2273
2292
2274
2293
2294
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2275
2295
def test_pexcept_style (base_app , capsys ):
2276
2296
msg = Exception ('testing...' )
2277
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2278
2297
2279
2298
base_app .pexcept (msg )
2280
2299
out , err = capsys .readouterr ()
2281
2300
assert err .startswith (ansi .style_error ("EXCEPTION of type 'Exception' occurred with message: testing..." ))
2282
2301
2283
2302
2303
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2284
2304
def test_pexcept_no_style (base_app , capsys ):
2285
2305
msg = Exception ('testing...' )
2286
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2287
2306
2288
2307
base_app .pexcept (msg , apply_style = False )
2289
2308
out , err = capsys .readouterr ()
2290
2309
assert err .startswith ("EXCEPTION of type 'Exception' occurred with message: testing..." )
2291
2310
2292
2311
2312
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2293
2313
def test_pexcept_not_exception (base_app , capsys ):
2294
2314
# Pass in a msg that is not an Exception object
2295
2315
msg = False
2296
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2297
2316
2298
2317
base_app .pexcept (msg )
2299
2318
out , err = capsys .readouterr ()
@@ -2322,20 +2341,20 @@ def test_ppaged_none(outsim_app):
2322
2341
assert not out
2323
2342
2324
2343
2344
+ @with_ansi_style (ansi .AllowStyle .TERMINAL )
2325
2345
def test_ppaged_strips_ansi_when_redirecting (outsim_app ):
2326
2346
msg = 'testing...'
2327
2347
end = '\n '
2328
- ansi .allow_style = ansi .AllowStyle .TERMINAL
2329
2348
outsim_app ._redirecting = True
2330
2349
outsim_app .ppaged (ansi .style (msg , fg = ansi .Fg .RED ))
2331
2350
out = outsim_app .stdout .getvalue ()
2332
2351
assert out == msg + end
2333
2352
2334
2353
2354
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2335
2355
def test_ppaged_strips_ansi_when_redirecting_if_always (outsim_app ):
2336
2356
msg = 'testing...'
2337
2357
end = '\n '
2338
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2339
2358
outsim_app ._redirecting = True
2340
2359
colored_msg = ansi .style (msg , fg = ansi .Fg .RED )
2341
2360
outsim_app .ppaged (colored_msg )
@@ -2526,9 +2545,9 @@ def do_echo_error(self, args):
2526
2545
self .perror (args )
2527
2546
2528
2547
2548
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2529
2549
def test_ansi_pouterr_always_tty (mocker , capsys ):
2530
2550
app = AnsiApp ()
2531
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2532
2551
mocker .patch .object (app .stdout , 'isatty' , return_value = True )
2533
2552
mocker .patch .object (sys .stderr , 'isatty' , return_value = True )
2534
2553
@@ -2549,9 +2568,9 @@ def test_ansi_pouterr_always_tty(mocker, capsys):
2549
2568
assert 'oopsie' in err
2550
2569
2551
2570
2571
+ @with_ansi_style (ansi .AllowStyle .ALWAYS )
2552
2572
def test_ansi_pouterr_always_notty (mocker , capsys ):
2553
2573
app = AnsiApp ()
2554
- ansi .allow_style = ansi .AllowStyle .ALWAYS
2555
2574
mocker .patch .object (app .stdout , 'isatty' , return_value = False )
2556
2575
mocker .patch .object (sys .stderr , 'isatty' , return_value = False )
2557
2576
@@ -2572,9 +2591,9 @@ def test_ansi_pouterr_always_notty(mocker, capsys):
2572
2591
assert 'oopsie' in err
2573
2592
2574
2593
2594
+ @with_ansi_style (ansi .AllowStyle .TERMINAL )
2575
2595
def test_ansi_terminal_tty (mocker , capsys ):
2576
2596
app = AnsiApp ()
2577
- ansi .allow_style = ansi .AllowStyle .TERMINAL
2578
2597
mocker .patch .object (app .stdout , 'isatty' , return_value = True )
2579
2598
mocker .patch .object (sys .stderr , 'isatty' , return_value = True )
2580
2599
@@ -2594,9 +2613,9 @@ def test_ansi_terminal_tty(mocker, capsys):
2594
2613
assert 'oopsie' in err
2595
2614
2596
2615
2616
+ @with_ansi_style (ansi .AllowStyle .TERMINAL )
2597
2617
def test_ansi_terminal_notty (mocker , capsys ):
2598
2618
app = AnsiApp ()
2599
- ansi .allow_style = ansi .AllowStyle .TERMINAL
2600
2619
mocker .patch .object (app .stdout , 'isatty' , return_value = False )
2601
2620
mocker .patch .object (sys .stderr , 'isatty' , return_value = False )
2602
2621
@@ -2609,9 +2628,9 @@ def test_ansi_terminal_notty(mocker, capsys):
2609
2628
assert out == err == 'oopsie\n '
2610
2629
2611
2630
2631
+ @with_ansi_style (ansi .AllowStyle .NEVER )
2612
2632
def test_ansi_never_tty (mocker , capsys ):
2613
2633
app = AnsiApp ()
2614
- ansi .allow_style = ansi .AllowStyle .NEVER
2615
2634
mocker .patch .object (app .stdout , 'isatty' , return_value = True )
2616
2635
mocker .patch .object (sys .stderr , 'isatty' , return_value = True )
2617
2636
@@ -2624,9 +2643,9 @@ def test_ansi_never_tty(mocker, capsys):
2624
2643
assert out == err == 'oopsie\n '
2625
2644
2626
2645
2646
+ @with_ansi_style (ansi .AllowStyle .NEVER )
2627
2647
def test_ansi_never_notty (mocker , capsys ):
2628
2648
app = AnsiApp ()
2629
- ansi .allow_style = ansi .AllowStyle .NEVER
2630
2649
mocker .patch .object (app .stdout , 'isatty' , return_value = False )
2631
2650
mocker .patch .object (sys .stderr , 'isatty' , return_value = False )
2632
2651
0 commit comments