Skip to content

Commit

Permalink
Unpin rstcheck dependency and update to version 6.x
Browse files Browse the repository at this point in the history
- Removed the version pin(5.0.0) for rstcheck to allow updates to the latest version (6.x).
- Updated the rstcheck.py script to handle changes in the rstcheck-core API and output format.

Signed-off-by: Ganesh Hubale <ganeshhubale03@gmail.com>
  • Loading branch information
ganeshhubale committed Jan 11, 2025
1 parent 07c85f9 commit 1718db1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 56 deletions.
79 changes: 25 additions & 54 deletions tests/checkers/rstcheck.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,34 @@
"""Sanity test using rstcheck and sphinx."""
from __future__ import annotations

import re
import subprocess
from rstcheck_core.runner import RstcheckMainRunner
from rstcheck_core.config import RstcheckConfig
import pathlib
import sys


def main():
paths = sys.argv[1:] or sys.stdin.read().splitlines()

encoding = 'utf-8'

ignore_substitutions = (
'br',
# Define the paths to check (passed as CLI arguments or from stdin)
paths = [pathlib.Path(p) for p in (sys.argv[1:] or sys.stdin.read().splitlines())]

# Define the configuration for rstcheck
config = RstcheckConfig(
ignore_roles=[
"ansplugin", "ansopt", "ansretval", "ansval", "ansenvvar", "ansenvvarref"
],
ignore_substitutions=["br"],
report_level="warning", # Adjust report level as needed -> ["info": 1, "warning": 2, "error": 3,"severe": 4, "none": 5,]
recursive=True, # Set to True to check directories recursively
)

cmd = [
sys.executable,
'-c', 'import rstcheck; rstcheck.main();',
'--report', 'warning',
'--ignore-roles', 'ansplugin,ansopt,ansretval,ansval,ansenvvar,ansenvvarref',
'--ignore-substitutions', ','.join(ignore_substitutions),
] + paths

process = subprocess.run(cmd,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
)

if process.stdout:
raise Exception(process.stdout)

pattern = re.compile(r'^(?P<path>[^:]*):(?P<line>[0-9]+): \((?P<level>INFO|WARNING|ERROR|SEVERE)/[0-4]\) (?P<message>.*)$')

results = parse_to_list_of_dict(pattern, process.stderr.decode(encoding))

for result in results:
print('%s:%s:%s: %s' % (result['path'], result['line'], 0, result['message']))


def parse_to_list_of_dict(pattern, value):
matched = []
unmatched = []

for line in value.splitlines():
match = re.search(pattern, line)

if match:
matched.append(match.groupdict())
else:
unmatched.append(line)

if unmatched:
raise Exception('Pattern "%s" did not match values:\n%s' % (pattern, '\n'.join(unmatched)))
# Initialize the runner
runner = RstcheckMainRunner(
check_paths=paths,
rstcheck_config=config,
overwrite_config=True,
)

return matched
# Run the checks
exit_code = runner.run()

# Exit with the appropriate code
sys.exit(exit_code)

if __name__ == '__main__':
if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion tests/constraints-base.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Known limitations for indirect/transitive dependencies.

rstcheck < 6 # rstcheck 6.x has problem with rstcheck.core triggered by include files w/ sphinx directives https://github.com/rstcheck/rstcheck-core/issues/3
rstcheck>=6.2.4
sphinx-rtd-theme>=2.0.0 # Fix 404 pages with new sphinx -- https://github.com/ansible/ansible-documentation/issues/678
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ requests==2.32.3
# via sphinx
resolvelib==1.0.1
# via -r tests/requirements-relaxed.in
rstcheck==5.0.0
rstcheck>=6.2.4
# via
# -c tests/constraints-base.in
# -r tests/requirements-relaxed.in
Expand Down

0 comments on commit 1718db1

Please sign in to comment.