mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 02:35:37 +01:00
xtask: test-lint counts items under cfg(all(test, ...)) as test code
test_regions matched only the exact `#[cfg(test)]`, so helpers in a module under `#[cfg(all(test, windows))]` or `#[cfg(all(test, not(target_arch = "wasm32")))]` (as in apps/vectorcraft-web/src/file_drops.rs) were not test code: a sleep in such a helper passed the check, and its asserting helpers were not found. A cfg predicate now counts when it is `test` or an `all(...)` with such a predicate among its arguments, at any depth and with any spacing. `any(test, ...)`, `not(test)` and `feature = "test"` do not count.
This commit is contained in:
1 parent
ebb84ec16c
commit
fbcc1627c4
1 file changed
+47
-6
+47
-6
@@ -41,7 +41,8 @@
|
||||
//! that keeps files ending in `.rs`), outside the source audits in [`SOURCE_AUDITS`].
|
||||
//!
|
||||
//! Test code is a test file (`tests.rs`, `tests_*.rs`, `*_tests.rs`, a file in a `tests` or
|
||||
//! `benches` folder, `crates/testkit`), an item under `#[cfg(test)]`, or a test function.
|
||||
//! `benches` folder, `crates/testkit`), an item under `#[cfg(test)]` or `#[cfg(all(test, …))]`,
|
||||
//! or a test function.
|
||||
//!
|
||||
//! An asserting helper is a function or macro in the crate's test code (or in `crates/testkit`)
|
||||
//! that asserts, panics or calls one that does (every one of that name, when there are several),
|
||||
@@ -972,20 +973,52 @@ fn is_test_file(path: &str) -> bool {
|
||||
|| path.starts_with("crates/testkit/")
|
||||
}
|
||||
|
||||
/// The byte ranges of test code in a file: all of a test file, else each item under
|
||||
/// `#[cfg(test)]` that has a body in the file, and each test function.
|
||||
/// Whether a `cfg` predicate holds only in test builds: `test`, or an `all(…)` with such a
|
||||
/// predicate among its arguments.
|
||||
fn cfg_requires_test(pred: &[u8]) -> bool {
|
||||
let pred: Vec<u8> = pred.iter().copied().filter(|c| !c.is_ascii_whitespace()).collect();
|
||||
if pred == b"test" {
|
||||
return true;
|
||||
}
|
||||
let Some(args) = pred.strip_prefix(b"all(").and_then(|a| a.strip_suffix(b")")) else { return false };
|
||||
let mut depth = 0usize;
|
||||
let mut start = 0;
|
||||
for (i, &c) in args.iter().enumerate() {
|
||||
match c {
|
||||
b'(' => depth += 1,
|
||||
b')' => depth = depth.saturating_sub(1),
|
||||
b',' if depth == 0 => {
|
||||
if cfg_requires_test(&args[start..i]) {
|
||||
return true;
|
||||
}
|
||||
start = i + 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
cfg_requires_test(&args[start..])
|
||||
}
|
||||
|
||||
/// The byte ranges of test code in a file: all of a test file, else each item under a `#[cfg(…)]`
|
||||
/// that holds only in test builds ([`cfg_requires_test`]) and has a body in the file, and each
|
||||
/// test function.
|
||||
fn test_regions(path: &str, m: &[u8], tests: &[TestFn]) -> Vec<(usize, usize)> {
|
||||
if is_test_file(path) {
|
||||
return vec![(0, m.len())];
|
||||
}
|
||||
let mut out: Vec<(usize, usize)> = tests.iter().map(|t| (t.attr, t.close)).collect();
|
||||
for at in words(m, b"cfg", 0, m.len()) {
|
||||
let attr = skip_ws(m, at + "cfg".len());
|
||||
if !m[..at].ends_with(b"#[") || !m[attr..].starts_with(b"(test)]") {
|
||||
let open = skip_ws(m, at + "cfg".len());
|
||||
if !m[..at].ends_with(b"#[") || m.get(open) != Some(&b'(') {
|
||||
continue;
|
||||
}
|
||||
let Some(close) = matching(m, open) else { continue };
|
||||
let end = skip_ws(m, close + 1);
|
||||
if m.get(end) != Some(&b']') || !cfg_requires_test(&m[open + 1..close]) {
|
||||
continue;
|
||||
}
|
||||
// The item's body: the first `{` before any `;` (`mod tests;` has none here).
|
||||
let after = attr + "(test)]".len();
|
||||
let after = end + 1;
|
||||
let Some(open) = m[after..].iter().position(|&c| c == b'{' || c == b';').map(|p| p + after) else { continue };
|
||||
if let (b'{', Some(close)) = (m[open], matching(m, open)) {
|
||||
out.push((at, close));
|
||||
@@ -3517,6 +3550,14 @@ mod tests {
|
||||
assert_eq!(hits_in("xtask/src/test_lint.rs", shipped, &ctx()), [], "a name that only starts with test is not a test file");
|
||||
let after = "#[cfg(test)]\nmod tests {\n fn w() { thread::sleep(D); }\n}\npub fn later() {\n thread::sleep(D);\n}\n";
|
||||
assert_eq!(hits_in("crates/x/src/lib.rs", after, &ctx()), [(3, "sleep")], "code after a #[cfg(test)] module is not test code");
|
||||
for gate in ["all(test, windows)", "all( test , not(target_arch = \"wasm32\") )", "all(feature = \"x\", all(unix, test))"] {
|
||||
let gated = format!("#[cfg({gate})]\nmod tests {{\n fn w() {{ thread::sleep(D); }}\n}}\n");
|
||||
assert_eq!(hits_in("crates/x/src/lib.rs", &gated, &ctx()), [(3, "sleep")], "{gate} holds only in tests");
|
||||
}
|
||||
for gate in ["any(test, feature = \"x\")", "not(test)", "all(unix, not(test))", "feature = \"test\""] {
|
||||
let gated = format!("#[cfg({gate})]\nmod helpers {{\n fn w() {{ thread::sleep(D); }}\n}}\n");
|
||||
assert_eq!(hits_in("crates/x/src/lib.rs", &gated, &ctx()), [], "{gate} also holds outside tests");
|
||||
}
|
||||
for test_file in [
|
||||
"crates/x/src/tests.rs",
|
||||
"crates/x/src/tests_wheel.rs",
|
||||
|
||||
Reference in new issue
Block a user