Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ensure all lints in src/lints are registered for use at runtime. #860

Merged
merged 1 commit into from
Aug 15, 2024
Merged
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
Ensure all lints in src/lints are registered for use at runtime.
obi1kenobi committed Aug 15, 2024
commit 1b594d9963c7b00469c9cd7cbcd299a0f7ec6d9a
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ anstream = "0.6.13"
assert_cmd = "2.0"
similar-asserts = { version = "1.5.0", features = ["serde"] }
predicates = "3.1.0"
fs-err = "2.11.0"

# In dev and test profiles, compile all dependencies with optimizations enabled,
# but still checking debug assertions and overflows.
50 changes: 50 additions & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
@@ -223,6 +223,9 @@ impl From<Vec<Arc<OverrideMap>>> for OverrideStack {

#[cfg(test)]
mod tests {
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};
use std::{collections::BTreeMap, path::Path};

@@ -694,6 +697,42 @@ mod tests {
RequiredSemverUpdate::Minor
);
}

pub(super) fn check_all_lint_files_are_used_in_add_lints(added_lints: &[&str]) {
let mut lints_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
lints_dir.push("src");
lints_dir.push("lints");

let expected_lints: BTreeSet<_> = added_lints.iter().copied().collect();
let mut missing_lints: BTreeSet<String> = Default::default();

let dir_contents =
fs_err::read_dir(lints_dir).expect("failed to read 'src/lints' directory");
for file in dir_contents {
let file = file.expect("failed to examine file");
let path = file.path();

// Check if we found a `*.ron` file. If so, that's a lint.
if path.extension().map(|x| x.to_string_lossy()) == Some(Cow::Borrowed("ron")) {
let stem = path
.file_stem()
.map(|x| x.to_string_lossy())
.expect("failed to get file name as utf-8");

// Check if the lint was added using our `add_lints!()` macro.
// If not, that's an error.
if !expected_lints.contains(stem.as_ref()) {
missing_lints.insert(stem.to_string());
}
}
}

assert!(
missing_lints.is_empty(),
"some lints in 'src/lints/' haven't been registered using the `add_lints!()` macro, \
so they won't be part of cargo-semver-checks: {missing_lints:?}"
)
}
}

macro_rules! add_lints {
@@ -706,6 +745,17 @@ macro_rules! add_lints {
super::tests::check_query_execution(stringify!($name))
}
)*

#[test]
fn all_lint_files_are_used_in_add_lints() {
let added_lints = [
$(
stringify!($name),
)*
];

super::tests::check_all_lint_files_are_used_in_add_lints(&added_lints);
}
}

fn get_queries() -> Vec<(&'static str, &'static str)> {