|
1 |
| -"""Sanity test using rstcheck and sphinx.""" |
2 |
| -from __future__ import annotations |
3 |
| - |
4 |
| -import re |
5 |
| -import subprocess |
| 1 | +from rstcheck_core.runner import RstcheckMainRunner |
| 2 | +from rstcheck_core.config import RstcheckConfig |
| 3 | +import pathlib |
6 | 4 | import sys
|
7 | 5 |
|
8 |
| - |
9 | 6 | def main():
|
10 |
| - paths = sys.argv[1:] or sys.stdin.read().splitlines() |
11 |
| - |
12 |
| - encoding = 'utf-8' |
13 |
| - |
14 |
| - ignore_substitutions = ( |
15 |
| - 'br', |
| 7 | + # Define the paths to check (passed as CLI arguments or from stdin) |
| 8 | + paths = [pathlib.Path(p) for p in (sys.argv[1:] or sys.stdin.read().splitlines())] |
| 9 | + |
| 10 | + # Define the configuration for rstcheck |
| 11 | + config = RstcheckConfig( |
| 12 | + ignore_roles=[ |
| 13 | + "ansplugin", "ansopt", "ansretval", "ansval", "ansenvvar", "ansenvvarref" |
| 14 | + ], |
| 15 | + ignore_substitutions=["br"], |
| 16 | + report_level="warning", # Adjust report level as needed -> ["info": 1, "warning": 2, "error": 3,"severe": 4, "none": 5,] |
| 17 | + recursive=True, # Set to True to check directories recursively |
16 | 18 | )
|
17 | 19 |
|
18 |
| - cmd = [ |
19 |
| - sys.executable, |
20 |
| - '-c', 'import rstcheck; rstcheck.main();', |
21 |
| - '--report', 'warning', |
22 |
| - '--ignore-roles', 'ansplugin,ansopt,ansretval,ansval,ansenvvar,ansenvvarref', |
23 |
| - '--ignore-substitutions', ','.join(ignore_substitutions), |
24 |
| - ] + paths |
25 |
| - |
26 |
| - process = subprocess.run(cmd, |
27 |
| - stdin=subprocess.DEVNULL, |
28 |
| - stdout=subprocess.PIPE, |
29 |
| - stderr=subprocess.PIPE, |
30 |
| - check=False, |
31 |
| - ) |
32 |
| - |
33 |
| - if process.stdout: |
34 |
| - raise Exception(process.stdout) |
35 |
| - |
36 |
| - pattern = re.compile(r'^(?P<path>[^:]*):(?P<line>[0-9]+): \((?P<level>INFO|WARNING|ERROR|SEVERE)/[0-4]\) (?P<message>.*)$') |
37 |
| - |
38 |
| - results = parse_to_list_of_dict(pattern, process.stderr.decode(encoding)) |
39 |
| - |
40 |
| - for result in results: |
41 |
| - print('%s:%s:%s: %s' % (result['path'], result['line'], 0, result['message'])) |
42 |
| - |
43 |
| - |
44 |
| -def parse_to_list_of_dict(pattern, value): |
45 |
| - matched = [] |
46 |
| - unmatched = [] |
47 |
| - |
48 |
| - for line in value.splitlines(): |
49 |
| - match = re.search(pattern, line) |
50 |
| - |
51 |
| - if match: |
52 |
| - matched.append(match.groupdict()) |
53 |
| - else: |
54 |
| - unmatched.append(line) |
55 |
| - |
56 |
| - if unmatched: |
57 |
| - raise Exception('Pattern "%s" did not match values:\n%s' % (pattern, '\n'.join(unmatched))) |
| 20 | + # Initialize the runner |
| 21 | + runner = RstcheckMainRunner( |
| 22 | + check_paths=paths, |
| 23 | + rstcheck_config=config, |
| 24 | + overwrite_config=True, |
| 25 | + ) |
58 | 26 |
|
59 |
| - return matched |
| 27 | + # Run the checks |
| 28 | + exit_code = runner.run() |
60 | 29 |
|
| 30 | + # Exit with the appropriate code |
| 31 | + sys.exit(exit_code) |
61 | 32 |
|
62 |
| -if __name__ == '__main__': |
| 33 | +if __name__ == "__main__": |
63 | 34 | main()
|
0 commit comments