From 2d076247b3a6cb01e5830ec4122c4d0ffbad96c8 Mon Sep 17 00:00:00 2001 From: Isaac Good Date: Mon, 9 Nov 2026 09:13:32 -0800 Subject: [PATCH] Test generator, `problem_spec_dir()`: remove code that walks the path. Merge changes from other tracks. This code checks if the current path is a child of the problem spec repo. The generator lives in the track repo, which is generally not nested inside the problem spec repo. The prior logic checks the XDG cache path, which is what `configlet` uses. Contributors ought to be running `configlet sync` so the path used by the configlet ought to exist and should be reliable. As such, an additional check is not needed No diffs in the generated files. --- bin/generate_tests | 122 ++++++++++-------- .../practice/isbn-verifier/.meta/template.j2 | 2 +- exercises/practice/isogram/.meta/template.j2 | 2 +- exercises/practice/leap/.meta/template.j2 | 2 +- exercises/practice/luhn/.meta/template.j2 | 2 +- .../matching-brackets/.meta/template.j2 | 2 +- exercises/practice/pangram/.meta/template.j2 | 2 +- exercises/practice/triangle/.meta/template.j2 | 2 +- 8 files changed, 78 insertions(+), 58 deletions(-) diff --git a/bin/generate_tests b/bin/generate_tests index 6dbb93f..c5ccbf3 100755 --- a/bin/generate_tests +++ b/bin/generate_tests @@ -26,21 +26,19 @@ def problem_spec_dir() -> pathlib.Path: raise LookupError("Could not find problem specs") -def flatten_cases(cases: list[dict]) -> list[tuple[list[str], dict]]: +def flatten_cases(cases: list[dict], included: list[str]) -> list[tuple[list[str], dict]]: """Recursive flatten test cases, returning individual cases with parent descriptions.""" for case_or_group in cases: if "cases" in case_or_group: - for groups, child_case in flatten_cases(case_or_group["cases"]): + for groups, child_case in flatten_cases(case_or_group["cases"], included): yield ([case_or_group["description"]] + groups, child_case) else: - yield ([], case_or_group) + if case_or_group["uuid"] in included: + yield ([], case_or_group) -def get_cases(specs: pathlib.Path, exercise: pathlib.Path) -> list[dict]: - """Return flattened, filtered cases with additional metadata attached.""" - canonical_path = specs / "exercises" / exercise.name / "canonical-data.json" - with open(canonical_path, "r", encoding="utf-8") as f: - canonical = json.load(f) +def get_test_uuids(exercise: pathlib.Path) -> list[str]: + """Return UUIDs of included test cases from tests.toml.""" with open(exercise / ".meta" / "tests.toml", "rb") as f: tests = tomllib.load(f) @@ -49,34 +47,70 @@ def get_cases(specs: pathlib.Path, exercise: pathlib.Path) -> list[dict]: for test in tests.values() if test.get("include", True) and "reimplements" in test } + + return [ + uuid for uuid, details in tests.items() + if details.get("include", True) and uuid not in reimplemented + ] + + +def update_test_case(case: dict, groups: list[str]) -> None: + """Update test case data in place for use by the templates.""" + case["descriptions"] = groups + [case["description"]] + case["expect_error"] = isinstance(case["expected"], dict) and "error" in case["expected"] + if case["expect_error"]: + case["expect_error_msg"] = case["expected"]["error"] + + +def get_cases(specs: pathlib.Path, exercise: pathlib.Path) -> list[dict]: + """Return flattened, filtered cases with additional metadata attached.""" + canonical_path = specs / "exercises" / exercise.name / "canonical-data.json" + with open(canonical_path, "r", encoding="utf-8") as f: + canonical = json.load(f) + cases = [] - for groups, case in flatten_cases(canonical["cases"]): - # Filter out test cases with include=false or not listed. - if case["uuid"] not in tests or case["uuid"] in reimplemented: - continue - if not tests[case["uuid"]].get("include", True): - continue - # Add metadata. - case["descriptions"] = groups + [case["description"]] - case["expect_error"] = isinstance(case["expected"], dict) and "error" in case["expected"] - if case["expect_error"]: - case["expect_error_msg"] = case["expected"]["error"] + for groups, case in flatten_cases(canonical["cases"], get_test_uuids(exercise)): + update_test_case(case, groups) cases.append(case) return cases -def filter_tojson(data, separators=(',', ':'), indent=None) -> str: +def get_grouped_cases(specs: pathlib.Path, exercise: pathlib.Path) -> list[dict]: + """Return filtered cases, in the original nested hierarchy, with additional metadata attached.""" + canonical_path = specs / "exercises" / exercise.name / "canonical-data.json" + with open(canonical_path, "r", encoding="utf-8") as f: + canonical = json.load(f) + + cases = canonical["cases"] + for groups, case in flatten_cases(cases, get_test_uuids(exercise)): + update_test_case(case, groups) + return cases + + +def quote(obj) -> str: + """Convert Python objects to native objects.""" + if obj is True: + return '"true"' + if obj is False: + return '"false"' + if isinstance(obj, str): + return shlex.quote(str) + if isinstance(obj, list): + return [quote(val) for val in obj] + raise ValueError(f"quote() does not support {type(obj)}") + +def tojson(data, separators=(',', ':'), indent=None) -> str: """Filter `tojson` that JSON encodes a string with flexible settings.""" return json.dumps(data, separators=separators, indent=indent) def jinja_env(exercise: pathlib.Path) -> jinja2.Environment: """Return a configured Jinja env with filters added.""" - env = jinja2.Environment(loader=jinja2.FileSystemLoader(exercise / ".meta")) + env = jinja2.Environment(loader=jinja2.FileSystemLoader(["bin", exercise / ".meta"])) # Shell quoting - env.filters["quote"] = shlex.quote + env.filters["quote"] = quote # JSON formatting, default to compact form (`jq -c`). - env.filters["tojson"] = filter_tojson + env.filters["tojson"] = tojson # String escaping, ANSI-C style. env.filters["repr"] = repr # Return a dict with only specified keys kepts. @@ -87,25 +121,9 @@ def jinja_env(exercise: pathlib.Path) -> jinja2.Environment: return env -def bool_to_str(obj): - """Recursively convert boolean values to strings in collections.""" - if isinstance(obj, dict): - return {key: bool_to_str(val) for key, val in obj.items()} - if isinstance(obj, list): - return [bool_to_str(val) for val in obj] - if obj is True: - return "true" - if obj is False: - return "false" - return obj - - def generate(specs: pathlib.Path, exercise: pathlib.Path) -> None: """Generate and write test file for a given spec and exercise.""" - cases = get_cases(specs, exercise) - for case in cases: - case["expected"] = bool_to_str(case["expected"]) - + config = json.loads((exercise / ".meta/config.json").read_text()) timestamp = datetime.datetime.now(tz=datetime.UTC).replace(microsecond=0).isoformat() header = textwrap.dedent(f"""\ #!/usr/bin/env bats @@ -115,21 +133,21 @@ def generate(specs: pathlib.Path, exercise: pathlib.Path) -> None: """ ).strip() data = { - "cases": cases, + "cases": get_cases(specs, exercise), "header": header, - "solution": json.loads((exercise / ".meta/config.json").read_text())["files"]["solution"][0] + "solution": config["files"]["solution"][0], } # Render the template. try: template = jinja_env(exercise).get_template("template.j2") - out = template.render(data) + out = template.render(data).strip() except jinja2.exceptions.TemplateAssertionError as e: e.add_note(f"Error rendering template for {exercise.name}") raise # Check for changes or the lack thereof. - test_file = exercise / json.loads((exercise / ".meta/config.json").read_text())["files"]["test"][0] + test_file = exercise / config["files"]["test"][0] if test_file.exists(): old_content = [i for i in test_file.read_text().strip().splitlines() if "generated on" not in i] new_content = [i for i in out.splitlines() if "generated on" not in i] @@ -143,10 +161,10 @@ def generate(specs: pathlib.Path, exercise: pathlib.Path) -> None: def argparser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser() parser.add_argument( - "--no-pull", - action="store_false", + "--pull", + action="store_true", dest="pull", - help="Do not run `git pull` on the problem specs repo", + help="Run `git pull` on the problem specs repo", ) parser.add_argument( "exercises", @@ -165,10 +183,12 @@ def main(): exercises = args.exercises # Generate all exercises with templates if none are specified as args. if not exercises: - exercises = [ - i.parent.parent - for i in pathlib.Path("exercises/practice").glob("*/.meta/template.j2") - ] + exercises = sorted( + { + i.parent.parent + for i in pathlib.Path("exercises/practice").glob("*/.meta/template.j2") + } + ) else: # Turn strings to paths and make them relative to the practice exercises. out = [] diff --git a/exercises/practice/isbn-verifier/.meta/template.j2 b/exercises/practice/isbn-verifier/.meta/template.j2 index dbfc95a..f1a2ff9 100644 --- a/exercises/practice/isbn-verifier/.meta/template.j2 +++ b/exercises/practice/isbn-verifier/.meta/template.j2 @@ -4,6 +4,6 @@ {% if loop.first %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip run gawk -f {{ solution }} <<< "{{ case["input"]["isbn"] }}" assert_success - assert_output "{{ case["expected"] }}" + assert_output {{ case["expected"] | quote }} } {% endfor %} diff --git a/exercises/practice/isogram/.meta/template.j2 b/exercises/practice/isogram/.meta/template.j2 index 75ee007..f13ea81 100644 --- a/exercises/practice/isogram/.meta/template.j2 +++ b/exercises/practice/isogram/.meta/template.j2 @@ -4,6 +4,6 @@ {% if loop.first %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip run gawk -f {{ solution }} <<< "{{ case["input"]["phrase"] }}" assert_success - assert_output "{{ case["expected"] }}" + assert_output {{ case["expected"] | quote }} } {% endfor %} diff --git a/exercises/practice/leap/.meta/template.j2 b/exercises/practice/leap/.meta/template.j2 index ee11a93..c3172dd 100644 --- a/exercises/practice/leap/.meta/template.j2 +++ b/exercises/practice/leap/.meta/template.j2 @@ -5,6 +5,6 @@ run gawk -f {{ solution }} <<< "{{ case["input"]["year"] }}" assert_success - assert_output "{{ case["expected"] }}" + assert_output {{ case["expected"] | quote }} } {% endfor %} diff --git a/exercises/practice/luhn/.meta/template.j2 b/exercises/practice/luhn/.meta/template.j2 index 4cfc1dc..146bc03 100644 --- a/exercises/practice/luhn/.meta/template.j2 +++ b/exercises/practice/luhn/.meta/template.j2 @@ -4,6 +4,6 @@ {% if loop.first %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip run gawk -f {{ solution }} <<< "{{ case["input"]["value"] }}" assert_success - assert_output "{{ case["expected"] }}" + assert_output {{ case["expected"] | quote }} } {% endfor %} diff --git a/exercises/practice/matching-brackets/.meta/template.j2 b/exercises/practice/matching-brackets/.meta/template.j2 index 9df6544..ac3c679 100644 --- a/exercises/practice/matching-brackets/.meta/template.j2 +++ b/exercises/practice/matching-brackets/.meta/template.j2 @@ -4,6 +4,6 @@ {% if loop.first %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip run gawk -f {{ solution }} <<< '{{ case["input"]["value"] }}' assert_success - assert_output "{{ case["expected"] }}" + assert_output {{ case["expected"] | quote }} } {% endfor %} diff --git a/exercises/practice/pangram/.meta/template.j2 b/exercises/practice/pangram/.meta/template.j2 index fa06633..11f2599 100644 --- a/exercises/practice/pangram/.meta/template.j2 +++ b/exercises/practice/pangram/.meta/template.j2 @@ -4,6 +4,6 @@ {% if loop.first %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip run gawk -f {{ solution }} <<< '{{ case["input"]["sentence"] }}' assert_success - assert_output "{{ case["expected"] }}" + assert_output {{ case["expected"] | quote }} } {% endfor %} diff --git a/exercises/practice/triangle/.meta/template.j2 b/exercises/practice/triangle/.meta/template.j2 index e6a5562..ed2f090 100644 --- a/exercises/practice/triangle/.meta/template.j2 +++ b/exercises/practice/triangle/.meta/template.j2 @@ -4,6 +4,6 @@ {% if loop.first %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip run gawk -f {{ solution }} -v type={{ case["property"] }} <<< "{{ case["input"]["sides"] | join(" ") }}" assert_success - assert_output "{{ case["expected"] }}" + assert_output {{ case["expected"] | quote }} } {% endfor %}