diff --git a/docs/development.md b/docs/development.md index 27cec890a1..6f43d470b9 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. 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. 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 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. 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 5216fe8ece..c95215cd4c 100644 --- a/xtask/src/test_lint.rs +++ b/xtask/src/test_lint.rs @@ -60,7 +60,9 @@ //! test bound (in scope) to such values, so nothing the code under test returned is checked; //! * `never-run`: a test that the `cfg` attributes on it, on the items around it or on the `mod` //! declarations above its file leave out of every target CI runs tests on (Linux and FreeBSD on -//! x86_64, and Windows for the app and the UI crate's print tests); +//! x86_64, and Windows for the app and the UI crate's print tests). A test that still builds on +//! macOS or Windows is listed without failing the check: it tests code or behavior of that +//! platform and runs locally there; //! * `exact-pixel`: in a file that uses egui, an assertion that a screen rect (an identifier `rect`, //! `rect_…` or `…_rect`, whole or through a corner, edge or size) equals numbers written out. //! @@ -77,8 +79,8 @@ //! `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` 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 +//! `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 //! 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 @@ -249,9 +251,17 @@ pub struct Hit { /// attributes of a `never-run` test, and the loop or spawn statement around a `sleep`. An /// 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. + pub report_only: bool, } impl Hit { + /// Whether the hit fails the check when it is on a changed line. + fn fails(&self) -> bool { + self.rule.fails() && !self.report_only + } + fn show(&self) -> String { let test = if self.test.is_empty() { String::new() } else { format!(" `{}`", self.test) }; format!("{}:{}: [{}]{test}: {}", self.path, self.line, self.rule.id(), self.note) @@ -528,7 +538,7 @@ impl Options { fn finish(counted: &[Hit], older: usize, files: usize, base: &str) -> Result<(), String> { let older = if older == 0 { String::new() } else { format!("; {older} hit(s) on unchanged lines of those files are not counted") }; - let (failing, reported): (Vec<&Hit>, Vec<&Hit>) = counted.iter().partition(|h| h.rule.fails()); + 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 {}", @@ -1414,7 +1424,7 @@ struct File<'a> { impl File<'_> { fn hit(&self, at: usize, rule: Rule, test: &str, note: String) -> Hit { let line = self.lines.of(at); - Hit { path: self.path.to_owned(), line, rule, test: test.to_owned(), note, first: line, last: line, gates: vec![] } + Hit { path: self.path.to_owned(), line, rule, test: test.to_owned(), note, first: line, last: line, gates: vec![], report_only: false } } fn test_at(&self, pos: usize) -> &str { @@ -4320,6 +4330,15 @@ const CI_TARGETS: &[Target] = &[Target { os: "linux", family: "unix", arch: "x86 /// The printing workflow also runs the app's tests and the UI crate's print tests on Windows. const WINDOWS_TARGET: Target = Target { os: "windows", family: "windows", arch: "x86_64" }; +/// Desktop targets that CI does not test on, by name. A test that builds on one of them tests code +/// or behavior of that platform. +const PLATFORM_TARGETS: &[(&str, Target)] = &[ + ("macOS", Target { os: "macos", family: "unix", arch: "aarch64" }), + ("macOS", Target { os: "macos", family: "unix", arch: "x86_64" }), + ("Windows", Target { os: "windows", family: "windows", arch: "x86_64" }), + ("Windows", Target { os: "windows", family: "windows", arch: "aarch64" }), +]; + fn windows_runs(path: &str) -> bool { path.starts_with("apps/vectorcraft/") || path == "crates/ui-egui/src/tests/printps.rs" } @@ -5060,7 +5079,14 @@ fn never_run(v: &View, ctx: &Ctx) -> Option { let off = |t: &Target| preds.iter().any(|p| cfg_value(p, t) == Some(false)); if CI_TARGETS.iter().chain(windows).all(off) { let shown: Vec = preds.iter().filter(|p| p.as_str() != "test").map(|p| format!("cfg({p})")).collect(); - let mut hit = v.whole(Rule::NeverRun, format!("{} leaves it out of every target CI tests on", shown.join(", "))); + let mut note = format!("{} leaves it out of every target CI tests on", shown.join(", ")); + let mut builds: Vec<&str> = PLATFORM_TARGETS.iter().filter(|(_, t)| !off(t)).map(|(name, _)| *name).collect(); + builds.dedup(); + if !builds.is_empty() { + note += &format!("; it builds on {}, so run it there", builds.join(" and ")); + } + let mut hit = v.whole(Rule::NeverRun, note); + hit.report_only = !builds.is_empty(); // Enclosing attributes count as separate lines, so an edit to code between them and the // test does not count. hit.gates = attr_lines.into_iter().filter(|&l| l < hit.first).collect(); @@ -6496,8 +6522,8 @@ mod tests { 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"]); - let mac = "#[cfg(target_os = \"macos\")]\n#[test]\nfn mac() {\n assert_eq!(f(), 1);\n}\n"; - assert!(finish(&lint_file("crates/x/src/tests.rs", mac, &ctx()), 0, 1, "base").is_err(), "never-run 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"; @@ -6971,6 +6997,25 @@ mod tests { assert_eq!(only("never-run", other), [], "a cfg on another item"); } + #[test] + fn never_run_lists_tests_that_build_on_macos_or_windows_without_failing() { + let test = |gate: &str| format!("#[cfg({gate})]\n#[test]\nfn t() {{\n assert_eq!(f(), 1);\n}}\n"); + let check = |path: &str, gate: &str| { + let found = lint_file(path, &test(gate), &ctx()); + assert_eq!(found.iter().filter(|h| h.rule == Rule::NeverRun).count(), 1, "{gate} is listed"); + finish(&found, 0, 1, "base") + }; + // A touchpad gesture test that egui can only drive on macOS. + assert!(check("crates/ui-egui/src/tests/quickpenwheel.rs", "target_os = \"macos\"").is_ok(), "macOS"); + // A test of performance counters that only Windows has. + assert!(check("apps/vectorcraft-cli/src/perf.rs", "windows").is_ok(), "Windows"); + assert!(check("crates/x/src/tests.rs", "all(target_os = \"macos\", target_arch = \"aarch64\")").is_ok(), "Apple silicon"); + assert!(check("crates/x/src/tests.rs", "target_os = \"ios\"").is_err(), "no desktop target builds it"); + assert!(check("crates/x/src/tests.rs", "all(windows, unix)").is_err(), "no target builds it"); + let found = lint_file("apps/vectorcraft-cli/src/perf.rs", &test("windows"), &ctx()); + assert!(found[0].note.ends_with("it builds on Windows, so run it there"), "{}", found[0].note); + } + #[test] fn module_gates_reads_each_candidate_once() { let paths: Vec = (0..32).map(|i| format!("crates/x/src/parent/child{i}.rs")).collect();