From 1bb0b12e8edc2cf319ee311879c50d0de3da3bde Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Fri, 7 Jun 2024 10:20:31 -0600 Subject: [PATCH 1/2] chore: Add tests to ensure lints and groups stay sorted --- src/cargo/util/lints.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index 1d9573f8980..0b9771980b9 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -14,8 +14,8 @@ use toml_edit::ImDocument; const LINT_GROUPS: &[LintGroup] = &[TEST_DUMMY_UNSTABLE]; pub const LINTS: &[Lint] = &[ - IM_A_TEAPOT, IMPLICIT_FEATURES, + IM_A_TEAPOT, UNKNOWN_LINTS, UNUSED_OPTIONAL_DEPENDENCY, ]; @@ -875,3 +875,42 @@ pub fn unused_dependencies( } Ok(()) } + +#[cfg(test)] +mod tests { + use snapbox::ToDebug; + + #[test] + fn ensure_sorted_lints() { + // This will be printed out if the fields are not sorted. + let location = std::panic::Location::caller(); + println!("\nTo fix this test, sort `LINTS` in {}\n", location.file(),); + + let actual = super::LINTS + .iter() + .map(|l| l.name.to_uppercase()) + .collect::>(); + + let mut expected = actual.clone(); + expected.sort(); + snapbox::assert_data_eq!(actual.to_debug(), expected.to_debug()); + } + + #[test] + fn ensure_sorted_lint_groups() { + // This will be printed out if the fields are not sorted. + let location = std::panic::Location::caller(); + println!( + "\nTo fix this test, sort `LINT_GROUPS` in {}\n", + location.file(), + ); + let actual = super::LINT_GROUPS + .iter() + .map(|l| l.name.to_uppercase()) + .collect::>(); + + let mut expected = actual.clone(); + expected.sort(); + snapbox::assert_data_eq!(actual.to_debug(), expected.to_debug()); + } +} From 959b7121b08c558732913464489d15215d5fe88d Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Fri, 7 Jun 2024 11:21:53 -0600 Subject: [PATCH 2/2] chore: Add tests to ensure lints and groups stay updated --- src/cargo/util/lints.rs | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index 0b9771980b9..569176169fb 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -878,7 +878,9 @@ pub fn unused_dependencies( #[cfg(test)] mod tests { + use itertools::Itertools; use snapbox::ToDebug; + use std::collections::HashSet; #[test] fn ensure_sorted_lints() { @@ -913,4 +915,82 @@ mod tests { expected.sort(); snapbox::assert_data_eq!(actual.to_debug(), expected.to_debug()); } + + #[test] + fn ensure_updated_lints() { + let path = snapbox::utils::current_rs!(); + let expected = std::fs::read_to_string(&path).unwrap(); + let expected = expected + .lines() + .filter_map(|l| { + if l.ends_with(": Lint = Lint {") { + Some( + l.chars() + .skip(6) + .take_while(|c| *c != ':') + .collect::(), + ) + } else { + None + } + }) + .collect::>(); + let actual = super::LINTS + .iter() + .map(|l| l.name.to_uppercase()) + .collect::>(); + let diff = expected.difference(&actual).sorted().collect::>(); + + let mut need_added = String::new(); + for name in &diff { + need_added.push_str(&format!("{}\n", name)); + } + assert!( + diff.is_empty(), + "\n`LINTS` did not contain all `Lint`s found in {}\n\ + Please add the following to `LINTS`:\n\ + {}", + path.display(), + need_added + ); + } + + #[test] + fn ensure_updated_lint_groups() { + let path = snapbox::utils::current_rs!(); + let expected = std::fs::read_to_string(&path).unwrap(); + let expected = expected + .lines() + .filter_map(|l| { + if l.ends_with(": LintGroup = LintGroup {") { + Some( + l.chars() + .skip(6) + .take_while(|c| *c != ':') + .collect::(), + ) + } else { + None + } + }) + .collect::>(); + let actual = super::LINT_GROUPS + .iter() + .map(|l| l.name.to_uppercase()) + .collect::>(); + let diff = expected.difference(&actual).sorted().collect::>(); + + let mut need_added = String::new(); + for name in &diff { + need_added.push_str(&format!("{}\n", name)); + } + assert!( + diff.is_empty(), + "\n`LINT_GROUPS` did not contain all `LintGroup`s found in {}\n\ + Please add the following to `LINT_GROUPS`:\n\ + {}", + path.display(), + need_added + ); + } }