diff --git a/tests/checkers/resource_files.py b/tests/checkers/resource_files.py index 82ad0145f..49f904d62 100644 --- a/tests/checkers/resource_files.py +++ b/tests/checkers/resource_files.py @@ -1,6 +1,34 @@ +from pathlib import Path + import sphinxlint +MAX_IMAGE_SIZES = { # in bytes + '.png': 505000, + '.gif': 2100000, +} + +def log_error(file, line, msg, checker_name): + """ Log an error in sphinx-lint's log format to ease the processing of linting errors on Runbot. + """ + print(f"{file}:{line or 0}: {msg} ({checker_name})") + + +def check_image_size(file): + """ Check that images are not larger than the maximum file size allowed for their extension. """ + file_path = Path(file) + file_size = file_path.stat().st_size + max_size = MAX_IMAGE_SIZES.get(file_path.suffix) + if max_size and file_size > max_size: + log_error( + file_path, + 0, + f"the file has a size of {round(file_size / 10**6, 2)} MB, larger than the maximum" + f" allowed size of {round(max_size / 10**6, 2)} MB; compress it with pngquant", + 'image-size', + ) + + @sphinxlint.checker('') def check_file_extensions(file, lines, options=None): """ Check that there is no file without extension. """ diff --git a/tests/main.py b/tests/main.py index c2a740930..30ed0b030 100644 --- a/tests/main.py +++ b/tests/main.py @@ -1,5 +1,6 @@ import re import sys +from itertools import chain from unittest.mock import patch import sphinxlint @@ -14,6 +15,13 @@ CUSTOM_RST_DIRECTIVES = [ 'tab', 'tabs', 'group-tab', 'code-tab', # sphinx_tabs ] + +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): + checkers.resource_files.check_image_size(path) + + """ The following checkers are selected. @@ -68,4 +76,5 @@ if __name__ == '__main__': 'sphinxlint.three_dot_directive_re', re.compile(rf'\.\.\. {sphinxlint.ALL_DIRECTIVES}::'), ): + run_additional_checks() sys.exit(sphinxlint.main())