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.
This commit is contained in:
David Paz 2022-07-15 14:40:59 +02:00
parent ac248b5011
commit 51e5b74f80
No known key found for this signature in database
GPG key ID: 6600CC47B1C1B063
4 changed files with 21 additions and 25 deletions

View file

@ -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__':

View file

@ -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