testing-conventions 0.0.25

Enforce testing conventions in libraries (Python, TypeScript, and Rust).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Integration tests for patch (changed-line) coverage (Python — #132, parent #46).
//!
//! Every line a `<base>...HEAD` diff touches must be covered by the unit suite.
//! Each test builds a throwaway git repo (per the #3 guardrail the codebases are
//! the fixtures — red cases where a changed line is left uncovered, clean cases
//! where it isn't), runs REAL coverage.py over it via the SDK
//! (`patch_coverage::check`) and the CLI (`run`), and asserts the uncovered lines
//! / exit code. Requires `coverage` + `pytest` + `git` on PATH.
//!
//! Opens at RED per AGENTS.md: detection is stubbed (`check` reports nothing), so
//! a repo whose change leaves a line uncovered still comes back clean. The
//! implementation — diffing `<base>...HEAD` and intersecting the changed lines
//! with coverage.py's missing lines/branches — follows once CI witnesses these
//! red.

use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};

use testing_conventions::patch_coverage::check;
use testing_conventions::run;

/// A throwaway git repo, removed on drop. A test writes a baseline source + its
/// colocated test, `commit`s it, captures `head()` as the `base`, then mutates
/// and commits the "after" so `<base>...HEAD` is the change under test.
struct TempRepo(PathBuf);

impl TempRepo {
    fn new(slug: &str) -> Self {
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let root = std::env::temp_dir().join(format!(
            "tc-patch-cov-{}-{}-{}",
            slug,
            std::process::id(),
            COUNTER.fetch_add(1, Ordering::Relaxed),
        ));
        std::fs::create_dir_all(&root).unwrap();
        git(&root, &["init", "-q"]);
        git(&root, &["config", "user.email", "test@example.com"]);
        git(&root, &["config", "user.name", "Test"]);
        TempRepo(root)
    }

    /// Write `contents` to `rel`, creating parent directories.
    fn write(&self, rel: &str, contents: &str) {
        let path = self.0.join(rel);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, contents).unwrap();
    }

    /// Stage everything and commit, advancing HEAD.
    fn commit(&self, message: &str) {
        git(&self.0, &["add", "-A"]);
        git(
            &self.0,
            &["-c", "commit.gpgsign=false", "commit", "-q", "-m", message],
        );
    }

    /// The current HEAD SHA — captured as the `base` before mutating.
    fn head(&self) -> String {
        let out = Command::new("git")
            .args(["rev-parse", "HEAD"])
            .current_dir(&self.0)
            .output()
            .expect("git rev-parse should run");
        assert!(out.status.success(), "git rev-parse failed");
        String::from_utf8(out.stdout).unwrap().trim().to_string()
    }
}

