Skip to content

Commit c044d05

Browse files
committed
Tests: Sundae Recipe: Workaround Rich bug on Windows: Textualize/rich#3693
1 parent 6112486 commit c044d05

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

tests/test_000_ictruck/test_430_recipes_sundae.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
# ruff: noqa: RUF001
2323

24+
import re
25+
2426
import pytest
2527

2628
from rich.console import Console
@@ -37,8 +39,7 @@ def __init__( self ):
3739
import os
3840
blackhole = open( # noqa: SIM115
3941
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 )
4243
self.print_calls = [ ]
4344

4445
def print( self, text, style = None, end = '\n' ):
@@ -53,6 +54,12 @@ def print_exception( self ):
5354
self.console.print_exception( )
5455

5556

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+
5663
@pytest.fixture
5764
def test_console( ):
5865
''' Provides a test-specific Console implementation. '''
@@ -161,8 +168,8 @@ def test_020_produce_special_prefix(
161168
else: presentation = getattr( recipes.PrefixLabelPresentation, label_as )
162169
control = recipes.PrefixFormatControl(
163170
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 ) )
166173
assert prefix == expected_prefix
167174
if expected_style and control.colorize:
168175
label = expected_prefix.split( '|' )[ 0 ].strip( )
@@ -192,8 +199,8 @@ def test_021_produce_trace_prefix(
192199
else: presentation = getattr( recipes.PrefixLabelPresentation, label_as )
193200
control = recipes.PrefixFormatControl(
194201
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 ) )
197204
assert prefix == expected_prefix
198205
if expected_style and control.colorize:
199206
label = expected_prefix.split( '|' )[ 0 ].strip( )
@@ -298,7 +305,7 @@ def test_030_formatter_output_custom_flavor(
298305
formatter = recipes._produce_formatter_factory(
299306
test_console, fake_auxiliaries )(
300307
configuration.FormatterControl( ), 'test', 'note' )
301-
result = formatter( { 'key': 'value' } )
308+
result = _strip_ansi_c1( formatter( { 'key': 'value' } ) )
302309
assert result == "{'key': 'value'}\n"
303310

304311

@@ -315,7 +322,7 @@ def test_031_formatter_stack_trace(
315322
formatter = recipes._produce_formatter_factory(
316323
test_console, fake_auxiliaries )(
317324
configuration.FormatterControl( ), 'test', 'errorx' )
318-
result = formatter( "Error message" )
325+
result = _strip_ansi_c1( formatter( "Error message" ) )
319326
assert result.endswith( "\nError message" )
320327
assert len( result ) > len( "\nError message" )
321328

@@ -327,7 +334,7 @@ def test_032_formatter_output_level(
327334
formatter = recipes._produce_formatter_factory(
328335
test_console, fake_auxiliaries )(
329336
configuration.FormatterControl( ), 'test', 1 )
330-
result = formatter( { 'key': 'value' } )
337+
result = _strip_ansi_c1( formatter( { 'key': 'value' } ) )
331338
assert result == "{'key': 'value'}\n"
332339

333340

@@ -350,7 +357,7 @@ def test_100_register_module(
350357
auxiliaries = fake_auxiliaries )
351358
debugger = truck( 'note' )
352359
debugger( "Integration test" )
353-
output = simple_output.getvalue()
360+
output = _strip_ansi_c1( simple_output.getvalue( ) )
354361
assert output == "NOTE| Integration test\n"
355362

356363

@@ -370,7 +377,7 @@ def test_101_register_module_colorize_default(
370377
auxiliaries = fake_auxiliaries )
371378
debugger = truck( 'note' )
372379
debugger( "Colorize test" )
373-
output = simple_output.getvalue( )
380+
output = _strip_ansi_c1( simple_output.getvalue( ) )
374381
assert output == "NOTE| Colorize test\n"
375382
assert any(
376383
call[ 0 ] == 'NOTE' and call[ 1 ].color.name == 'blue'
@@ -393,7 +400,7 @@ def test_102_register_module_label_as_default(
393400
auxiliaries = fake_auxiliaries )
394401
debugger = truck( 'note' )
395402
debugger( "Label as default test" )
396-
output = simple_output.getvalue()
403+
output = _strip_ansi_c1( simple_output.getvalue( ) )
397404
assert output == "NOTE| Label as default test\n"
398405

399406

@@ -418,7 +425,7 @@ def test_103_register_module_custom_styles(
418425
auxiliaries = fake_auxiliaries )
419426
debugger = truck( 'note' )
420427
debugger( "Custom styles test" )
421-
output = simple_output.getvalue( )
428+
output = _strip_ansi_c1( simple_output.getvalue( ) )
422429
assert output == "NOTE| Custom styles test\n"
423430
# assert any(
424431
# call[ 0 ] == 'NOTE' and call[ 1 ].color.name == 'magenta'
@@ -446,7 +453,7 @@ def test_104_register_module_custom_template(
446453
auxiliaries = fake_auxiliaries )
447454
debugger = truck( 'note' )
448455
debugger( "Custom template test" )
449-
output = simple_output.getvalue( )
456+
output = _strip_ansi_c1( simple_output.getvalue( ) )
450457
assert output == (
451458
f"[{__name__}] NOTE @ 2025-04-01 12:00:00 "
452459
'>>> "Custom template test": Custom template test\n' )
@@ -470,7 +477,7 @@ def test_105_register_module_custom_ts_format(
470477
auxiliaries = fake_auxiliaries )
471478
debugger = truck( 'note' )
472479
debugger( "Custom ts format test" )
473-
output = simple_output.getvalue()
480+
output = _strip_ansi_c1( simple_output.getvalue( ) )
474481
assert output == "12:00:00 NOTE| Custom ts format test\n"
475482

476483

@@ -492,7 +499,7 @@ def test_200_invalid_prefix_template(
492499

493500

494501
def test_201_invalid_ts_format( recipes, test_console, fake_auxiliaries ):
495-
''' Test invalid timestamp format raises exception. '''
502+
''' Invalid timestamp format raises exception. '''
496503
def raise_value_error( fmt ): raise ValueError( "Invalid format" )
497504
fake_auxiliaries = recipes.Auxiliaries(
498505
exc_info_discoverer = fake_auxiliaries.exc_info_discoverer,

0 commit comments

Comments
 (0)