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

Show on the map →

Called by none

Calls _tree, harvest_generation

Checks

  • assert len(rules) == 1, rules
  • 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"]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-012measuredtest_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
lines 132–132rules = harvest_generation(_tree(GEN), GEN)["generated"]lines 134–134assert len(rules) == 1, ruleslines 135–135rule = rules[0]lines 136–136assert rule["kind"] == "custom_command"lines 137–137assert rule["line"] == 4, f"the rule's own line, for a reader to go read it: {rule}"lines 138–138assert [o["name"] for o in rule["outputs"]] == ["error.c"], rule["outputs"]lines 139–139assert "generate_errors.pl" in [c["name"] for c in rule["command"]], rule["command"]lines 140–140assert "error.fmt" in [d["name"] for d in rule["depends"]], rule["depends"]

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

Show on the map →

Called by none

Calls _tree, harvest_generation

Checks

  • 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"]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-012measuredtest_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
lines 182–188src = b""" add_custom_target(mbedcrypto_generated_files_target DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/error.c ${CMAKE_CURRENT_BINARY_DIR}/version_features.c ) """lines 189–189rules = harvest_generation(_tree(src), src)["generated"]lines 191–191assert len(rules) == 1 and rules[0]["kind"] == "custom_target", ruleslines 192–192assert rules[0]["outputs"] == [], "a target produces no file of its own"lines 193–193assert [d["name"] for d in rules[0]["depends"]] == ["error.c", "version_features.c"]

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

Show on the map →

Called by none

Calls _tree, harvest_generation

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

requirementlinkedpath to the implementer
REQ-DDB-PIPE-012measuredtest_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
lines 272–278src = b""" add_custom_command( OUTPUT ${BIN}/ok.c COMMAND tool ${SRC}/.. ${SRC}/. ${SRC}/dir/ DEPENDS ${SRC}/.. ) """lines 279–279rule = harvest_generation(_tree(src), src)["generated"][0]lines 281–281assert [c["name"] for c in rule["command"] if c["name"]] == ["tool"], rule["command"]lines 282–282assert [d["name"] for d in rule["depends"] if d["name"]] == [], rule["depends"]

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

Show on the map →

Called by none

Calls _tree, harvest_generation

Checks

  • assert harvest_generation(_tree(NO_COMMANDS), NO_COMMANDS) == {}

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-012measuredtest_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
lines 172–172assert harvest_generation(_tree(NO_COMMANDS), NO_COMMANDS) == {}

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

Show on the map →

Called by none

Calls _tree, harvest_generation

Checks

  • assert len(commands) == 4, [r["line"] for r in commands]
  • assert "error.c" in produced and "version_features.c" in produced, produced
  • assert "generate_errors.pl" in generators, generators

Result: skipped

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-012inferredtest_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
lines 204–204repo = _MBEDTLS_REPOlines 205–205src = (repo / "library/CMakeLists.txt").read_bytes()lines 207–207rules = harvest_generation(_tree(src), src)["generated"]lines 208–208commands = [r for r in rules if r["kind"] == "custom_command"]lines 209–209produced = {o["name"] for r in commands for o in r["outputs"]}lines 211–211assert len(commands) == 4, [r["line"] for r in commands]lines 212–212assert "error.c" in produced and "version_features.c" in produced, producedlines 213–213generators = {c["name"] for r in commands for c in r["command"] if c["name"]}lines 214–214assert "generate_errors.pl" in generators, generators

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

Show on the map →

Called by none

Calls _tree, harvest_generation

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

requirementlinkedpath to the implementer
REQ-DDB-PIPE-012inferredtest_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, outputs

1 more lines, not shown

Activity · 10 steps
lines 293–293repo = _MBEDTLS_REPOlines 294–294outputs: set[str] = set()lines 295–295◇ for f in repo.rglob("CMakeLists.txt")lines 296–296src = f.read_bytes()lines 297–297◇ for rule in harvest_generation(_tree(src), src).get("generated", [])lines 298–298outputs |= {o["name"] for o in rule["outputs"] if o["name"]}lines 300–300assert outputs, "precondition: the tree does hold generation rules"lines 301–301bad = {o for o in outputs if o.isupper() or o in {".", ".."} or "/" in o}lines 302–302assert not bad, f"these are not filenames: {sorted(bad)}"lines 303–303assert "error.c" in outputs, outputs

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

Show on the map →

Called by none

Calls _tree, harvest_generation

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

requirementlinkedpath to the implementer
REQ-DDB-PIPE-012measuredtest_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
lines 238–247src = 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 ) """lines 248–248rule = harvest_generation(_tree(src), src)["generated"][0]lines 250–250assert [o["name"] for o in rule["outputs"]] == ["real.c"], rule["outputs"]lines 256–258assert [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']}" )lines 259–261assert not any("CMAKE_CURRENT_SOURCE_DIR" in c["literal"] for c in rule["command"]), ( f"the working directory leaked into COMMAND: {rule['command']}" )lines 262–262assert [d["name"] for d in rule["depends"]] == ["gen.pl"], rule["depends"]

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

Show on the map →

Called by none

Calls _tree, harvest_generation

Checks

  • assert out["literal"] == "${CMAKE_CURRENT_BINARY_DIR}/error.c", out
  • assert out["name"] == "error.c", out
  • assert 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

requirementlinkedpath to the implementer
REQ-DDB-PIPE-012measuredtest_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

Activity · 7 steps
lines 154–154rule = harvest_generation(_tree(GEN), GEN)["generated"][0]lines 156–156out = rule["outputs"][0]lines 157–157assert out["literal"] == "${CMAKE_CURRENT_BINARY_DIR}/error.c", outlines 158–158assert out["name"] == "error.c", outlines 160–160glob = next(d for d in rule["depends"] if "error_headers" in d["literal"])lines 161–161assert glob["name"] == "", f"a bare variable has no filename and must not be given one: {glob}"lines 162–162assert glob["unresolved"] is True, glob