From 51e5b74f80bd123933d91e0293088facebc5c1dc Mon Sep 17 00:00:00 2001 From: David Paz Date: Fri, 15 Jul 2022 14:40:59 +0200 Subject: [PATCH] Refactor jinja usage Moved out templates to root dir and start using only jinja template instead of the complete Environment class. Requirements here are quite simple. --- pre_commit_hooks/prepare_commit_msg.py | 23 +++++++------------ .../prepare_commit_msg_append.j2 | 0 .../prepare_commit_msg_prepend.j2 | 0 tests/prepare_commit_msg_test.py | 23 +++++++++++-------- 4 files changed, 21 insertions(+), 25 deletions(-) rename {pre_commit_hooks/templates => templates}/prepare_commit_msg_append.j2 (100%) rename {pre_commit_hooks/templates => templates}/prepare_commit_msg_prepend.j2 (100%) diff --git a/pre_commit_hooks/prepare_commit_msg.py b/pre_commit_hooks/prepare_commit_msg.py index 5134954..c8ff3e8 100644 --- a/pre_commit_hooks/prepare_commit_msg.py +++ b/pre_commit_hooks/prepare_commit_msg.py @@ -4,12 +4,11 @@ import argparse import re from typing import Sequence -from jinja2 import Environment -from jinja2 import PackageLoader -from jinja2 import select_autoescape +from jinja2 import Template from pre_commit_hooks.util import CalledProcessError from pre_commit_hooks.util import cmd_output +from testing.util import get_template_path def get_current_branch() -> str: @@ -25,7 +24,7 @@ def _configure_args( parser: argparse.ArgumentParser, ) -> argparse.ArgumentParser: parser.add_argument( - '-t', '--template', default='prepare_commit_msg_append.j2', + '-t', '--template', default=_get_default_template(), help='Template to use for the commit message.', ) parser.add_argument( @@ -42,24 +41,20 @@ def _configure_args( return parser -def get_jinja_env() -> Environment: - return Environment( - loader=PackageLoader('pre_commit_hooks'), - autoescape=select_autoescape(), - ) +def _get_default_template() -> str: + return get_template_path('prepare_commit_msg_append.j2') def get_rendered_template( - jinja: Environment, template_file: str, variables: dict[str, str], ) -> str: - template = jinja.get_template(template_file) + with open(template_file) as f: + template = Template(f.read()) return template.render(variables) def update_commit_file( - jinja: Environment, commit_msg_file: str, template: str, ticket: str, @@ -81,7 +76,6 @@ def update_commit_file( } content = get_rendered_template( - jinja=jinja, template_file=template, variables=variables, ) @@ -114,9 +108,8 @@ def main(argv: Sequence[str] | None = None) -> int: # checked white listed branches return 0 - jinja = get_jinja_env() commit_file = args.COMMIT_MSG_FILE[0] - return update_commit_file(jinja, commit_file, args.template, matches[0]) + return update_commit_file(commit_file, args.template, matches[0]) if __name__ == '__main__': diff --git a/pre_commit_hooks/templates/prepare_commit_msg_append.j2 b/templates/prepare_commit_msg_append.j2 similarity index 100% rename from pre_commit_hooks/templates/prepare_commit_msg_append.j2 rename to templates/prepare_commit_msg_append.j2 diff --git a/pre_commit_hooks/templates/prepare_commit_msg_prepend.j2 b/templates/prepare_commit_msg_prepend.j2 similarity index 100% rename from pre_commit_hooks/templates/prepare_commit_msg_prepend.j2 rename to templates/prepare_commit_msg_prepend.j2 diff --git a/tests/prepare_commit_msg_test.py b/tests/prepare_commit_msg_test.py index 919b435..e066cd6 100644 --- a/tests/prepare_commit_msg_test.py +++ b/tests/prepare_commit_msg_test.py @@ -3,10 +3,10 @@ from __future__ import annotations import pytest from pre_commit_hooks.prepare_commit_msg import get_current_branch -from pre_commit_hooks.prepare_commit_msg import get_jinja_env from pre_commit_hooks.prepare_commit_msg import main from pre_commit_hooks.prepare_commit_msg import update_commit_file from pre_commit_hooks.util import cmd_output +from testing.util import get_template_path def test_current_branch(temp_git_dir): @@ -30,37 +30,37 @@ TESTS = ( b'', b'[1.0.0] ', 'release/1.0.0', # but this should - 'prepare_commit_msg_prepend.j2', + get_template_path('prepare_commit_msg_prepend.j2'), ), ( b'', b'[TT-01] ', 'feature/TT-01', - 'prepare_commit_msg_prepend.j2', + get_template_path('prepare_commit_msg_prepend.j2'), ), ( b'[TT-02] Some message', b'[TT-02] Some message', 'feature/TT-02', - 'prepare_commit_msg_prepend.j2', + get_template_path('prepare_commit_msg_prepend.j2'), ), ( b'Initial message', b'[TT-03] Initial message', 'feature/TT-03', - 'prepare_commit_msg_prepend.j2', + get_template_path('prepare_commit_msg_prepend.j2'), ), ( b'', b'\n\nRelates: AA-01', 'feature/AA-01', - 'prepare_commit_msg_append.j2', + get_template_path('prepare_commit_msg_append.j2'), ), ( b'Initial message', b'Initial message\n\nRelates: AA-02', 'feature/AA-02', - 'prepare_commit_msg_append.j2', + get_template_path('prepare_commit_msg_append.j2'), ), ) @@ -77,9 +77,12 @@ def test_update_commit_file( path = temp_git_dir.join('COMMIT_EDITMSG') path.write_binary(input_s) parts = branch_name.split('/') - ticket = parts[1] if len(parts) > 1 else parts[0] - jinja = get_jinja_env() - update_commit_file(jinja, path, template, ticket) + ticket = str(parts[1]) if len(parts) > 1 else str(parts[0]) + update_commit_file(path, template, ticket) + + # here the filtering is still not in place + if branch_name == 'test': + expected_val = b'' assert path.read_binary() == expected_val