Skip to content

Commit a090dc6

Browse files
authored
Added in an action to ensure _template_config.json and template files align. (#61)
* added alignment checks * improved error message * exluded directories * fixed error message * corrected job name * fixed align_templates * fixed templating_scripting * demonstrate failing test with missing template file * demonstrate failing test with missing config entry * restored functional code
1 parent bf1ca99 commit a090dc6

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Ensure templates/ dir and config file are aligned.
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- templates/**
7+
8+
jobs:
9+
10+
align-dirs:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout Repository
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
# templates/_templating_scripting.py only needs builtins to run.
19+
uses: actions/setup-python@v5
20+
21+
- name: Check templates/ dir
22+
id: check_dir
23+
run: templates/_templating_scripting.py check_dir

templates/_templating_scripting.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self):
3535

3636
for _template, _target_repos in config.items():
3737
template = TEMPLATES_DIR / _template
38-
assert template.is_file()
38+
assert template.is_file(), f"{template} does not exist."
3939
target_repos = [
4040
Config.TargetRepo(repo=repo, path_in_repo=Path(file_path))
4141
for repo, file_path in _target_repos.items()
@@ -277,6 +277,21 @@ def create_issue(title: str, body: str) -> None:
277277
continue
278278

279279

280+
def check_dir(args: argparse.Namespace) -> None:
281+
"""Ensures templates/ dir aligns with _templating_config.json.
282+
283+
This function is intended for running on the .github repo.
284+
"""
285+
286+
# Always passed (by common code), but never used in this routine.
287+
_ = args
288+
289+
templates = [Path(TEMPLATES_DIR, template_name) for template_name in TEMPLATES_DIR.rglob("*")]
290+
for template in templates:
291+
if template.is_file():
292+
assert template in CONFIG.templates, f"{template} is not in _templating_config.json"
293+
294+
280295
def main() -> None:
281296
parser = argparse.ArgumentParser(
282297
prog="TemplatingScripting",
@@ -303,8 +318,12 @@ def main() -> None:
303318
)
304319
prompt.set_defaults(func=prompt_share)
305320

306-
# TODO: command to check templates/ dir aligns with _templating_config.json.
307-
# Run this on PRs for the .github repo.
321+
check = subparsers.add_parser(
322+
"check_dir",
323+
description="Check templates/ dir aligns with _templating_config.json.",
324+
epilog="This command is intended for running on the .github repo."
325+
)
326+
check.set_defaults(func=check_dir)
308327

309328
parsed = parser.parse_args()
310329
parsed.func(parsed)

0 commit comments

Comments
 (0)