21
21
22
22
# ruff: noqa: RUF001
23
23
24
+ import re
25
+
24
26
import pytest
25
27
26
28
from rich .console import Console
@@ -37,8 +39,7 @@ def __init__( self ):
37
39
import os
38
40
blackhole = open ( # noqa: SIM115
39
41
os .devnull , 'w' , encoding = locale .getpreferredencoding ( ) )
40
- # Explict 'no_color' needed on Windows.
41
- self .console = Console ( file = blackhole , no_color = True )
42
+ self .console = Console ( file = blackhole )
42
43
self .print_calls = [ ]
43
44
44
45
def print ( self , text , style = None , end = '\n ' ):
@@ -53,6 +54,12 @@ def print_exception( self ):
53
54
self .console .print_exception ( )
54
55
55
56
57
+ def _strip_ansi_c1 ( text ):
58
+ # Needed to work around https://github.com/Textualize/rich/issues/3693.
59
+ regex = re .compile ( r'''\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])''' )
60
+ return regex .sub ( '' , text )
61
+
62
+
56
63
@pytest .fixture
57
64
def test_console ( ):
58
65
''' Provides a test-specific Console implementation. '''
@@ -161,8 +168,8 @@ def test_020_produce_special_prefix(
161
168
else : presentation = getattr ( recipes .PrefixLabelPresentation , label_as )
162
169
control = recipes .PrefixFormatControl (
163
170
colorize = True , label_as = presentation )
164
- prefix = recipes ._produce_special_prefix (
165
- test_console , fake_auxiliaries , control , 'test_module' , flavor )
171
+ prefix = _strip_ansi_c1 ( recipes ._produce_special_prefix (
172
+ test_console , fake_auxiliaries , control , 'test_module' , flavor ) )
166
173
assert prefix == expected_prefix
167
174
if expected_style and control .colorize :
168
175
label = expected_prefix .split ( '|' )[ 0 ].strip ( )
@@ -192,8 +199,8 @@ def test_021_produce_trace_prefix(
192
199
else : presentation = getattr ( recipes .PrefixLabelPresentation , label_as )
193
200
control = recipes .PrefixFormatControl (
194
201
colorize = True , label_as = presentation )
195
- prefix = recipes ._produce_trace_prefix (
196
- test_console , fake_auxiliaries , control , 'test_module' , level )
202
+ prefix = _strip_ansi_c1 ( recipes ._produce_trace_prefix (
203
+ test_console , fake_auxiliaries , control , 'test_module' , level ) )
197
204
assert prefix == expected_prefix
198
205
if expected_style and control .colorize :
199
206
label = expected_prefix .split ( '|' )[ 0 ].strip ( )
@@ -298,7 +305,7 @@ def test_030_formatter_output_custom_flavor(
298
305
formatter = recipes ._produce_formatter_factory (
299
306
test_console , fake_auxiliaries )(
300
307
configuration .FormatterControl ( ), 'test' , 'note' )
301
- result = formatter ( { 'key' : 'value' } )
308
+ result = _strip_ansi_c1 ( formatter ( { 'key' : 'value' } ) )
302
309
assert result == "{'key': 'value'}\n "
303
310
304
311
@@ -315,7 +322,7 @@ def test_031_formatter_stack_trace(
315
322
formatter = recipes ._produce_formatter_factory (
316
323
test_console , fake_auxiliaries )(
317
324
configuration .FormatterControl ( ), 'test' , 'errorx' )
318
- result = formatter ( "Error message" )
325
+ result = _strip_ansi_c1 ( formatter ( "Error message" ) )
319
326
assert result .endswith ( "\n Error message" )
320
327
assert len ( result ) > len ( "\n Error message" )
321
328
@@ -327,7 +334,7 @@ def test_032_formatter_output_level(
327
334
formatter = recipes ._produce_formatter_factory (
328
335
test_console , fake_auxiliaries )(
329
336
configuration .FormatterControl ( ), 'test' , 1 )
330
- result = formatter ( { 'key' : 'value' } )
337
+ result = _strip_ansi_c1 ( formatter ( { 'key' : 'value' } ) )
331
338
assert result == "{'key': 'value'}\n "
332
339
333
340
@@ -350,7 +357,7 @@ def test_100_register_module(
350
357
auxiliaries = fake_auxiliaries )
351
358
debugger = truck ( 'note' )
352
359
debugger ( "Integration test" )
353
- output = simple_output .getvalue ()
360
+ output = _strip_ansi_c1 ( simple_output .getvalue ( ) )
354
361
assert output == "NOTE| Integration test\n "
355
362
356
363
@@ -370,7 +377,7 @@ def test_101_register_module_colorize_default(
370
377
auxiliaries = fake_auxiliaries )
371
378
debugger = truck ( 'note' )
372
379
debugger ( "Colorize test" )
373
- output = simple_output .getvalue ( )
380
+ output = _strip_ansi_c1 ( simple_output .getvalue ( ) )
374
381
assert output == "NOTE| Colorize test\n "
375
382
assert any (
376
383
call [ 0 ] == 'NOTE' and call [ 1 ].color .name == 'blue'
@@ -393,7 +400,7 @@ def test_102_register_module_label_as_default(
393
400
auxiliaries = fake_auxiliaries )
394
401
debugger = truck ( 'note' )
395
402
debugger ( "Label as default test" )
396
- output = simple_output .getvalue ()
403
+ output = _strip_ansi_c1 ( simple_output .getvalue ( ) )
397
404
assert output == "NOTE| Label as default test\n "
398
405
399
406
@@ -418,7 +425,7 @@ def test_103_register_module_custom_styles(
418
425
auxiliaries = fake_auxiliaries )
419
426
debugger = truck ( 'note' )
420
427
debugger ( "Custom styles test" )
421
- output = simple_output .getvalue ( )
428
+ output = _strip_ansi_c1 ( simple_output .getvalue ( ) )
422
429
assert output == "NOTE| Custom styles test\n "
423
430
# assert any(
424
431
# call[ 0 ] == 'NOTE' and call[ 1 ].color.name == 'magenta'
@@ -446,7 +453,7 @@ def test_104_register_module_custom_template(
446
453
auxiliaries = fake_auxiliaries )
447
454
debugger = truck ( 'note' )
448
455
debugger ( "Custom template test" )
449
- output = simple_output .getvalue ( )
456
+ output = _strip_ansi_c1 ( simple_output .getvalue ( ) )
450
457
assert output == (
451
458
f"[{ __name__ } ] NOTE @ 2025-04-01 12:00:00 "
452
459
'>>> "Custom template test": Custom template test\n ' )
@@ -470,7 +477,7 @@ def test_105_register_module_custom_ts_format(
470
477
auxiliaries = fake_auxiliaries )
471
478
debugger = truck ( 'note' )
472
479
debugger ( "Custom ts format test" )
473
- output = simple_output .getvalue ()
480
+ output = _strip_ansi_c1 ( simple_output .getvalue ( ) )
474
481
assert output == "12:00:00 NOTE| Custom ts format test\n "
475
482
476
483
@@ -492,7 +499,7 @@ def test_200_invalid_prefix_template(
492
499
493
500
494
501
def test_201_invalid_ts_format ( recipes , test_console , fake_auxiliaries ):
495
- ''' Test invalid timestamp format raises exception. '''
502
+ ''' Invalid timestamp format raises exception. '''
496
503
def raise_value_error ( fmt ): raise ValueError ( "Invalid format" )
497
504
fake_auxiliaries = recipes .Auxiliaries (
498
505
exc_info_discoverer = fake_auxiliaries .exc_info_discoverer ,
0 commit comments