mod common;
use common::{assert_no_control_chars, dup_vars_file_warning, fixture, mds_bin};
#[test]
fn check_collecting_warnings_returns_warnings_for_empty_include() {
let path = fixture("include_empty_body.mds");
let ((), warnings) = mds::check_collecting_warnings(&path, None)
.expect("check_collecting_warnings should succeed on a valid file");
assert!(
warnings.iter().any(|w| w.contains("empty output")),
"expected at least one warning about empty @include, got: {warnings:?}"
);
}
#[test]
fn check_str_collecting_warnings_no_warnings_for_clean_source() {
let source = "---\nname: Test\n---\nHello {{name}}!\n";
let ((), warnings) = mds::check_str_collecting_warnings(source, None, None)
.expect("check_str_collecting_warnings should succeed on clean source");
assert!(
warnings.is_empty(),
"clean source should produce no warnings, got: {warnings:?}"
);
}
#[test]
fn check_str_collecting_warnings_errors_on_invalid_source() {
let source = "{{undefined_variable}}";
let result = mds::check_str_collecting_warnings(source, None, None);
assert!(
result.is_err(),
"check_str_collecting_warnings should return Err for undefined variable"
);
}
#[test]
fn warning_cap_at_max_warnings() {
let dir = tempfile::tempdir().unwrap();
let lib_path = dir.path().join("empty_lib.mds");
std::fs::write(&lib_path, "@define noop():\n@end\n").unwrap();
let mut src = String::from("@import \"./empty_lib.mds\" as lib\n");
for _ in 0..1010 {
src.push_str("@include lib\n");
}
let main_path = dir.path().join("main.mds");
std::fs::write(&main_path, &src).unwrap();
let result = mds::compile_collecting_warnings(&main_path, None)
.expect("template should compile successfully");
let warnings = result.warnings;
assert_eq!(
warnings.len(),
1000,
"warnings must be capped at exactly 1000, got {}",
warnings.len()
);
}
#[test]
fn check_empty_body_no_warning_in_quiet_mode() {
let output = mds_bin()
.args([
"check",
fixture("include_empty_body.mds").to_str().unwrap(),
"--quiet",
])
.output()
.unwrap();
assert!(output.status.success(), "quiet check should succeed");
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
stderr.is_empty(),
"quiet flag should suppress warnings for check command, got: {stderr}"
);
}
#[test]
fn include_empty_body_no_warning_in_quiet_mode() {
let output = mds_bin()
.args([
"build",
fixture("include_empty_body.mds").to_str().unwrap(),
"-o",
"-",
"--quiet",
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(output.status.success(), "quiet build should succeed");
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
stderr.is_empty(),
"quiet flag should suppress the empty-include warning, got: {stderr}"
);
}
#[test]
fn include_empty_body_emits_warning() {
let output = mds_bin()
.args([
"build",
fixture("include_empty_body.mds").to_str().unwrap(),
"-o",
"-",
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"build should succeed even when include is empty"
);
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
stderr.contains("warning") && stderr.contains("fns"),
"expected warning about empty @include on stderr, got: {stderr}"
);
}
fn count_occurrences(haystack: &str, needle: &str) -> usize {
let mut count = 0;
let mut start = 0;
while let Some(pos) = haystack[start..].find(needle) {
count += 1;
start += pos + needle.len();
}
count
}
const DUP_SET_WARNING: &str =
"warning: variable 'x' is set more than once by --set; the last value wins";
const DUP_SET_STRING_WARNING: &str =
"warning: variable 'x' is set more than once by --set-string; the last value wins";
#[test]
fn i1_set_duplicate_warns_exactly_once_on_stderr() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
std::fs::write(&src, "Hello world").unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--set",
"x=1",
"--set",
"x=2",
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"build with duplicate --set must succeed"
);
let stderr = String::from_utf8(output.stderr).unwrap();
let count = count_occurrences(&stderr, DUP_SET_WARNING);
assert_eq!(
count, 1,
"I1: expected warning exactly once, found {count} times; stderr:\n{stderr}"
);
}
#[test]
fn i2_set_string_duplicate_warns_exactly_once_on_stderr() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
std::fs::write(&src, "Hello world").unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--set-string",
"x=a",
"--set-string",
"x=b",
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"build with duplicate --set-string must succeed"
);
let stderr = String::from_utf8(output.stderr).unwrap();
let count = count_occurrences(&stderr, DUP_SET_STRING_WARNING);
assert_eq!(
count, 1,
"I2: expected warning exactly once, found {count} times; stderr:\n{stderr}"
);
}
#[test]
fn i3_quiet_suppresses_duplicate_warning() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
std::fs::write(&src, "Hello world").unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--set",
"x=1",
"--set",
"x=2",
"--quiet",
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(output.status.success(), "quiet build must succeed");
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
!stderr.contains(DUP_SET_WARNING),
"I3: --quiet must suppress duplicate-key warning; got stderr:\n{stderr}"
);
}
#[test]
fn i4_check_parity_with_build() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
std::fs::write(&src, "Hello world").unwrap();
let output = mds_bin()
.args([
"check",
src.to_str().unwrap(),
"--set",
"x=1",
"--set",
"x=2",
])
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"check with duplicate --set must succeed"
);
let stderr = String::from_utf8(output.stderr).unwrap();
let count = count_occurrences(&stderr, DUP_SET_WARNING);
assert_eq!(
count, 1,
"I4: check must warn exactly once; found {count} times; stderr:\n{stderr}"
);
}
#[test]
fn i5_lint_parity_with_build() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
std::fs::write(&src, "Hello world").unwrap();
let output = mds_bin()
.args([
"lint",
src.to_str().unwrap(),
"--set",
"x=1",
"--set",
"x=2",
])
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"lint with duplicate --set must succeed (no lint errors); stderr:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8(output.stderr).unwrap();
let count = count_occurrences(&stderr, DUP_SET_WARNING);
assert_eq!(
count, 1,
"I5: lint must warn exactly once; found {count} times; stderr:\n{stderr}"
);
}
#[test]
fn i6_triple_repeat_warns_exactly_once() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
std::fs::write(&src, "Hello world").unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--set",
"x=1",
"--set",
"x=2",
"--set",
"x=3",
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"build with triple --set must succeed"
);
let stderr = String::from_utf8(output.stderr).unwrap();
let count = count_occurrences(&stderr, DUP_SET_WARNING);
assert_eq!(
count, 1,
"I6: triple repeat must still produce exactly one warning; found {count}; stderr:\n{stderr}"
);
}
#[test]
fn i7_hostile_key_is_wire_escaped_in_warning() {
let esc: char = '\u{1b}';
let rlo: char = '\u{202e}';
let hostile_key = format!("{esc}[31m{rlo}");
let expected_esc_form = "\\u001B"; let expected_rlo_form = "\\u202E";
let arg_set_first = format!("{hostile_key}=1");
let arg_set_second = format!("{hostile_key}=2");
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
std::fs::write(&src, "Hello world").unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--set",
&arg_set_first,
"--set",
&arg_set_second,
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"build with hostile key must succeed"
);
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
stderr.contains(expected_esc_form),
"I7: expected escaped ESC form '{}' in stderr; got:\n{:?}",
expected_esc_form,
stderr
);
assert!(
stderr.contains(expected_rlo_form),
"I7: expected escaped RLO form '{}' in stderr; got:\n{:?}",
expected_rlo_form,
stderr
);
assert_no_control_chars(&stderr, "I7 stderr");
}
#[test]
fn i10_vars_file_duplicate_key_warns_exactly_once_on_build() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
let vars = dir.path().join("vars.json");
std::fs::write(&src, "Hello world").unwrap();
std::fs::write(&vars, r#"{"x": 1, "x": 2}"#).unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--vars",
vars.to_str().unwrap(),
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"I10: build with duplicate vars-file key must succeed"
);
let stderr = String::from_utf8(output.stderr).unwrap();
let expected = dup_vars_file_warning("x", &vars);
let count = count_occurrences(&stderr, &expected);
assert_eq!(
count, 1,
"I10: expected warning exactly once, found {count} times; stderr:\n{stderr}"
);
}
#[test]
fn i11_quiet_suppresses_vars_file_duplicate_warning() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
let vars = dir.path().join("vars.json");
std::fs::write(&src, "Hello world").unwrap();
std::fs::write(&vars, r#"{"x": 1, "x": 2}"#).unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--vars",
vars.to_str().unwrap(),
"--quiet",
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(output.status.success(), "I11: quiet build must succeed");
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
stderr.is_empty(),
"I11: --quiet must suppress the duplicate vars-file warning, got: {stderr}"
);
}
#[test]
fn i12_vars_file_duplicate_key_warns_on_check() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
let vars = dir.path().join("vars.json");
std::fs::write(&src, "Hello world").unwrap();
std::fs::write(&vars, r#"{"x": 1, "x": 2}"#).unwrap();
let output = mds_bin()
.args([
"check",
src.to_str().unwrap(),
"--vars",
vars.to_str().unwrap(),
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(output.status.success(), "I12: check must succeed");
let stderr = String::from_utf8(output.stderr).unwrap();
let expected = dup_vars_file_warning("x", &vars);
assert_eq!(
count_occurrences(&stderr, &expected),
1,
"I12: check must warn exactly once for a duplicate vars-file key; stderr:\n{stderr}"
);
}
#[test]
fn i13_vars_file_duplicate_key_warns_on_lint() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
let vars = dir.path().join("vars.json");
std::fs::write(&src, "Hello world").unwrap();
std::fs::write(&vars, r#"{"x": 1, "x": 2}"#).unwrap();
let output = mds_bin()
.args([
"lint",
src.to_str().unwrap(),
"--vars",
vars.to_str().unwrap(),
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(output.status.success(), "I13: lint must succeed");
let stderr = String::from_utf8(output.stderr).unwrap();
let expected = dup_vars_file_warning("x", &vars);
assert_eq!(
count_occurrences(&stderr, &expected),
1,
"I13: lint must warn exactly once for a duplicate vars-file key; stderr:\n{stderr}"
);
}
#[test]
fn i14_vars_file_triple_repeat_warns_once() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
let vars = dir.path().join("vars.json");
std::fs::write(&src, "Hello world").unwrap();
std::fs::write(&vars, r#"{"x": 1, "x": 2, "x": 3}"#).unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--vars",
vars.to_str().unwrap(),
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(output.status.success(), "I14: build must succeed");
let stderr = String::from_utf8(output.stderr).unwrap();
let expected = dup_vars_file_warning("x", &vars);
assert_eq!(
count_occurrences(&stderr, &expected),
1,
"I14: triple repeat must still produce exactly one warning; stderr:\n{stderr}"
);
}
#[test]
fn i15_hostile_vars_file_key_is_wire_escaped_in_warning() {
let esc_json_escape = format!("\\u{:04x}", 0x1bu32);
let rlo_json_escape = format!("\\u{:04x}", 0x202eu32);
let hostile_key_in_json = format!("{esc_json_escape}[31m{rlo_json_escape}");
let expected_esc_form = "\\u001B";
let expected_rlo_form = "\\u202E";
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
let vars = dir.path().join("vars.json");
std::fs::write(&src, "Hello world").unwrap();
let vars_json = format!(r#"{{"{hostile_key_in_json}": 1, "{hostile_key_in_json}": 2}}"#);
std::fs::write(&vars, vars_json).unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--vars",
vars.to_str().unwrap(),
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"I15: build with hostile vars-file key must succeed"
);
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
stderr.contains(expected_esc_form),
"I15: expected escaped ESC form '{expected_esc_form}' in stderr; got:\n{stderr:?}"
);
assert!(
stderr.contains(expected_rlo_form),
"I15: expected escaped RLO form '{expected_rlo_form}' in stderr; got:\n{stderr:?}"
);
assert_no_control_chars(&stderr, "I15 stderr");
}
#[test]
fn i15b_hostile_vars_file_key_with_newline_is_wire_escaped() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("t.mds");
let vars = dir.path().join("vars.json");
std::fs::write(&src, "Hello world").unwrap();
std::fs::write(&vars, r#"{"a\nb": 1, "a\nb": 2}"#).unwrap();
let output = mds_bin()
.args([
"build",
src.to_str().unwrap(),
"-o",
"-",
"--vars",
vars.to_str().unwrap(),
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.unwrap();
assert!(
output.status.success(),
"I15b: build with a newline-in-key vars file must succeed"
);
let stderr = String::from_utf8(output.stderr).unwrap();
let escaped_key = "a\\u000Ab";
let expected = dup_vars_file_warning(escaped_key, &vars);
assert_eq!(
count_occurrences(&stderr, &expected),
1,
"I15b: expected the exact pinned warning line with the key WIRE-escaped \
as {escaped_key:?}; got stderr:\n{stderr:?}"
);
for line in stderr.lines() {
assert!(
!line.starts_with("b'"),
"I15b: a stderr line looks like the forged tail of a raw-LF-split \
hostile key (line: {line:?}); this would only happen if a raw LF \
reached eprint_warning unescaped; full stderr:\n{stderr:?}"
);
}
assert_no_control_chars(&stderr, "I15b stderr");
}
#[test]
fn r2_include_export_hides_prompt_warns_about_export_list() {
let output = mds_bin()
.args([
"build",
"-o",
"-",
fixture("include_export_hides_prompt.mds").to_str().unwrap(),
])
.output()
.unwrap();
assert!(
output.status.success(),
"build of include_export_hides_prompt.mds should succeed; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
stderr.contains("empty output"),
"R2: expected 'empty output' warning; got: {stderr:?}"
);
assert!(
stderr.contains("does not export") || stderr.contains("@export list"),
"R2: WARN-B must mention the @export list; got: {stderr:?}"
);
assert!(
!stderr.contains("no body text"),
"R2: WARN-B must NOT say 'no body text'; got: {stderr:?}"
);
assert_no_control_chars(&stderr, "R2 warning line");
}