Files
odoo-docs/tests/main.py
Antoine Vandevenne (anv) cc24435e3f [IMP] tests: enable additional resource file-related checkers
The following custom checkers were never run by the
`ci/documentation_guidelines` build and had to be run locally with
`make review`:
- `check_image_size`: Check that images are not larger than the maximum
  file size allowed for their extension.
- `check_image_color_depth`: Check that PNG images are compressed to
  8-bit color depth with PNGQuant.
- `check_resource_file_name`: Check that resource file names use hyphens
   rather than underscores.

Since reviewers systematically perform these checks manually, it makes
sense to include them in the standard test suite. Should a check raise
a false positive (e.g., an `example_db.zip` resource file is added and
hyphens should not be used), the red CI can be safely ignored as it is
not required for merging.

closes odoo/documentation#14717

X-original-commit: 41f2e20636
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
2025-09-30 17:13:08 +00:00

93 lines
4.0 KiB
Python

import argparse
import os
import re
import sys
from itertools import chain
from unittest.mock import patch
import sphinxlint
import checkers
CUSTOM_RST_DIRECTIVES = [
'card', 'cards', # cards
'example', 'exercise', # custom_admonitions
'spoiler', # spoilers
'tab', 'tabs', 'group-tab', 'code-tab', # sphinx_tabs
]
ADDITIONAL_CHECKERS = [
checkers.resource_files.check_image_size,
checkers.resource_files.check_image_color_depth,
checkers.resource_files.check_resource_file_name,
checkers.resource_files.check_resource_file_referenced,
]
def run_additional_checks(argv=None):
_enabled_checkers, args = sphinxlint.parse_args(argv)
for path in chain.from_iterable(sphinxlint.walk(path, args.ignore) for path in args.paths):
if 'content/' in path and not path.endswith('.rst'): # Leave root and locale files alone.
for checker in ADDITIONAL_CHECKERS:
checker(path)
"""
The following built-in checkers are enabled for `make test`:
- backtick-before-role: Search for roles preceded by a backtick.
- bad-dedent: Check for mis-alignment in indentation in code blocks.
- carriage-return: Check for carriage returns (\r) in lines.
- directive-missing-colons: Search for directive wrongly typed as comments.
- directive-with-three-dots: Search for directives with three dots instead of two.
- horizontal-tab: Check for horizontal tabs (\t) in lines.
- hyperlink-reference-missing-backtick: Search for missing backticks in front of hyperlink
references.
- missing-backtick-after-role: Search for roles missing their closing backticks.
- missing-colon-in-role: Search for missing colons in roles.
- missing-final-newline: Check that the last line of the file ends with a newline.
- missing-space-after-literal: Search for inline literals immediately followed by a character.
- missing-space-after-role: Search for roles immediately followed by a character.
- missing-space-before-default-role: Search for missing spaces before default role.
- missing-space-before-role: Search for missing spaces before roles.
- missing-space-in-hyperlink: Search for hyperlinks missing a space.
- missing-underscore-after-hyperlink: Search for hyperlinks missing underscore after their closing
backtick.
- python-syntax: Search invalid syntax in Python examples.
- role-with-double-backticks: Search for roles with double backticks.
- role-without-backticks: Search roles without backticks.
- trailing-whitespace: Check for trailing whitespaces at end of lines.
- unbalanced-inline-literals-delimiters: Search for unbalanced inline literals delimiters.
All the custom checkers defined in checkers/* files are also enabled, except for the following ones,
which are only enabled for `make review`:
- line-too-long: Check for line lengths.
- early-line-break: Check for early line breaks.
"""
if __name__ == '__main__':
# Patch sphinxlint's global constants to include our custom directives and parse their content.
with patch(
'sphinxlint.DIRECTIVES_CONTAINING_RST',
sphinxlint.DIRECTIVES_CONTAINING_RST + CUSTOM_RST_DIRECTIVES,
), patch(
'sphinxlint.DIRECTIVES_CONTAINING_RST_RE',
'(' + '|'.join(sphinxlint.DIRECTIVES_CONTAINING_RST) + ')',
), patch(
'sphinxlint.ALL_DIRECTIVES',
'(' + '|'.join(sphinxlint.DIRECTIVES_CONTAINING_RST
+ sphinxlint.DIRECTIVES_CONTAINING_ARBITRARY_CONTENT)
+ ')',
), patch(
'sphinxlint.seems_directive_re',
re.compile(rf"^\s*(?<!\.)\.\. {sphinxlint.ALL_DIRECTIVES}([^a-z:]|:(?!:))"),
), patch(
'sphinxlint.three_dot_directive_re',
re.compile(rf'\.\.\. {sphinxlint.ALL_DIRECTIVES}::'),
):
parser = argparse.ArgumentParser()
if os.getenv('REVIEW') == '1': # Enable checkers for `make review`.
setattr(sphinxlint.check_line_too_long, 'enabled', True)
setattr(checkers.rst_style.check_early_line_breaks, 'enabled', True)
run_additional_checks()
sys.exit(sphinxlint.main())