diff --git a/crates/testkit/src/guards.rs b/crates/testkit/src/guards.rs index d6b7f13f59..032a175cb3 100644 --- a/crates/testkit/src/guards.rs +++ b/crates/testkit/src/guards.rs @@ -1544,9 +1544,11 @@ fn extern_prelude_names(paths: &[String], contents: &[&str]) -> Vec bool { let mut segments = path.rsplit('/'); let Some(name) = segments.next() else { return false }; @@ -1554,10 +1556,12 @@ fn cargo_target(path: &str) -> bool { return false; } let folders: Vec<&str> = segments.collect(); - let package = |rest: &[&str]| !rest.iter().any(|s| matches!(*s, "src" | "bin" | "tests" | "examples" | "benches")); + let package = |rest: &[&str]| rest.first() != Some(&"src") && !rest.iter().any(|s| matches!(*s, "bin" | "tests" | "examples" | "benches")); match folders.as_slice() { - ["bin", "src", rest @ ..] | ["tests" | "examples" | "benches", rest @ ..] => package(rest), - [_, "bin", "src", rest @ ..] | [_, "tests" | "examples" | "benches", rest @ ..] if name == "main.rs" => package(rest), + ["bin", "src", ..] => true, + [_, "bin", "src", ..] if name == "main.rs" => true, + ["tests" | "examples" | "benches", rest @ ..] => package(rest), + [_, "tests" | "examples" | "benches", rest @ ..] if name == "main.rs" => package(rest), rest if name == "build.rs" => package(rest), _ => false, } @@ -1658,9 +1662,10 @@ fn muda_crate_bindings<'a>( /// `extern crate muda as menu;` outside any braces of a crate root (`src/lib.rs` and `src/main.rs`; `build.rs`, /// another `lib.rs` or `main.rs`, or a file directly in a `bin`, `examples`, `tests` or `benches` folder other /// than `mod.rs`, unless a `mod` declaration in another of `files` names it, for a file in those folders one -/// without a `#[path]` attribute; a package's `build.rs`, a file directly in its `src/bin`, `tests`, `examples` -/// or `benches` folder, and a `main.rs` one folder below those, are crate roots even when a declaration names -/// them, where a package folder has no such folder or `src` above it) also adds `menu` to +/// without a `#[path]` attribute; a file directly in `src/bin`, a package's `build.rs`, a file directly in its +/// `tests`, `examples` or `benches` folder, and a `main.rs` one folder below those, are crate roots even when a +/// declaration names them, where a package folder is not directly inside `src` and has no `bin`, `tests`, +/// `examples` or `benches` folder above it) also adds `menu` to /// the crate's extern prelude, so in that file and in every file of `files` that a chain of `mod` /// declarations from it names, `menu` is read like `muda` and `::menu::PredefinedMenuItem::quit` is /// reported. Elsewhere a path that starts with `::` names the crate only through `muda`. It also diff --git a/crates/testkit/tests/guards.rs b/crates/testkit/tests/guards.rs index 9b5b9d9567..a8823795ba 100644 --- a/crates/testkit/tests/guards.rs +++ b/crates/testkit/tests/guards.rs @@ -614,6 +614,17 @@ mod nested { ("src/bin/tool/main.rs", local), ]); assert_eq!(found.iter().map(|f| (f.path.as_str(), f.line)).collect::>(), vec![("build.rs", 2), ("src/bin/tool/main.rs", 2)]); + // A folder named `src` above the package, as in a path `rust_sources` returns, does not change that. + let found = predefined_quit([ + ("/home/dev/src/app/src/bin/tool.rs", "mod helper;\n"), + ("/home/dev/src/app/src/bin/helper.rs", local), + ("/home/dev/src/app/src/lib.rs", "#[path = \"../build.rs\"]\nmod build;\n"), + ("/home/dev/src/app/build.rs", local), + ]); + assert_eq!( + found.iter().map(|f| (f.path.as_str(), f.line)).collect::>(), + vec![("/home/dev/src/app/src/bin/helper.rs", 2), ("/home/dev/src/app/build.rs", 2)] + ); // A `tests` folder inside a test's module folder holds modules, so a declared file there is not a root. assert!( predefined_quit([