testing-conventions 0.0.63

Enforce testing conventions in libraries (Python, TypeScript, and Rust).
Documentation
//! Integration tests for the config schema + loader (issue #12).
//!
//! These pin the contract from the README's "Configuration" section: one config
//! file is read into the in-memory `Config`, and the self-guard rejects a config
//! that fails its own validation (unknown keys, malformed TOML) rather than
//! silently accepting it.
//!
//! Per the #3 guardrail, the loader ships a clean fixture (`valid.toml`, must
//! load) and red fixtures (`unknown_key.toml` / `malformed.toml`, must fail).

use std::path::PathBuf;

use testing_conventions::config::{
    load_config, Config, PythonConfig, PythonCoverage, Rule, RustConfig, RustCoverage,
    TypeScriptConfig, TypeScriptCoverage,
};

/// Absolute path to a file under `tests/fixtures/`.
fn fixture(name: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures")
        .join(name)
}

/// The in-memory shape we expect `valid.toml` to parse into.
fn expected_valid() -> Config {
    Config {
        python: Some(PythonConfig {
            coverage: Some(PythonCoverage {
                branch: true,
                fail_under: 100,
            }),
            exempt: vec![],
            build_command: None,
            reason: String::new(),
        }),
        typescript: Some(TypeScriptConfig {
            coverage: Some(TypeScriptCoverage {
                lines: 100,
                branches: 100,
                functions: 100,
                statements: 100,
            }),
            exempt: vec![],
            build_command: None,
            reason: String::new(),
        }),
        rust: Some(RustConfig {
            coverage: Some(RustCoverage {
                regions: Some(100),
                lines: 100,
                functions: None,
                branch: None,
            }),
            features: vec![],
            exempt: vec![],
            build_command: None,
            reason: String::new(),
        }),
        e2e: None,
    }
}

#[test]
fn loads_the_canonical_config_into_memory() {
    let config = load_config(fixture("valid.toml")).expect("the canonical config should load");
    assert_eq!(config, expected_valid());
}

#[test]
fn rejects_unknown_keys_self_guard() {
    let result = load_config(fixture("unknown_key.toml"));
    assert!(
        result.is_err(),
        "an unknown config key must be rejected (self-guard), got: {result:?}"
    );
}

#[test]
fn rejects_malformed_toml() {
    let result = load_config(fixture("malformed.toml"));
    assert!(
        result.is_err(),
        "malformed TOML must be rejected, got: {result:?}"
    );
}

#[test]
fn errors_on_a_missing_file() {
    let result = load_config(fixture("does_not_exist.toml"));
    assert!(
        result.is_err(),
        "a missing config file must be an error, got: {result:?}"
    );
}

#[test]
fn loads_exemptions_with_optional_coverage() {
    // `exempt.toml` declares exemptions but no coverage thresholds — both keys
    // are optional (issue #32).
    let config = load_config(fixture("exempt.toml")).expect("an exempt-only config should load");
    let python = config.python.expect("[python] table present");
    assert!(python.coverage.is_none(), "coverage is optional");
    // A whole-file presence exemption and a separate line-scoped coverage exemption for
    // the same file — `coverage` requires `lines`, so the two can't share one entry (#226).
    assert_eq!(python.exempt.len(), 2);
    assert_eq!(python.exempt[0].path, "src/cli.py");
    assert_eq!(python.exempt[0].rules, vec![Rule::ColocatedTest]);
    assert!(python.exempt[0].lines.is_empty());
    assert_eq!(python.exempt[1].rules, vec![Rule::Coverage]);
    assert_eq!(
        python.exempt[1].lines,
        vec![testing_conventions::config::LineSpec::Range(5, 6)]
    );
    assert_eq!(
        config.typescript.expect("[typescript] table").exempt[0].rules,
        vec![Rule::ColocatedTest]
    );
}

#[test]
fn rejects_an_exemption_without_a_reason_self_guard() {
    // The reason is required — a reasonless exemption can never be a silent pass.
    assert!(
        load_config(fixture("exempt_no_reason.toml")).is_err(),
        "an exemption missing its reason must be rejected (self-guard)"
    );
}

#[test]
fn rejects_an_exemption_with_a_blank_reason_self_guard() {
    // Distinct from a *missing* reason: the `reason` key is present but blank.
    // The loader's validation step must still reject it on load.
    assert!(
        load_config(fixture("exempt_empty_reason.toml")).is_err(),
        "an exemption with a blank reason must be rejected (self-guard)"
    );
}

#[test]
fn loads_a_python_build_command_with_a_reason() {
    // #289: `[python].build_command` (plus a required `reason`) is a valid config key. The
    // binary never runs it — detect derives it and the workflow's jobs do — but the schema
    // must accept it, since `deny_unknown_fields` otherwise rejects a consumer's config.
    let config = load_config(fixture("python_build_command.toml"))
        .expect("a [python].build_command with a reason should load");
    let python = config.python.expect("[python] table present");
    assert_eq!(
        python.build_command.as_deref(),
        Some("uv run maturin develop")
    );
    assert!(!python.reason.trim().is_empty(), "the reason must survive");
}