impl Drop for TempRepo {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

fn git(dir: &Path, args: &[&str]) {
    let status = Command::new("git")
        .args(args)
        .current_dir(dir)
        .status()
        .expect("git should run");
    assert!(status.success(), "git {args:?} failed");
}

/// The uncovered changed lines for `<base>...HEAD` (no exemptions), as
/// `<file>:<line>` strings.
fn uncovered(repo: &TempRepo, base: &str) -> Vec<String> {
    check(&repo.0, base, &[])
        .expect("checking a readable repo should succeed")
        .iter()
        .map(|u| format!("{}:{}", u.file, u.line))
        .collect()
}

/// Result of `unit patch-coverage <repo> --language <lang> --base <base>
/// [--config <repo>/<config>]`, run in-process.
fn run_patch_coverage(
    repo: &TempRepo,
    language: &str,
    base: &str,
    config: Option<&str>,
) -> anyhow::Result<i32> {
    let mut argv: Vec<OsString> = vec![
        "testing-conventions".into(),
        "unit".into(),
        "patch-coverage".into(),
        repo.0.clone().into_os_string(),
        "--language".into(),
        language.into(),
        "--base".into(),
        base.into(),
    ];
    if let Some(name) = config {
        argv.push("--config".into());
        argv.push(repo.0.join(name).into_os_string());
    }
    run(argv)
}

/// Raw CLI invocation (after the program name), so a clap usage error can be
/// asserted rather than unwrapped away.
fn run_cli(args: &[&str]) -> anyhow::Result<i32> {
    let argv: Vec<OsString> = std::iter::once(OsString::from("testing-conventions"))
        .chain(args.iter().copied().map(OsString::from))
        .collect();
    run(argv)
}

/// Fully covered by `WIDGET_TEST_PY` at baseline (both branches taken).
const WIDGET_PY: &str = r#"def widget(n):
    if n > 0:
        return "pos"
    return "neg"
"#;
const WIDGET_TEST_PY: &str = r#"from widget import widget


def test_widget():
    assert widget(1) == "pos"
    assert widget(-1) == "neg"
"#;

/// Adds an `n == 42` branch the baseline test never exercises: line 5
/// (`return "answer"`) is a missing line and line 4 (`if n == 42:`) is the source
/// of a branch never taken — so both changed lines are uncovered.
const WIDGET_PY_UNCOVERED: &str = r#"def widget(n):
    if n > 0:
        return "pos"
    if n == 42:
        return "answer"
    return "neg"
"#;

// ---- Detection via the SDK (`check`) -------------------------------------

#[test]
fn python_uncovered_changed_line_is_reported() {
    // The core red case: the change adds a line (and a branch) the suite never
    // runs, so both new lines come back uncovered.
    let repo = TempRepo::new("uncovered");
    repo.write("widget.py", WIDGET_PY);
    repo.write("widget_test.py", WIDGET_TEST_PY);
    repo.commit("base");
    let base = repo.head();

    repo.write("widget.py", WIDGET_PY_UNCOVERED);
    repo.commit("add an untested branch");

    let reported = uncovered(&repo, &base);
    assert!(
        reported.contains(&"widget.py:5".to_string()),
        "the uncovered new line should be reported; got: {reported:?}"
    );
    assert!(
        reported.contains(&"widget.py:4".to_string()),
        "the uncovered new branch source should be reported; got: {reported:?}"
    );
}

#[test]
fn python_covered_change_is_clean() {
    // Editing a line the suite already exercises keeps the change fully covered.
    let repo = TempRepo::new("covered");
    repo.write("widget.py", WIDGET_PY);
    repo.write("widget_test.py", WIDGET_TEST_PY);
    repo.commit("base");
    let base = repo.head();

    repo.write(
        "widget.py",
        r#"def widget(n):
    if n > 0:
        return "positive"
    return "neg"
"#,
    );
    repo.write(
        "widget_test.py",
        r#"from widget import widget


def test_widget():
    assert widget(1) == "positive"
    assert widget(-1) == "neg"
"#,
    );
    repo.commit("reword a covered line and update its test");

    assert!(
        uncovered(&repo, &base).is_empty(),
        "a change to a covered line is clean"
    );
}

#[test]
fn a_change_touching_no_python_is_clean() {
    // A diff with no `.py` source returns clean immediately — there's no changed
    // line to measure, so the suite isn't even run.
    let repo = TempRepo::new("no-py");
    repo.write("widget.py", WIDGET_PY);
    repo.write("widget_test.py", WIDGET_TEST_PY);
    repo.write("README.md", "# project\n");
    repo.commit("base");
    let base = repo.head();

    repo.write("README.md", "# project\n\nnow with docs\n");
    repo.commit("docs only");

    assert!(
        uncovered(&repo, &base).is_empty(),
        "a change that touches no Python source is clean"
    );
}

#[test]
fn python_added_untested_file_changed_lines_are_uncovered() {
    // Unlike co-change (#33), an *added* file's new lines ARE patch-coverage
    // subjects: a brand-new source the suite never imports is wholly uncovered.
    let repo = TempRepo::new("added");
    repo.write("widget.py", WIDGET_PY);
    repo.write("widget_test.py", WIDGET_TEST_PY);
    repo.commit("base");
    let base = repo.head();

    repo.write("lonely.py", "def lonely():\n    return 41\n");
    repo.commit("add a brand-new untested source");

    let reported = uncovered(&repo, &base);
    assert!(
        reported.contains(&"lonely.py:1".to_string()),
        "the added file's uncovered lines should be reported; got: {reported:?}"
    );
}

#[test]
fn python_changed_comment_line_is_not_a_subject() {
    // A comment isn't executable, so coverage has nothing to measure on it — a
    // changed comment line is never flagged.
    let repo = TempRepo::new("comment");
    repo.write("widget.py", WIDGET_PY);
    repo.write("widget_test.py", WIDGET_TEST_PY);
    repo.commit("base");
    let base = repo.head();

    repo.write(
        "widget.py",
        r#"def widget(n):
    # branch on the sign of n
    if n > 0:
        return "pos"
    return "neg"
"#,
    );
    repo.commit("add an explanatory comment");

    assert!(
        uncovered(&repo, &base).is_empty(),
        "a changed comment line has nothing to cover"
    );
}

#[test]
fn an_unknown_base_ref_is_an_error() {
    // A base that can't be resolved must surface, never silently pass as "clean".
    let repo = TempRepo::new("bad-base");
    repo.write("widget.py", WIDGET_PY);
    repo.write("widget_test.py", WIDGET_TEST_PY);
    repo.commit("base");

    assert!(
        check(&repo.0, "no-such-ref", &[]).is_err(),
        "an unresolvable base ref must error"
    );
}

// ---- Exit codes via the CLI (`run`) --------------------------------------

#[test]
fn python_subcommand_exits_nonzero_on_an_uncovered_changed_line() {
    let repo = TempRepo::new("cli-red");
    repo.write("widget.py", WIDGET_PY);
    repo.write("widget_test.py", WIDGET_TEST_PY);
    repo.commit("base");
    let base = repo.head();
    repo.write("widget.py", WIDGET_PY_UNCOVERED);
    repo.commit("add an untested branch");

    assert_eq!(run_patch_coverage(&repo, "python", &base, None).unwrap(), 1);
}

#[test]
fn python_subcommand_exits_zero_when_the_change_is_covered() {
    let repo = TempRepo::new("cli-clean");
    repo.write("widget.py", WIDGET_PY);
    repo.write("widget_test.py", WIDGET_TEST_PY);
    repo.commit("base");
    let base = repo.head();
    repo.write(
        "widget.py",
        r#"def widget(n):
    if n > 0:
        return "positive"
    return "neg"
"#,
    );
    repo.write(
        "widget_test.py",
        r#"from widget import widget


def test_widget():
    assert widget(1) == "positive"
    assert widget(-1) == "neg"
"#,
    );
    repo.commit("reword a covered line and update its test");

    assert_eq!(run_patch_coverage(&repo, "python", &base, None).unwrap(), 0);
}

// ---- Exemptions (#32 machinery, rule `coverage`) -------------------------

#[test]
fn python_a_coverage_exemption_lifts_an_uncovered_changed_line() {
    // A `coverage` exemption omits a file from the run, so its changed lines have
    // nothing to cover — the same waiver the floor (#26) honors.
    let repo = TempRepo::new("exempt");
    repo.write(
        "testing-conventions.toml",
        "[[python.exempt]]\npath = \"shim.py\"\nrules = [\"coverage\"]\n\
         reason = \"thin launcher; logic lives in tested modules\"\n",
    );
    repo.write("widget.py", WIDGET_PY);
    repo.write("widget_test.py", WIDGET_TEST_PY);
    repo.write("shim.py", "def shim():\n    return 0\n");
    repo.commit("base");
    let base = repo.head();

    repo.write("shim.py", "def shim():\n    return 1\n");
    repo.commit("edit the untested launcher");

    // Flagged with no config…
    assert_eq!(run_patch_coverage(&repo, "python", &base, None).unwrap(), 1);
    // …and lifted by the `coverage` exemption.
    assert_eq!(
        run_patch_coverage(&repo, "python", &base, Some("testing-conventions.toml")).unwrap(),
        0
    );
}

// ---- CLI surface & errors ------------------------------------------------

#[test]
fn patch_coverage_requires_language() {
    let err = run_cli(&["unit", "patch-coverage", "/tmp", "--base", "HEAD"])
        .expect_err("--language is required");
    let clap_err = err
        .downcast_ref::<clap::Error>()
        .expect("a missing required flag should surface as a clap::Error");
    assert_eq!(
        clap_err.kind(),
        clap::error::ErrorKind::MissingRequiredArgument
    );
}

#[test]
fn patch_coverage_rejects_rust() {
    // Rust patch coverage (`cargo llvm-cov`) is a separate item.
    let repo = TempRepo::new("rust-reject");
    repo.write("lib.rs", "pub fn f() {}\n");
    repo.commit("base");
    let base = repo.head();

    let err = run_patch_coverage(&repo, "rust", &base, None).unwrap_err();
    assert!(err.to_string().contains("separate item"), "got: {err}");
}

#[test]
fn patch_coverage_rejects_typescript() {
    // The TypeScript twin is a later slice.
    let repo = TempRepo::new("ts-reject");
    repo.write("widget.ts", "export const f = () => 1;\n");
    repo.commit("base");
    let base = repo.head();

    let err = run_patch_coverage(&repo, "typescript", &base, None).unwrap_err();
    assert!(err.to_string().contains("separate item"), "got: {err}");
}