mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-09 21:04:17 +00:00
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:
parent
ac248b5011
commit
51e5b74f80
4 changed files with 21 additions and 25 deletions
|
|
@ -4,12 +4,11 @@ import argparse
|
||||||
import re
|
import re
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
|
|
||||||
from jinja2 import Environment
|
from jinja2 import Template
|
||||||
from jinja2 import PackageLoader
|
|
||||||
from jinja2 import select_autoescape
|
|
||||||
|
|
||||||
from pre_commit_hooks.util import CalledProcessError
|
from pre_commit_hooks.util import CalledProcessError
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
from testing.util import get_template_path
|
||||||
|
|
||||||
|
|
||||||
def get_current_branch() -> str:
|
def get_current_branch() -> str:
|
||||||
|
|
@ -25,7 +24,7 @@ def _configure_args(
|
||||||
parser: argparse.ArgumentParser,
|
parser: argparse.ArgumentParser,
|
||||||
) -> argparse.ArgumentParser:
|
) -> argparse.ArgumentParser:
|
||||||
parser.add_argument(
|
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.',
|
help='Template to use for the commit message.',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
@ -42,24 +41,20 @@ def _configure_args(
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def get_jinja_env() -> Environment:
|
def _get_default_template() -> str:
|
||||||
return Environment(
|
return get_template_path('prepare_commit_msg_append.j2')
|
||||||
loader=PackageLoader('pre_commit_hooks'),
|
|
||||||
autoescape=select_autoescape(),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_rendered_template(
|
def get_rendered_template(
|
||||||
jinja: Environment,
|
|
||||||
template_file: str,
|
template_file: str,
|
||||||
variables: dict[str, str],
|
variables: dict[str, str],
|
||||||
) -> str:
|
) -> str:
|
||||||
template = jinja.get_template(template_file)
|
with open(template_file) as f:
|
||||||
|
template = Template(f.read())
|
||||||
return template.render(variables)
|
return template.render(variables)
|
||||||
|
|
||||||
|
|
||||||
def update_commit_file(
|
def update_commit_file(
|
||||||
jinja: Environment,
|
|
||||||
commit_msg_file: str,
|
commit_msg_file: str,
|
||||||
template: str,
|
template: str,
|
||||||
ticket: str,
|
ticket: str,
|
||||||
|
|
@ -81,7 +76,6 @@ def update_commit_file(
|
||||||
}
|
}
|
||||||
|
|
||||||
content = get_rendered_template(
|
content = get_rendered_template(
|
||||||
jinja=jinja,
|
|
||||||
template_file=template,
|
template_file=template,
|
||||||
variables=variables,
|
variables=variables,
|
||||||
)
|
)
|
||||||
|
|
@ -114,9 +108,8 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||||
# checked white listed branches
|
# checked white listed branches
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
jinja = get_jinja_env()
|
|
||||||
commit_file = args.COMMIT_MSG_FILE[0]
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@ from __future__ import annotations
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.prepare_commit_msg import get_current_branch
|
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 main
|
||||||
from pre_commit_hooks.prepare_commit_msg import update_commit_file
|
from pre_commit_hooks.prepare_commit_msg import update_commit_file
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
from testing.util import get_template_path
|
||||||
|
|
||||||
|
|
||||||
def test_current_branch(temp_git_dir):
|
def test_current_branch(temp_git_dir):
|
||||||
|
|
@ -30,37 +30,37 @@ TESTS = (
|
||||||
b'',
|
b'',
|
||||||
b'[1.0.0] ',
|
b'[1.0.0] ',
|
||||||
'release/1.0.0', # but this should
|
'release/1.0.0', # but this should
|
||||||
'prepare_commit_msg_prepend.j2',
|
get_template_path('prepare_commit_msg_prepend.j2'),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
b'',
|
b'',
|
||||||
b'[TT-01] ',
|
b'[TT-01] ',
|
||||||
'feature/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',
|
||||||
b'[TT-02] Some message',
|
b'[TT-02] Some message',
|
||||||
'feature/TT-02',
|
'feature/TT-02',
|
||||||
'prepare_commit_msg_prepend.j2',
|
get_template_path('prepare_commit_msg_prepend.j2'),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
b'Initial message',
|
b'Initial message',
|
||||||
b'[TT-03] Initial message',
|
b'[TT-03] Initial message',
|
||||||
'feature/TT-03',
|
'feature/TT-03',
|
||||||
'prepare_commit_msg_prepend.j2',
|
get_template_path('prepare_commit_msg_prepend.j2'),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
b'',
|
b'',
|
||||||
b'\n\nRelates: AA-01',
|
b'\n\nRelates: AA-01',
|
||||||
'feature/AA-01',
|
'feature/AA-01',
|
||||||
'prepare_commit_msg_append.j2',
|
get_template_path('prepare_commit_msg_append.j2'),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
b'Initial message',
|
b'Initial message',
|
||||||
b'Initial message\n\nRelates: AA-02',
|
b'Initial message\n\nRelates: AA-02',
|
||||||
'feature/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 = temp_git_dir.join('COMMIT_EDITMSG')
|
||||||
path.write_binary(input_s)
|
path.write_binary(input_s)
|
||||||
parts = branch_name.split('/')
|
parts = branch_name.split('/')
|
||||||
ticket = parts[1] if len(parts) > 1 else parts[0]
|
ticket = str(parts[1]) if len(parts) > 1 else str(parts[0])
|
||||||
jinja = get_jinja_env()
|
update_commit_file(path, template, ticket)
|
||||||
update_commit_file(jinja, path, template, ticket)
|
|
||||||
|
# here the filtering is still not in place
|
||||||
|
if branch_name == 'test':
|
||||||
|
expected_val = b''
|
||||||
|
|
||||||
assert path.read_binary() == expected_val
|
assert path.read_binary() == expected_val
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue