mod common;
use common::mds_bin;
use std::fs;
use std::path::Path;
fn create_plain_mds(dir: &Path, name: &str) {
fs::write(dir.join(name), "Hello, world!\n").unwrap();
}
fn create_messages_mds(dir: &Path, name: &str) {
fs::write(
dir.join(name),
"@message system:\nYou are a helpful assistant.\n@end\n@message user:\nHello!\n@end\n",
)
.unwrap();
}
fn create_bad_mds(dir: &Path, name: &str) {
fs::write(dir.join(name), "{{undefined_var_xyz}}\n").unwrap();
}
fn build_dir(dir: &Path, extra_args: &[&str]) -> std::process::Output {
mds_bin()
.arg("build")
.arg(dir)
.args(extra_args)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap()
}
fn build_file(path: &Path, extra_args: &[&str]) -> std::process::Output {
mds_bin()
.arg("build")
.arg(path)
.args(extra_args)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap()
}
fn check_dir(dir: &Path, extra_args: &[&str]) -> std::process::Output {
mds_bin()
.arg("check")
.arg(dir)
.args(extra_args)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap()
}
#[test]
fn dir_build_subtree_mirror_intrinsic_ext() {
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
fs::create_dir(src.path().join("sub")).unwrap();
create_plain_mds(src.path(), "plain.mds");
create_messages_mds(&src.path().join("sub"), "messages.mds");
let output = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert!(
output.status.success(),
"dir build should succeed; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let plain_out = out.path().join("plain.md");
assert!(
plain_out.exists(),
"expected out/plain.md to be created; out dir: {:?}",
fs::read_dir(out.path()).map(|rd| rd
.filter_map(|e| e.ok().map(|e| e.path()))
.collect::<Vec<_>>())
);
let plain_content = fs::read_to_string(&plain_out).unwrap();
assert!(
plain_content.contains("Hello"),
"plain.md should contain rendered content; got: {plain_content:?}"
);
let msg_out = out.path().join("sub").join("messages.json");
assert!(
msg_out.exists(),
"expected out/sub/messages.json to be created"
);
let msg_content = fs::read_to_string(&msg_out).unwrap();
let parsed: serde_json::Value =
serde_json::from_str(&msg_content).expect("messages.json should be valid JSON");
assert!(parsed.is_array(), "messages.json should be a JSON array");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("built"),
"stderr should contain build summary; got: {stderr}"
);
}
#[test]
fn dir_build_partials_produce_no_output() {
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "_partial.mds");
create_plain_mds(src.path(), "main.mds");
let output = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert!(
output.status.success(),
"dir build should succeed; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let partial_out = out.path().join("_partial.md");
assert!(
!partial_out.exists(),
"_partial.md must not be created (partials are skipped)"
);
let main_out = out.path().join("main.md");
assert!(main_out.exists(), "main.md should be created");
}
#[test]
fn dir_build_continue_on_error_summary_nonzero() {
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "good.mds");
create_bad_mds(src.path(), "bad.mds");
let output = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert!(
!output.status.success(),
"dir build with a failing file must exit non-zero"
);
let good_out = out.path().join("good.md");
assert!(
good_out.exists(),
"good.md should still be created despite bad.mds failing"
);
let bad_out = out.path().join("bad.md");
assert!(
!bad_out.exists(),
"bad.md must not be created when compilation fails"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("built") && stderr.contains("failed"),
"stderr must contain a build summary with 'built' and 'failed'; got: {stderr}"
);
assert!(
stderr.contains("1 built") && stderr.contains("1 failed"),
"stderr must show '1 built, 1 failed'; got: {stderr}"
);
}
#[test]
fn dir_build_bare_writes_next_to_source() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "page.mds");
create_messages_mds(src.path(), "chat.mds");
let output = build_dir(src.path(), &[]);
assert!(
output.status.success(),
"bare dir build should succeed; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let page_out = src.path().join("page.md");
assert!(page_out.exists(), "page.md should appear next to page.mds");
let chat_out = src.path().join("chat.json");
assert!(
chat_out.exists(),
"chat.json should appear next to chat.mds"
);
let _ = fs::remove_file(&page_out);
let _ = fs::remove_file(&chat_out);
}
#[cfg(unix)]
#[test]
fn dir_build_symlinked_file_skipped() {
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
let real_file = src.path().join("real.mds");
fs::write(&real_file, "Real content.\n").unwrap();
let link_file = src.path().join("link.mds");
std::os::unix::fs::symlink(&real_file, &link_file).unwrap();
let output = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert!(
output.status.success(),
"build should succeed; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let link_out = out.path().join("link.md");
assert!(
!link_out.exists(),
"symlinked .mds file must not produce output"
);
let real_out = out.path().join("real.md");
assert!(real_out.exists(), "real.md should be created from real.mds");
}
#[cfg(unix)]
#[test]
fn dir_build_symlinked_subdir_skipped() {
let real_dir = tempfile::tempdir().unwrap();
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
fs::write(real_dir.path().join("child.mds"), "Child.\n").unwrap();
let link_dir = src.path().join("linked_sub");
std::os::unix::fs::symlink(real_dir.path(), &link_dir).unwrap();
create_plain_mds(src.path(), "root.mds");
let output = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert!(
output.status.success(),
"build should succeed; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let child_out = out.path().join("linked_sub").join("child.md");
assert!(
!child_out.exists(),
"symlinked subdirectory must not be traversed"
);
}
#[cfg(unix)]
#[test]
fn dir_build_symlinked_entry_root_rejected() {
let real_dir = tempfile::tempdir().unwrap();
create_plain_mds(real_dir.path(), "page.mds");
let link_dir = tempfile::tempdir().unwrap();
let link_path = link_dir.path().join("linked");
std::os::unix::fs::symlink(real_dir.path(), &link_path).unwrap();
let output = build_dir(&link_path, &[]);
assert!(
!output.status.success(),
"build on a symlinked entry root must fail"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("symlink") || stderr.contains("directory"),
"error must mention symlink; got: {stderr}"
);
}
#[test]
fn dir_build_oversized_file_fails_others_succeed() {
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "good.mds");
let big_content = "x".repeat(10 * 1024 * 1024 + 1);
fs::write(src.path().join("big.mds"), &big_content).unwrap();
let output = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert!(
!output.status.success(),
"dir build with an oversized file must exit non-zero"
);
let good_out = out.path().join("good.md");
assert!(
good_out.exists(),
"good.md should still be created despite the oversized file"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("failed"),
"stderr must mention failure in summary; got: {stderr}"
);
}
#[test]
fn dir_check_validates_tree_exits_zero_on_all_ok() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "page.mds");
create_messages_mds(src.path(), "chat.mds");
let output = check_dir(src.path(), &[]);
assert!(
output.status.success(),
"check on a valid tree should succeed; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("passed"),
"stderr should contain check summary; got: {stderr}"
);
}
#[test]
fn dir_check_continues_on_error_nonzero_exit() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "good.mds");
create_bad_mds(src.path(), "bad.mds");
let output = check_dir(src.path(), &[]);
assert!(
!output.status.success(),
"check with a failing file must exit non-zero"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("passed") || stderr.contains("failed"),
"stderr must contain check summary; got: {stderr}"
);
}
#[test]
fn dir_check_empty_dir_exits_one() {
let src = tempfile::tempdir().unwrap();
let output = check_dir(src.path(), &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"check on an empty dir must exit 1; stderr: {stderr}"
);
assert!(
stderr.contains("no .mds files found in"),
"stderr must carry the empty-tree diagnostic; got: {stderr:?}"
);
assert!(
stderr.contains("nothing was checked"),
"stderr must say nothing was checked; got: {stderr:?}"
);
}
#[test]
fn dir_check_empty_dir_quiet_still_emits_diagnostic() {
let src = tempfile::tempdir().unwrap();
let output = check_dir(src.path(), &["--quiet"]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"check --quiet on an empty dir must exit 1; stderr: {stderr}"
);
assert!(
!stderr.is_empty(),
"stderr must not be empty under --quiet on an empty tree"
);
assert!(
stderr.contains("no .mds files found in"),
"empty-tree diagnostic must appear under --quiet; got: {stderr:?}"
);
assert!(
stderr.contains("nothing was checked"),
"stderr must say nothing was checked under --quiet; got: {stderr:?}"
);
}
#[test]
fn dir_check_partials_only_exits_one() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "_only.mds");
let output = check_dir(src.path(), &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"check on a partials-only dir must exit 1; stderr: {stderr}"
);
assert!(
stderr.contains("nothing was checked"),
"stderr must say nothing was checked; got: {stderr:?}"
);
assert!(
stderr.contains("all are _-prefixed partials"),
"stderr must say all are _-prefixed partials; got: {stderr:?}"
);
create_plain_mds(src.path(), "real.mds");
let output2 = check_dir(src.path(), &[]);
let stderr2 = String::from_utf8_lossy(&output2.stderr);
assert_eq!(
output2.status.code(),
Some(0),
"check with a real file present must succeed; stderr: {stderr2}"
);
assert!(
stderr2.contains("1 passed, 0 failed"),
"stderr must show the check summary; got: {stderr2:?}"
);
}
#[test]
fn dir_check_missing_root_exits_two() {
let src = tempfile::tempdir().unwrap();
let missing = src.path().join("nope");
let output = check_dir(&missing, &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(2),
"check on a missing path must exit 2; stderr: {stderr}"
);
assert!(
!stderr.contains("no .mds files found in"),
"a missing path must NOT produce the empty-tree diagnostic; got: {stderr:?}"
);
}
#[test]
fn dir_check_mixed_content_file_nonzero() {
let src = tempfile::tempdir().unwrap();
fs::write(
src.path().join("mixed.mds"),
"Some text before.\n@message user:\nHello!\n@end\n",
)
.unwrap();
let output = check_dir(src.path(), &[]);
assert!(
!output.status.success(),
"check on a mixed-content file must exit non-zero"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("mixed") || stderr.contains("failed"),
"error must mention mixed content or failure; got: {stderr}"
);
}
#[test]
fn dir_build_stale_output_cleaned_on_format_flip() {
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
let mds_path = src.path().join("page.mds");
fs::write(&mds_path, "Hello, world!\n").unwrap();
let output1 = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert!(
output1.status.success(),
"first build should succeed; stderr: {}",
String::from_utf8_lossy(&output1.stderr)
);
let md_out = out.path().join("page.md");
assert!(md_out.exists(), "page.md should be created on first build");
fs::write(&mds_path, "@message user:\nHello!\n@end\n").unwrap();
let output2 = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert!(
output2.status.success(),
"second build should succeed; stderr: {}",
String::from_utf8_lossy(&output2.stderr)
);
let json_out = out.path().join("page.json");
assert!(
json_out.exists(),
"page.json should be created on second build"
);
assert!(
!md_out.exists(),
"stale page.md should be removed after format flip to messages"
);
}
#[test]
fn dir_build_empty_dir_exits_one() {
let src = tempfile::tempdir().unwrap();
let output = build_dir(src.path(), &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"build on an empty dir must exit 1; stderr: {stderr}"
);
assert!(
stderr.contains("no .mds files found in"),
"stderr must carry the empty-tree diagnostic; got: {stderr:?}"
);
assert!(
stderr.contains("nothing was built"),
"stderr must say nothing was built; got: {stderr:?}"
);
}
#[test]
fn dir_build_empty_dir_quiet_still_emits_diagnostic() {
let src = tempfile::tempdir().unwrap();
let output = build_dir(src.path(), &["--quiet"]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"build --quiet on an empty dir must exit 1; stderr: {stderr}"
);
assert!(
!stderr.is_empty(),
"stderr must not be empty under --quiet on an empty tree"
);
assert!(
stderr.contains("no .mds files found in"),
"empty-tree diagnostic must appear under --quiet; got: {stderr:?}"
);
assert!(
stderr.contains("nothing was built"),
"stderr must say nothing was built under --quiet; got: {stderr:?}"
);
}
#[test]
fn dir_build_partials_only_exits_one() {
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "_only.mds");
let output = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"build on a partials-only dir must exit 1; stderr: {stderr}"
);
assert!(
stderr.contains("1 .mds file(s) found in"),
"stderr must carry the partials-only count diagnostic; got: {stderr:?}"
);
assert!(
stderr.contains("all are _-prefixed partials"),
"stderr must say all are _-prefixed partials; got: {stderr:?}"
);
assert!(
stderr.contains("nothing was built"),
"stderr must say nothing was built; got: {stderr:?}"
);
assert!(
!out.path().join("_only.md").exists(),
"a partial must never produce output"
);
create_plain_mds(src.path(), "real.mds");
let output2 = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert_eq!(
output2.status.code(),
Some(0),
"build with a real file present must succeed; stderr: {}",
String::from_utf8_lossy(&output2.stderr)
);
assert!(
out.path().join("real.md").exists(),
"real.md should be created"
);
assert!(
!out.path().join("_only.md").exists(),
"_only.md must not be created (partials are skipped)"
);
}
#[test]
fn dir_build_partials_only_quiet_still_emits_diagnostic() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "_only.mds");
let output = build_dir(src.path(), &["--quiet"]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"build --quiet on a partials-only dir must exit 1; stderr: {stderr}"
);
assert!(
!stderr.is_empty(),
"stderr must not be empty under --quiet on a partials-only tree"
);
assert!(
stderr.contains("nothing was built"),
"stderr must say nothing was built under --quiet; got: {stderr:?}"
);
}
fn lint_dir(dir: &Path, extra_args: &[&str]) -> std::process::Output {
mds_bin()
.arg("lint")
.arg(dir)
.args(extra_args)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap()
}
fn fmt_dir(dir: &Path, extra_args: &[&str]) -> std::process::Output {
mds_bin()
.arg("fmt")
.arg(dir)
.args(extra_args)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap()
}
#[test]
fn dir_build_all_excluded_exits_nonzero() {
let src = tempfile::tempdir().unwrap();
let hidden = src.path().join(".github");
fs::create_dir_all(hidden.join("prompts")).unwrap();
fs::write(hidden.join("prompts").join("system.mds"), "Hello!\n").unwrap();
let output = build_dir(src.path(), &[]);
assert!(
!output.status.success(),
"build with all candidates in excluded dirs must exit non-zero; exit: {:?}; stderr: {}",
output.status.code(),
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("excluded") || stderr.contains("default-excluded"),
"stderr must mention excluded directories; got: {stderr}"
);
assert!(
stderr.contains('1'),
"stderr must carry the skip count (1 file excluded); got: {stderr}"
);
}
#[test]
fn dir_build_all_excluded_quiet_still_emits_diagnostic() {
let src = tempfile::tempdir().unwrap();
let nm = src.path().join("node_modules");
fs::create_dir(&nm).unwrap();
fs::write(nm.join("lib.mds"), "Hello!\n").unwrap();
fs::write(nm.join("other.mds"), "World!\n").unwrap();
let output = build_dir(src.path(), &["--quiet"]);
assert!(
!output.status.success(),
"build with all candidates excluded must exit non-zero even under --quiet"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!stderr.is_empty(),
"stderr must not be empty under --quiet when all candidates are excluded"
);
assert!(
stderr.contains("excluded") || stderr.contains("default-excluded"),
"all-excluded diagnostic must appear under --quiet; got: {stderr}"
);
assert!(
stderr.contains('2'),
"stderr must report 2 skipped files; got: {stderr}"
);
}
#[test]
fn dir_build_genuinely_empty_exits_one_without_excluded_diagnostic() {
let src = tempfile::tempdir().unwrap();
let output = build_dir(src.path(), &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"genuinely empty dir must exit 1; stderr: {stderr}"
);
assert!(
stderr.contains("no .mds files found in"),
"empty-dir message must be the empty-tree diagnostic; got: {stderr:?}"
);
assert!(
!stderr.contains("excluded"),
"empty-dir must NOT show the excluded diagnostic; got: {stderr:?}"
);
let control = tempfile::tempdir().unwrap();
let hidden = control.path().join(".github");
fs::create_dir(&hidden).unwrap();
fs::write(hidden.join("x.mds"), "Hello!\n").unwrap();
let control_out = build_dir(control.path(), &[]);
let control_stderr = String::from_utf8_lossy(&control_out.stderr);
assert_eq!(
control_out.status.code(),
Some(1),
"positive control: the all-excluded case also exits 1; stderr: {control_stderr}"
);
assert!(
control_stderr.contains("excluded"),
"positive control: the all-excluded case must carry the excluded diagnostic; \
got: {control_stderr:?}"
);
assert!(
!control_stderr.contains("no .mds files found in"),
"positive control: the all-excluded case must NOT carry the empty-tree \
diagnostic; got: {control_stderr:?}"
);
}
#[test]
fn dir_build_mixed_excluded_and_normal_processes_normal() {
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "normal.mds");
let hidden = src.path().join(".prompts");
fs::create_dir(&hidden).unwrap();
fs::write(hidden.join("excluded.mds"), "Hello!\n").unwrap();
let output = build_dir(src.path(), &["--out-dir", out.path().to_str().unwrap()]);
assert!(
output.status.success(),
"build with mixed excluded+normal files must succeed; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(
out.path().join("normal.md").exists(),
"normal.md must be built"
);
assert!(
!out.path().join(".prompts").join("excluded.md").exists(),
"excluded.md must not be built"
);
}
#[test]
fn dir_check_all_excluded_exits_nonzero() {
let src = tempfile::tempdir().unwrap();
let hidden = src.path().join(".claude");
fs::create_dir(&hidden).unwrap();
fs::write(hidden.join("prompt.mds"), "Hello!\n").unwrap();
let output = check_dir(src.path(), &[]);
assert!(
!output.status.success(),
"check with all candidates in excluded dirs must exit non-zero; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("excluded") || stderr.contains("default-excluded"),
"stderr must mention excluded directories; got: {stderr}"
);
assert!(
stderr.contains('1'),
"stderr must carry the skip count; got: {stderr}"
);
}
#[test]
fn dir_check_all_excluded_quiet_still_emits_diagnostic() {
let src = tempfile::tempdir().unwrap();
let hidden = src.path().join(".cursor");
fs::create_dir_all(hidden.join("rules")).unwrap();
fs::write(hidden.join("rules").join("rule.mds"), "Hello!\n").unwrap();
let output = check_dir(src.path(), &["--quiet"]);
assert!(
!output.status.success(),
"check with all candidates excluded must exit non-zero even under --quiet"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!stderr.is_empty() && (stderr.contains("excluded") || stderr.contains("default-excluded")),
"all-excluded diagnostic must appear under --quiet; got: {stderr:?}"
);
}
#[test]
fn dir_lint_all_excluded_exits_nonzero() {
let src = tempfile::tempdir().unwrap();
let nm = src.path().join("node_modules");
fs::create_dir(&nm).unwrap();
fs::write(nm.join("template.mds"), "Hello!\n").unwrap();
let output = lint_dir(src.path(), &[]);
assert!(
!output.status.success(),
"lint with all candidates in excluded dirs must exit non-zero; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("excluded") || stderr.contains("default-excluded"),
"stderr must mention excluded directories; got: {stderr}"
);
}
#[test]
fn dir_lint_all_excluded_quiet_still_emits_diagnostic() {
let src = tempfile::tempdir().unwrap();
let hidden = src.path().join(".github");
fs::create_dir(&hidden).unwrap();
fs::write(hidden.join("workflow.mds"), "Hello!\n").unwrap();
let output = lint_dir(src.path(), &["--quiet"]);
assert!(
!output.status.success(),
"lint with all candidates excluded must exit non-zero even under --quiet"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!stderr.is_empty() && (stderr.contains("excluded") || stderr.contains("default-excluded")),
"all-excluded diagnostic must appear under --quiet; got: {stderr:?}"
);
}
#[test]
fn dir_fmt_all_excluded_exits_nonzero() {
let src = tempfile::tempdir().unwrap();
let hidden = src.path().join(".prompts");
fs::create_dir(&hidden).unwrap();
fs::write(hidden.join("system.mds"), "Hello!\n").unwrap();
let output = fmt_dir(src.path(), &[]);
assert!(
!output.status.success(),
"fmt with all candidates in excluded dirs must exit non-zero; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("excluded") || stderr.contains("default-excluded"),
"stderr must mention excluded directories; got: {stderr}"
);
}
#[test]
fn dir_fmt_all_excluded_quiet_still_emits_diagnostic() {
let src = tempfile::tempdir().unwrap();
let nm = src.path().join("node_modules");
fs::create_dir(&nm).unwrap();
fs::write(nm.join("component.mds"), "Hello!\n").unwrap();
let output = fmt_dir(src.path(), &["--quiet"]);
assert!(
!output.status.success(),
"fmt with all candidates excluded must exit non-zero even under --quiet"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!stderr.is_empty() && (stderr.contains("excluded") || stderr.contains("default-excluded")),
"all-excluded diagnostic must appear under --quiet; got: {stderr:?}"
);
}
#[test]
fn dir_fmt_empty_dir_exits_one() {
let src = tempfile::tempdir().unwrap();
let output = fmt_dir(src.path(), &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"fmt on an empty dir must exit 1; stderr: {stderr}"
);
assert!(
stderr.contains("no .mds files found in"),
"stderr must carry the empty-tree diagnostic; got: {stderr:?}"
);
assert!(
stderr.contains("nothing was formatted"),
"stderr must say nothing was formatted; got: {stderr:?}"
);
}
#[test]
fn dir_fmt_empty_dir_quiet_still_emits_diagnostic() {
let src = tempfile::tempdir().unwrap();
let output = fmt_dir(src.path(), &["--quiet"]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"fmt --quiet on an empty dir must exit 1; stderr: {stderr}"
);
assert!(
!stderr.is_empty(),
"stderr must not be empty under --quiet on an empty tree"
);
assert!(
stderr.contains("no .mds files found in"),
"empty-tree diagnostic must appear under --quiet; got: {stderr:?}"
);
assert!(
stderr.contains("nothing was formatted"),
"stderr must say nothing was formatted under --quiet; got: {stderr:?}"
);
}
#[test]
fn dir_fmt_empty_dir_check_flag_exits_one() {
let src = tempfile::tempdir().unwrap();
let output = fmt_dir(src.path(), &["--check"]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(1),
"fmt --check on an empty dir must exit 1; stderr: {stderr}"
);
assert!(
stderr.contains("no .mds files found in"),
"stderr must carry the empty-tree diagnostic under --check; got: {stderr:?}"
);
assert!(
stderr.contains("nothing was formatted"),
"stderr must say nothing was formatted under --check; got: {stderr:?}"
);
}
#[test]
fn dir_fmt_partials_only_still_formats() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "_only.mds");
let output = fmt_dir(src.path(), &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(0),
"fmt on a partials-only dir must still succeed; stderr: {stderr}"
);
assert!(
stderr.contains("1 unchanged"),
"stderr must show the fmt summary; got: {stderr:?}"
);
assert!(
!stderr.contains("nothing was formatted"),
"fmt must not treat a partials-only tree as nothing-to-do; got: {stderr:?}"
);
}
#[test]
fn dir_lint_partials_only_still_lints() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "_only.mds");
let output = lint_dir(src.path(), &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(0),
"lint on a partials-only dir must still succeed; stderr: {stderr}"
);
assert!(
stderr.contains("1 clean"),
"stderr must show the lint summary; got: {stderr:?}"
);
assert!(
!stderr.contains("nothing was linted"),
"lint must not treat a partials-only tree as nothing-to-do; got: {stderr:?}"
);
}
#[test]
fn dir_build_quiet_suppresses_summary_on_success() {
let src1 = tempfile::tempdir().unwrap();
create_plain_mds(src1.path(), "a.mds");
create_plain_mds(src1.path(), "b.mds");
let quiet_out = build_dir(src1.path(), &["--quiet"]);
assert_eq!(
quiet_out.status.code(),
Some(0),
"quiet build over an all-success tree must exit 0; stderr: {}",
String::from_utf8_lossy(&quiet_out.stderr)
);
let quiet_stderr = String::from_utf8_lossy(&quiet_out.stderr);
assert!(
quiet_stderr.is_empty(),
"AC-Q01: --quiet build with no failures must produce empty stderr; got: {quiet_stderr:?}"
);
let src2 = tempfile::tempdir().unwrap();
create_plain_mds(src2.path(), "a.mds");
create_plain_mds(src2.path(), "b.mds");
let control_out = build_dir(src2.path(), &[]);
let control_stderr = String::from_utf8_lossy(&control_out.stderr);
assert!(
control_stderr.contains("2 built"),
"positive control: non-quiet run must print '2 built' so the quiet absence assertion \
is non-vacuous; got: {control_stderr:?}"
);
}
#[test]
fn dir_build_quiet_still_prints_summary_on_failure() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "good.mds");
create_bad_mds(src.path(), "bad.mds");
let output = build_dir(src.path(), &["--quiet"]);
assert_eq!(
output.status.code(),
Some(1),
"quiet build with a failing file must exit 1; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("1 built"),
"AC-Q02: --quiet build with failures must print summary so non-zero exit is explained; \
got: {stderr:?}"
);
assert!(
stderr.contains("1 failed"),
"AC-Q02: summary must include '1 failed'; got: {stderr:?}"
);
}
#[test]
fn dir_build_quiet_works_in_pre_subcommand_position() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "a.mds");
create_plain_mds(src.path(), "b.mds");
let post = build_dir(src.path(), &["--quiet"]);
let pre = mds_bin()
.arg("--quiet")
.arg("build")
.arg(src.path())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert_eq!(
pre.status.code(),
post.status.code(),
"pre-subcommand --quiet must produce the same exit code as post-subcommand --quiet"
);
assert_eq!(
pre.stderr, post.stderr,
"pre-subcommand --quiet must produce the same stderr as post-subcommand --quiet"
);
assert!(
post.stderr.is_empty(),
"AC-Q30 anchor: an all-success tree under --quiet must produce empty stderr in \
BOTH orderings, not merely equal stderr; got: {}",
String::from_utf8_lossy(&post.stderr)
);
let control_src = tempfile::tempdir().unwrap();
create_plain_mds(control_src.path(), "a.mds");
create_plain_mds(control_src.path(), "b.mds");
let control = build_dir(control_src.path(), &[]);
assert!(
String::from_utf8_lossy(&control.stderr).contains("2 built, 0 failed"),
"positive control: the same tree without --quiet must print '2 built, 0 failed'; got: {}",
String::from_utf8_lossy(&control.stderr)
);
}
fn create_defs_only_mds(dir: &Path, name: &str) {
fs::write(
dir.join(name),
"@define greet(name):\n Hi {{name}}\n@end\n\n@export greet\n",
)
.unwrap();
}
#[test]
fn d5_dir_build_summary_reports_empty_outputs() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "a.mds");
create_defs_only_mds(src.path(), "lib.mds");
let output = build_dir(src.path(), &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
output.status.success(),
"both files compile successfully; stderr: {stderr}"
);
let lib_out = src.path().join("lib.md");
assert_eq!(
fs::metadata(&lib_out).map(|m| m.len()).ok(),
Some(0),
"fixture invariant: lib.md must be a zero-byte output"
);
assert!(
stderr.contains("2 built (1 empty), 0 failed"),
"R5: summary must count zero-byte outputs; got: {stderr}"
);
}
#[test]
fn d5_dir_build_summary_omits_empty_clause_when_none() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "a.mds");
let output = build_dir(src.path(), &[]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("1 built, 0 failed"),
"summary keeps its historical form when no output is empty; got: {stderr}"
);
assert!(
!stderr.contains("empty"),
"R5: the empty clause must be ABSENT when every output has content \
(no golden churn); got: {stderr}"
);
}
#[test]
fn d5_dir_build_quiet_gate_unchanged_with_empty_outputs() {
let src = tempfile::tempdir().unwrap();
create_plain_mds(src.path(), "a.mds");
create_defs_only_mds(src.path(), "lib.mds");
let output = build_dir(src.path(), &["--quiet"]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(output.status.success(), "all-success run; stderr: {stderr}");
assert!(
!stderr.contains("built"),
"R5: --quiet must still suppress the summary on an all-success run even \
when empty outputs exist (gate unchanged); got: {stderr}"
);
}
fn temp_residue_recursive(dir: &Path) -> Vec<String> {
let mut found = Vec::new();
let mut stack = vec![dir.to_path_buf()];
while let Some(d) = stack.pop() {
let Ok(rd) = fs::read_dir(&d) else { continue };
for entry in rd.flatten() {
let p = entry.path();
let name = entry.file_name().to_string_lossy().into_owned();
if name.starts_with(".mds-tmp-") {
found.push(p.display().to_string());
}
if entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
stack.push(p);
}
}
}
found
}
#[test]
fn dir_build_out_dir_source_map_no_temp_residue() {
let src = tempfile::tempdir().unwrap();
let out = tempfile::tempdir().unwrap();
for name in ["one.mds", "two.mds", "three.mds"] {
create_plain_mds(src.path(), name);
}
let output = build_dir(
src.path(),
&["--out-dir", out.path().to_str().unwrap(), "--source-map"],
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
output.status.success(),
"dir build with --source-map must succeed; stderr: {stderr}"
);
for stem in ["one", "two", "three"] {
let md = out.path().join(format!("{stem}.md"));
let map = out.path().join(format!("{stem}.md.map"));
assert!(md.is_file(), "{stem}.md must be written");
assert!(map.is_file(), "{stem}.md.map sidecar must be written");
}
let residue = temp_residue_recursive(out.path());
assert!(
residue.is_empty(),
"a successful dir build must leave no temp file; found: {residue:?}"
);
}
#[cfg(unix)]
#[test]
fn dir_build_write_failure_preserves_existing_outputs() {
use std::os::unix::fs::PermissionsExt as _;
let src = tempfile::tempdir().unwrap();
let root = tempfile::tempdir().unwrap();
let out = root.path().join("out");
fs::create_dir(&out).unwrap();
create_plain_mds(src.path(), "a.mds");
create_plain_mds(src.path(), "b.mds");
fs::write(out.join("a.md"), "OLD").unwrap();
fs::write(out.join("b.md"), "OLD").unwrap();
fs::set_permissions(&out, fs::Permissions::from_mode(0o555)).unwrap();
let output = build_dir(src.path(), &["--out-dir", out.to_str().unwrap()]);
let _ = fs::set_permissions(&out, fs::Permissions::from_mode(0o755));
let stderr = String::from_utf8_lossy(&output.stderr);
assert_ne!(
output.status.code(),
Some(0),
"a dir build into a read-only output dir must fail; stderr: {stderr}"
);
assert!(
stderr.contains("0 built"),
"the summary must report nothing built; got: {stderr}"
);
assert!(
stderr.contains("failed"),
"the summary must report the failures; got: {stderr}"
);
assert!(
stderr.contains("error:"),
"each failure must be reported on stderr; got: {stderr}"
);
assert!(
stderr.contains("a.md"),
"the per-file error must name the artifact; got: {stderr}"
);
assert_eq!(
fs::read_to_string(out.join("a.md")).unwrap(),
"OLD",
"a.md must survive the failed write"
);
assert_eq!(
fs::read_to_string(out.join("b.md")).unwrap(),
"OLD",
"b.md must survive the failed write"
);
let residue = temp_residue_recursive(&out);
assert!(
residue.is_empty(),
"a failed dir build must leave no temp file; found: {residue:?}"
);
}
#[cfg(unix)]
#[test]
fn dir_build_source_map_sidecar_symlink_target_rejected() {
let src = tempfile::tempdir().unwrap();
let root = tempfile::tempdir().unwrap();
let out = root.path().join("out");
fs::create_dir(&out).unwrap();
create_plain_mds(src.path(), "page.mds");
let real = root.path().join("real.map");
fs::write(&real, "OLD").unwrap();
std::os::unix::fs::symlink(&real, out.join("page.md.map")).unwrap();
let output = build_dir(
src.path(),
&["--out-dir", out.to_str().unwrap(), "--source-map"],
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_ne!(
output.status.code(),
Some(0),
"a symlinked sidecar must fail the dir build; stderr: {stderr}"
);
assert!(
out.join("page.md").is_file(),
"the compiled artifact is written before the sidecar and must survive"
);
assert!(
stderr.contains("symlink"),
"the refusal must say why; got: {stderr}"
);
assert!(
stderr.contains("1 failed"),
"the summary must report exactly one failure; got: {stderr}"
);
assert_eq!(
fs::read_to_string(&real).unwrap(),
"OLD",
"the symlink target must not be written through"
);
}
#[cfg(unix)]
#[test]
fn build_non_utf8_path_exits_2_and_writes_no_map() {
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
let control = tempfile::tempdir().unwrap();
create_plain_mds(control.path(), "ok.mds");
let control_out = build_file(&control.path().join("ok.mds"), &["--source-map"]);
let control_stderr = String::from_utf8_lossy(&control_out.stderr);
assert_eq!(
control_out.status.code(),
Some(0),
"control: a normally named source must build; stderr: {control_stderr}"
);
let map_path = control.path().join("ok.md.map");
assert!(
map_path.is_file(),
"control: --source-map must write a sidecar, or the hostile arm's \
'no .map' assertion proves nothing"
);
let map: serde_json::Value =
serde_json::from_str(&fs::read_to_string(&map_path).unwrap()).expect("sidecar is JSON");
let sources: Vec<&str> = map["sources"]
.as_array()
.expect("sources must be an array")
.iter()
.filter_map(|s| s.as_str())
.collect();
assert_eq!(
sources,
vec!["ok.mds"],
"control: the sidecar must name the source it was built from"
);
let dir = tempfile::tempdir().unwrap();
let raw: Vec<u8> = vec![0xff, 0xfe, b'.', b'm', b'd', b's'];
let hostile = dir.path().join(OsString::from_vec(raw));
if fs::write(&hostile, "Hello, world!\n").is_err() {
#[cfg(not(target_os = "macos"))]
panic!(
"build_non_utf8_path_exits_2_and_writes_no_map: a non-UTF-8 filename was \
rejected by the filesystem — unexpected on this platform"
);
#[cfg(target_os = "macos")]
return;
}
let output = build_file(&hostile, &["--source-map"]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(2),
"#217: a path that cannot be named must be an I/O failure; stderr: {stderr}"
);
assert!(
stderr.contains("not valid UTF-8"),
"the diagnostic must say why; got: {stderr}"
);
let names: Vec<String> = fs::read_dir(dir.path())
.unwrap()
.filter_map(Result::ok)
.map(|e| e.file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(
names.len(),
1,
"neither an output nor a sidecar may be written for a rejected path; got {names:?}"
);
}
#[cfg(unix)]
#[test]
fn dir_build_symlinked_ancestor_root_mirrors_without_warning() {
let tmp = tempfile::tempdir().unwrap();
let real_src = tmp.path().join("real").join("src");
fs::create_dir_all(real_src.join("sub")).unwrap();
create_plain_mds(&real_src, "a.mds");
create_plain_mds(&real_src.join("sub"), "b.mds");
std::os::unix::fs::symlink(tmp.path().join("real"), tmp.path().join("link")).unwrap();
let out = tempfile::tempdir().unwrap();
let output = build_dir(
&tmp.path().join("link").join("src"),
&["--out-dir", out.path().to_str().unwrap()],
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(0),
"a root under a symlinked ancestor must build; stderr: {stderr}"
);
assert!(
out.path().join("a.md").is_file(),
"top-level source must mirror into the out-dir; stderr: {stderr}"
);
assert!(
out.path().join("sub").join("b.md").is_file(),
"the subtree must be preserved, not flattened; stderr: {stderr}"
);
assert!(
stderr.contains("2 built, 0 failed"),
"control needle: the real summary must be present, or the negative assertion \
below would pass on empty stderr; got: {stderr}"
);
assert!(
!stderr.contains("is outside the build root"),
"a mirrored build must not report the out-of-root flatten; got: {stderr}"
);
}
#[test]
fn dir_build_dotdot_root_mirrors_without_warning() {
let tmp = tempfile::tempdir().unwrap();
fs::create_dir_all(tmp.path().join("sub")).unwrap();
let src = tmp.path().join("src");
fs::create_dir_all(src.join("nested")).unwrap();
create_plain_mds(&src, "a.mds");
create_plain_mds(&src.join("nested"), "b.mds");
let out = tempfile::tempdir().unwrap();
let output = build_dir(
&tmp.path().join("sub").join("..").join("src"),
&["--out-dir", out.path().to_str().unwrap()],
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.code(),
Some(0),
"a root with a '..' component must build; stderr: {stderr}"
);
assert!(
out.path().join("a.md").is_file(),
"top-level source must mirror into the out-dir; stderr: {stderr}"
);
assert!(
out.path().join("nested").join("b.md").is_file(),
"the subtree must be preserved, not flattened; stderr: {stderr}"
);
assert!(
stderr.contains("2 built, 0 failed"),
"control needle: the real summary must be present, or the negative assertion \
below would pass on empty stderr; got: {stderr}"
);
assert!(
!stderr.contains("is outside the build root"),
"a mirrored build must not report the out-of-root flatten; got: {stderr}"
);
}