use std::path::Path;
use std::process::Command;
fn write_fixture_with_dangling_include(root: &Path) {
std::fs::create_dir_all(root.join("snippets")).expect("create snippets dir");
std::fs::create_dir_all(root.join("docs")).expect("create docs dir");
std::fs::write(
root.join("snippets/hello.md"),
"# Hello\n\n```json\n{\"hello\": \"world\"}\n```\n",
)
.expect("write snippet fixture");
std::fs::write(root.join("docs/guide.md"), "# Guide\n\n--8<-- \"missing-fixture.md\"\n")
.expect("write docs fixture");
std::fs::write(
root.join("alef.toml"),
r#"
[workspace]
languages = ["python"]
[workspace.docs.snippets]
dirs = ["snippets"]
docs_dirs = ["docs"]
[[crates]]
name = "fixture-crate"
sources = ["src/lib.rs"]
"#,
)
.expect("write alef.toml");
}
fn write_fixture_with_missing_inline_dir(root: &Path) {
std::fs::create_dir_all(root.join("snippets")).expect("create snippets dir");
std::fs::write(root.join("snippets/hello.md"), "```json\n{\"hello\": \"world\"}\n```\n")
.expect("write snippet fixture");
std::fs::write(
root.join("alef.toml"),
r#"
[workspace]
languages = ["python"]
[workspace.docs.snippets]
dirs = ["snippets"]
inline_dirs = ["generated-snippets-not-yet-produced"]
[[crates]]
name = "fixture-crate"
sources = ["src/lib.rs"]
"#,
)
.expect("write alef.toml");
}
#[test]
fn check_fails_when_an_inline_dirs_root_does_not_exist() {
let dir = tempfile::tempdir().expect("create temp workspace");
write_fixture_with_missing_inline_dir(dir.path());
let output = Command::new(env!("CARGO_BIN_EXE_alef"))
.args(["snippets", "check", "--config"])
.arg(dir.path().join("alef.toml"))
.args(["--cache", "off"])
.env("RUST_LOG", "info")
.output()
.expect("run the alef binary");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let context = format!("stdout:\n{stdout}\nstderr:\n{stderr}");
assert!(
!output.status.success(),
"`check` must fail when a configured snippets root does not exist on disk — a repointed- \
but-never-populated directory must never read as an exhaustively validated one.\n{context}"
);
assert!(
stderr.contains("generated-snippets-not-yet-produced"),
"the diagnostic must name the missing directory so the misconfiguration is actionable, not \
just report a generic discovery failure.\n{context}"
);
}
#[test]
fn check_fails_on_a_dangling_include_target() {
let dir = tempfile::tempdir().expect("create temp workspace");
write_fixture_with_dangling_include(dir.path());
let output = Command::new(env!("CARGO_BIN_EXE_alef"))
.args(["snippets", "check", "--config"])
.arg(dir.path().join("alef.toml"))
.args(["--cache", "off"])
.env("RUST_LOG", "info")
.output()
.expect("run the alef binary");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let context = format!("stdout:\n{stdout}\nstderr:\n{stderr}");
assert!(
!output.status.success(),
"`check` must fail when a docs file includes a snippet that does not exist — \
this is exactly what `alef snippets audit` and `alef snippets gaps` already \
detect, and `check`'s own --help text promises it runs both.\n{context}"
);
assert!(
stderr.contains("snippet audit:") && stderr.contains("included snippet does not exist"),
"`check` must fail *because of* the audit finding. `run_check` returns FAILURE from ~8 other \
sites (config load, discovery, session resolution, report writing, ...), so asserting on the \
exit status alone would keep this test green while testing nothing.\n{context}"
);
assert!(
stderr.contains("snippet gap: missing include target"),
"the gap pass must report the same dangling include; the binary installs a stderr `fmt` \
subscriber at `info` by default, so its ERROR events are observable here.\n{context}"
);
assert!(
stderr.contains("missing-fixture.md"),
"the diagnostic must name the include target that could not be resolved.\n{context}"
);
}