#[test]
fn a_python_build_command_with_no_reason_loads() {
    // #335: `build_command` needs no reason — it supplies a necessary build fact rather than
    // waiving a check, so a bare command (reason absent or blank) loads, where #289 rejected it.
    assert!(
        load_config(fixture("python_build_command_no_reason.toml")).is_ok(),
        "a [python].build_command with no reason must load (reason is optional)"
    );
    assert!(
        load_config(fixture("python_build_command_blank_reason.toml")).is_ok(),
        "a [python].build_command with a blank reason must load (reason is optional)"
    );
}

#[test]
fn loads_a_typescript_build_command_with_a_reason() {
    // #335: `build_command` generalizes from `[python]`-only to all three language tables — a
    // necessary build declaration for a build the manifest structurally can't express (npm defines
    // no standard build command; the TS compile-before-pack is a project-specific script `pnpm
    // pack` doesn't run). Started red: the schema had no `build_command` on the `[typescript]`
    // table, so `deny_unknown_fields` rejected it.
    let config = load_config(fixture("typescript_build_command.toml"))
        .expect("a [typescript].build_command must load once the schema generalizes");
    let ts = config.typescript.expect("[typescript] table present");
    assert_eq!(ts.build_command.as_deref(), Some("pnpm build"));
    // An optional reason note is retained when present, but never required.
    assert!(
        !ts.reason.trim().is_empty(),
        "the reason note survives when present"
    );
}

#[test]
fn loads_a_rust_build_command_with_a_reason() {
    // #335: the same generalization for `[rust]`.
    let config = load_config(fixture("rust_build_command.toml"))
        .expect("a [rust].build_command with a reason must load once the schema generalizes");
    let rust = config.rust.expect("[rust] table present");
    assert_eq!(
        rust.build_command.as_deref(),
        Some("cargo run --bin codegen")
    );
    assert!(!rust.reason.trim().is_empty(), "the reason must survive");
}

#[test]
fn a_typescript_build_command_with_a_blank_reason_loads() {
    // The reason is optional in every language: a bare `[typescript].build_command` (blank reason)
    // loads — naming the build is a necessary fact, not a waiver to justify.
    assert!(
        load_config(fixture("typescript_build_command_blank_reason.toml")).is_ok(),
        "a [typescript].build_command with a blank reason must load (reason is optional)"
    );
}

#[test]
fn loads_an_e2e_extra_scope_and_exclude_table() {
    // #333: `[e2e].extra_scope` / `exclude` are valid config keys. The binary never uses them —
    // detect renders them into repeated `--extra-scope`/`--exclude` arguments the e2e-verify job
    // appends — but the schema must accept the table, since `deny_unknown_fields` otherwise
    // rejects a consumer's config (exactly like `[python].build_command`). Starts red: the schema
    // has no `[e2e]` table yet, so the load errors until it does.
    let config = load_config(fixture("e2e_extra_scope.toml"))
        .expect("an [e2e] extra_scope/exclude config must load (the schema must accept the table)");
    let e2e = config.e2e.expect("[e2e] table present");
    assert_eq!(e2e.extra_scope, vec!["packages/rust/src"]);
    assert_eq!(
        e2e.exclude,
        vec!["packages/rust/src/cli", "packages/rust/src/bin"]
    );
}

#[test]
fn e2e_table_keys_are_optional() {
    // Both keys default to empty, so an `[e2e]` table setting just one (or neither) loads — the
    // zero-config shape a package that declares only extra_scope, or only exclude, produces.
    let config = load_config(fixture("e2e_extra_scope_only.toml"))
        .expect("an [e2e] table with only extra_scope should load");
    let e2e = config.e2e.expect("[e2e] table present");
    assert_eq!(e2e.extra_scope, vec!["packages/rust/src"]);
    assert!(e2e.exclude.is_empty(), "exclude defaults to empty");
}

#[test]
fn rejects_an_unknown_e2e_key_self_guard() {
    // `deny_unknown_fields` still guards the table — a typo'd key inside `[e2e]` is rejected,
    // not silently accepted.
    assert!(
        load_config(fixture("e2e_unknown_key.toml")).is_err(),
        "an unknown key under [e2e] must be rejected (self-guard)"
    );
}

#[test]
fn partial_coverage_tables_inherit_defaults() {
    // Each table sets only one field; the rest fall back to the language's default
    // floor (#216). Previously a partial table errored on the required fields.
    let config = load_config(fixture("partial_coverage.toml"))
        .expect("a partial coverage table should load, filling defaults");
    assert_eq!(
        config.python.expect("[python]").coverage.expect("coverage"),
        PythonCoverage {
            branch: true,
            fail_under: 90,
        }
    );
    assert_eq!(
        config
            .typescript
            .expect("[typescript]")
            .coverage
            .expect("coverage"),
        TypeScriptCoverage {
            lines: 100,
            branches: 90,
            functions: 100,
            statements: 100,
        }
    );
    assert_eq!(
        config.rust.expect("[rust]").coverage.expect("coverage"),
        RustCoverage {
            regions: Some(90),
            lines: 100,
            functions: None,
            branch: None,
        }
    );
}

#[test]
fn an_unknown_field_in_a_coverage_table_still_errors() {
    // Field defaults fill *missing* keys; a typo'd key is still rejected.
    assert!(
        load_config(fixture("unknown_coverage_field.toml")).is_err(),
        "an unknown key inside a coverage table must still be rejected"
    );
}