@@ -82,7 +82,9 @@ def _create_app(rst_file: Path) -> SphinxTestApp:
8282@dataclass
8383class WarningInfo :
8484 #### Class to hold information about warnings
85- # The class contains the line number and the expected and not expected warnings.
85+ # The class contains the filename of the rst file,
86+ # line number and the expected and not expected warnings.
87+ filename : str = ""
8688 lineno : int = 0
8789 expected : list [str ] = field (default_factory = list )
8890 not_expected : list [str ] = field (default_factory = list )
@@ -106,6 +108,7 @@ def extract_test_data(rst_file: Path) -> list[WarningInfo] | None:
106108 for no , line in enumerate (f , start = 1 ):
107109 if line .startswith (".. " ): # Beginning of new need
108110 if test_info :
111+ test_info .filename = rst_file .name
109112 test_info .lineno = no
110113 statements .append (test_info )
111114 test_info = None
@@ -124,6 +127,20 @@ def extract_test_data(rst_file: Path) -> list[WarningInfo] | None:
124127 return statements
125128
126129
130+ def warning_matches (
131+ warning_info : WarningInfo , expected_message : str , warnings : list [str ]
132+ ) -> bool :
133+ ### Checks if any element of the warning list is includes the given warning info.
134+ # It returns True if found otherwise False.
135+ for warning in warnings :
136+ if (
137+ f"{ warning_info .filename } :{ str (warning_info .lineno )} " in warning
138+ and expected_message in warning
139+ ):
140+ return True
141+ return False
142+
143+
127144@pytest .mark .parametrize ("rst_file" , RST_FILES )
128145def test_check_rules (
129146 rst_file : str , sphinx_app_setup : Callable [[Path ], SphinxTestApp ]
@@ -137,14 +154,13 @@ def test_check_rules(
137154 app : SphinxTestApp = sphinx_app_setup (RST_DIR / rst_file )
138155 os .chdir (app .srcdir ) # Change working directory to the source directory
139156 app .build ()
140- warn_text : str = app .warning .getvalue ()
157+ warnings = app .warning .getvalue (). splitlines ()
141158 for test in test_data :
142159 for expected in test .expected :
143- assert (
144- f" { Path ( rst_file ). name } : { test . lineno } : WARNING: { expected } " in warn_text
145- ), f"Expected warning: { [ expected ] } not found"
160+ assert warning_matches (
161+ test , expected , warnings
162+ ), f"Expected warning: { expected } not found"
146163 for not_expected in test .not_expected :
147- assert (
148- f"{ Path (rst_file ).name } :{ test .lineno } : WARNING: { not_expected } "
149- not in warn_text
150- ), f"Unexpected warning: { [not_expected ]} found"
164+ assert not warning_matches (
165+ test , not_expected , warnings
166+ ), f"Unexpected warning: { not_expected } found"
0 commit comments