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
//! Integration tests for the Rust coverage rule (#37).
//!
//! These run REAL `cargo llvm-cov` over the fixture crates via the SDK
//! (`coverage::measure_rust`) and assert pass/fail. Per the #3 guardrail the
//! *crates themselves* are the fixtures: `above` (every region and line
//! exercised by colocated inline tests) clears a 100 floor, `below` (one branch
//! arm left uncovered) fails 100 but clears a lower floor, and `exempt_cov`
//! clears 100 only once its untested shim is omitted by a `coverage` exemption.
//! Requires `cargo-llvm-cov`.
use std::path::PathBuf;
use testing_conventions::coverage::{measure_rust, Outcome, RustThresholds};
fn crate_dir(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/unit_coverage/rust")
.join(name)
}
const FULL: RustThresholds = RustThresholds {
regions: 100,
lines: 100,
};
const MID: RustThresholds = RustThresholds {
regions: 80,
lines: 80,
};
#[test]
fn above_passes_a_100_floor() {
assert_eq!(
measure_rust(&crate_dir("above"), FULL, &[]).unwrap(),
Outcome::Pass
);
}
#[test]
fn below_fails_a_100_floor() {
assert!(matches!(
measure_rust(&crate_dir("below"), FULL, &[]).unwrap(),
Outcome::Fail(_)
));
}
#[test]
fn below_passes_a_lower_floor() {
// `below` is ~88% regions / ~87% lines — under 100 (the uncovered `else` arm)
// but comfortably over an 80 floor, so the floor is a real, configurable knob.
assert_eq!(
measure_rust(&crate_dir("below"), MID, &[]).unwrap(),
Outcome::Pass
);
}
#[test]
fn a_coverage_exemption_omits_the_file_and_lets_the_floor_pass() {
// `exempt_cov` sits at ~75% only because of shim.rs (its `launch` is never
// exercised); omitting it — the `coverage`-rule exemption the CLI resolves
// from config — leaves core.rs, fully covered, to clear 100. Without the
// exemption this crate fails the floor (#32).
assert_eq!(
measure_rust(&crate_dir("exempt_cov"), FULL, &["src/shim.rs".to_string()]).unwrap(),
Outcome::Pass
);
}
#[test]
fn a_suite_that_cannot_run_is_an_error_not_a_silent_pass() {
// An empty directory is not a cargo crate; `cargo llvm-cov` exits non-zero, so
// measuring it must error rather than report a vacuous pass.
let empty = std::env::temp_dir().join(format!("tc-rust-empty-{}", std::process::id()));
std::fs::create_dir_all(&empty).unwrap();
let result = measure_rust(&empty, MID, &[]);
let _ = std::fs::remove_dir_all(&empty);
assert!(result.is_err());
}