@@ -18,6 +18,7 @@ class CodeBlock:
1818 lineno : int
1919 syntax : str | None = None
2020 expected_output : str | None = None
21+ expected_output_ignore_whitespace : bool = False
2122 importorskip : str | None = None
2223 marks : list [str ] = field (default_factory = lambda : [])
2324
@@ -34,6 +35,7 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
3435 marks = []
3536 continued_block = None
3637 expected_output_block = None
38+ expected_output_ignore_whitespace = False
3739 importorskip = None
3840 k = 1
3941
@@ -58,18 +60,21 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
5860 if m is not None :
5961 keyword = m .group (1 ).strip ("- " )
6062 # handle special tags
61- if keyword == "expected-output" :
63+ if keyword in { "expected-output" , "expected-output-ignore-whitespace" } :
6264 if len (out ) == 0 :
6365 raise RuntimeError (
64- "Found <!--pytest-codeblocks-expected-output --> "
66+ f "Found <!--pytest-codeblocks-{ keyword } --> "
6567 + "but no previous code block."
6668 )
6769 if out [- 1 ].expected_output is not None :
6870 raise RuntimeError (
69- "Found <!--pytest-codeblocks-expected-output --> "
71+ f "Found <!--pytest-codeblocks-{ keyword } --> "
7072 + "but block already has expected_output."
7173 )
7274 expected_output_block = out [- 1 ]
75+ if keyword == "expected-output-ignore-whitespace" :
76+ # \s: regex matches all whitespace characters
77+ expected_output_ignore_whitespace = True
7378
7479 elif keyword == "cont" :
7580 if len (out ) == 0 :
@@ -115,7 +120,7 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
115120 marks .append ("pytest.mark.xfail" )
116121
117122 else :
118- raise RuntimeError (f'Unknown pytest-codeblocks keyword "{ keyword } . "' )
123+ raise RuntimeError (f'Unknown pytest-codeblocks keyword "{ keyword } "' )
119124
120125 continue
121126
@@ -162,6 +167,9 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
162167
163168 elif expected_output_block :
164169 expected_output_block .expected_output = code
170+ expected_output_block .expected_output_ignore_whitespace = (
171+ expected_output_ignore_whitespace
172+ )
165173 expected_output_block = None
166174
167175 else :
0 commit comments