diff --git a/docs/development.md b/docs/development.md index 77e4b736a5..0e528cdab4 100644 --- a/docs/development.md +++ b/docs/development.md @@ -353,7 +353,7 @@ The app decides which cursors exist; whoever draws them works from the list. For ## Test lint -`cargo xtask test-lint` (part of `cargo xtask ci`) checks tests that a wrong result can pass, in the lines and tests changed since a base. It fails on an `assert_eq!` or `assert_ne!` of an expression with itself, such as `assert_eq!(x, x)` (`self-compare`), `#[ignore]` without a reason (`ignore-without-reason`), `thread::sleep` in test code (`sleep`), test code that reads Rust source outside the audits listed in `SOURCE_AUDITS` (`reads-source`), and a test that a `cfg` on it or on a `mod` above it excludes from every CI target that builds the crate (`never-run`). `never-run` reports a test only when it can evaluate the `cfg` on every such target; a predicate it does not evaluate, such as a `feature`, counts as unknown, so `#[cfg(all(feature = "gpu", not(feature = "gpu")))]` is not reported. A `never-run` test that still builds on macOS or Windows, such as a test of a macOS-only module, is listed without failing; run it locally on that platform. It lists the hits of the other rules without failing: a test whose only checks are that a value exists or a collection is not empty (`is_ok`, `is_some`, `!is_empty`, `len() > 0`: `only-weak`), `assert!(….all(…))` on a collection the test does not show to be non-empty (`all-unguarded`), a process test that checks only the exit status, a test with no assertion and no asserting helper, a silent early `return` on a value from the code under test, an environment variable that no CI leg sets, and the hits of `census`, `mirror`, `constant-echo`, `fixture-only` and `exact-pixel`. The base is `--base ` or `TEST_LINT_BASE`; in CI it is the first parent of the checked-out commit, and elsewhere the merge base with `upstream/main` or `origin/main`. Hits on unchanged lines are counted but do not fail it, except a `never-run` test in an unchanged file whose last CI route the change takes away: a changed `mod` declaration or `cfg` in a file that loads it, or a removed `mod` or `#[path]` declaration that was the last one loading it. A `// test-lint: allow ` comment exempts its own line and the line below it; `xtask/src/test_lint.rs` lists the rules and their ids. `cargo xtask test-lint --all` lists every hit in the workspace by crate and rule, and does not fail. +`cargo xtask test-lint` (part of `cargo xtask ci`) checks tests that a wrong result can pass, in the lines and tests changed since a base. It fails on a hit of any of its rules: an `assert_eq!` or `assert_ne!` of an expression with itself, such as `assert_eq!(x, x)` (`self-compare`), `#[ignore]` without a reason (`ignore-without-reason`), `thread::sleep` in test code (`sleep`), test code that reads Rust source outside the audits listed in `SOURCE_AUDITS` (`reads-source`), a test that a `cfg` on it or on a `mod` above it excludes from every CI target that builds the crate (`never-run`), a test whose only checks are that a value exists or a collection is not empty (`is_ok`, `is_some`, `!is_empty`, `len() > 0`: `only-weak`), `assert!(….all(…))` on a collection the test does not show to be non-empty (`all-unguarded`), a process test that checks only the exit status (`exit-status-only`), a test with no assertion and no asserting helper (`no-assertion`), a silent early `return` on a value from the code under test (`early-return`), an environment variable that no CI leg sets (`env-gate-unset`), and the hits of `census`, `mirror`, `constant-echo`, `fixture-only` and `exact-pixel`. `never-run` reports a test only when it can evaluate the `cfg` on every such target; a predicate it does not evaluate, such as a `feature`, counts as unknown, so `#[cfg(all(feature = "gpu", not(feature = "gpu")))]` is not reported. A `never-run` test that still builds on macOS or Windows, such as a test of a macOS-only module, is listed without failing; run it locally on that platform. The base is `--base ` or `TEST_LINT_BASE`; in CI it is the first parent of the checked-out commit, and elsewhere the merge base with `upstream/main` or `origin/main`. Hits on unchanged lines are counted but do not fail it, except a `never-run` test in an unchanged file whose last CI route the change takes away: a changed `mod` declaration or `cfg` in a file that loads it, or a removed `mod` or `#[path]` declaration that was the last one loading it. A `// test-lint: allow ` comment exempts its own line and the line below it; `xtask/src/test_lint.rs` lists the rules and their ids. `cargo xtask test-lint --all` lists every hit in the workspace by crate and rule, and does not fail. ## Tests diff --git a/xtask/src/test_lint.rs b/xtask/src/test_lint.rs index 44d73697e2..7bf34a56be 100644 --- a/xtask/src/test_lint.rs +++ b/xtask/src/test_lint.rs @@ -82,9 +82,9 @@ //! rules about a whole test it can be on any line from the line above the test attribute to the //! `fn` line. An allow without a reason exempts nothing. //! -//! `cargo xtask test-lint` fails on hits of `self-compare`, `ignore-without-reason`, `sleep`, -//! `reads-source` and `never-run` (except a test that builds on macOS or Windows) in the lines and -//! tests changed since a base, and lists the other rules' hits there without failing. The base is `--base ` or `TEST_LINT_BASE`; in GitHub Actions the first parent of `HEAD` (a pull request's +//! `cargo xtask test-lint` fails on a hit of any rule in the lines and tests changed since a base, +//! except a `never-run` test that builds on macOS or Windows, which it lists without failing. The +//! base is `--base ` or `TEST_LINT_BASE`; in GitHub Actions the first parent of `HEAD` (a pull request's //! merge commit is checked out, and a shallow checkout fetches that parent); elsewhere the merge //! base of `HEAD` and `upstream/main` or `origin/main`. Uncommitted and untracked files count as //! changed. Hits on unchanged lines are counted but do not fail it. `--all` lists every hit in the @@ -223,11 +223,6 @@ impl Rule { } } - /// Whether a hit on a changed line fails the command. The other rules only report. - pub fn fails(self) -> bool { - matches!(self, Rule::SelfCompare | Rule::IgnoreWithoutReason | Rule::Sleep | Rule::ReadsSource | Rule::NeverRun) - } - fn from_id(id: &str) -> Option { Rule::ALL.into_iter().find(|r| r.id() == id) } @@ -256,14 +251,15 @@ pub struct Hit { /// allow comment there does not exempt it. pub gates: Vec, /// A `never-run` hit on a test that builds on macOS or Windows, which CI does not test on: it is - /// listed with the report-only rules. + /// listed without failing the check. pub report_only: bool, } impl Hit { - /// Whether the hit fails the check when it is on a changed line. + /// Whether the hit fails the check when it is on a changed line: every hit does, except a + /// `never-run` test that builds on macOS or Windows. fn fails(&self) -> bool { - self.rule.fails() && !self.report_only + !self.report_only } fn show(&self) -> String { @@ -555,7 +551,7 @@ fn finish(counted: &[Hit], older: usize, files: usize, base: &str) -> Result<(), let (failing, reported): (Vec<&Hit>, Vec<&Hit>) = counted.iter().partition(|h| h.fails()); if !reported.is_empty() { println!( - "test-lint: {} hit(s) of report-only rules in the lines changed since {base} (they do not fail the check):\n {}", + "test-lint: {} hit(s) on tests that build only on macOS or Windows in the lines changed since {base} (run them there; they do not fail the check):\n {}", reported.len(), listing(&reported).join("\n ") ); @@ -6159,8 +6155,11 @@ mod tests { } #[test] - fn census_is_report_only() { - assert!(!Rule::Census.fails()); + fn census_fails_the_check() { + let census = "#[test]\nfn t() {\n for c in COLORS {\n assert!(c.name.starts_with('a'));\n }\n}\n"; + let hits = lint_file("crates/x/src/tests.rs", census, &ctx()); + assert_eq!(hits.iter().map(|h| h.rule).collect::>(), [Rule::Census]); + assert!(finish(&hits, 0, 1, "base").is_err(), "census fails the check"); assert_eq!(Rule::from_id("census"), Some(Rule::Census)); } @@ -7284,23 +7283,26 @@ mod tests { } #[test] - fn only_failing_rules_fail_the_check() { - let failing: Vec<&str> = Rule::ALL.into_iter().filter(|r| r.fails()).map(Rule::id).collect(); - assert_eq!(failing, ["self-compare", "ignore-without-reason", "sleep", "reads-source", "never-run"]); + fn every_rule_fails_the_check() { let ios = "#[cfg(target_os = \"ios\")]\n#[test]\nfn ios() {\n assert_eq!(f(), 1);\n}\n"; assert!(finish(&lint_file("crates/x/src/tests.rs", ios, &ctx()), 0, 1, "base").is_err(), "never-run fails the check"); let weak = "#[test]\nfn weak() {\n assert!(f().is_ok());\n}\n"; let unguarded = "#[test]\nfn all() {\n assert!(f().iter().all(|x| valid(x)));\n}\n"; let skipped = "#[test]\n#[ignore]\nfn skipped() {\n assert_eq!(f(), 1);\n}\n"; + let silent = "#[test]\nfn silent() {\n f();\n}\n"; let hits_of = |src: &str| lint_file("crates/x/src/tests.rs", src, &ctx()); - assert_eq!(finish(&hits_of(unguarded), 0, 1, "base"), Ok(()), "all-unguarded only reports"); - assert_eq!(finish(&hits_of(weak), 0, 1, "base"), Ok(()), "only-weak only reports"); - let all = hits_of(&format!("{weak}{unguarded}{skipped}")); - assert_eq!(all.len(), 3); - let Err(message) = finish(&all, 0, 1, "base") else { panic!("ignore-without-reason fails the check") }; - assert!(message.starts_with("1 test-lint hit(s)"), "{message}"); - assert!(message.contains("[ignore-without-reason] `skipped`"), "{message}"); - assert!(!message.contains("[only-weak]") && !message.contains("[all-unguarded]"), "{message}"); + assert!(finish(&hits_of(unguarded), 0, 1, "base").is_err(), "all-unguarded fails the check"); + assert!(finish(&hits_of(weak), 0, 1, "base").is_err(), "only-weak fails the check"); + assert!(finish(&hits_of(silent), 0, 1, "base").is_err(), "no-assertion fails the check"); + let all = hits_of(&format!("{weak}{unguarded}{skipped}{silent}")); + assert_eq!(all.len(), 4); + let Err(message) = finish(&all, 0, 1, "base") else { panic!("the hits fail the check") }; + assert!(message.starts_with("4 test-lint hit(s)"), "{message}"); + for listed in ["[only-weak] `weak`", "[all-unguarded] `all`", "[ignore-without-reason] `skipped`", "[no-assertion] `silent`"] { + assert!(message.contains(listed), "{listed} in {message}"); + } + let allowed = "#[test]\n// test-lint: allow only-weak the value is random\nfn weak() {\n assert!(f().is_ok());\n}\n"; + assert_eq!(finish(&hits_of(allowed), 0, 1, "base"), Ok(()), "an allow with a reason exempts the test"); assert_eq!(finish(&[], 3, 1, "base"), Ok(())); }