use super::*;
use crate::module_scan::rust_files;
use crate::module_scan::source_texts::SourceTexts;
#[allow(clippy::type_complexity)]
fn reachable_modules(
src_dir: &Path,
files: &[PathBuf],
root_relative: Option<&Path>,
edition: crate::module_scan::Edition,
) -> Result<
(
std::collections::BTreeSet<String>,
std::collections::BTreeMap<String, PathBuf>,
Vec<(PathBuf, String)>,
std::collections::BTreeSet<String>,
),
String,
> {
walk::reachable_modules(
&SourceTexts::default(),
src_dir,
files,
root_relative,
edition,
)
}
struct TempSrcTree {
_root: xingbiao::ScratchRoot,
src: PathBuf,
}
impl TempSrcTree {
fn new(label: &str) -> Self {
let root = xingbiao::scratch_root(&format!("guibiao-reachability-{label}"));
let src = root.join("src");
std::fs::create_dir_all(&src).expect("create temp src");
Self { _root: root, src }
}
fn src(&self) -> &Path {
&self.src
}
}
#[test]
fn declared_modules_finds_only_top_level_declarations() {
let source = r#"
pub mod kernel;
mod projection;
pub(crate) mod runner;
mod inline { mod nested_child; } // nested_child is depth 1, not a root module
// mod commented_out;
fn f() { let _ = "mod string_literal;"; }
"#;
let mut mods = declared_modules(source);
mods.sort();
assert_eq!(
mods,
vec![
"inline".to_string(),
"kernel".to_string(),
"projection".to_string(),
"runner".to_string(),
],
"only top-level mod declarations count; nested, commented, and quoted are excluded"
);
}
#[test]
fn reachable_modules_follows_mod_declarations_not_filenames() {
let tree = TempSrcTree::new("declared-not-filenames");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("kernel")).expect("create temp src/kernel");
std::fs::write(
src.join("lib.rs"),
"pub mod kernel;\nuse serde::Deserialize;\n",
)
.expect("write lib.rs");
std::fs::write(src.join("kernel.rs"), "// kernel declares no submodule\n")
.expect("write kernel.rs");
std::fs::write(src.join("serde.rs"), "// root orphan, undeclared\n").expect("write serde.rs");
std::fs::write(
src.join("kernel/orphan.rs"),
"use crate::projection::Thing;\n",
)
.expect("write kernel/orphan.rs");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, _remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
assert!(reachable.contains("crate"), "{reachable:?}");
assert!(
reachable.contains("crate::kernel"),
"a declared `mod kernel;` is reachable: {reachable:?}"
);
assert!(
!reachable.contains("crate::serde"),
"an undeclared root orphan is not reachable: {reachable:?}"
);
assert!(
!reachable.contains("crate::kernel::orphan"),
"an undeclared subtree orphan is not reachable: {reachable:?}"
);
}
#[test]
fn a_stray_lib_beside_a_custom_root_is_not_a_second_crate_root() {
let tree = TempSrcTree::new("custom-root");
let src = tree.src().to_path_buf();
std::fs::write(src.join("core.rs"), "pub mod real;\n").expect("write core.rs");
std::fs::write(
src.join("real.rs"),
"// real, declared from the true root\n",
)
.expect("write real.rs");
std::fs::write(src.join("lib.rs"), "pub mod ghost;\n").expect("write stray lib.rs");
std::fs::write(
src.join("ghost.rs"),
"// declared only by the uncompiled lib.rs\n",
)
.expect("write ghost.rs");
let files = rust_files(&src).expect("list files");
let root_relative = std::path::PathBuf::from("core.rs");
let (reachable, _inline_only, _remapped, _remap_shadowed) = reachable_modules(
&src,
&files,
Some(&root_relative),
crate::module_scan::Edition::Rust2018,
)
.expect("walk modules");
assert!(
reachable.contains("crate"),
"the custom root seeds crate: {reachable:?}"
);
assert!(
reachable.contains("crate::real"),
"a module declared from the true root is reachable: {reachable:?}"
);
assert!(
!reachable.contains("crate::ghost"),
"a module declared only by the stray, uncompiled lib.rs is NOT reachable: {reachable:?}"
);
}
#[test]
fn path_remapped_modules_are_followed_to_their_target() {
let tree = TempSrcTree::new("path-remap");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[path = \"weird.rs\"]\npub mod kernel;\npub mod normal;\n",
)
.expect("write lib.rs");
let target = src.join("weird.rs");
std::fs::write(&target, "use crate::projection::Thing;\n").expect("write remapped file");
let orphan = src.join("kernel.rs");
std::fs::write(&orphan, "use crate::wrong_file_if_observed::Thing;\n")
.expect("write conventional orphan");
std::fs::write(src.join("normal.rs"), "// normal module\n").expect("write normal.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert!(reachable.contains("crate::normal"), "{reachable:?}");
assert!(
reachable.contains("crate::kernel"),
"a #[path]-remapped module is now followed to its target: {reachable:?}"
);
assert_eq!(
remapped,
vec![(target.clone(), "crate::kernel".to_string())],
"the remap is recorded under its logical path: {remapped:?}"
);
assert!(
governed
.iter()
.any(|(file, module)| file == &target && module == "crate::kernel"),
"the real remapped target is governed under crate::kernel: {governed:?}"
);
assert!(
!governed.iter().any(|(file, _)| file == &orphan),
"the conventional orphan must not be governed in the remap's place: {governed:?}"
);
}
#[test]
fn a_plain_child_of_a_path_remapped_module_is_governed_from_the_remaps_own_directory() {
let tree = TempSrcTree::new("remap-plain-child");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("other")).expect("create temp src/other");
std::fs::write(
src.join("lib.rs"),
"#[path = \"other/weird.rs\"]\npub mod kernel;\n",
)
.expect("write lib.rs");
std::fs::write(src.join("other/weird.rs"), "pub mod child;\n").expect("write remap target");
let child_file = src.join("other/child.rs");
std::fs::write(&child_file, "use crate::projection::Thing;\n").expect("write child.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert!(
reachable.contains("crate::kernel::child"),
"the remap target's own plain child is reachable: {reachable:?}"
);
assert!(
governed
.iter()
.any(|(file, module)| file == &child_file && module == "crate::kernel::child"),
"the remap target's own plain child is governed under its logical path, so its real \
`use` is observed: {governed:?}"
);
}
#[test]
fn path_attribute_detection_is_specific() {
assert_eq!(
declared_modules("#[pathology]\npub mod kernel;\n"),
vec!["kernel".to_string()],
"only the real `path` attribute is a remap marker"
);
assert_eq!(
declared_modules("# [ path = \"weird.rs\" ]\npub mod kernel;\n"),
vec!["kernel".to_string()],
);
}
#[test]
fn a_cfg_attr_nested_path_collects_conditional_remaps() {
let mods = declared_modules("#[cfg_attr(windows, path = \"os/windows.rs\")]\npub mod os;\n");
assert_eq!(mods, vec!["os".to_string()]);
let mods_a = declared_modules("#[cfg_attr(all(unix), deprecated, path = \"p.rs\")]\nmod a;\n");
assert_eq!(mods_a, vec!["a".to_string()]);
let mods_m =
declared_modules("#[cfg_attr(a, cfg_attr(b, path = \"secret.rs\"))]\npub mod m;\n");
assert_eq!(mods_m, vec!["m".to_string()]);
}
#[test]
fn a_deeply_nested_cfg_attr_path_is_followed_without_native_recursion() {
const DEPTH: usize = 512;
let tree = TempSrcTree::new("deeply-nested-cfg-attr");
let src = tree.src().to_path_buf();
let mut attribute = String::from("#[cfg_attr");
for _ in 0..DEPTH {
attribute.push_str("(predicate, cfg_attr");
}
attribute.push_str("(predicate, path = \"target.rs\")");
for _ in 0..=DEPTH {
attribute.push(')');
}
attribute.push_str("]\npub mod target;\n");
std::fs::write(src.join("lib.rs"), attribute).expect("write lib.rs");
let target = src.join("target.rs");
std::fs::write(&target, "use crate::projection::Thing;\n").expect("write target.rs");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk deeply nested cfg_attr");
assert!(reachable.contains("crate::target"), "{reachable:?}");
assert!(
remapped
.iter()
.any(|(file, module)| file == &target && module == "crate::target"),
"{remapped:?}"
);
}
#[test]
fn mixed_direct_and_conditional_path_attrs_keep_the_module_regardless_of_order() {
assert_eq!(
declared_modules(
"#[cfg_attr(some_platform, path = \"b.rs\")]\n#[path = \"a.rs\"]\npub mod x;\n"
),
vec!["x".to_string()],
"cfg_attr before the direct #[path] must not drop the module",
);
assert_eq!(
declared_modules(
"#[path = \"a.rs\"]\n#[cfg_attr(some_platform, path = \"b.rs\")]\npub mod x;\n"
),
vec!["x".to_string()],
"the direct #[path] first must keep working as before",
);
}
#[test]
fn stacked_cfg_attr_path_only_targets_are_governed_without_a_plain_file() {
let tree = TempSrcTree::new("stacked-cfg-attr-path-only");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[cfg_attr(unix, path = \"unix_imp.rs\")]\n#[cfg_attr(not(unix), path = \"other_imp.rs\")]\npub mod imp;\n",
)
.expect("write lib.rs");
let unix_target = src.join("unix_imp.rs");
let other_target = src.join("other_imp.rs");
std::fs::write(&unix_target, "use crate::from_unix::Thing;\n").expect("write unix_imp.rs");
std::fs::write(&other_target, "use crate::from_other::Thing;\n").expect("write other_imp.rs");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("stacked cfg_attr(path)-only must not hard error");
assert!(reachable.contains("crate::imp"), "{reachable:?}");
assert!(
remapped
.iter()
.any(|(file, module)| file == &unix_target && module == "crate::imp"),
"{remapped:?}"
);
assert!(
remapped
.iter()
.any(|(file, module)| file == &other_target && module == "crate::imp"),
"{remapped:?}"
);
}
#[test]
fn single_cfg_attr_path_only_target_is_governed_without_a_plain_file() {
let tree = TempSrcTree::new("single-cfg-attr-path-only");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[cfg_attr(unix, path = \"unix_imp.rs\")]\npub mod imp;\n",
)
.expect("write lib.rs");
let target = src.join("unix_imp.rs");
std::fs::write(&target, "use crate::from_unix::Thing;\n").expect("write unix_imp.rs");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("single cfg_attr(path)-only must not hard error");
assert!(reachable.contains("crate::imp"), "{reachable:?}");
assert!(
remapped
.iter()
.any(|(file, module)| file == &target && module == "crate::imp"),
"{remapped:?}"
);
}
#[test]
fn a_cfg_attr_path_target_absent_with_no_plain_file_is_still_a_scan_error() {
let tree = TempSrcTree::new("cfg-attr-path-absent-no-plain");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[cfg_attr(windows, path = \"windows_only.rs\")]\npub mod imp;\n",
)
.expect("write lib.rs");
let files = rust_files(&src).expect("list files");
let result = reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018);
let err = result.expect_err(
"a cfg_attr(path) target absent with no plain conventional file is still a scan error",
);
assert!(
err.contains("crate::imp") && err.contains("could not be located"),
"expected the missing-plain-file constitution error, got: {err}"
);
}
#[test]
fn both_conventional_forms_present_stays_an_ambiguity_alongside_a_resolved_cfg_attr_target() {
let tree = TempSrcTree::new("dual-form-with-cfg-attr");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[cfg_attr(unix, path = \"unix_imp.rs\")]\npub mod imp;\n",
)
.expect("write lib.rs");
std::fs::write(src.join("unix_imp.rs"), "use crate::from_unix::Thing;\n")
.expect("write unix_imp.rs");
std::fs::write(src.join("imp.rs"), "// conventional flat form\n").expect("write imp.rs");
std::fs::create_dir_all(src.join("imp")).expect("mkdir imp");
std::fs::write(src.join("imp/mod.rs"), "// conventional nested form\n")
.expect("write imp/mod.rs");
let files = rust_files(&src).expect("list files");
let result = reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018);
let err = result.expect_err("both conventional forms present must still be an ambiguity error");
assert!(
err.contains("resolves to both"),
"expected the dual-backed ambiguity constitution error, got: {err}"
);
}
#[test]
fn a_cfg_attr_without_a_path_meta_is_not_a_remap() {
assert_eq!(
declared_modules("#[cfg_attr(test, derive(Debug))]\npub mod real;\n"),
vec!["real".to_string()],
"a cfg_attr without a path meta is not a remap",
);
assert_eq!(
declared_modules("#[cfg_attr(feature = \"path\", deprecated)]\npub mod real;\n"),
vec!["real".to_string()],
"a `path` inside a predicate string is not a path meta",
);
assert_eq!(
declared_modules("#[cfg_attr(unix, target_path = \"x\")]\npub mod real;\n"),
vec!["real".to_string()],
);
assert_eq!(
declared_modules("#[cfg_attr(a, cfg_attr(b, deprecated))]\npub mod real;\n"),
vec!["real".to_string()],
"a nested cfg_attr without a path meta is not a remap",
);
assert_eq!(
declared_modules("#[cfg_attr(path = \"x\", deprecated)]\npub mod real;\n"),
vec!["real".to_string()],
"a `path` cfg predicate key is not an applied path remap",
);
}
#[test]
fn a_cfg_attr_nested_path_on_an_inline_module_does_not_drop_it() {
assert_eq!(
declared_modules("#[cfg_attr(windows, path = \"x.rs\")]\npub mod a { pub mod inner; }\n"),
vec!["a".to_string()],
);
}
#[test]
fn a_path_attr_on_an_inline_module_does_not_drop_it() {
assert_eq!(
declared_modules("#[path = \"x.rs\"]\npub mod a { pub mod inner; }\n"),
vec!["a".to_string()],
"an inline module with a (no-op) #[path] is still declared",
);
assert_eq!(
declared_modules("#[path = \"x.rs\"]\npub mod a;\n"),
vec!["a".to_string()],
"a #[path]-remapped FILE module is declared, to be followed to its target",
);
}
#[test]
fn a_block_comment_before_a_mod_name_does_not_fuse_it() {
assert_eq!(
declared_modules("mod/*c*/foo;"),
vec!["foo".to_string()],
"a block comment after `mod` must not swallow the declaration",
);
}
#[test]
fn a_custom_crate_root_filename_maps_to_crate() {
let tree = TempSrcTree::new("custom-root-maps-to-crate");
let src = tree.src().to_path_buf();
std::fs::write(src.join("core.rs"), "pub mod sub;\n").expect("write core.rs");
std::fs::write(src.join("sub.rs"), "// sub\n").expect("write sub.rs");
let files = rust_files(&src).expect("list files");
let (with_root, _, _, _) = reachable_modules(
&src,
&files,
Some(std::path::Path::new("core.rs")),
crate::module_scan::Edition::Rust2018,
)
.expect("walk");
let (without_root, _, _, _) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018).expect("walk");
assert!(
with_root.contains("crate::sub"),
"with the custom root mapped to crate, its submodule is reachable: {with_root:?}"
);
assert!(
!without_root.contains("crate::sub"),
"without the root override, core.rs maps to crate::core and sub is unreachable: {without_root:?}"
);
}
#[test]
fn declared_modules_ignores_a_mod_inside_a_macro_invocation() {
assert!(declared_modules("some_macro!( mod ghost; );").is_empty());
assert!(declared_modules("some_macro![ mod ghost; ];").is_empty());
assert!(declared_modules("macro_rules! m { () => { mod ghost; }; }").is_empty());
assert_eq!(declared_modules("mod real;"), vec!["real".to_string()]);
}
#[test]
fn declared_modules_observes_mod_inside_cfg_if_macro_body() {
let src = r#"
cfg_if::cfg_if! {
if #[cfg(feature = "x")] {
mod child;
fn f() {
mod local_inner {
use crate::secret;
}
}
}
}
"#;
assert_eq!(declared_modules(src), vec!["child".to_string()]);
let src_parens = r#"
cfg_if::cfg_if!(
if #[cfg(feature = "x")] {
mod child_paren;
}
);
"#;
assert_eq!(
declared_modules(src_parens),
vec!["child_paren".to_string()]
);
let src_unary = r#"
const FLAG: bool = ! {
mod local_child {}
false
};
"#;
assert!(declared_modules(src_unary).is_empty());
let src_inside_fn = r#"
fn owner() {
cfg_if::cfg_if! {
if #[cfg(feature = "x")] {
mod item_local_child;
}
}
}
"#;
assert!(declared_modules(src_inside_fn).is_empty());
}
#[test]
fn declared_modules_ignores_mod_inside_const_or_static_block() {
let src = r#"
const _: () = {
mod child {
use crate::secret;
}
};
const X: () = if true {
()
} else {
mod child {
use crate::secret;
}
};
static FOO: () = {
mod inner;
};
"#;
assert!(declared_modules(src).is_empty());
}
#[test]
fn an_inline_modules_file_backed_child_is_reachable() {
let tree = TempSrcTree::new("inline-file-child-reachable");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("parent")).expect("create temp src/parent");
std::fs::write(
src.join("lib.rs"),
"pub mod parent {\n pub mod child;\n}\n",
)
.expect("write lib.rs");
std::fs::write(
src.join("parent/child.rs"),
"use crate::projection::Thing;\n",
)
.expect("write parent/child.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, _remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
assert!(
inline_only.contains_key("crate::parent"),
"parent has no file of its own: {inline_only:?}"
);
assert!(
reachable.contains("crate::parent::child"),
"the real compiled file-backed child of an inline module must be reachable: {reachable:?}"
);
assert!(
!inline_only.contains_key("crate::parent::child"),
"the child is file-backed, not inline-only: {inline_only:?}"
);
}
#[test]
fn an_inline_modules_file_backed_child_is_governed() {
let tree = TempSrcTree::new("inline-file-child-governed");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("parent")).expect("create temp src/parent");
std::fs::write(
src.join("lib.rs"),
"pub mod parent {\n pub mod child;\n}\n",
)
.expect("write lib.rs");
let child_file = src.join("parent/child.rs");
std::fs::write(&child_file, "use crate::projection::Thing;\n").expect("write child.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert!(
governed
.iter()
.any(|(file, module)| file == &child_file && module == "crate::parent::child"),
"the real compiled child file must be governed: {governed:?}"
);
}
#[test]
fn a_chain_of_inline_modules_reaches_its_file_backed_leaf() {
let tree = TempSrcTree::new("inline-chain");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("kernel/parent/a/b")).expect("mkdirs");
std::fs::write(src.join("lib.rs"), "pub mod kernel;\n").expect("write lib.rs");
std::fs::write(
src.join("kernel.rs"),
"pub mod parent {\n pub mod a {\n pub mod b {\n pub mod c;\n }\n }\n}\n",
)
.expect("write kernel.rs");
std::fs::write(
src.join("kernel/parent/a/b/c.rs"),
"use crate::projection::Thing;\n",
)
.expect("write the deep leaf file");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, _remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
assert!(
reachable.contains("crate::kernel::parent::a::b::c"),
"a file-backed leaf beneath a chain of inline modules must be reachable: {reachable:?}"
);
}
#[test]
fn an_inline_modules_mod_rs_style_child_is_reachable() {
let tree = TempSrcTree::new("inline-mod-rs-child");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("parent/child")).expect("mkdirs");
std::fs::write(
src.join("lib.rs"),
"pub mod parent {\n pub mod child;\n}\n",
)
.expect("write lib.rs");
std::fs::write(
src.join("parent/child/mod.rs"),
"use crate::projection::Thing;\n",
)
.expect("write parent/child/mod.rs");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, _remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
assert!(
reachable.contains("crate::parent::child"),
"a mod.rs-style child beneath an inline parent must be reachable: {reachable:?}"
);
}
#[test]
fn an_inline_only_grandparents_conventional_orphan_stays_excluded() {
let tree = TempSrcTree::new("inline-orphan");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("parent")).expect("mkdirs");
std::fs::write(
src.join("lib.rs"),
"pub mod parent {\n pub mod child;\n}\n",
)
.expect("write lib.rs");
std::fs::write(
src.join("parent/child.rs"),
"use crate::projection::Thing;\n",
)
.expect("write the real compiled child");
std::fs::write(
src.join("parent.rs"),
"use crate::wrong_file_if_observed::Thing;\n",
)
.expect("write the conventional orphan Rust never compiles");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, _remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
assert!(
inline_only.contains_key("crate::parent"),
"parent is declared inline-only: {inline_only:?}"
);
assert!(
reachable.contains("crate::parent::child"),
"the real compiled child stays reachable: {reachable:?}"
);
}
#[test]
fn a_path_remapped_child_nested_in_an_inline_parent_is_followed() {
let tree = TempSrcTree::new("inline-path-remap");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("parent")).expect("mkdirs");
std::fs::write(
src.join("lib.rs"),
"pub mod parent {\n #[path = \"weird.rs\"]\n pub mod child;\n}\n",
)
.expect("write lib.rs");
let target = src.join("parent/weird.rs");
std::fs::write(&target, "use crate::projection::Thing;\n")
.expect("write the real #[path] target");
let orphan = src.join("parent/child.rs");
std::fs::write(&orphan, "use crate::wrong_file_if_observed::Thing;\n")
.expect("write the conventional orphan the remap must not fall back to");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert!(
inline_only.contains_key("crate::parent"),
"parent is declared inline-only: {inline_only:?}"
);
assert!(
reachable.contains("crate::parent::child"),
"a #[path]-remapped child nested in an inline parent is followed to its target, \
resolved relative to parent's own accumulated directory: {reachable:?}"
);
assert_eq!(
remapped,
vec![(target, "crate::parent::child".to_string())],
"resolved from src/parent/, not src/: {remapped:?}"
);
assert!(
!governed.iter().any(|(file, _)| file == &orphan),
"the conventional orphan must not be governed in the remap's place: {governed:?}"
);
}
#[test]
fn a_path_remap_value_with_a_backslash_newline_continuation_is_followed() {
let tree = TempSrcTree::new("path-remap-line-continuation");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[path = \"moved\\\n b.rs\"]\npub mod kernel;\n",
)
.expect("write lib.rs");
std::fs::write(src.join("movedb.rs"), "// the continuation-named target\n")
.expect("write movedb.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate::kernel",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert_eq!(
governed.len(),
1,
"crate::kernel must be governed by exactly the continuation-decoded target: {governed:?}"
);
assert!(
governed[0].0.ends_with("movedb.rs"),
"the governing file must be the continuation-decoded target, not a stale orphan: {governed:?}"
);
}
#[test]
fn a_path_remap_to_a_missing_target_is_a_scan_error() {
let tree = TempSrcTree::new("path-remap-missing");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[path = \"absent.rs\"]\npub mod kernel;\n",
)
.expect("write lib.rs");
let files = rust_files(&src).expect("list files");
let result = reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018);
assert!(
result.is_err(),
"a #[path] target that does not exist is a scan error, not a silent skip: {result:?}"
);
}
#[test]
fn a_path_remap_cycle_is_a_scan_error_not_a_hang() {
let tree = TempSrcTree::new("path-remap-cycle");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("a")).expect("mkdirs");
std::fs::write(
src.join("lib.rs"),
"pub mod a {\n #[path = \"../lib.rs\"]\n pub mod b;\n}\n",
)
.expect("write lib.rs");
let files = rust_files(&src).expect("list files");
let result = reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018);
let err = result.expect_err(
"a #[path] chain cycling back to an already-open file is a scan error, not a hang",
);
assert!(
err.contains("cycles back"),
"expected the ancestor-cycle guard's own message, got: {err}"
);
}
#[test]
fn two_declarations_sharing_one_path_remap_target_is_not_a_cycle() {
let tree = TempSrcTree::new("path-remap-shared");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[path = \"s.rs\"]\npub mod a;\n#[path = \"s.rs\"]\npub mod b;\n",
)
.expect("write lib.rs");
std::fs::write(src.join("s.rs"), "// shared target\n").expect("write s.rs");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("two modules sharing one #[path] target is not a cycle (rustc compiles it)");
assert!(reachable.contains("crate::a"), "{reachable:?}");
assert!(reachable.contains("crate::b"), "{reachable:?}");
assert_eq!(remapped.len(), 2, "{remapped:?}");
}
#[test]
fn cfg_gated_sibling_path_declarations_are_followed_cfg_blind_both() {
let tree = TempSrcTree::new("cfg-dual-path");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[cfg(unix)]\n#[path = \"unix_impl.rs\"]\npub mod imp;\n#[cfg(windows)]\n#[path = \"windows_impl.rs\"]\npub mod imp;\n",
)
.expect("write lib.rs");
let unix_target = src.join("unix_impl.rs");
std::fs::write(&unix_target, "use crate::projection::Unix;\n").expect("write unix_impl.rs");
let windows_target = src.join("windows_impl.rs");
std::fs::write(&windows_target, "use crate::projection::Windows;\n")
.expect("write windows_impl.rs");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("both cfg-gated targets are followed");
assert!(reachable.contains("crate::imp"), "{reachable:?}");
let mut targets: Vec<&PathBuf> = remapped
.iter()
.filter(|(_, module)| module == "crate::imp")
.map(|(file, _)| file)
.collect();
targets.sort();
let mut expected = vec![&unix_target, &windows_target];
expected.sort();
assert_eq!(
targets, expected,
"both platform targets are followed under crate::imp, cfg-blind: {remapped:?}"
);
}
#[test]
fn a_nested_path_crossing_into_a_cfg_siblings_own_target_is_not_a_cycle() {
let tree = TempSrcTree::new("cfg-cross-arm-nested");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[cfg(feature = \"a\")]\n#[path = \"variant_a.rs\"]\npub mod imp;\n#[cfg(feature = \"b\")]\n#[path = \"variant_b.rs\"]\npub mod imp;\n",
)
.expect("write lib.rs");
let variant_a = src.join("variant_a.rs");
std::fs::write(
&variant_a,
"#[path = \"variant_b.rs\"]\nmod also_b;\nuse crate::projection::A;\n",
)
.expect("write variant_a.rs");
let variant_b = src.join("variant_b.rs");
std::fs::write(&variant_b, "use crate::projection::B;\n").expect("write variant_b.rs");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018).expect(
"a nested #[path] crossing into a mutually-exclusive cfg sibling's own target must \
not be misreported as a cycle",
);
assert!(reachable.contains("crate::imp"), "{reachable:?}");
assert!(
reachable.contains("crate::imp::also_b"),
"the nested #[path] inside variant_a.rs is followed and governed: {reachable:?}"
);
let also_b_targets: Vec<&PathBuf> = remapped
.iter()
.filter(|(_, module)| module == "crate::imp::also_b")
.map(|(file, _)| file)
.collect();
assert_eq!(
also_b_targets,
vec![&variant_b],
"crate::imp::also_b resolves to variant_b.rs: {remapped:?}"
);
}
#[test]
fn a_nested_path_inside_an_inline_cfg_siblings_plain_child_is_not_a_cycle() {
let tree = TempSrcTree::new("cfg-inline-plain-child-cross-arm");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("x")).expect("create temp src/x");
std::fs::write(
src.join("lib.rs"),
"#[cfg(feature = \"u\")]\npub mod x {\n pub mod y;\n}\n#[cfg(feature = \"w\")]\n#[path = \"windows_x.rs\"]\npub mod x;\n",
)
.expect("write lib.rs");
std::fs::write(
src.join("x/y.rs"),
"#[path = \"../windows_x.rs\"]\nmod cross;\n",
)
.expect("write x/y.rs");
std::fs::write(src.join("windows_x.rs"), "// the other cfg arm's target\n")
.expect("write windows_x.rs");
let files = rust_files(&src).expect("list files");
let (reachable, _inline_only, remapped, _remap_shadowed) = reachable_modules(
&src,
&files,
None,
crate::module_scan::Edition::Rust2018,
)
.expect(
"a plain child's own nested #[path] crossing into a cfg sibling's target must not be a cycle",
);
assert!(reachable.contains("crate::x::y"), "{reachable:?}");
assert!(
reachable.contains("crate::x::y::cross"),
"the nested #[path] inside y.rs is followed: {reachable:?}"
);
assert!(
remapped
.iter()
.any(|(_, module)| module == "crate::x::y::cross"),
"{remapped:?}"
);
}
#[test]
fn a_grandchild_of_a_probed_plain_child_is_governed() {
let tree = TempSrcTree::new("probed-child-grandchild");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("other/child")).expect("create temp dirs");
std::fs::write(
src.join("lib.rs"),
"#[path = \"other/weird.rs\"]\npub mod kernel;\n",
)
.expect("write lib.rs");
std::fs::write(src.join("other/weird.rs"), "pub mod child;\n").expect("write weird.rs");
std::fs::write(src.join("other/child.rs"), "pub mod grandchild;\n")
.expect("write other/child.rs");
let grandchild_file = src.join("other/child/grandchild.rs");
std::fs::write(&grandchild_file, "use crate::projection::Thing;\n")
.expect("write grandchild.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert!(
reachable.contains("crate::kernel::child::grandchild"),
"{reachable:?}"
);
assert!(
governed
.iter()
.any(|(file, module)| file == &grandchild_file
&& module == "crate::kernel::child::grandchild"),
"the probed child's own grandchild is governed under its logical path: {governed:?}"
);
}
#[test]
fn a_stray_file_at_a_remapped_modules_naive_structural_path_is_not_phantom_governed() {
let tree = TempSrcTree::new("remap-stray-structural-sibling");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("other")).expect("create temp src/other");
std::fs::create_dir_all(src.join("kernel")).expect("create temp src/kernel");
std::fs::write(
src.join("lib.rs"),
"#[path = \"other/weird.rs\"]\npub mod kernel;\n",
)
.expect("write lib.rs");
std::fs::write(src.join("other/weird.rs"), "pub mod child;\n").expect("write weird.rs");
let real_child = src.join("other/child.rs");
std::fs::write(&real_child, "// the real, rustc-compiled child\n").expect("write real child");
std::fs::write(
src.join("kernel/child.rs"),
"this is not even valid rust syntax {{{",
)
.expect("write stray file");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert!(
governed
.iter()
.any(|(file, module)| file == &real_child && module == "crate::kernel::child"),
"the real probed child is governed: {governed:?}"
);
assert_eq!(
governed
.iter()
.filter(|(_, module)| module == "crate::kernel::child")
.count(),
1,
"the stray file at the naive structural location must NOT be phantom-governed alongside the real one: {governed:?}"
);
}
#[test]
fn a_plain_file_sibling_of_a_path_remap_is_still_governed() {
let tree = TempSrcTree::new("cfg-plain-path-sibling");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[cfg(unix)]\npub mod x;\n#[cfg(windows)]\n#[path = \"windows_x.rs\"]\npub mod x;\n",
)
.expect("write lib.rs");
let plain = src.join("x.rs");
std::fs::write(&plain, "use crate::projection::Unix;\n").expect("write x.rs");
let remapped_target = src.join("windows_x.rs");
std::fs::write(&remapped_target, "use crate::projection::Windows;\n")
.expect("write windows_x.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert!(reachable.contains("crate::x"), "{reachable:?}");
assert!(
governed.iter().any(|(f, _)| f == &plain),
"the plain-file sibling must still be governed, not suppressed by the #[path] \
sibling: {governed:?}"
);
assert!(
governed.iter().any(|(f, _)| f == &remapped_target),
"the #[path] sibling's real target must also be governed: {governed:?}"
);
assert!(
!remap_shadowed.contains("crate::x"),
"a plain-file sibling means x.rs is real, not an orphan-shadow: {remap_shadowed:?}"
);
}
#[test]
fn an_inline_sibling_of_a_path_remap_is_still_governed() {
let tree = TempSrcTree::new("cfg-inline-path-sibling");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("x")).expect("mkdir x");
std::fs::write(
src.join("lib.rs"),
"#[cfg(unix)]\npub mod x {\n pub mod y;\n}\n#[cfg(windows)]\n#[path = \"windows_x.rs\"]\npub mod x;\n",
)
.expect("write lib.rs");
std::fs::write(src.join("x/y.rs"), "use crate::projection::Unix;\n").expect("write x/y.rs");
let remapped_target = src.join("windows_x.rs");
std::fs::write(&remapped_target, "use crate::projection::Windows;\n")
.expect("write windows_x.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert!(
reachable.contains("crate::x::y"),
"the inline sibling's own file-backed child must still be reachable: {reachable:?}"
);
assert!(
remapped
.iter()
.any(|(f, m)| f == &remapped_target && m == "crate::x"),
"the #[path] sibling's real target must also be followed: {remapped:?}"
);
assert!(
governed
.iter()
.any(|(f, m)| f == &remapped_target && m == "crate::x"),
"crate::x is governed via its #[path] target regardless of the inline sibling: {governed:?}"
);
}
#[test]
fn an_inline_sibling_of_a_plain_file_is_still_governed() {
let tree = TempSrcTree::new("cfg-plain-inline-sibling");
let src = tree.src().to_path_buf();
std::fs::create_dir_all(src.join("x")).expect("mkdirs");
std::fs::write(
src.join("lib.rs"),
"#[cfg(not(feature = \"b\"))]\npub mod x;\n#[cfg(feature = \"b\")]\npub mod x {\n pub mod y;\n}\n",
)
.expect("write lib.rs");
let plain = src.join("x.rs");
std::fs::write(&plain, "use crate::projection::Plain;\n").expect("write x.rs");
let inline_child = src.join("x/y.rs");
std::fs::write(&inline_child, "use crate::projection::InlineChild;\n").expect("write x/y.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, _remapped, _remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
assert!(
!inline_only.contains_key("crate::x"),
"a plain file is declared, so crate::x is not inline-only: {inline_only:?}"
);
assert!(
reachable.contains("crate::x::y"),
"the inline sibling's own file-backed child must still be reachable even though a \
plain-file sibling of crate::x also exists: {reachable:?}"
);
}
#[test]
fn governed_files_does_not_duplicate_a_plain_files_own_path_remap_target() {
let tree = TempSrcTree::new("duplicate-remap-target");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[cfg(not(feature = \"b\"))]\npub mod a;\n#[cfg(feature = \"b\")]\n#[path = \"a.rs\"]\npub mod a;\n",
)
.expect("write lib.rs");
std::fs::write(src.join("a.rs"), "use crate::projection::Thing;\n").expect("write a.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
let a_entries: Vec<_> = governed
.iter()
.filter(|(_, module)| module == "crate::a")
.collect();
assert_eq!(
a_entries.len(),
1,
"the plain sibling and its own #[path] target are the same file — governed once, \
not twice: {governed:?}"
);
}
#[test]
fn an_inner_attribute_is_no_attribute_of_the_mod_after_it() {
let mut mismatches = Vec::new();
for (lib, module, expected) in [
("#![cfg(unix)]\nmod c;\n", "crate::c", "src/c.rs"),
("#![path = \"x.rs\"]\nmod c;\n", "crate::c", "src/c.rs"),
(
"mod tests { #![cfg(test)] mod c; }\n",
"crate::tests::c",
"src/tests/c.rs",
),
] {
let tree = TempSrcTree::new("inner-attribute");
let src = tree.src().to_path_buf();
std::fs::write(src.join("lib.rs"), lib).expect("write lib.rs");
std::fs::write(src.join("x.rs"), "\n").expect("write x.rs");
let files = rust_files(&src).expect("list files");
let result = reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018);
let err = result.err().unwrap_or_default();
let located = format!(
"module '{module}' is declared (`mod c;`) but its source file could not be located"
);
if !(err.contains(&located) && err.contains(expected)) {
mismatches.push(format!("{lib:?}: {err}"));
}
}
assert!(
mismatches.is_empty(),
"a missing file for a `mod` after an inner attribute is a scan error naming it:\n{}",
mismatches.join("\n")
);
}
#[test]
fn a_path_attribute_is_read_by_its_position() {
const DIRECT_THEN_DIRECT: &str = "#[path = \"a.rs\"]\n#[path = \"b.rs\"]";
const DIRECT_THEN_CFG: &str = "#[path = \"a.rs\"]\n#[cfg_attr(unix, path = \"b.rs\")]";
const CFG_THEN_DIRECT: &str = "#[cfg_attr(unix, path = \"b.rs\")]\n#[path = \"a.rs\"]";
const INLINE_DIRECT_THEN_CFG: &str = "#[path = \"d\"]\n#[cfg_attr(unix, path = \"c\")]";
const INLINE_DIRECT_THEN_DIRECT: &str = "#[path = \"d\"]\n#[path = \"c\"]";
const INLINE_CFG_THEN_DIRECT: &str = "#[cfg_attr(unix, path = \"c\")]\n#[path = \"d\"]";
type Row<'a> = (&'a str, &'a str, bool, bool, Result<&'a [&'a str], &'a str>);
let rows: [Row; 11] = [
("dd-both", DIRECT_THEN_DIRECT, false, true, Ok(&["a.rs"])),
(
"dd-first-only",
DIRECT_THEN_DIRECT,
false,
false,
Ok(&["a.rs"]),
),
("dc-both", DIRECT_THEN_CFG, false, true, Ok(&["a.rs"])),
(
"dc-first-only",
DIRECT_THEN_CFG,
false,
false,
Ok(&["a.rs"]),
),
(
"cd-both",
CFG_THEN_DIRECT,
false,
true,
Ok(&["a.rs", "b.rs"]),
),
(
"cd-direct-only",
CFG_THEN_DIRECT,
false,
false,
Ok(&["a.rs"]),
),
(
"inline-dc-both",
INLINE_DIRECT_THEN_CFG,
true,
true,
Ok(&["d/k.rs"]),
),
(
"inline-dc-first-only",
INLINE_DIRECT_THEN_CFG,
true,
false,
Ok(&["d/k.rs"]),
),
(
"inline-dd-first-only",
INLINE_DIRECT_THEN_DIRECT,
true,
false,
Ok(&["d/k.rs"]),
),
(
"inline-cd-both",
INLINE_CFG_THEN_DIRECT,
true,
true,
Ok(&["c/k.rs", "d/k.rs"]),
),
(
"inline-cd-direct-only",
INLINE_CFG_THEN_DIRECT,
true,
false,
Err("c/k.rs"),
),
];
let mut mismatches = Vec::new();
for (row, attrs, inline, later, expected) in rows {
let tree = TempSrcTree::new(row);
let src = tree.src().to_path_buf();
let (module, first, second) = if inline {
std::fs::create_dir_all(src.join("c")).expect("create c");
std::fs::create_dir_all(src.join("d")).expect("create d");
("crate::m::k", "d/k.rs", "c/k.rs")
} else {
("crate::m", "a.rs", "b.rs")
};
let body = if inline {
"mod m { pub mod k; }"
} else {
"mod m;"
};
std::fs::write(src.join("lib.rs"), format!("{attrs}\n{body}\n")).expect("write lib.rs");
std::fs::write(src.join(first), "\n").expect("write the first path's file");
if later {
std::fs::write(src.join(second), "\n").expect("write the later path's file");
}
let files = rust_files(&src).expect("list files");
let walked = reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018);
let answer: Result<Vec<String>, String> =
walked.map(|(reachable, inline_only, remapped, remap_shadowed)| {
let mut governed: Vec<String> = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
)
.into_iter()
.filter(|(_, m)| m == module)
.map(|(file, _)| {
file.strip_prefix(&src)
.expect("a governed file lies under src")
.to_string_lossy()
.replace('\\', "/")
})
.collect();
governed.sort();
governed
});
let matches = match (&answer, expected) {
(Ok(governed), Ok(files)) => governed == files,
(Err(err), Err(file)) => err.contains(file) && err.contains("could not be located"),
_ => false,
};
if !matches {
mismatches.push(format!("{row}: {answer:?}"));
}
}
assert!(mismatches.is_empty(), "{}", mismatches.join("\n"));
}
#[test]
fn an_inline_modules_direct_path_after_a_candidate_is_descended_as_a_candidate() {
for (row, candidate_exists, expected) in [
(
"inline-cd-candidate-only",
true,
Ok(vec!["c/k.rs".to_string()]),
),
("inline-cd-neither", false, Err("d/k.rs")),
] {
let tree = TempSrcTree::new(row);
let src = tree.src().to_path_buf();
if candidate_exists {
std::fs::create_dir_all(src.join("c")).expect("create c");
std::fs::write(src.join("c/k.rs"), "\n").expect("write c/k.rs");
}
std::fs::write(
src.join("lib.rs"),
"#[cfg_attr(unix, path = \"c\")]\n#[path = \"d\"]\nmod m { pub mod k; }\n",
)
.expect("write lib.rs");
let files = rust_files(&src).expect("list files");
let walked = reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2021);
let answer: Result<Vec<String>, String> =
walked.map(|(reachable, inline_only, remapped, remap_shadowed)| {
let mut governed: Vec<String> = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
)
.into_iter()
.filter(|(_, m)| m == "crate::m::k")
.map(|(file, _)| {
file.strip_prefix(&src)
.expect("a governed file lies under src")
.to_string_lossy()
.replace('\\', "/")
})
.collect();
governed.sort();
governed
});
match (&answer, expected) {
(Ok(governed), Ok(files)) => assert_eq!(governed, &files, "{row}"),
(Err(err), Err(file)) => assert!(
err.contains(file) && err.contains("could not be located"),
"{row}: {err}"
),
_ => panic!("{row}: {answer:?}"),
}
}
}
#[test]
fn a_declaration_a_predicate_may_remove_or_redirect_tolerates_its_absent_file() {
let mut mismatches = Vec::new();
for (name, lib, file, governed) in [
(
"direct-after-candidate",
"#[cfg_attr(unix, path = \"b.rs\")]\n#[path = \"a.rs\"]\nmod m;\n",
Some("b.rs"),
Some("b.rs"),
),
(
"cfg-inside-cfg-attr",
"#[cfg_attr(all(), cfg(any()))]\nmod m;\n",
None,
None,
),
] {
let tree = TempSrcTree::new(name);
let src = tree.src().to_path_buf();
std::fs::write(src.join("lib.rs"), lib).expect("write lib.rs");
if let Some(file) = file {
std::fs::write(src.join(file), "\n").expect("write the candidate");
}
let files = rust_files(&src).expect("list files");
match reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018) {
Ok((reachable, inline_only, remapped, remap_shadowed)) => {
let found: Vec<String> = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
)
.into_iter()
.filter(|(_, module)| module == "crate::m")
.map(|(file, _)| file.file_name().unwrap().to_string_lossy().into_owned())
.collect();
if found.first().map(String::as_str) != governed {
mismatches.push(format!("{name}: governed {found:?}"));
}
}
Err(err) => mismatches.push(format!("{name}: {err}")),
}
}
assert!(mismatches.is_empty(), "{}", mismatches.join("\n"));
}
#[test]
fn block_declared_path_modules_are_numbered_apart() {
let tree = TempSrcTree::new("block-path-mods");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"pub fn f() { #[path = \"x.rs\"] mod m; }\npub fn g() { #[path = \"y.rs\"] mod m; }\n",
)
.expect("write lib.rs");
std::fs::write(src.join("x.rs"), "\n").expect("write x.rs");
std::fs::write(src.join("y.rs"), "\n").expect("write y.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let mut governed: Vec<(String, String)> = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
)
.into_iter()
.map(|(file, module)| {
(
file.file_name().unwrap().to_string_lossy().into_owned(),
module,
)
})
.collect();
governed.sort();
assert_eq!(
governed,
[
("lib.rs".to_string(), "crate".to_string()),
("x.rs".to_string(), "crate::{block}::m".to_string()),
("y.rs".to_string(), "crate::{block 2}::m".to_string()),
]
);
}
#[cfg(unix)]
#[test]
fn a_path_in_a_symlinked_file_resolves_from_the_symlinks_directory() {
let tree = TempSrcTree::new("symlink-path-base");
let src = tree.src().to_path_buf();
let elsewhere = src.parent().expect("src has a parent").join("elsewhere");
std::fs::create_dir_all(&elsewhere).expect("create elsewhere");
std::fs::write(src.join("lib.rs"), "pub mod a;\n").expect("write lib.rs");
std::fs::write(elsewhere.join("a.rs"), "#[path = \"x.rs\"]\npub mod k;\n").expect("write a.rs");
std::os::unix::fs::symlink("../elsewhere/a.rs", src.join("a.rs")).expect("create the symlink");
std::fs::write(src.join("x.rs"), "\n").expect("write x.rs");
let files = rust_files(&src).expect("list files");
let (reachable, inline_only, remapped, remap_shadowed) =
reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.expect("walk modules");
let governed = governed_files(
&src,
&files,
"crate",
&reachable,
&inline_only,
&remapped,
&remap_shadowed,
None,
ScanDepth::Subtree,
);
assert!(
governed
.iter()
.any(|(file, module)| file == &src.join("x.rs") && module == "crate::a::k"),
"{governed:?}"
);
}
#[test]
fn a_direct_path_after_a_candidate_no_file_backs_is_a_scan_error() {
let tree = TempSrcTree::new("direct-after-absent-candidate");
let src = tree.src().to_path_buf();
std::fs::write(
src.join("lib.rs"),
"#[cfg_attr(unix, path = \"b.rs\")]\n#[path = \"a.rs\"]\nmod m;\n",
)
.expect("write lib.rs");
let files = rust_files(&src).expect("list files");
let err = reachable_modules(&src, &files, None, crate::module_scan::Edition::Rust2018)
.err()
.unwrap_or_default();
assert!(
err.contains("a.rs") && err.contains("does not exist"),
"{err}"
);
}