use super::helpers::*;
#[cfg(unix)]
#[test]
pub(super) fn unreadable_governed_file_is_a_scan_error() {
let ws = TempWorkspace::new("unreadable");
let file = ws.write("lib.rs", "use crate::forbidden::Thing;\n");
let Some(_unreadable) = xingbiao::Unreadable::try_new(&file) else {
return;
};
let metadata = ws.metadata("x");
let boundary = ModuleBoundary::in_crate("x")
.module("crate")
.must_not_import("crate::forbidden")
.because("the test module must not import the forbidden module");
let mut violations = Vec::new();
let result = check_module_boundary(&metadata, &boundary, &mut violations);
assert!(
result.is_err(),
"an unreadable governed file must be a scan error, not a silent skip"
);
}
#[cfg(unix)]
#[test]
pub(super) fn unreadable_governed_directory_is_a_scan_error() {
let ws = TempWorkspace::new("unreadable-dir");
ws.write("lib.rs", "// nothing\n");
ws.write("sub/inner.rs", "use crate::forbidden::Thing;\n");
let sub = ws.src().join("sub");
let Some(_unreadable) = xingbiao::Unreadable::try_new(&sub) else {
return;
};
let metadata = ws.metadata("x");
let boundary = ModuleBoundary::in_crate("x")
.module("crate")
.must_not_import("crate::forbidden")
.because("the test module must not import the forbidden module");
let mut violations = Vec::new();
let result = check_module_boundary(&metadata, &boundary, &mut violations);
assert!(
result.is_err(),
"an unreadable governed directory must be a scan error, not a silent skip"
);
}
#[test]
pub(super) fn a_raw_identifier_module_is_governed_and_its_import_observed() {
let ws = TempWorkspace::new("rawid");
ws.write("lib.rs", "pub mod r#type;\npub mod r#mod {}\n");
ws.write("type.rs", "use crate::r#mod::Thing;\n");
let metadata = ws.metadata("x");
let boundary = ModuleBoundary::in_crate("x")
.module("crate::type")
.must_not_import("crate::mod")
.because("a raw-identifier module must be governable");
let mut violations = Vec::new();
let result = check_module_boundary(&metadata, &boundary, &mut violations);
assert!(
result.is_ok(),
"a raw-identifier module must be found, not an unknown-module error: {result:?}"
);
assert_eq!(
violations.len(),
1,
"the forbidden import from inside the raw-identifier module must be observed: {violations:?}"
);
assert_eq!(violations[0].target(), "crate::type");
assert_eq!(violations[0].finding, "crate::mod::Thing");
}
#[test]
pub(super) fn module_boundary_uses_the_package_target_src_path() {
let ws = TempWorkspace::new("custom-lib-path");
let root = ws.write_at("lib.rs", "pub mod kernel;\npub mod io {}\n");
ws.write_at("kernel.rs", "use crate::io::Sink;\n");
let manifest = ws.dir().join("Cargo.toml");
let metadata = serde_json::json!({
"packages": [{
"name": "x",
"manifest_path": manifest.to_string_lossy().into_owned(),
"dependencies": [],
"targets": [{
"kind": ["lib"],
"src_path": root.to_string_lossy().into_owned()
}]
}]
});
let boundary = ModuleBoundary::in_crate("x")
.module("crate::kernel")
.must_not_import("crate::io")
.because("module boundaries must scan the compiled source root");
let mut violations = Vec::new();
let result = check_module_boundary(&metadata, &boundary, &mut violations);
assert!(
result.is_ok(),
"a custom [lib] path must not be misresolved to manifest_dir/src: {result:?}"
);
assert_eq!(
violations.len(),
1,
"the forbidden import under the custom source root must be observed"
);
assert_eq!(violations[0].finding, "crate::io::Sink");
}
#[test]
pub(super) fn path_remapped_module_is_followed_not_governed_via_a_conventional_orphan() {
let (result, violations) = run_module_check(
"path-remap-boundary",
&[
(
"lib.rs",
"pub mod projection {}\n#[path = \"weird.rs\"]\npub mod kernel;\n",
),
("weird.rs", "use crate::projection::Thing;\n"),
("kernel.rs", "use crate::projection::Wrong;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::kernel")
.must_not_import("crate::projection")
.because("closing the #[path]-following divergence from 渾儀/漏刻"),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(violations[0].target(), "crate::kernel");
assert_eq!(
violations[0].finding, "crate::projection::Thing",
"the real target's import is observed, never the orphan's: {violations:?}"
);
let file = violations[0]
.file
.as_deref()
.expect("a module-import violation carries its source file");
assert!(
file.ends_with("weird.rs"),
"the violation names the real #[path] target, not the conventional orphan: {file}"
);
}
#[test]
pub(super) fn an_unconditional_path_on_an_inline_module_relocates_its_own_file_form_children() {
let (result, violations) = run_module_check(
"inline-path-relocate",
&[
(
"lib.rs",
"#[path = \"thread_files\"]\npub mod thread {\n pub mod local_data;\n}\n\
pub mod secret { pub struct Thing; }\n",
),
("thread_files/local_data.rs", "use crate::secret::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::thread")
.must_not_import("crate::secret")
.because("an inline module's #[path] must relocate its own children, not no-op"),
);
result.expect("the relocated child must be a valid, governable target");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(violations[0].target(), "crate::thread");
let file = violations[0]
.file
.as_deref()
.expect("a module-import violation carries its source file");
assert!(
file.ends_with("thread_files/local_data.rs")
|| file.ends_with("thread_files\\local_data.rs"),
"the violation must name the #[path]-relocated file, not a naive thread/local_data.rs: {file}"
);
}
#[test]
pub(super) fn an_inline_module_target_is_a_self_describing_constitution_error() {
let ws = TempWorkspace::new("inline");
ws.write(
"lib.rs",
"pub mod kernel { use crate::projection::Thing; }\npub mod projection { pub struct Thing; }\n",
);
let metadata = ws.metadata("app");
let inline = ModuleBoundary::in_crate("app")
.module("crate::kernel")
.must_not_import("crate::projection")
.because("the kernel must not import a projection");
let mut violations = Vec::new();
let inline_err = check_module_boundary(&metadata, &inline, &mut violations)
.expect_err("an inline target must be a constitution error");
assert_eq!(
inline_err,
inline_module_target_error(
"crate::kernel",
"app",
"kernel",
Some("lib.rs"),
"kernel.rs"
)
);
assert_ne!(inline_err, unknown_module_error("crate::kernel", "app"));
let typo = ModuleBoundary::in_crate("app")
.module("crate::ghost")
.must_not_import("crate::projection")
.because("typo");
let typo_err = check_module_boundary(&metadata, &typo, &mut violations)
.expect_err("an unknown module is a constitution error");
assert_eq!(typo_err, unknown_module_error("crate::ghost", "app"));
}
#[test]
pub(super) fn an_inline_target_with_no_reported_target_names_no_compilation_unit() {
let ws = TempWorkspace::new("inline-notarget");
ws.write("lib.rs", "pub mod kernel { use crate::secret::Thing; }\n");
let metadata = serde_json::json!({
"packages": [{
"name": "x",
"manifest_path": ws.dir().join("Cargo.toml").to_string_lossy(),
}],
});
let boundary = ModuleBoundary::in_crate("x")
.module("crate::kernel")
.must_not_import("crate::secret")
.because("the kernel must not import a secret");
let mut violations = Vec::new();
let err = check_module_boundary(&metadata, &boundary, &mut violations)
.expect_err("an inline target on the no-target fallback is still the inline refusal");
assert!(
err.contains("declared inline"),
"expected the inline-target refusal, got: {err}"
);
assert!(
err.contains("`src/kernel.rs`"),
"the suggested extraction path resolves from the conventional source directory: {err}"
);
assert!(
!err.contains("compilation unit"),
"no target was reported, so there is no compilation unit to name: {err}"
);
}
#[test]
pub(super) fn an_inline_target_with_a_same_named_orphan_file_is_still_a_constitution_error() {
let (result, _) = run_module_check(
"inline-orphan",
&[
("lib.rs", "pub mod kernel { use crate::secret::Thing; }\n"),
("kernel.rs", "// clean — no forbidden import\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::kernel")
.must_not_import("crate::secret")
.because("the kernel must not import a secret"),
);
let err = result.expect_err(
"an inline target must stay the inline constitution error even with a same-named \
orphan file — governing the orphan and missing the inline body is the forbidden \
false negative, never a silent pass",
);
assert_eq!(
err,
inline_module_target_error("crate::kernel", "x", "kernel", Some("lib.rs"), "kernel.rs")
);
}
#[test]
pub(super) fn an_orphan_beside_an_inline_module_contributes_no_phantom_child() {
let (result, _) = run_module_check(
"inline-phantom",
&[
("lib.rs", "pub mod kernel { }\n"),
("kernel.rs", "pub mod deep;\n"), ("kernel/deep.rs", "use crate::secret::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::kernel::deep")
.must_not_import("crate::secret")
.because("deep must not import a secret"),
);
let err = result.expect_err("a phantom child of an orphan is not a reachable module");
assert_eq!(err, unknown_module_error("crate::kernel::deep", "x"));
}
#[test]
pub(super) fn a_file_backed_module_is_still_governed() {
let (result, violations) = run_module_check(
"file-backed",
&[
("lib.rs", "pub mod secret {}\npub mod real;\n"),
("real.rs", "use crate::secret::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::real")
.must_not_import("crate::secret")
.because("real must not import a secret"),
);
result.expect("a file-backed module is a valid, governable target");
assert!(
!violations.is_empty(),
"the file-backed module's forbidden import must still be observed"
);
}
#[test]
pub(super) fn a_cfg_dual_declared_module_keeps_governing_its_conventional_file() {
let (result, violations) = run_module_check(
"cfg-dual",
&[
(
"lib.rs",
"pub mod secret {}\n#[cfg(feature = \"k\")]\npub mod kernel;\n\
#[cfg(not(feature = \"k\"))]\npub mod kernel { }\n",
),
("kernel.rs", "use crate::secret::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::kernel")
.must_not_import("crate::secret")
.because("kernel must not import a secret"),
);
result.expect("a cfg-dual-declared module keeps its conventional file as a valid target");
assert!(
!violations.is_empty(),
"the conventional file must still be observed — the cfg-blind bound is unchanged"
);
}
#[test]
pub(super) fn a_dual_backed_module_is_a_scan_error_not_silently_accepted() {
let (result, _violations) = run_module_check(
"dual-backed",
&[
("lib.rs", "pub mod child;\n"),
("child.rs", "// flat form\n"),
("child/mod.rs", "// nested form\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::child")
.must_not_import("crate::forbidden")
.because("child must not import forbidden"),
);
let err = result.expect_err(
"both conventional forms present is a genuine ambiguity, never a silent accept",
);
assert!(
err.contains("resolves to both") && err.contains("child.rs") && err.contains("mod.rs"),
"the error must name the ambiguity and both real files: {err}"
);
}
#[test]
pub(super) fn an_unconditional_missing_plain_module_file_is_a_scan_error_not_a_silent_gap() {
let (result, _violations) = run_module_check(
"missing-plain-unconditional",
&[("lib.rs", "pub mod child;\n")],
ModuleBoundary::in_crate("x")
.module("crate::child")
.must_not_import("crate::forbidden")
.because("child must not import forbidden"),
);
let err = result.expect_err(
"an unconditional plain mod with no backing file must fail loud, never silently vanish",
);
assert!(
err.contains("crate::child") && err.contains("could not be located"),
"the error must name the module and the missing-file cause: {err}"
);
}
#[test]
pub(super) fn a_cfg_gated_missing_plain_module_file_does_not_fail_an_unrelated_boundary() {
let (result, violations) = run_module_check(
"missing-plain-cfg-gated",
&[
(
"lib.rs",
"pub mod forbidden {}\n#[cfg(feature = \"absent\")]\npub mod child;\npub mod present;\n",
),
("present.rs", "use crate::forbidden::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::present")
.must_not_import("crate::forbidden")
.because("present must not import forbidden"),
);
result.expect("a #[cfg]-gated missing sibling must not fail an unrelated boundary");
assert_eq!(
violations.len(),
1,
"the unrelated boundary must still observe its own real violation: {violations:?}"
);
}
#[test]
pub(super) fn a_missing_module_file_declared_inside_a_cfg_if_arm_is_tolerated() {
let (result, violations) = run_module_check(
"missing-plain-cfg-if-arm",
&[
(
"lib.rs",
"pub mod forbidden {}\ncfg_if::cfg_if! {\n\
if #[cfg(unix)] {\n\
pub mod unix_impl;\n\
} else {\n\
pub mod windows_impl;\n\
}\n\
}\n\
pub mod present;\n",
),
("unix_impl.rs", "// clean\n"),
("present.rs", "use crate::forbidden::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::present")
.must_not_import("crate::forbidden")
.because("present must not import forbidden"),
);
result.expect("an arm-declared module with no file must not fail an unrelated boundary");
assert_eq!(
violations.len(),
1,
"the unrelated boundary must still observe its own real violation: {violations:?}"
);
}
#[test]
pub(super) fn a_module_in_an_unlabelled_group_under_cfg_if_is_judged() {
let (result, violations) = run_module_check(
"unlabelled-cfg-if-group",
&[(
"lib.rs",
"pub mod forbidden { pub struct T; }\n\
macro_rules! cfg_if { ($($t:tt)*) => {} }\n\
cfg_if! { { mod m; } }\n\
use forbidden::T;\n",
)],
ModuleBoundary::in_crate("x")
.module("crate")
.must_not_import("crate::forbidden")
.because("probe"),
);
assert!(
result.is_ok(),
"a module in an unlabelled group under cfg_if! must be judged: {result:?}"
);
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(violations[0].finding, "crate::forbidden::T");
}
#[test]
pub(super) fn an_arm_declared_module_whose_file_exists_is_still_governed() {
let (result, violations) = run_module_check(
"present-plain-cfg-if-arm",
&[
(
"lib.rs",
"pub mod forbidden {}\ncfg_if::cfg_if! {\n\
if #[cfg(unix)] {\n\
pub mod unix_impl;\n\
} else {\n\
pub mod windows_impl;\n\
}\n\
}\n",
),
("unix_impl.rs", "use crate::forbidden::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::unix_impl")
.must_not_import("crate::forbidden")
.because("the present arm's module is still observed"),
);
result.expect("the present arm's module must resolve");
assert_eq!(
violations.len(),
1,
"the arm module whose file exists must still be governed: {violations:?}"
);
}
#[test]
pub(super) fn a_dual_backed_module_declared_inside_a_cfg_if_arm_is_still_a_scan_error() {
let (result, _violations) = run_module_check(
"dual-backed-cfg-if-arm",
&[
(
"lib.rs",
"cfg_if::cfg_if! {\n\
if #[cfg(unix)] {\n\
pub mod child;\n\
}\n\
}\n",
),
("child.rs", "// flat form\n"),
("child/mod.rs", "// nested form\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::child")
.must_not_import("crate::forbidden")
.because("an arm-declared dual-backed module is still unresolvable"),
);
let err = result.expect_err("both conventional forms present is never tolerated");
assert!(
err.contains("resolves to both"),
"the ambiguity must be reported, not tolerated as a cfg-conditional absence: {err}"
);
}
#[test]
pub(super) fn a_cfg_attr_decorated_missing_module_file_is_not_tolerated() {
let (result, _violations) = run_module_check(
"missing-plain-cfg-attr",
&[(
"lib.rs",
"#[cfg_attr(unix, allow(dead_code))]\npub mod child;\n",
)],
ModuleBoundary::in_crate("x")
.module("crate::child")
.must_not_import("crate::forbidden")
.because("a cfg_attr-decorated missing file is not cfg-conditional"),
);
let err = result.expect_err("cfg_attr must not grant the absent-file tolerance");
assert!(
err.contains("could not be located"),
"the absence must be reported, not tolerated: {err}"
);
}
#[test]
pub(super) fn a_cfg_gated_dual_backed_module_is_still_a_scan_error() {
let (result, _violations) = run_module_check(
"dual-backed-cfg-gated",
&[
("lib.rs", "#[cfg(feature = \"never\")]\npub mod child;\n"),
("child.rs", "// flat form\n"),
("child/mod.rs", "// nested form\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::child")
.must_not_import("crate::forbidden")
.because("a cfg-gated dual-backed module is still unresolvable"),
);
let err = result.expect_err("both conventional forms present is never tolerated");
assert!(
err.contains("resolves to both"),
"the ambiguity must be reported, not tolerated as a cfg-conditional absence: {err}"
);
}
#[test]
pub(super) fn a_missing_path_remap_target_declared_inside_a_cfg_if_arm_is_tolerated() {
let (result, violations) = run_module_check(
"missing-path-cfg-if-arm",
&[
(
"lib.rs",
"pub mod forbidden {}\ncfg_if::cfg_if! {\n\
if #[cfg(windows)] {\n\
#[path = \"windows_impl.rs\"]\n\
pub mod imp;\n\
}\n\
}\n\
pub mod present;\n",
),
("present.rs", "use crate::forbidden::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::present")
.must_not_import("crate::forbidden")
.because("present must not import forbidden"),
);
result.expect("an arm-declared #[path] with a missing target must not fail the scan");
assert_eq!(
violations.len(),
1,
"the unrelated boundary must still observe its own real violation: {violations:?}"
);
}
#[test]
pub(super) fn a_boundary_anchored_directly_at_a_cfg_gated_missing_module_is_unknown_not_clean() {
let (result, _violations) = run_module_check(
"missing-plain-cfg-gated-anchor",
&[("lib.rs", "#[cfg(feature = \"absent\")]\npub mod child;\n")],
ModuleBoundary::in_crate("x")
.module("crate::child")
.must_not_import("crate::forbidden")
.because("child must not import forbidden"),
);
let err = result.expect_err(
"anchoring directly at a module absent on this build must fail loud, never vacuously pass",
);
assert_eq!(err, unknown_module_error("crate::child", "x"));
}
#[test]
pub(super) fn an_inline_arm_paired_with_a_tolerated_away_plain_arm_still_reports_the_inline_error()
{
let (result, _violations) = run_module_check(
"inline-plus-tolerated-plain",
&[(
"lib.rs",
"#[cfg(unix)]\npub mod engine { pub struct A; }\n\
#[cfg(windows)]\npub mod engine;\n",
)],
ModuleBoundary::in_crate("x")
.module("crate::engine")
.must_not_import("crate::forbidden")
.because("engine must not import forbidden"),
);
let err = result.expect_err(
"an inline arm alongside a tolerated-away plain arm is still an inline target, not unknown",
);
assert_eq!(
err,
inline_module_target_error("crate::engine", "x", "engine", Some("lib.rs"), "engine.rs")
);
}
#[test]
pub(super) fn a_cfg_gated_unconditional_path_target_does_not_fail_an_unrelated_boundary_when_missing()
{
let (result, violations) = run_module_check(
"cfg-gated-path-target-missing",
&[
(
"lib.rs",
"pub mod forbidden {}\n#[cfg(windows)]\n#[path = \"windows_impl.rs\"]\npub mod imp;\npub mod present;\n",
),
("present.rs", "use crate::forbidden::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::present")
.must_not_import("crate::forbidden")
.because("present must not import forbidden"),
);
result.expect("a #[cfg]-gated #[path] target with no file must not fail an unrelated boundary");
assert_eq!(
violations.len(),
1,
"the unrelated boundary must still observe its own real violation: {violations:?}"
);
}
#[test]
pub(super) fn a_cfg_gated_unconditional_path_target_is_tolerated_regardless_of_attribute_order() {
let (result, violations) = run_module_check(
"cfg-gated-path-target-order",
&[
(
"lib.rs",
"pub mod forbidden {}\n#[path = \"windows_impl.rs\"]\n#[cfg(windows)]\npub mod imp;\npub mod present;\n",
),
("present.rs", "use crate::forbidden::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::present")
.must_not_import("crate::forbidden")
.because("present must not import forbidden"),
);
result.expect("attribute order must not affect the bare-#[cfg] tolerance");
assert_eq!(
violations.len(),
1,
"the unrelated boundary must still observe its own real violation: {violations:?}"
);
}
#[test]
pub(super) fn a_cfg_attr_wrapped_path_is_recognized_as_a_remap() {
let (result, violations) = run_module_check(
"cfg-attr-path",
&[
(
"lib.rs",
"pub mod forbidden {}\n#[cfg_attr(unix, path = \"weird.rs\")]\npub mod foo;\n",
),
("foo.rs", "use crate::forbidden::Y;\n"),
("weird.rs", "// the cfg(unix) remap target, clean\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::foo")
.must_not_import("crate::forbidden")
.because("foo must not import forbidden"),
);
assert!(
result.is_ok(),
"a cfg_attr-remapped module performs union-scan over physically existing target files"
);
assert_eq!(violations.len(), 1);
assert_eq!(violations[0].finding, "crate::forbidden::Y");
}
pub(super) fn restrict_kernel_to_types(governed: &str, allowed: &[&str]) -> ModuleBoundary {
ModuleBoundary::in_crate("x")
.module(governed)
.restrict_imports_to(allowed.to_vec())
.because("the kernel may import only the allowed modules")
}
#[test]
pub(super) fn restrict_imports_to_flags_an_import_outside_the_allowlist() {
let (result, violations) = run_module_check(
"restrict-outside",
&[
("lib.rs", "pub mod types {}\npub mod kernel;\n"),
("kernel.rs", "use crate::io::Sink;\n"),
],
restrict_kernel_to_types("crate::kernel", &["crate::types"]),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(violations[0].target(), "crate::kernel");
assert_eq!(violations[0].finding, "crate::io::Sink");
}
#[test]
pub(super) fn a_module_violation_carries_its_offending_file() {
let (result, violations) = run_module_check(
"module-file",
&[
("lib.rs", "pub mod types {}\npub mod kernel;\n"),
("kernel.rs", "use crate::io::Sink;\n"),
],
restrict_kernel_to_types("crate::kernel", &["crate::types"]),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
let file = violations[0]
.file
.as_deref()
.expect("a module-import violation carries its source file");
assert!(
file.ends_with("kernel.rs"),
"file names the offending source: {file}"
);
}
#[test]
pub(super) fn a_module_backed_by_two_files_yields_one_violation_with_a_file() {
let (result, violations) = run_module_check(
"module-two-files",
&[
(
"lib.rs",
"pub mod forbidden;\n#[cfg(unix)]\n#[path = \"imp_unix.rs\"]\npub mod imp;\n\
#[cfg(not(unix))]\n#[path = \"imp_other.rs\"]\npub mod imp;\n",
),
("forbidden.rs", "pub struct Thing;\n"),
("imp_unix.rs", "use crate::forbidden::Thing;\n"),
("imp_other.rs", "use crate::forbidden::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::imp")
.must_not_import("crate::forbidden")
.because("imp must not import forbidden"),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(
violations.len(),
1,
"two files of one module collapse to one violation: {violations:?}"
);
assert_eq!(violations[0].finding, "crate::forbidden::Thing");
assert!(
violations[0].file.is_some(),
"the surviving violation carries a representative file"
);
}
#[test]
pub(super) fn restrict_imports_to_is_clean_within_the_allowlist() {
let (result, violations) = run_module_check(
"restrict-within",
&[
("lib.rs", "pub mod types {}\npub mod kernel;\n"),
("kernel.rs", "use crate::types::Id;\n"),
],
restrict_kernel_to_types("crate::kernel", &["crate::types"]),
);
assert!(result.is_ok(), "{result:?}");
assert!(violations.is_empty(), "{violations:?}");
}
#[test]
pub(super) fn restrict_imports_to_allows_the_governed_modules_own_subtree() {
let (result, violations) = run_module_check(
"restrict-ownsubtree",
&[
("lib.rs", "pub mod types {}\npub mod kernel;\n"),
(
"kernel.rs",
"use crate::kernel;\nuse crate::kernel::detail::Thing;\nuse self::other::Thing2;\n",
),
],
restrict_kernel_to_types("crate::kernel", &["crate::types"]),
);
assert!(result.is_ok(), "{result:?}");
assert!(violations.is_empty(), "own-subtree imports: {violations:?}");
}
#[test]
pub(super) fn restrict_imports_to_with_an_empty_allowlist_forbids_outward_imports() {
let (result, violations) = run_module_check(
"restrict-empty",
&[
("lib.rs", "pub mod kernel;\n"),
("kernel.rs", "use crate::types::Id;\n"),
],
restrict_kernel_to_types("crate::kernel", &[]),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(violations[0].finding, "crate::types::Id");
}
#[test]
pub(super) fn restrict_imports_to_does_not_treat_a_prefix_colliding_sibling_as_allowed() {
let (result, violations) = run_module_check(
"restrict-sibling",
&[
("lib.rs", "pub mod types {}\npub mod kernel;\n"),
(
"kernel.rs",
"use crate::types::Id;\nuse crate::types_extra::Y;\n",
),
],
restrict_kernel_to_types("crate::kernel", &["crate::types"]),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(
violations.len(),
1,
"only the sibling violates: {violations:?}"
);
assert_eq!(violations[0].finding, "crate::types_extra::Y");
}
#[test]
pub(super) fn restrict_imports_to_never_flags_an_external_import() {
let (result, violations) = run_module_check(
"restrict-external",
&[
("lib.rs", "pub mod kernel;\n"),
("kernel.rs", "use serde::Deserialize;\n"),
],
restrict_kernel_to_types("crate::kernel", &[]),
);
assert!(result.is_ok(), "{result:?}");
assert!(
violations.is_empty(),
"externals are out of scope: {violations:?}"
);
}
#[test]
pub(super) fn restrict_imports_to_governs_a_super_reaching_outward_import() {
let (result, violations) = run_module_check(
"restrict-super",
&[
("lib.rs", "pub mod types {}\npub mod kernel;\n"),
("kernel.rs", "use super::other::Thing;\n"),
],
restrict_kernel_to_types("crate::kernel", &["crate::types"]),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(
violations[0].finding, "crate::other::Thing",
"super:: resolves to an absolute outward path that is governed"
);
}
#[test]
pub(super) fn restrict_imports_to_canonicalizes_a_raw_identifier_allowlist_entry() {
let (result, violations) = run_module_check(
"restrict-rawid",
&[
("lib.rs", "pub mod r#type {}\npub mod kernel;\n"),
("kernel.rs", "use crate::r#type::Thing;\n"),
],
restrict_kernel_to_types("crate::kernel", &["crate::r#type"]),
);
assert!(result.is_ok(), "{result:?}");
assert!(
violations.is_empty(),
"the raw-id entry canonicalizes to match the import: {violations:?}"
);
}
#[test]
pub(super) fn restrict_imports_to_on_the_crate_root_is_a_constitution_error() {
let (result, _violations) = run_module_check(
"restrict-crate",
&[("lib.rs", "use crate::anything::X;\n")],
restrict_kernel_to_types("crate", &["crate::types"]),
);
let err = result.expect_err("governing `crate` must be a constitution error");
assert_eq!(err, restrict_imports_to_on_crate_error("x"));
}
#[test]
pub(super) fn restrict_imports_to_honors_warn_severity_and_its_distinct_label() {
let (result, violations) = run_module_check(
"restrict-warn",
&[
("lib.rs", "pub mod types {}\npub mod kernel;\n"),
("kernel.rs", "use crate::io::Sink;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::kernel")
.restrict_imports_to(["crate::types"])
.warn()
.because("the kernel should import only types"),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(violations[0].severity, Severity::Warn);
assert_eq!(violations[0].rule, "restrict imports to");
}
pub(super) fn protect_internal_from(importer: &str) -> ModuleBoundary {
ModuleBoundary::in_crate("x")
.module("crate::internal")
.must_not_be_imported_by(importer)
.because("internal is private to its layer")
}
#[test]
pub(super) fn must_not_be_imported_by_flags_the_forbidden_importer_only() {
let (result, violations) = run_module_check(
"inbound-basic",
&[
("lib.rs", "pub mod internal;\npub mod http;\npub mod api;\n"),
("internal.rs", "// protected\n"),
("http.rs", "use crate::internal::Secret;\n"),
("api.rs", "use crate::internal::Secret;\n"),
],
protect_internal_from("crate::http"),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(violations[0].target(), "crate::internal");
assert_eq!(violations[0].finding, "crate::http");
assert_eq!(violations[0].rule, "module must not be imported by");
}
#[test]
pub(super) fn must_not_be_imported_by_flags_ancestor_glob_import() {
let (result, violations) = run_module_check(
"inbound-ancestor-glob",
&[
("lib.rs", "pub mod internal;\npub mod http;\n"),
("internal.rs", "// protected\n"),
("http.rs", "use crate::*;\n"),
],
protect_internal_from("crate::http"),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(violations[0].target(), "crate::internal");
assert_eq!(violations[0].finding, "crate::http");
}
#[test]
pub(super) fn must_not_be_imported_by_flags_an_inline_module_importer() {
let (result, violations) = run_module_check(
"inbound-inline-importer",
&[
(
"lib.rs",
"pub mod internal;\nmod http { use crate::internal::Secret; }\n",
),
("internal.rs", "// protected\n"),
],
protect_internal_from("crate::http"),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(violations[0].target(), "crate::internal");
assert_eq!(violations[0].finding, "crate::http");
}
#[test]
pub(super) fn must_not_be_imported_by_applies_beneath_the_importer() {
let (result, violations) = run_module_check(
"inbound-beneath-importer",
&[
("lib.rs", "pub mod internal;\npub mod http;\n"),
("internal.rs", "// protected\n"),
("http.rs", "pub mod v1;\n"),
("http/v1.rs", "use crate::internal::Secret;\n"),
],
protect_internal_from("crate::http"),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(violations.len(), 1, "{violations:?}");
assert_eq!(
violations[0].finding, "crate::http::v1",
"the importer beneath crate::http is named"
);
}
#[test]
pub(super) fn must_not_be_imported_by_applies_beneath_the_protected_module() {
let (result, violations) = run_module_check(
"inbound-beneath-protected",
&[
("lib.rs", "pub mod internal;\npub mod http;\n"),
("internal.rs", "// protected\n"),
("http.rs", "use crate::internal::deep::Thing;\n"),
],
protect_internal_from("crate::http"),
);
assert!(result.is_ok(), "{result:?}");
assert_eq!(
violations.len(),
1,
"an import beneath the protected module violates: {violations:?}"
);
assert_eq!(violations[0].finding, "crate::http");
}
#[test]
pub(super) fn must_not_be_imported_by_ignores_prefix_colliding_siblings_on_both_sides() {
let (result, violations) = run_module_check(
"inbound-collision",
&[
(
"lib.rs",
"pub mod internal;\npub mod http;\npub mod httpx;\n",
),
("internal.rs", "// protected\n"),
("http.rs", "use crate::internal_util::X;\n"),
("httpx.rs", "use crate::internal::Secret;\n"),
],
protect_internal_from("crate::http"),
);
assert!(result.is_ok(), "{result:?}");
assert!(
violations.is_empty(),
"::-delimited containment must not match siblings on either side: {violations:?}"
);
}
#[test]
pub(super) fn must_not_be_imported_by_does_not_flag_the_protected_modules_own_subtree() {
let (result, violations) = run_module_check(
"inbound-own-subtree",
&[
("lib.rs", "pub mod a;\n"),
("a.rs", "pub mod b;\n"),
("a/b.rs", "use crate::a::b::detail::Thing;\n"),
],
ModuleBoundary::in_crate("x")
.module("crate::a::b")
.must_not_be_imported_by("crate::a")
.because("a::b is private"),
);
assert!(result.is_ok(), "{result:?}");
assert!(
violations.is_empty(),
"the protected module's own subtree is not an importer: {violations:?}"
);
}
#[test]
pub(super) fn a_walk_refusal_names_its_compilation_unit() {
let (result, _) = run_module_check(
"walk-refusal-unit",
&[
("lib.rs", "pub mod kernel;\nmod ghost;\n"),
("kernel.rs", ""),
],
ModuleBoundary::in_crate("x")
.module("crate::kernel")
.must_not_import("crate::ghost")
.because("the kernel must not import a ghost"),
);
let err = result.expect_err("a declared module with no file is a scan error");
assert!(
err.starts_with(
"cannot walk crate 'x' in compilation unit 'lib.rs': module 'crate::ghost'"
),
"{err}"
);
}
#[test]
pub(super) fn a_walk_refusal_names_where_each_declaration_is_written() {
let boundary = || {
ModuleBoundary::in_crate("x")
.module("crate::kernel")
.must_not_import("crate::ghost")
.because("the kernel must not import a ghost")
};
let (nested, _) = run_module_check(
"walk-refusal-declared-in",
&[
("lib.rs", "pub mod kernel;\npub mod a;\n"),
("kernel.rs", ""),
("a.rs", "mod ghost;\n"),
],
boundary(),
);
let nested = nested.expect_err("a declared module with no file is a scan error");
assert!(
nested.contains("could not be located (declared in '")
&& nested.contains("a.rs', expected '"),
"{nested}"
);
let (candidates, _) = run_module_check(
"walk-refusal-candidates",
&[
(
"lib.rs",
"pub mod kernel;\npub fn f() { #[cfg_attr(unix, path = \"d\")] \
#[cfg_attr(windows, path = \"e\")] mod k { pub mod m; } }\n",
),
("kernel.rs", ""),
],
boundary(),
);
let candidates = candidates.expect_err("a block module no candidate backs is a scan error");
let separator = std::path::MAIN_SEPARATOR;
for expected in [
format!("{separator}d{separator}m.rs"),
format!("{separator}e{separator}m.rs"),
] {
assert!(candidates.contains(&expected), "{expected} in {candidates}");
}
}