Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
9 changes: 9 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod layout;
mod level;
mod manifest;
mod modules;
mod nextest_config;
mod report;
mod rustfmt_config;
mod settings;
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -333,6 +339,8 @@ struct Repository {
scripts: Vec<TextFile>,
/// The manifests, justfiles and workflows, with their comments.
commented: Vec<Commented>,
/// `.config/nextest.toml`, empty when missing.
nextest: TomlFile<NextestConfig>,
}

impl Repository {
Expand All @@ -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,
Expand Down
84 changes: 84 additions & 0 deletions crates/sourceorigin-standard/src/rules/nextest_config.rs
Original file line number Diff line number Diff line change
@@ -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<String, Profile>,
}

/// A profile, or one of its overrides.
#[derive(Deserialize)]
struct Profile {
#[serde(rename = "slow-timeout")]
slow_timeout: Option<Spanned<toml::Value>>,
retries: Option<Spanned<toml::Value>>,
#[serde(default)]
overrides: Vec<Profile>,
}

impl Profile {
/// It and its overrides.
fn all(&self) -> impl Iterator<Item = &Self> {
std::iter::once(self).chain(&self.overrides)
}
}

impl TomlFile<NextestConfig> {
/// A default profile without a `slow-timeout`, each `slow-timeout`
/// without `terminate-after`, and each `retries`.
fn findings(&self) -> Vec<Finding> {
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<Finding> {
self.nextest.findings()
}
}
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod fix;
mod framework;
mod generated_lints;
mod layout;
mod nextest_config;
mod report;
mod rustfmt_config;
mod toolchain;
Expand Down
35 changes: 35 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/nextest_config.rs
Original file line number Diff line number Diff line change
@@ -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)"
]
);
}