Skip to content
Closed

main #462

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 71 additions & 55 deletions bin/generate_tests
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,22 @@ def problem_spec_dir() -> pathlib.Path:
specs = pathlib.Path(cache_dir) / "exercism/configlet/problem-specifications"
if specs.exists():
return specs
cur = pathlib.Path(os.getcwd())
for i in cur.parents:
if i.name == "problem-specifications":
return i
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)

Expand All @@ -53,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.
Expand All @@ -91,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
Expand All @@ -119,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]
Expand All @@ -147,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",
Expand All @@ -169,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 = []
Expand Down
109 changes: 14 additions & 95 deletions exercises/practice/etl/test-etl.bats
Original file line number Diff line number Diff line change
@@ -1,114 +1,33 @@
#!/usr/bin/env bats
load bats-extra

@test 'single letter' {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run gawk -f etl.awk << END_INPUT
1: A
END_INPUT
# generated on 2026-11-04T19:33:58+00:00

@test "single letter" {
# [[ $BATS_RUN_SKIPPED == "true" ]] || skip
run gawk -f etl.awk <<< "{'1': ['A']}"
assert_success
assert_output "a,1"
assert_output "{'a': 1}"
}

@test 'single score with multiple letters' {
@test "single score with multiple letters" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run gawk -f etl.awk << END_INPUT
1: "A", "E", "I", "O", "U"
END_INPUT

run gawk -f etl.awk <<< "{'1': ['A', 'E', 'I', 'O', 'U']}"
assert_success
assert_line --index 0 -- "a,1"
assert_line --index 1 -- "e,1"
assert_line --index 2 -- "i,1"
assert_line --index 3 -- "o,1"
assert_line --index 4 -- "u,1"
assert_equal "${#lines[@]}" 5
assert_output "{'a': 1, 'e': 1, 'i': 1, 'o': 1, 'u': 1}"
}

@test 'a score with no letters' {
@test "multiple scores with multiple letters" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run gawk -f etl.awk << END_INPUT
1: A
7:
END_INPUT

run gawk -f etl.awk <<< "{'1': ['A', 'E'], '2': ['D', 'G']}"
assert_success
assert_output "a,1"
assert_output "{'a': 1, 'd': 2, 'e': 1, 'g': 2}"
}

@test 'multiple scores with multiple letters' {
@test "multiple scores with differing numbers of letters" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run gawk -f etl.awk << END_INPUT
1: "E", "A"
2:"D","G"
END_INPUT

run gawk -f etl.awk <<< "{'1': ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], '2': ['D', 'G'], '3': ['B', 'C', 'M', 'P'], '4': ['F', 'H', 'V', 'W', 'Y'], '5': ['K'], '8': ['J', 'X'], '10': ['Q', 'Z']}"
assert_success
assert_line --index 0 -- "a,1"
assert_line --index 1 -- "d,2"
assert_line --index 2 -- "e,1"
assert_line --index 3 -- "g,2"
assert_equal "${#lines[@]}" 4
assert_output "{'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10}"
}

@test 'multiple scores with multiple letters, blank lines' {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run gawk -f etl.awk << END_INPUT

2:"D","G"

1: "E", "A"
END_INPUT

assert_success
assert_line --index 0 -- "a,1"
assert_line --index 1 -- "d,2"
assert_line --index 2 -- "e,1"
assert_line --index 3 -- "g,2"
assert_equal "${#lines[@]}" 4
}

@test 'multiple scores with differing numbers of letters' {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

# there are tab characters below
run gawk -f etl.awk << END_INPUT
1: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"
2: "D", "G"
4: "F", "H", "V", "W", "Y"
3: "m", "B", "C", "P"
5: "K"
8: "J", "X"
10: "Q", "Z"
END_INPUT

assert_success
assert_line --index 0 -- a,1
assert_line --index 1 -- b,3
assert_line --index 2 -- c,3
assert_line --index 3 -- d,2
assert_line --index 4 -- e,1
assert_line --index 5 -- f,4
assert_line --index 6 -- g,2
assert_line --index 7 -- h,4
assert_line --index 8 -- i,1
assert_line --index 9 -- j,8
assert_line --index 10 -- k,5
assert_line --index 11 -- l,1
assert_line --index 12 -- m,3
assert_line --index 13 -- n,1
assert_line --index 14 -- o,1
assert_line --index 15 -- p,3
assert_line --index 16 -- q,10
assert_line --index 17 -- r,1
assert_line --index 18 -- s,1
assert_line --index 19 -- t,1
assert_line --index 20 -- u,1
assert_line --index 21 -- v,4
assert_line --index 22 -- w,4
assert_line --index 23 -- x,8
assert_line --index 24 -- y,4
assert_line --index 25 -- z,10
assert_equal "${#lines[@]}" 26
}
Loading
Loading