test_cmake_generation.py¶
tests/test_cmake_generation.py
THE RICHEST FACT IN A CMakeLists.txt WAS BEING DISCARDED.
test_a_custom_command_is_harvested
None test_cmake_generation.test_a_custom_command_is_harvested()
A generation rule is harvested with its output, command and inputs.
tests/test_cmake_generation.py:128
Checks
assert len(rules) == 1, rulesassert rule["kind"] == "custom_command"assert rule["line"] == 4, f"the rule's own line, for a reader to go read it: {rule}"assert [o["name"] for o in rule["outputs"]] == ["error.c"], rule["outputs"]assert "generate_errors.pl" in [c["name"] for c in rule["command"]], rule["command"]assert "error.fmt" in [d["name"] for d in rule["depends"]], rule["depends"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-012 | measured | test_a_custom_command_is_harvested → harvest_generation |
Source · 15 lines
def test_a_custom_command_is_harvested() -> None:
"""The three keyword sections are a FLAT argument list in the grammar, so they are split on
the keywords rather than read as structure.
"""
rules = harvest_generation(_tree(GEN), GEN)["generated"]
assert len(rules) == 1, rules
rule = rules[0]
assert rule["kind"] == "custom_command"
assert rule["line"] == 4, f"the rule's own line, for a reader to go read it: {rule}"
assert [o["name"] for o in rule["outputs"]] == ["error.c"], rule["outputs"]
assert "generate_errors.pl" in [c["name"] for c in rule["command"]], rule["command"]
assert "error.fmt" in [d["name"] for d in rule["depends"]], rule["depends"]
Activity · 8 steps
test_a_custom_target_is_harvested_as_a_target
None test_cmake_generation.test_a_custom_target_is_harvested_as_a_target()
add_custom_target's DEPENDS are harvested as the aggregate they are.
tests/test_cmake_generation.py:177
Checks
assert len(rules) == 1 and rules[0]["kind"] == "custom_target", rulesassert rules[0]["outputs"] == [], "a target produces no file of its own"assert [d["name"] for d in rules[0]["depends"]] == ["error.c", "version_features.c"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-012 | measured | test_a_custom_target_is_harvested_as_a_target → harvest_generation |
Source · 19 lines
def test_a_custom_target_is_harvested_as_a_target() -> None:
"""`add_custom_target(... DEPENDS a b c)` names no OUTPUT — it is the aggregate that forces
the rules above it to run, and mbedtls has two. Recording it as a `custom_command` would
claim the target is a generated FILE.
"""
src = b"""
add_custom_target(mbedcrypto_generated_files_target
DEPENDS
${CMAKE_CURRENT_BINARY_DIR}/error.c
${CMAKE_CURRENT_BINARY_DIR}/version_features.c
)
"""
rules = harvest_generation(_tree(src), src)["generated"]
assert len(rules) == 1 and rules[0]["kind"] == "custom_target", rules
assert rules[0]["outputs"] == [], "a target produces no file of its own"
assert [d["name"] for d in rules[0]["depends"]] == ["error.c", "version_features.c"]
Activity · 5 steps
test_a_directory_reference_yields_no_filename
None test_cmake_generation.test_a_directory_reference_yields_no_filename()
A directory reference is not a filename.
tests/test_cmake_generation.py:267
Checks
assert [c["name"] for c in rule["command"] if c["name"]] == ["tool"], rule["command"]assert [d["name"] for d in rule["depends"] if d["name"]] == [], rule["depends"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-012 | measured | test_a_directory_reference_yields_no_filename → harvest_generation |
Source · 18 lines
def test_a_directory_reference_yields_no_filename() -> None:
"""`${CMAKE_CURRENT_SOURCE_DIR}/..` has the basename `..`, which named a generated file in
the first run over the real tree. `.` and `..` are directory references and a trailing slash
means the argument is a directory, so none of them is a filename.
"""
src = b"""
add_custom_command(
OUTPUT ${BIN}/ok.c
COMMAND tool ${SRC}/.. ${SRC}/. ${SRC}/dir/
DEPENDS ${SRC}/..
)
"""
rule = harvest_generation(_tree(src), src)["generated"][0]
assert [c["name"] for c in rule["command"] if c["name"]] == ["tool"], rule["command"]
assert [d["name"] for d in rule["depends"] if d["name"]] == [], rule["depends"]
Activity · 4 steps
test_a_file_with_no_rules_harvests_nothing
None test_cmake_generation.test_a_file_with_no_rules_harvests_nothing()
A file with no generation rules yields nothing, not an empty claim.
tests/test_cmake_generation.py:167
Checks
assert harvest_generation(_tree(NO_COMMANDS), NO_COMMANDS) == {}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-012 | measured | test_a_file_with_no_rules_harvests_nothing → harvest_generation |
Source · 8 lines
def test_a_file_with_no_rules_harvests_nothing() -> None:
"""An empty payload rather than a `generated: []` row per file, for the reason
`_ScriptSymbolHarvester` gives about partitions: a row per file recording that a C file
generates nothing is 40,000 rows of nothing on a large target.
"""
assert harvest_generation(_tree(NO_COMMANDS), NO_COMMANDS) == {}
Activity · 1 steps
test_the_real_mbedtls_cmakelists_yields_all_four_generators
None test_cmake_generation.test_the_real_mbedtls_cmakelists_yields_all_four_generators()
The real mbedtls file yields every rule in it.
tests/test_cmake_generation.py:199
Checks
assert len(commands) == 4, [r["line"] for r in commands]assert "error.c" in produced and "version_features.c" in produced, producedassert "generate_errors.pl" in generators, generators
Result: skipped
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-012 | inferred | test_the_real_mbedtls_cmakelists_yields_all_four_generators → harvest_generation |
Source · 21 lines
def test_the_real_mbedtls_cmakelists_yields_all_four_generators() -> None:
"""PARSED FROM THE FROZEN CHECKOUT, not a fixture, because the fixture is mine and the file
is theirs. Four `add_custom_command` rules and three distinct generators across two
languages — a harvest written for mbedtls Q4's one mark would have missed three of them.
"""
repo = _MBEDTLS_REPO
src = (repo / "library/CMakeLists.txt").read_bytes()
rules = harvest_generation(_tree(src), src)["generated"]
commands = [r for r in rules if r["kind"] == "custom_command"]
produced = {o["name"] for r in commands for o in r["outputs"]}
assert len(commands) == 4, [r["line"] for r in commands]
assert "error.c" in produced and "version_features.c" in produced, produced
generators = {c["name"] for r in commands for c in r["command"] if c["name"]}
assert "generate_errors.pl" in generators, generators
# ─── keywords I did not list, found by running on the whole tree ─────────────
Activity · 9 steps
test_the_whole_mbedtls_tree_yields_only_plausible_outputs
None test_cmake_generation.test_the_whole_mbedtls_tree_yields_only_plausible_outputs()
The real mbedtls tree yields no nonsense output names.
tests/test_cmake_generation.py:288
Checks
assert outputs, "precondition: the tree does hold generation rules"assert not bad, f"these are not filenames: {sorted(bad)}"assert "error.c" in outputs, outputs
Result: skipped
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-012 | inferred | test_the_whole_mbedtls_tree_yields_only_plausible_outputs → harvest_generation |
Source · 16 lines
def test_the_whole_mbedtls_tree_yields_only_plausible_outputs() -> None:
"""THE CHECK THAT CAUGHT IT, kept as a test. One file parsed cleanly and the tree did not:
15 distinct outputs, of which `WORKING_DIRECTORY` and `..` were not files. Asserted over
EVERY CMakeLists in the checkout, because a per-file test is what let this through.
"""
repo = _MBEDTLS_REPO
outputs: set[str] = set()
for f in repo.rglob("CMakeLists.txt"):
src = f.read_bytes()
for rule in harvest_generation(_tree(src), src).get("generated", []):
outputs |= {o["name"] for o in rule["outputs"] if o["name"]}
assert outputs, "precondition: the tree does hold generation rules"
bad = {o for o in outputs if o.isupper() or o in {".", ".."} or "/" in o}
assert not bad, f"these are not filenames: {sorted(bad)}"
assert "error.c" in outputs, outputs1 more lines, not shown
Activity · 10 steps
test_unlisted_keywords_close_the_section
None test_cmake_generation.test_unlisted_keywords_close_the_section()
An unlisted CMake keyword does not leak into a section.
tests/test_cmake_generation.py:222
Checks
assert [o["name"] for o in rule["outputs"]] == ["real.c"], rule["outputs"]assert [c["name"] for c in rule["command"] if c["name"]] == ["gen.pl"], (f"WORKING_DIRECTORY and its value must not become part of COMMAND: {rule['command']}" )assert not any("CMAKE_CURRENT_SOURCE_DIR" in c["literal"] for c in rule["command"]), (f"the working directory leaked into COMMAND: {rule['command']}" )assert [d["name"] for d in rule["depends"]] == ["gen.pl"], rule["depends"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-012 | measured | test_unlisted_keywords_close_the_section → harvest_generation |
Source · 43 lines
def test_unlisted_keywords_close_the_section() -> None:
"""FOUND BY INDEXING ALL OF mbedtls, not by reading one file. The first version listed only
OUTPUT, COMMAND and DEPENDS, so every OTHER `add_custom_command` keyword was treated as a
value of whichever section preceded it. Across the real tree that produced two nonsense
outputs: `WORKING_DIRECTORY` and `..`.
`add_custom_command` has a dozen keywords — WORKING_DIRECTORY, COMMENT, VERBATIM,
MAIN_DEPENDENCY, BYPRODUCTS, IMPLICIT_DEPENDS, DEPFILE, JOB_POOL, USES_TERMINAL,
COMMAND_EXPAND_LISTS, APPEND — and enumerating only the three this harvest wants is how the
other nine became data.
SO AN UNKNOWN ALL-CAPS TOKEN ALSO CLOSES THE SECTION. CMake keywords are uppercase by
convention and the grammar does not mark them, so the conservative rule is that a bare
uppercase word ends the current section rather than joining it. A keyword added by a future
CMake then costs a dropped section, not a fabricated filename.
"""
src = b"""
add_custom_command(
OUTPUT ${BIN}/real.c
COMMAND ${PERL} gen.pl
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..
COMMENT "Generating real.c"
VERBATIM
DEPENDS gen.pl
)
"""
rule = harvest_generation(_tree(src), src)["generated"][0]
assert [o["name"] for o in rule["outputs"]] == ["real.c"], rule["outputs"]
## FILTERED TO NAMED ENTRIES, because `${PERL}` is a legitimate COMMAND argument — the
## interpreter — recorded with an empty name and `unresolved: True`. An earlier version of
## this assertion compared the whole list and failed on that record, which was the test being
## wrong rather than the code: the property is that WORKING_DIRECTORY's value is absent, not
## that the command holds exactly one entry.
assert [c["name"] for c in rule["command"] if c["name"]] == ["gen.pl"], (
f"WORKING_DIRECTORY and its value must not become part of COMMAND: {rule['command']}"
)
assert not any("CMAKE_CURRENT_SOURCE_DIR" in c["literal"] for c in rule["command"]), (
f"the working directory leaked into COMMAND: {rule['command']}"
)
assert [d["name"] for d in rule["depends"]] == ["gen.pl"], rule["depends"]
Activity · 6 steps
test_variables_are_recorded_not_resolved
None test_cmake_generation.test_variables_are_recorded_not_resolved()
A variable reference is kept verbatim and never resolved.
tests/test_cmake_generation.py:145
Checks
assert out["literal"] == "${CMAKE_CURRENT_BINARY_DIR}/error.c", outassert out["name"] == "error.c", outassert glob["name"] == "", f"a bare variable has no filename and must not be given one: {glob}"assert glob["unresolved"] is True, glob
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-012 | measured | test_variables_are_recorded_not_resolved → harvest_generation |
Source · 20 lines
def test_variables_are_recorded_not_resolved() -> None:
"""`${CMAKE_CURRENT_BINARY_DIR}/error.c` depends on the configure step this harvest never
ran. Resolving it would publish a path that may not exist; dropping it would lose the only
name the mark wants. So both are kept: the literal, and the basename.
`${error_headers}` is a bare glob variable with no basename at all, and it must be recorded
as unresolved rather than reported as a file — the `absence is not emptiness` rule applied to
a build variable.
"""
rule = harvest_generation(_tree(GEN), GEN)["generated"][0]
out = rule["outputs"][0]
assert out["literal"] == "${CMAKE_CURRENT_BINARY_DIR}/error.c", out
assert out["name"] == "error.c", out
glob = next(d for d in rule["depends"] if "error_headers" in d["literal"])
assert glob["name"] == "", f"a bare variable has no filename and must not be given one: {glob}"
assert glob["unresolved"] is True, glob