Skip to content

Commit 26af8ea

Browse files
committed
chore: Add tests to ensure lints and groups stay updated
1 parent 1bb0b12 commit 26af8ea

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/cargo/util/lints.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,9 @@ pub fn unused_dependencies(
878878

879879
#[cfg(test)]
880880
mod tests {
881+
use itertools::Itertools;
881882
use snapbox::ToDebug;
883+
use std::collections::HashSet;
882884

883885
#[test]
884886
fn ensure_sorted_lints() {
@@ -913,4 +915,78 @@ mod tests {
913915
expected.sort();
914916
snapbox::assert_data_eq!(actual.to_debug(), expected.to_debug());
915917
}
918+
919+
#[test]
920+
fn ensure_updated_lints() {
921+
let path = snapbox::utils::current_rs!();
922+
let expected = std::fs::read_to_string(path).unwrap();
923+
let expected = expected
924+
.lines()
925+
.filter_map(|l| {
926+
if l.ends_with(": Lint = Lint {") {
927+
Some(
928+
l.chars()
929+
.skip(6)
930+
.take_while(|c| *c != ':')
931+
.collect::<String>(),
932+
)
933+
} else {
934+
None
935+
}
936+
})
937+
.collect::<HashSet<_>>();
938+
let actual = super::LINTS
939+
.iter()
940+
.map(|l| l.name.to_uppercase())
941+
.collect::<HashSet<_>>();
942+
let diff = expected.difference(&actual).sorted().collect::<Vec<_>>();
943+
944+
let mut need_added = String::new();
945+
for name in &diff {
946+
need_added.push_str(&format!("{}\n", name));
947+
}
948+
assert!(
949+
diff.is_empty(),
950+
"\n`LINTS` did not contain all `Lint`s found in {path}\n\
951+
Please add the following to `LINTS`:\n\
952+
{need_added}"
953+
);
954+
}
955+
956+
#[test]
957+
fn ensure_updated_lint_groups() {
958+
let path = snapbox::utils::current_rs!();
959+
let expected = std::fs::read_to_string(path).unwrap();
960+
let expected = expected
961+
.lines()
962+
.filter_map(|l| {
963+
if l.ends_with(": LintGroup = LintGroup {") {
964+
Some(
965+
l.chars()
966+
.skip(6)
967+
.take_while(|c| *c != ':')
968+
.collect::<String>(),
969+
)
970+
} else {
971+
None
972+
}
973+
})
974+
.collect::<HashSet<_>>();
975+
let actual = super::LINT_GROUPS
976+
.iter()
977+
.map(|l| l.name.to_uppercase())
978+
.collect::<HashSet<_>>();
979+
let diff = expected.difference(&actual).sorted().collect::<Vec<_>>();
980+
981+
let mut need_added = String::new();
982+
for name in &diff {
983+
need_added.push_str(&format!("{}\n", name));
984+
}
985+
assert!(
986+
diff.is_empty(),
987+
"\n`LINT_GROUPS` did not contain all `LintGroup`s found in {path}\n\
988+
Please add the following to `LINT_GROUPS`:\n\
989+
{need_added}"
990+
);
991+
}
916992
}

0 commit comments

Comments
 (0)