Skip to content

Commit be0241c

Browse files
authored
tooling: Add docu and change matching function (#933)
Add documentation for the file based rule checks. Test function checks file name and line number separately from warning message now.
1 parent 105f054 commit be0241c

2 files changed

Lines changed: 68 additions & 9 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# File based rule checks
2+
3+
## Test Function
4+
The functionality of the Sphinx build rules can be verified with test rst files.
5+
6+
The function *test_check_rules* in *test_rules_file_based.py* is executed for
7+
each rst file in the directory *rst*.
8+
It creates a SphinxTestApp and a document source folder with an index.rst file
9+
that contains a toctree with the given rst file.
10+
11+
It uses the SphinxTestApp to build the documentation and checks for the
12+
**expected/not expected** warnings.
13+
14+
## Create a test rst file
15+
To add a new test case create a new rst file in the rst directory.
16+
The test files can also be organized in a subfolder structure below directory rst.
17+
The test files are expected to contain the following format:
18+
19+
#EXPECT: <warning message>
20+
#EXPECT-NOT: <warning message>
21+
22+
<need information>
23+
24+
**\<warning message>**<br>
25+
Message text which is expected/not expected during the
26+
Sphinx build to be shown.
27+
This message is checked for the Sphinx-Needs directive
28+
specified after the EXPECT/EXPECT-NOT statement.
29+
30+
**\<need information>**<br>
31+
One or more Sphinx-Needs directives needed for the
32+
Sphinx document build
33+
34+
**Example:**
35+
36+
EXPECT: std_wp__test__abcd: is missing required option: `status`.
37+
38+
.. std_wp:: Test requirement
39+
:id: std_wp__test__abcd
40+
41+
This example verifies that the warning message
42+
*std_wp__test__abcd: is missing required option: `status`*
43+
is shown during the Sphinx build.

tooling/docs/_tooling/extensions/score_metamodel/tests/test_rules_file_based.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ def _create_app(rst_file: Path) -> SphinxTestApp:
8282
@dataclass
8383
class 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)
128145
def 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

Comments
 (0)