Add new --validate option - #642
Conversation
CAOShurong
left a comment
There was a problem hiding this comment.
I tested the exact head c1259ab66ec7e4ee6964a901b1486a730f1029eb from source and from a clean wheel install on Python 3.13.1. The valid-template path exits 0 and writes {"valid": true}; malformed Jinja exits 1 and writes {"valid": false, ...}; normal rendering still works. I also ran all 36 non-runner test scripts, flake8, and the sdist/wheel build successfully.
One exit-code issue blocks approval. Both newly introduced rejected invocations print an error but exit 0:
python -m docxtpl valid.docx --report report.json
Error: --report requires --validate.
EXIT=0
python -m docxtpl missing.docx --validate
Error: The specified template_path "...missing.docx" is not valid.
EXIT=0
validate_all_args() raises RuntimeError, but main() catches it and returns normally; SystemExit(1) is reached only after run_validation() starts. A CI script therefore cannot distinguish a rejected validation request from a successful validation.
Please make validation-related argument/path errors exit nonzero and add automated CLI coverage for valid syntax, invalid syntax, a missing template, and --report without --validate. No test file currently exercises the new CLI behavior.
I used OpenAI Codex to help run the exact-head regression matrix and inspect the control flow; I personally checked the commands, outputs, exit codes, and this review.
This PR adds a new
--validateoption to the CLI tool, that allows to check whether a template has valid Jinja syntax without supplying any data JSON file.A
--report FILEoption is also added that can be used to write the validation report to the given file path, in JSON format.