test_rustdoc.py

tests/test_rustdoc.py

Tests for clew/rustdoc.py — the rustdoc-JSON analog of doxygen.py.

test_discover_targets_documents_both_lib_and_same_named_bin

None test_rustdoc.test_discover_targets_documents_both_lib_and_same_named_bin(Path tmp_path)

A package with both a lib and a same-named bin documents both, not just the lib.

tests/test_rustdoc.py:193

Show on the map →

Called by none

Calls _discover_targets

Checks

  • assert kinds == {("dual", "lib"), ("dual", "bin")}, (f"a package with both a lib and a same-named bin must document both; got {kinds}" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-006measuredtest_discover_targets_documents_both_lib_and_same_named_bin → _discover_targets → clean_subprocess_env
Source · 32 lines
def test_discover_targets_documents_both_lib_and_same_named_bin(tmp_path: Path) -> None:
    """RUSTDOC_INTEGRATION_FEEDBACK.md, finding 1: `knots` and `tools_sqc` both pair a
    `[lib]` with a same-named `[[bin]]`, and the old lib-instead-of-bin selection silently
    dropped every module reachable only from the bin (`main.rs`, CLI parsing, config
    loading, output formatting) with no error — `cargo metadata` succeeds either way, so
    nothing in the build log said a target was skipped. This crate reproduces that exact
    shape and asserts both targets come back.

    @brief A package with both a lib and a same-named bin documents both, not just the lib.
    @return None.
    @version 1
    """
    if shutil.which("cargo") is None:
        pytest.skip("needs cargo")
    (tmp_path / "Cargo.toml").write_text(
        '[package]\nname = "dual"\nversion = "0.1.0"\nedition = "2021"\n\n'
        '[lib]\nname = "dual"\n\n[[bin]]\nname = "dual"\npath = "src/main.rs"\n',
        encoding="utf-8",
    )
    src = tmp_path / "src"
    src.mkdir()
    (src / "lib.rs").write_text("pub fn helper() {}\n", encoding="utf-8")
    (src / "main.rs").write_text("fn main() {}\n", encoding="utf-8")

    targets = _discover_targets(tmp_path)

    kinds = {(t.name, t.kind) for t in targets}
    assert kinds == {("dual", "lib"), ("dual", "bin")}, (
        f"a package with both a lib and a same-named bin must document both; got {kinds}"
    )

Activity · 10 steps
lines 205–205◇ if shutil.which("cargo") is Nonelines 206–206pytest.skip("needs cargo")lines 207–211(tmp_path / "Cargo.toml").write_text( '[package]\nname = "dual"\nversion = "0.1.0"\nedition = "2021"\n\n' '[lib]\nname = "dual"\n\n[[bin]]\nname = "dual"\npath = "src/main.rs"\n', encoding="utf-8", )lines 212–212src = tmp_path / "src"lines 213–213src.mkdir()lines 214–214(src / "lib.rs").write_text("pub fn helper() {}\n", encoding="utf-8")lines 215–215(src / "main.rs").write_text("fn main() {}\n", encoding="utf-8")lines 217–217targets = _discover_targets(tmp_path)lines 219–219kinds = {(t.name, t.kind) for t in targets}lines 220–222assert kinds == {("dual", "lib"), ("dual", "bin")}, ( f"a package with both a lib and a same-named bin must document both; got {kinds}" )

test_run_rustdoc_against_real_crate

None test_rustdoc.test_run_rustdoc_against_real_crate(Path tmp_path)

The end-to-end contract: a real `cargo +nightly rustdoc` run against a real crate lands a doxygen-shaped database with the right path/memberdef rows — the same shape `tests/richdb.py` hand-makes for the C fixture.

tests/test_rustdoc.py:233

Show on the map →

Called by none

Calls run_rustdoc

Checks

  • assert result == db_path
  • assert {"add", "helper", "MAX_RETRIES"} <= names
  • assert brief == "Adds two numbers together."
  • assert add_static == 0 # pub fn
  • assert helper_static == 1 # private fn
  • assert path_row[0] == "src/main.rs"
  • assert conn.execute("SELECT COUNT(*) FROM compounddef").fetchone()[0] == 0

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-006measuredtest_run_rustdoc_against_real_crate → run_rustdoc → _require_nightly_rustdoc → clean_subprocess_env
Source · 37 lines
def test_run_rustdoc_against_real_crate(tmp_path: Path) -> None:
    """The end-to-end contract: a real `cargo +nightly rustdoc` run against a
    real crate lands a doxygen-shaped database with the right path/memberdef
    rows — the same shape `tests/richdb.py` hand-makes for the C fixture.
    """
    db_path = tmp_path / "rustdoc.db"
    result = run_rustdoc(RUSTSAMPLE, db_path)
    assert result == db_path
    conn = sqlite3.connect(str(db_path))
    try:
        names = {row[0] for row in conn.execute("SELECT name FROM memberdef")}
        assert {"add", "helper", "MAX_RETRIES"} <= names

        brief = conn.execute(
            "SELECT briefdescription FROM memberdef WHERE name = 'add'"
        ).fetchone()[0]
        assert brief == "Adds two numbers together."

        add_static, helper_static = (
            conn.execute("SELECT static FROM memberdef WHERE name = ?", (name,)).fetchone()[0]
            for name in ("add", "helper")
        )
        assert add_static == 0  # pub fn
        assert helper_static == 1  # private fn

        path_row = conn.execute(
            "SELECT p.name FROM memberdef m JOIN path p ON p.rowid = m.file_id WHERE m.name = 'add'"
        ).fetchone()
        assert path_row[0] == "src/main.rs"

        # Every downstream stage that guards with _table_exists (dispatch_edges.py)
        # still gets a real, empty compounddef table rather than a missing one.
        assert conn.execute("SELECT COUNT(*) FROM compounddef").fetchone()[0] == 0
    finally:
        conn.close()

Activity · 15 steps
lines 238–238db_path = tmp_path / "rustdoc.db"lines 239–239result = run_rustdoc(RUSTSAMPLE, db_path)lines 240–240assert result == db_pathlines 241–241conn = sqlite3.connect(str(db_path))lines 242–242trylines 243–243names = {row[0] for row in conn.execute("SELECT name FROM memberdef")}lines 244–244assert {"add", "helper", "MAX_RETRIES"} <= nameslines 246–248brief = conn.execute( "SELECT briefdescription FROM memberdef WHERE name = 'add'" ).fetchone()[0]lines 249–249assert brief == "Adds two numbers together."lines 251–254add_static, helper_static = ( conn.execute("SELECT static FROM memberdef WHERE name = ?", (name,)).fetchone()[0] for name in ("add", "helper") )lines 255–255assert add_static == 0lines 256–256assert helper_static == 1lines 258–260path_row = conn.execute( "SELECT p.name FROM memberdef m JOIN path p ON p.rowid = m.file_id WHERE m.name = 'add'" ).fetchone()lines 261–261assert path_row[0] == "src/main.rs"lines 265–265assert conn.execute("SELECT COUNT(*) FROM compounddef").fetchone()[0] == 0

test_run_rustdoc_refuses_a_non_cargo_repo

None test_rustdoc.test_run_rustdoc_refuses_a_non_cargo_repo(Path tmp_path)

tests/test_rustdoc.py:225

Show on the map →

Called by none

Calls _nightly_rustdoc_available, run_rustdoc

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-006measuredtest_run_rustdoc_refuses_a_non_cargo_repo → run_rustdoc → _require_nightly_rustdoc → clean_subprocess_env
Source · 8 lines
def test_run_rustdoc_refuses_a_non_cargo_repo(tmp_path: Path) -> None:
    if not _nightly_rustdoc_available():
        pytest.skip("needs cargo + a nightly toolchain")
    with pytest.raises(RustdocUnavailableError):
        run_rustdoc(tmp_path, tmp_path / "out.db")


@pytestmark_nightly