diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index c7ca7f8..44d45e2 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -45,6 +45,7 @@ A repository adopts the standard in four steps: - `toolchain`: `rust-toolchain.toml` pins `channel = "stable"` and installs at least the `rustfmt` and `clippy` components. - `rustfmt-config`: `rustfmt.toml` sets `edition = "2024"`, `newline_style = "Unix"` and `use_field_init_shorthand = true`. - `clippy-config`: `clippy.toml` sets `allow-expect-in-tests`, `allow-panic-in-tests` and `allow-dbg-in-tests` to `true`, and never `allow-unwrap-in-tests`. +- `nextest-config`: `.config/nextest.toml`'s default profile sets a `slow-timeout`, every `slow-timeout` in it has a `terminate-after`, and no profile or override sets `retries`. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 0566708..b089499 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -17,6 +17,7 @@ mod layout; mod level; mod manifest; mod modules; +mod nextest_config; mod report; mod rustfmt_config; mod settings; @@ -44,6 +45,7 @@ pub use fix::Fix; use layout::CargoConfig; use manifest::{Manifest, RootManifest}; use modules::Modules; +use nextest_config::NextestConfig; pub use report::{Report, RuleReport}; use settings::Settings; use source::{Source, Unparsed}; @@ -162,6 +164,10 @@ rules! { /// and `allow-dbg-in-tests` to `true`, and never /// `allow-unwrap-in-tests`. ClippyConfig = "clippy-config" => clippy_config, + /// `.config/nextest.toml`'s default profile sets a `slow-timeout`, every + /// `slow-timeout` in it has a `terminate-after`, and no profile or + /// override sets `retries`. + NextestConfig = "nextest-config" => nextest_config, } /// Why the rules couldn't be checked. @@ -333,6 +339,8 @@ struct Repository { scripts: Vec, /// The manifests, justfiles and workflows, with their comments. commented: Vec, + /// `.config/nextest.toml`, empty when missing. + nextest: TomlFile, } impl Repository { @@ -356,6 +364,7 @@ impl Repository { commented: Commented::load_all(root)?, scripts: TextFile::load_all(root, &cargo_locked::FILES)?, agent_files: AgentFiles::load(root)?, + nextest: TomlFile::load_or_empty(root, nextest_config::PATH)?, metadata, sources, unparsed, diff --git a/crates/sourceorigin-standard/src/rules/nextest_config.rs b/crates/sourceorigin-standard/src/rules/nextest_config.rs new file mode 100644 index 0000000..717a91a --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/nextest_config.rs @@ -0,0 +1,84 @@ +//! `nextest-config`: a hung test fails, and a flaky one isn't retried. + +use std::collections::BTreeMap; + +use serde::Deserialize; +use toml::Spanned; + +use super::toml_file::TomlFile; +use super::{Finding, Repository}; + +/// nextest's configuration, from the repository's root. +pub(super) const PATH: &str = ".config/nextest.toml"; + +/// `.config/nextest.toml`, reduced to its profiles. +#[derive(Default, Deserialize)] +pub(super) struct NextestConfig { + #[serde(default)] + profile: BTreeMap, +} + +/// A profile, or one of its overrides. +#[derive(Deserialize)] +struct Profile { + #[serde(rename = "slow-timeout")] + slow_timeout: Option>, + retries: Option>, + #[serde(default)] + overrides: Vec, +} + +impl Profile { + /// It and its overrides. + fn all(&self) -> impl Iterator { + std::iter::once(self).chain(&self.overrides) + } +} + +impl TomlFile { + /// A default profile without a `slow-timeout`, each `slow-timeout` + /// without `terminate-after`, and each `retries`. + fn findings(&self) -> Vec { + let finding = |line, message: String| Finding { + path: PATH.into(), + line, + message, + }; + let mut findings = Vec::new(); + let default = self.value.profile.get("default"); + if default.is_none_or(|profile| profile.slow_timeout.is_none()) { + findings.push(finding( + 1, + "`[profile.default]` sets no `slow-timeout`: add \ + `slow-timeout = { period = \"60s\", terminate-after = 5 }`" + .to_owned(), + )); + } + for profile in self.value.profile.values().flat_map(Profile::all) { + if let Some(timeout) = &profile.slow_timeout + && timeout.get_ref().get("terminate-after").is_none() + { + findings.push(finding( + self.line(timeout), + "a `slow-timeout` without `terminate-after` lets a hung test run forever: \ + add `terminate-after`" + .to_owned(), + )); + } + if let Some(retries) = &profile.retries { + findings.push(finding( + self.line(retries), + "`retries` hides flaky tests: remove it, and fix the test".to_owned(), + )); + } + } + findings + } +} + +impl Repository { + /// What `.config/nextest.toml` lacks, or allows, of the standard. + pub(super) fn nextest_config(&self) -> Vec { + self.nextest.findings() + } +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 221b9dc..76bf1ee 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -11,6 +11,7 @@ mod fix; mod framework; mod generated_lints; mod layout; +mod nextest_config; mod report; mod rustfmt_config; mod toolchain; diff --git a/crates/sourceorigin-standard/src/rules/tests/nextest_config.rs b/crates/sourceorigin-standard/src/rules/tests/nextest_config.rs new file mode 100644 index 0000000..76d3ef4 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/nextest_config.rs @@ -0,0 +1,35 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn hung_tests_fail_and_flaky_ones_are_not_retried() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS).write( + ".config/nextest.toml", + "[profile.default]\nslow-timeout = { period = \"60s\", terminate-after = 5 }\n\n\ + [[profile.default.overrides]]\nfilter = \"binary(docker)\"\nslow-timeout = \"60s\"\n\n\ + [profile.ci]\nretries = 2\n", + ); + assert_eq!( + fixture.violations(Rule::NextestConfig), + [ + ".config/nextest.toml:6: a `slow-timeout` without `terminate-after` lets a hung test \ + run forever: add `terminate-after` (nextest-config)", + ".config/nextest.toml:9: `retries` hides flaky tests: remove it, and fix the test \ + (nextest-config)", + ] + ); +} + +#[test] +fn the_default_profile_sets_a_slow_timeout() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS); + assert_eq!( + fixture.violations(Rule::NextestConfig), + [ + ".config/nextest.toml:1: `[profile.default]` sets no `slow-timeout`: add \ + `slow-timeout = { period = \"60s\", terminate-after = 5 }` (nextest-config)" + ] + ); +}