mod common;
use std::collections::BTreeSet;
use std::ffi::OsStr;
use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::process::{Output, Stdio};
use std::time::{Duration, Instant};
use common::{mds_bin, write_atomic};
fn scratch() -> tempfile::TempDir {
tempfile::Builder::new()
.prefix("mds-path-labels-")
.tempdir()
.expect("create a scratch directory")
}
fn native(path: &str) -> String {
path.replace('/', std::path::MAIN_SEPARATOR_STR)
}
fn put(root: &Path, rel: &str, contents: &str) -> PathBuf {
let path = root.join(native(rel));
let parent = path.parent().expect("a file below the root");
std::fs::create_dir_all(parent).expect("create the fixture's directories");
std::fs::write(&path, contents).expect("write a fixture file");
path
}
fn typed_args(args: &[&str]) -> Vec<String> {
args.iter().map(|arg| native(arg)).collect()
}
fn run(cwd: &Path, args: &[impl AsRef<OsStr>]) -> Output {
mds_bin()
.current_dir(cwd)
.args(args)
.stdin(Stdio::null())
.output()
.expect("run mds")
}
fn run_with_stdin(cwd: &Path, args: &[impl AsRef<OsStr>], input: &'static str) -> Output {
let mut child = mds_bin()
.current_dir(cwd)
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn mds");
let mut stdin = child.stdin.take().expect("stdin is piped");
let writer = std::thread::spawn(move || {
let _ = stdin.write_all(input.as_bytes());
});
let output = child.wait_with_output().expect("wait for mds");
writer.join().expect("the stdin writer does not panic");
output
}
fn text(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).into_owned()
}
fn spellings(dir: &Path) -> Vec<String> {
let created = dir.to_string_lossy().into_owned();
let canonical = dir
.canonicalize()
.expect("the scratch directory resolves")
.to_string_lossy()
.into_owned();
let mut forms = Vec::new();
for form in [created, canonical] {
let plain = form.strip_prefix(r"\\?\").unwrap_or(&form).to_owned();
let public = plain.strip_prefix("/private").unwrap_or(&plain).to_owned();
for spelling in [
form.clone(),
format!(r"\\?\{plain}"),
format!("/private{public}"),
plain,
public,
] {
forms.push(spelling.replace('\\', r"\\"));
forms.push(spelling);
}
}
forms.push(
dir.file_name()
.expect("the scratch directory has a name")
.to_string_lossy()
.into_owned(),
);
forms.sort();
forms.dedup();
forms
}
fn squash(s: &str) -> String {
s.chars()
.filter(|c| !c.is_whitespace() && *c != '\u{2502}')
.collect()
}
fn leak(text: &str, dir: &Path) -> Option<String> {
let squashed = squash(text);
spellings(dir)
.into_iter()
.find(|spelling| text.contains(spelling.as_str()) || squashed.contains(&squash(spelling)))
}
#[test]
fn the_leak_check_finds_the_scratch_directory_in_every_spelling() {
let dir = scratch();
let canonical = dir.path().canonicalize().unwrap();
let mut planted = vec![dir.path().to_path_buf(), canonical.clone()];
if let Ok(public) = canonical.strip_prefix("/private") {
planted.push(Path::new("/").join(public));
}
for shown in planted {
let line = format!("Compiled to {}\n", shown.join("page.md").display());
assert!(
leak(&line, dir.path()).is_some(),
"the check must find {shown:?} in {line:?}"
);
let json = serde_json::to_string(&line).unwrap();
assert!(
leak(&json, dir.path()).is_some(),
"the check must find {shown:?} JSON-escaped in {json:?}"
);
let text = shown.to_string_lossy().into_owned();
let at = text
.rfind('-')
.expect("the scratch directory's name has a hyphen")
+ 1;
let wrapped = format!(
" \u{d7} cannot create output directory {}\n \u{2502} {}/out: File exists\n",
&text[..at],
&text[at..]
);
assert!(
leak(&wrapped, dir.path()).is_some(),
"the check must find {shown:?} wrapped in {wrapped:?}"
);
}
assert_eq!(
leak(
&format!("Compiled to {}\n", native("out/page.md")),
dir.path()
),
None,
"a relative path is no leak"
);
}
#[test]
fn compiled_to_under_a_relative_out_dir_names_the_out_dir_as_typed() {
let dir = scratch();
put(dir.path(), "src/sub/page.mds", "Page\n");
let out = run(dir.path(), &["build", "src", "--out-dir", "out"]);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(
stderr,
format!(
"Compiled to {}\n1 built, 0 failed\n",
native("out/sub/page.md")
),
"the output is named below the out-dir as typed"
);
assert!(
dir.path().join("out/sub/page.md").is_file(),
"the output was written"
);
assert_eq!(leak(&stderr, dir.path()), None, "stderr: {stderr}");
assert_eq!(leak(&text(&out.stdout), dir.path()), None);
}
#[test]
fn compiled_to_under_a_symlinked_out_dir_names_the_link_not_its_target() {
let dir = scratch();
put(dir.path(), "src/sub/page.mds", "Page\n");
std::fs::create_dir(dir.path().join("real-target")).unwrap();
if !common::make_symlink(
&dir.path().join("real-target"),
&dir.path().join("alias-link"),
) {
return;
}
let out = run(dir.path(), &["build", "src", "--out-dir", "alias-link"]);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(
stderr,
format!(
"Compiled to {}\n1 built, 0 failed\n",
native("alias-link/sub/page.md")
),
"the output is named below the link as typed"
);
assert!(
dir.path().join(native("real-target/sub/page.md")).is_file(),
"the output was written through the link"
);
assert!(
!stderr.contains("real-target"),
"never by the link's target; stderr: {stderr}"
);
assert_eq!(leak(&stderr, dir.path()), None, "stderr: {stderr}");
let typed = dir.path().join("alias-link");
let out = run(
dir.path(),
&["build", "src", "--out-dir", typed.to_str().unwrap()],
);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(
stderr,
format!(
"Compiled to {}\n1 built, 0 failed\n",
typed.join("sub").join("page.md").display()
),
"an absolute out-dir is named exactly as typed"
);
assert!(
!stderr.contains("real-target"),
"never by the link's target; stderr: {stderr}"
);
}
#[test]
fn source_map_lines_under_an_out_dir_name_the_out_dir_as_typed() {
let dir = scratch();
put(dir.path(), "src/sub/page.mds", "Page\n");
let out = run(
dir.path(),
&["build", "src", "--out-dir", "out", "--source-map"],
);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(
stderr,
format!(
"Compiled to {}\nSource map written to {}\n1 built, 0 failed\n",
native("out/sub/page.md"),
native("out/sub/page.md.map")
),
);
assert!(
dir.path().join("out/sub/page.md.map").is_file(),
"the map was written"
);
assert_eq!(leak(&stderr, dir.path()), None, "stderr: {stderr}");
}
#[test]
fn an_output_directory_that_cannot_be_created_is_named_as_its_output_is() {
let dir = scratch();
put(dir.path(), "src/sub/page.mds", "Page\n");
put(dir.path(), "out/sub", "not a directory\n");
let proj = dir.path().join("proj");
put(&proj, "mds.json", r#"{"build":{"output_dir":"dist"}}"#);
put(&proj, "page.mds", "Page\n");
put(&proj, "dist", "not a directory\n");
for (cwd, args, shown) in [
(
dir.path(),
&["build", "src", "--out-dir", "out"][..],
"out/sub/page.md",
),
(proj.as_path(), &["build", "page.mds"][..], "./dist/page.md"),
] {
let out = run(cwd, args);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(2), "{args:?}: stderr: {stderr}");
let expected = format!("cannot write {}: ", native(shown));
assert!(
stderr.contains(&expected),
"{args:?}: stderr must hold {expected:?}; got: {stderr}"
);
assert_eq!(
leak(&stderr, dir.path()),
None,
"{args:?}: stderr: {stderr}"
);
}
}
#[test]
fn a_config_output_dir_is_named_through_the_directory_mds_json_was_reached_by() {
let dir = scratch();
let proj = dir.path().join("proj");
put(&proj, "mds.json", r#"{"build":{"output_dir":"dist"}}"#);
put(&proj, "page.mds", "Page\n");
put(&proj, "src/sub/deep.mds", "Deep\n");
let cases: [(&[&str], String, &str); 3] = [
(
&["build", "page.mds"],
format!("Compiled to {}\n", native("./dist/page.md")),
"dist/page.md",
),
(
&["build", "src"],
format!(
"Compiled to {}\n1 built, 0 failed\n",
native("src/../dist/sub/deep.md")
),
"dist/sub/deep.md",
),
(
&["build", "src/sub/deep.mds"],
format!("Compiled to {}\n", native("src/sub/../../dist/deep.md")),
"dist/deep.md",
),
];
for (args, expected, written) in cases {
let out = run(&proj, &typed_args(args));
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "{args:?}: stderr: {stderr}");
assert_eq!(stderr, expected, "{args:?}");
assert!(
proj.join(written).is_file(),
"{args:?}: {written} was written"
);
assert_eq!(
leak(&stderr, dir.path()),
None,
"{args:?}: stderr: {stderr}"
);
}
}
#[test]
fn a_config_output_dir_names_the_map_written_and_the_stale_map_removed() {
let dir = scratch();
let proj = dir.path().join("proj");
put(&proj, "mds.json", r#"{"build":{"output_dir":"dist"}}"#);
put(&proj, "page.mds", "Page\n");
let out = run(&proj, &["build", "page.mds", "--source-map"]);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(
stderr,
format!(
"Compiled to {}\nSource map written to {}\n",
native("./dist/page.md"),
native("./dist/page.md.map")
),
);
assert!(
proj.join("dist/page.md.map").is_file(),
"the map was written"
);
assert_eq!(leak(&stderr, dir.path()), None, "stderr: {stderr}");
let out = run(&proj, &["build", "page.mds"]);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(
stderr,
format!(
"Compiled to {}\nRemoved stale map {}\n",
native("./dist/page.md"),
native("./dist/page.md.map")
),
);
assert!(
!proj.join("dist/page.md.map").exists(),
"the stale map was removed"
);
assert_eq!(leak(&stderr, dir.path()), None, "stderr: {stderr}");
}
#[test]
fn the_config_source_map_warning_names_mds_json_as_reached() {
let dir = scratch();
let proj = dir.path().join("proj");
put(&proj, "mds.json", r#"{"build":{"source_map":true}}"#);
put(&proj, "page.mds", "Page\n");
put(&proj, "src/deep.mds", "Deep\n");
for (args, config, stdout) in [
(["build", "page.mds", "-o", "-"], "./mds.json", "Page\n"),
(
["build", "src/deep.mds", "-o", "-"],
"src/../mds.json",
"Deep\n",
),
] {
let out = run(&proj, &typed_args(&args));
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "{args:?}: stderr: {stderr}");
assert_eq!(
stderr,
format!(
"warning: source_map in {} has no effect when writing to stdout (sidecar \
requires -o <file> or --out-dir); use --inline to embed the map, or \
--no-source-map to silence this warning\n",
native(config)
),
"{args:?}"
);
assert_eq!(
text(&out.stdout),
stdout,
"{args:?}: the output went to stdout"
);
assert_eq!(
leak(&stderr, dir.path()),
None,
"{args:?}: stderr: {stderr}"
);
}
}
#[test]
fn clean_names_a_file_argument_as_typed() {
let dir = scratch();
let file = put(
dir.path(),
"sub/page.mds",
"---\ngreeting: Hello\n---\n\n{{greeting}}, world!\n",
);
let out = run(dir.path(), &typed_args(&["lint", "sub/page.mds"]));
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(stderr, format!("Clean: {}\n", native("sub/page.mds")));
let out = run(&dir.path().join("sub"), &["lint", "page.mds"]);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(stderr, "Clean: page.mds\n", "a bare name stays bare");
let typed = file.to_str().unwrap();
let out = run(dir.path(), &["lint", typed]);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(
stderr,
format!("Clean: {typed}\n"),
"an absolute argument is named as typed"
);
}
fn diff_header(path: &str) -> String {
let path = native(path);
format!("--- {path}\n+++ {path}\n")
}
fn run_typed(
root: &Path,
args: &[&str],
stdin: Option<&'static str>,
) -> (Option<i32>, String, String, String) {
let args = typed_args(args);
let out = match stdin {
Some(input) => run_with_stdin(root, &args, input),
None => run(root, &args),
};
let (stdout, stderr) = (text(&out.stdout), text(&out.stderr));
assert_eq!(leak(&stdout, root), None, "{args:?}: stdout: {stdout}");
assert_eq!(leak(&stderr, root), None, "{args:?}: stderr: {stderr}");
(
out.status.code(),
stdout,
stderr,
format!("mds {}", args.join(" ")),
)
}
#[test]
fn lint_names_a_file_argument_as_typed_and_its_findings_by_its_file_name() {
const WARN: &str = "---\ngreeting: Hello\nunused_key: x\n---\n\n{{greeting}}, world!\n";
const FIXABLE: &str = "@if \"x\" == \"y\":\nhidden\n@end\nHello\n";
let dir = scratch();
let root = dir.path();
put(root, "sub/warn.mds", WARN);
put(root, "lints/deep/warn.mds", WARN);
put(root, "sub/f.mds", FIXABLE);
put(root, "fix/deep/f.mds", FIXABLE);
let lint = |args: &[&str], stdin: Option<&'static str>| run_typed(root, args, stdin);
for (input, stdin, frame, key) in [
("sub/warn.mds", None, "[warn.mds:3:1]", "warn.mds"),
("lints", None, "[deep/warn.mds:3:1]", "deep/warn.mds"),
("-", Some(WARN), "[<stdin>:3:1]", "<stdin>"),
] {
let (code, _, stderr, label) = lint(&["lint", input], stdin);
assert_eq!(code, Some(1), "{label}: stderr: {stderr}");
assert!(
stderr.contains(frame),
"{label}: the finding's frame names the input as {frame:?}; stderr: {stderr}"
);
assert!(
!stderr.contains(&format!("[{}", native(input))) || input == "-",
"{label}: never by the path as typed; stderr: {stderr}"
);
let (code, stdout, _, label) = lint(&["lint", input, "--format", "json"], stdin);
assert_eq!(code, Some(1), "{label}: stdout: {stdout}");
let key = format!("\"file\":\"{key}\"");
assert!(stdout.contains(&key), "{label}: {key}; stdout: {stdout}");
}
let (code, _, stderr, label) = lint(&["lint", "--fix", "--check", "sub/f.mds"], None);
assert_eq!(code, Some(1), "{label}: stderr: {stderr}");
let would = format!("Would fix: {}", native("sub/f.mds"));
assert!(
stderr.lines().any(|line| line == would),
"{label}: {would:?}; stderr: {stderr}"
);
let (code, _, stderr, label) = lint(&["lint", "--fix", "--check", "-"], Some(FIXABLE));
assert_eq!(code, Some(1), "{label}: stderr: {stderr}");
assert!(
stderr.lines().any(|line| line == "Would fix: <stdin>"),
"{label}: stderr: {stderr}"
);
for (input, shown) in [("sub/f.mds", "sub/f.mds"), ("fix", "fix/deep/f.mds")] {
let (code, stdout, stderr, label) = lint(&["lint", "--fix", "--diff", input], None);
assert_eq!(code, Some(1), "{label}: stderr: {stderr}");
assert!(
stdout.starts_with(&diff_header(shown)),
"{label}: the diff names the input as {shown:?}; stdout: {stdout}"
);
}
let (code, _, stderr, label) = lint(&["lint", "--fix", "sub/f.mds"], None);
assert_eq!(code, Some(0), "{label}: stderr: {stderr}");
assert_eq!(
stderr,
format!("Fixed: {}\n", native("sub/f.mds")),
"{label}"
);
let (code, _, stderr, label) = lint(&["lint", "--fix", "fix"], None);
assert_eq!(code, Some(0), "{label}: stderr: {stderr}");
assert_eq!(
stderr,
format!(
"Fixed: {}\n1 clean, 0 with warnings, 0 with errors, 0 resource-limited\n",
native("fix/deep/f.mds")
),
"{label}"
);
}
#[test]
fn fmt_names_a_file_argument_as_typed_and_a_directory_s_entries_below_it() {
let dir = scratch();
let root = dir.path();
put(root, "sub/messy.mds", "Hello");
put(root, "sub/fine.mds", "Fine\n");
put(root, "src/a.mds", "A");
put(root, "src/inner/b.mds", "B");
let fmt = |args: &[&str], stdin: Option<&'static str>| run_typed(root, args, stdin);
let cases = [
(
&["fmt", "--check", "sub/messy.mds"][..],
None,
1,
String::new(),
format!("Would reformat: {}\n", native("sub/messy.mds")),
),
(
&["fmt", "--check", "sub/fine.mds"][..],
None,
0,
String::new(),
format!("Unchanged: {}\n", native("sub/fine.mds")),
),
(
&["fmt", "--diff", "sub/messy.mds"][..],
None,
0,
diff_header("sub/messy.mds"),
String::new(),
),
(
&["fmt", "--check", "-"][..],
Some("Q"),
1,
String::new(),
"Would reformat: <stdin>\n".to_owned(),
),
(
&["fmt", "--diff", "-"][..],
Some("Q"),
0,
diff_header("<stdin>"),
String::new(),
),
];
for (args, stdin, exit, stdout_start, expected_stderr) in cases {
let (code, stdout, stderr, label) = fmt(args, stdin);
assert_eq!(code, Some(exit), "{label}: stderr: {stderr}");
assert!(
stdout.starts_with(&stdout_start),
"{label}: stdout starts {stdout_start:?}; stdout: {stdout}"
);
assert_eq!(stderr, expected_stderr, "{label}");
}
let (code, stdout, stderr, label) = fmt(&["fmt", "--diff", "src"], None);
assert_eq!(code, Some(0), "{label}: stderr: {stderr}");
for entry in ["src/a.mds", "src/inner/b.mds"] {
assert!(
stdout.contains(&diff_header(entry)),
"{label}: {entry} is named below the directory as typed; stdout: {stdout}"
);
}
assert_eq!(
stderr, "2 would reformat, 0 unchanged, 0 failed\n",
"{label}"
);
let (code, _, stderr, label) = fmt(&["fmt", "sub/messy.mds"], None);
assert_eq!(code, Some(0), "{label}: stderr: {stderr}");
assert_eq!(
stderr,
format!("Formatted: {}\n", native("sub/messy.mds")),
"{label}"
);
let (code, _, stderr, label) = fmt(&["fmt", "src"], None);
assert_eq!(code, Some(0), "{label}: stderr: {stderr}");
assert_eq!(
lines_starting(&stderr, "Formatted: "),
[
format!("Formatted: {}", native("src/a.mds")),
format!("Formatted: {}", native("src/inner/b.mds")),
],
"{label}: stderr: {stderr}"
);
assert!(
stderr.ends_with("2 formatted, 0 unchanged, 0 failed\n"),
"{label}: stderr: {stderr}"
);
}
#[test]
fn init_names_the_file_it_creates_as_typed() {
let dir = scratch();
let root = dir.path();
std::fs::create_dir_all(root.join("sub")).unwrap();
std::fs::create_dir_all(root.join("bare")).unwrap();
let typed = native("sub/new.mds");
let out = run(root, &typed_args(&["init", "sub/new.mds"]));
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(
stderr,
format!("Created {typed}\n Try: mds build {typed}\n")
);
assert!(root.join("sub/new.mds").is_file(), "the file was created");
assert_eq!(leak(&stderr, root), None, "stderr: {stderr}");
let out = run(root, &typed_args(&["init", "sub/new.mds"]));
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(1), "stderr: {stderr}");
let refusal = format!("{typed} already exists (use --force to overwrite)");
assert!(stderr.contains(&refusal), "{refusal:?}; stderr: {stderr}");
assert_eq!(leak(&stderr, root), None, "stderr: {stderr}");
let out = run(&root.join("bare"), &["init"]);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(stderr, "Created hello.mds\n Try: mds build hello.mds\n");
assert_eq!(leak(&stderr, root), None, "stderr: {stderr}");
}
struct Row {
cwd: &'static str,
args: &'static [&'static str],
stdin: Option<&'static str>,
exit: i32,
stdout: Option<&'static str>,
stderr: Option<&'static str>,
}
const ROWS: &[Row] = &[
Row {
cwd: ".",
args: &["build", "x.mds"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Compiled to ./x.md\n"),
},
Row {
cwd: ".",
args: &["build", "src/a.mds"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Compiled to src/a.md\n"),
},
Row {
cwd: ".",
args: &["build", "x.mds", "--source-map"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Source map written to ./x.md.map\n"),
},
Row {
cwd: ".",
args: &["build", "x.mds", "--out-dir", "o1"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Compiled to o1/x.md\n"),
},
Row {
cwd: ".",
args: &["build", "x.mds", "-o", "o2/y.md"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Compiled to o2/y.md\n"),
},
Row {
cwd: ".",
args: &["build", "-", "--out-dir", "o3"],
stdin: Some("Hi\n"),
exit: 0,
stdout: None,
stderr: Some("Compiled to o3/output.md\n"),
},
Row {
cwd: ".",
args: &["build", "src"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Compiled to src/sub/b.md\n"),
},
Row {
cwd: ".",
args: &["build", "src", "--out-dir", "o4", "--source-map"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Source map written to o4/sub/b.md.map\n"),
},
Row {
cwd: ".",
args: &["build", "bad", "--out-dir", "o5"],
stdin: None,
exit: 1,
stdout: None,
stderr: Some("undefined variable 'nope'"),
},
Row {
cwd: "proj",
args: &["build", "p.mds"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Compiled to ./dist/p.md\n"),
},
Row {
cwd: "proj",
args: &["build", "src", "--source-map"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Source map written to src/../dist/q.md.map\n"),
},
Row {
cwd: "smap",
args: &["build", "s.mds", "-o", "-"],
stdin: None,
exit: 0,
stdout: Some("S\n"),
stderr: Some("warning: source_map in ./mds.json has no effect"),
},
Row {
cwd: ".",
args: &["check", "x.mds"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("OK: x.mds\n"),
},
Row {
cwd: ".",
args: &["check", "src/a.mds"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("OK: src/a.mds\n"),
},
Row {
cwd: ".",
args: &["check", "-"],
stdin: Some("Hi\n"),
exit: 0,
stdout: None,
stderr: Some("OK: <stdin>\n"),
},
Row {
cwd: ".",
args: &["check", "src"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("2 passed, 0 failed\n"),
},
Row {
cwd: ".",
args: &["check", "bad"],
stdin: None,
exit: 1,
stdout: None,
stderr: Some("undefined variable 'nope'"),
},
Row {
cwd: ".",
args: &["lint", "lint/clean.mds"],
stdin: None,
exit: 0,
stdout: None,
stderr: Some("Clean: lint/clean.mds\n"),
},
Row {
cwd: ".",
args: &["lint", "lint/warn.mds"],
stdin: None,
exit: 1,
stdout: None,
stderr: Some("unused_key"),
},
Row {
cwd: ".",
args: &["lint", "lint"],
stdin: None,
exit: 1,
stdout: None,
stderr: Some("1 clean, 1 with warnings, 0 with errors, 0 resource-limited\n"),
},
Row {
cwd: ".",
args: &["lint", "lint", "--format", "json"],
stdin: None,
exit: 1,
stdout: Some("\"file\":\"warn.mds\""),
stderr: None,
},
Row {
cwd: ".",
args: &["lint", "fix", "--fix", "--check"],
stdin: None,
exit: 1,
stdout: None,
stderr: Some("Would fix: fix/f.mds\n"),
},
];
#[test]
fn no_listed_build_check_or_lint_run_names_the_working_directory() {
let dir = scratch();
let root = dir.path();
put(root, "x.mds", "Hello\n");
put(root, "src/a.mds", "A\n");
put(root, "src/sub/b.mds", "B\n");
put(root, "bad/undef.mds", "Hi {{nope}}\n");
put(root, "proj/mds.json", r#"{"build":{"output_dir":"dist"}}"#);
put(root, "proj/p.mds", "P\n");
put(root, "proj/src/q.mds", "Q\n");
put(root, "smap/mds.json", r#"{"build":{"source_map":true}}"#);
put(root, "smap/s.mds", "S\n");
put(
root,
"lint/clean.mds",
"---\ngreeting: Hello\n---\n\n{{greeting}}, world!\n",
);
put(
root,
"lint/warn.mds",
"---\ngreeting: Hello\nunused_key: never referenced in the body\n---\n\n{{greeting}}, world!\n",
);
put(
root,
"fix/f.mds",
"@if \"x\" == \"y\":\nhidden\n@end\nHello\n",
);
for row in ROWS {
let cwd = root.join(row.cwd);
let args = typed_args(row.args);
let out = match row.stdin {
Some(input) => run_with_stdin(&cwd, &args, input),
None => run(&cwd, &args),
};
let (stdout, stderr) = (text(&out.stdout), text(&out.stderr));
let label = format!("(in {}) mds {}", row.cwd, args.join(" "));
assert_eq!(
out.status.code(),
Some(row.exit),
"{label}: stdout: {stdout}; stderr: {stderr}"
);
if let Some(line) = row.stdout {
assert!(
stdout.contains(&native(line)),
"{label}: stdout must hold {line:?}; got: {stdout}"
);
}
if let Some(line) = row.stderr {
assert!(
stderr.contains(&native(line)),
"{label}: stderr must hold {line:?}; got: {stderr}"
);
}
assert_eq!(leak(&stdout, root), None, "{label}: stdout: {stdout}");
assert_eq!(leak(&stderr, root), None, "{label}: stderr: {stderr}");
}
}
#[test]
fn watch_startup_names_an_out_dir_and_a_config_output_dir_as_build_does() {
let dir = scratch();
put(dir.path(), "src/sub/page.mds", "Page\n");
let proj = dir.path().join("proj");
put(&proj, "mds.json", r#"{"build":{"output_dir":"dist"}}"#);
put(&proj, "p.mds", "P\n");
put(&proj, "src/q.mds", "Q\n");
let cases: [(&Path, &[&str], String); 3] = [
(
dir.path(),
&["watch", "src", "--out-dir", "out"],
format!("Compiled to {}\n", native("out/sub/page.md")),
),
(
&proj,
&["watch", "src"],
format!("Compiled to {}\n", native("src/../dist/q.md")),
),
(
&proj,
&["watch", "p.mds"],
format!("Compiled to {}\n", native("./dist/p.md")),
),
];
for (cwd, args, expected) in cases {
let (child, tap, _) = common::spawn_watch_ready(
mds_bin()
.current_dir(cwd)
.args(args)
.args(["--debounce", "0"])
.stdout(Stdio::null()),
);
let mut child = common::ChildGuard(child);
let stderr = tap.finish_text(&mut child);
assert!(
stderr.contains(&expected),
"{args:?}: stderr must hold {expected:?}; got: {stderr}"
);
let canonical = cwd.canonicalize().unwrap();
assert!(
!stderr.contains(&format!("Compiled to {}", canonical.display())),
"{args:?}: never below the canonical directory; got: {stderr}"
);
}
}
const WATCH_STEP: Duration = Duration::from_secs(10);
fn watch_live(
cwd: &Path,
args: &[impl AsRef<OsStr>],
tap_stdout: bool,
) -> (
common::ChildGuard,
common::StderrTap,
Option<common::StdoutTap>,
) {
let stdout = if tap_stdout {
Stdio::piped()
} else {
Stdio::null()
};
let (child, stderr, stdout) = common::spawn_watch_ready(
mds_bin()
.current_dir(cwd)
.args(args)
.args(["--debounce", "0"])
.stdout(stdout),
);
(common::ChildGuard(child), stderr, stdout)
}
#[cfg(unix)]
struct Writable(PathBuf);
#[cfg(unix)]
impl Drop for Writable {
fn drop(&mut self) {
use std::os::unix::fs::PermissionsExt as _;
let _ = std::fs::set_permissions(&self.0, std::fs::Permissions::from_mode(0o755));
}
}
fn lines_starting(text: &str, prefix: &str) -> Vec<String> {
let mut lines: Vec<String> = text
.lines()
.filter(|line| line.starts_with(prefix))
.map(str::to_owned)
.collect();
lines.sort();
lines
}
#[test]
fn watching_names_the_entry_and_the_directory_as_typed() {
let dir = scratch();
let root = dir.path();
put(root, "sub/page.mds", "Page\n");
put(root, "solo/x.mds", "X\n");
put(root, "src/a.mds", "A\n");
put(root, "src/sub/b.mds", "B\n");
let cases: [(&str, &[&str], &str, &[&str]); 6] = [
(
".",
&["watch", "sub/page.mds"],
"Watching sub/page.mds",
&["sub/page.md"],
),
(
".",
&["watch", "sub/../sub/page.mds"],
"Watching sub/../sub/page.mds",
&["sub/../sub/page.md"],
),
("solo", &["watch"], "Watching x.mds", &["./x.md"]),
(
".",
&["watch", "src"],
"Watching directory src",
&["src/a.md", "src/sub/b.md"],
),
(
"src",
&["watch", "."],
"Watching directory .",
&["./a.md", "./sub/b.md"],
),
(
".",
&["watch", "src/sub/.."],
"Watching directory src/sub/..",
&["src/sub/../a.md", "src/sub/../sub/b.md"],
),
];
for (cwd, args, banner, compiled) in cases {
let args = typed_args(args);
let label = format!("(in {cwd}) mds {}", args.join(" "));
let (mut child, tap, _) = watch_live(&root.join(cwd), &args, false);
let stderr = tap.finish_text(&mut child);
let mut expected: Vec<String> = compiled
.iter()
.map(|name| format!("Compiled to {}", native(name)))
.collect();
expected.sort();
assert_eq!(
stderr.lines().next(),
Some(native(banner).as_str()),
"{label}: the banner names the argument as typed; stderr: {stderr}"
);
assert_eq!(
lines_starting(&stderr, "Compiled to "),
expected,
"{label}: stderr: {stderr}"
);
assert_eq!(
stderr.lines().count(),
1 + compiled.len(),
"{label}: nothing but the banner and the startup outputs; stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "{label}: stderr: {stderr}");
}
}
#[test]
fn watching_names_a_path_reached_through_a_symlink_by_the_link() {
let dir = scratch();
let root = dir.path();
put(root, "real-target/page.mds", "Page\n");
put(root, "real-target/sub/b.mds", "B\n");
if !common::make_symlink(&root.join("real-target"), &root.join("alias-link")) {
return;
}
let cases: [(&[&str], &str, &str, &str, &str); 2] = [
(
&["watch", "alias-link/page.mds"],
"Watching alias-link/page.mds",
"alias-link/page.md",
"real-target/page.mds",
"real-target/page.md",
),
(
&["watch", "alias-link/sub"],
"Watching directory alias-link/sub",
"alias-link/sub/b.md",
"real-target/sub/b.mds",
"real-target/sub/b.md",
),
];
for (args, banner, output, edit, written) in cases {
let args = typed_args(args);
let (banner, output) = (native(banner), native(output));
let (mut child, tap, _) = watch_live(root, &args, false);
write_atomic(&root.join(native(edit)), "Edited\n");
common::wait_for_tap(&tap, "Recompiled ", WATCH_STEP);
let stderr = tap.finish_text(&mut child);
assert_eq!(
stderr.lines().next(),
Some(banner.as_str()),
"{args:?}: stderr: {stderr}"
);
assert_eq!(
lines_starting(&stderr, "Compiled to "),
[format!("Compiled to {output}")],
"{args:?}: stderr: {stderr}"
);
let recompiled = lines_starting(&stderr, "Recompiled ");
assert!(
!recompiled.is_empty()
&& recompiled
.iter()
.all(|line| line.starts_with(&format!("Recompiled {output} ("))),
"{args:?}: every rebuild names the output by the link; stderr: {stderr}"
);
assert_eq!(
std::fs::read_to_string(root.join(native(written))).unwrap(),
"Edited\n",
"{args:?}: the rebuild was written through the link"
);
assert!(
!stderr.contains("real-target"),
"{args:?}: never by the link's target; stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "{args:?}: stderr: {stderr}");
}
}
struct Session {
cwd: &'static str,
args: &'static [&'static str],
stdout: bool,
compiled: &'static [&'static str],
edit: &'static str,
recompiled: &'static str,
removed: Option<(&'static str, &'static str)>,
}
const SESSIONS: &[Session] = &[
Session {
cwd: ".",
args: &["watch", "w1/page.mds"],
stdout: false,
compiled: &["w1/page.md"],
edit: "w1/page.mds",
recompiled: "w1/page.md",
removed: None,
},
Session {
cwd: ".",
args: &["watch", "w2/page.mds", "--out-dir", "o2"],
stdout: false,
compiled: &["o2/page.md"],
edit: "w2/page.mds",
recompiled: "o2/page.md",
removed: None,
},
Session {
cwd: ".",
args: &["watch", "w3/page.mds", "-o", "o3/y.md"],
stdout: false,
compiled: &["o3/y.md"],
edit: "w3/page.mds",
recompiled: "o3/y.md",
removed: None,
},
Session {
cwd: ".",
args: &["watch", "w4/page.mds", "-o", "-"],
stdout: true,
compiled: &[],
edit: "w4/page.mds",
recompiled: "<stdout>",
removed: None,
},
Session {
cwd: "w5",
args: &["watch"],
stdout: false,
compiled: &["./x.md"],
edit: "x.mds",
recompiled: "./x.md",
removed: None,
},
Session {
cwd: "p6",
args: &["watch", "p.mds"],
stdout: false,
compiled: &["./dist/p.md"],
edit: "p.mds",
recompiled: "./dist/p.md",
removed: None,
},
Session {
cwd: ".",
args: &["watch", "d7"],
stdout: false,
compiled: &["d7/a.md", "d7/sub/b.md"],
edit: "d7/sub/b.mds",
recompiled: "d7/sub/b.md",
removed: Some(("d7/sub/b.mds", "d7/sub/b.md")),
},
Session {
cwd: ".",
args: &["watch", "d8", "--out-dir", "o8"],
stdout: false,
compiled: &["o8/a.md", "o8/sub/b.md"],
edit: "d8/sub/b.mds",
recompiled: "o8/sub/b.md",
removed: Some(("d8/sub/b.mds", "o8/sub/b.md")),
},
Session {
cwd: "d9",
args: &["watch", "."],
stdout: false,
compiled: &["./a.md", "./sub/b.md"],
edit: "sub/b.mds",
recompiled: "./sub/b.md",
removed: Some(("sub/b.mds", "./sub/b.md")),
},
Session {
cwd: "p10",
args: &["watch", "src"],
stdout: false,
compiled: &["src/../dist/q.md", "src/../dist/sub/r.md"],
edit: "src/sub/r.mds",
recompiled: "src/../dist/sub/r.md",
removed: Some(("src/sub/r.mds", "src/../dist/sub/r.md")),
},
];
#[test]
fn recompiled_and_removed_name_each_output_as_typed() {
let dir = scratch();
let root = dir.path();
for src in ["w1", "w2", "w3", "w4"] {
put(root, &format!("{src}/page.mds"), "Page\n");
}
put(root, "w5/x.mds", "X\n");
put(root, "p6/mds.json", r#"{"build":{"output_dir":"dist"}}"#);
put(root, "p6/p.mds", "P\n");
for src in ["d7", "d8", "d9"] {
put(root, &format!("{src}/a.mds"), "A\n");
put(root, &format!("{src}/sub/b.mds"), "B\n");
}
put(root, "p10/mds.json", r#"{"build":{"output_dir":"dist"}}"#);
put(root, "p10/src/q.mds", "Q\n");
put(root, "p10/src/sub/r.mds", "R\n");
for session in SESSIONS {
let cwd = root.join(session.cwd);
let args = typed_args(session.args);
let label = format!("(in {}) mds {}", session.cwd, args.join(" "));
let (mut child, tap, stdout_tap) = watch_live(&cwd, &args, session.stdout);
write_atomic(&cwd.join(native(session.edit)), "Edited\n");
common::wait_for_tap(&tap, "Recompiled ", WATCH_STEP);
if let Some((source, _)) = session.removed {
std::fs::remove_file(cwd.join(native(source))).expect("delete the source");
common::wait_for_tap(&tap, "(source deleted)", WATCH_STEP);
}
let stderr = tap.finish_text(&mut child);
let stdout = stdout_tap.map_or_else(String::new, |tap| tap.finish_text(&mut child));
let mut compiled: Vec<String> = session
.compiled
.iter()
.map(|name| format!("Compiled to {}", native(name)))
.collect();
compiled.sort();
assert_eq!(
lines_starting(&stderr, "Compiled to "),
compiled,
"{label}: stderr: {stderr}"
);
let recompiled = lines_starting(&stderr, "Recompiled ");
let prefix = format!("Recompiled {} (", native(session.recompiled));
assert!(
!recompiled.is_empty() && recompiled.iter().all(|line| line.starts_with(&prefix)),
"{label}: every rebuild names the output as {prefix:?}; stderr: {stderr}"
);
let removed: Vec<String> = session
.removed
.iter()
.map(|(_, output)| format!("Removed {} (source deleted)", native(output)))
.collect();
assert_eq!(
lines_starting(&stderr, "Removed "),
removed,
"{label}: stderr: {stderr}"
);
if session.stdout {
assert_eq!(
stdout, "Page\nEdited\n",
"{label}: the startup output and the rebuild went to stdout"
);
}
assert_eq!(leak(&stderr, root), None, "{label}: stderr: {stderr}");
assert_eq!(leak(&stdout, root), None, "{label}: stdout: {stdout}");
}
}
#[test]
fn an_emptied_output_is_named_as_its_recompiled_line_names_it() {
let dir = scratch();
let root = dir.path();
put(root, "e1/page.mds", "Page\n");
put(root, "e2/page.mds", "Page\n");
put(root, "e3/sub/a.mds", "A\n");
put(root, "e4/sub/a.mds", "A\n");
let sessions: [(&str, &[&str], &str, &str); 4] = [
(".", &["watch", "e1/page.mds"], "e1/page.mds", "e1/page.md"),
("e2", &["watch", "page.mds"], "page.mds", "./page.md"),
(
".",
&["watch", "e3", "--out-dir", "o3"],
"e3/sub/a.mds",
"o3/sub/a.md",
),
("e4", &["watch", "."], "sub/a.mds", "./sub/a.md"),
];
for (rel, args, emptied, output) in sessions {
let cwd = root.join(rel);
let args = typed_args(args);
let label = format!("(in {rel}) mds {}", args.join(" "));
let (mut child, tap, _) = watch_live(&cwd, &args, false);
drop(std::fs::File::create(cwd.join(native(emptied))).expect("empty the source"));
common::wait_for_tap(&tap, "Wrote an empty output: ", WATCH_STEP);
let stderr = tap.finish_text(&mut child);
assert_eq!(
lines_starting(&stderr, "Wrote an empty output: "),
[format!("Wrote an empty output: {}", native(output))],
"{label}: stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "{label}: stderr: {stderr}");
}
}
#[test]
fn removed_names_the_output_as_typed_when_the_vars_file_changes_in_the_same_batch() {
let dir = scratch();
let root = dir.path();
put(root, "d/a.mds", "A {{name}}\n");
put(root, "d/sub/b.mds", "B\n");
put(root, "vars.json", r#"{"name": "one"}"#);
let (child, tap, _) = common::spawn_watch_ready(
mds_bin()
.current_dir(root)
.args(typed_args(&["watch", "d", "--vars", "vars.json"]))
.args(["--debounce", "500"])
.stdout(Stdio::null()),
);
let mut child = common::ChildGuard(child);
std::fs::remove_file(root.join(native("d/sub/b.mds"))).expect("delete the source");
write_atomic(&root.join("vars.json"), r#"{"name": "two"}"#);
common::wait_for_tap(&tap, "(source deleted)", WATCH_STEP);
common::wait_for_tap(&tap, "Recompiled ", WATCH_STEP);
let stderr = tap.finish_text(&mut child);
assert_eq!(
lines_starting(&stderr, "Removed "),
[format!("Removed {} (source deleted)", native("d/sub/b.md"))],
"stderr: {stderr}"
);
let prefix = format!("Recompiled {} (", native("d/a.md"));
let recompiled = lines_starting(&stderr, "Recompiled ");
assert!(
!recompiled.is_empty() && recompiled.iter().all(|line| line.starts_with(&prefix)),
"the vars change rebuilt the other source, named as {prefix:?}; stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "stderr: {stderr}");
}
fn retire_a_kept_output(
cwd: &Path,
extra: &[&str],
edit_vars: bool,
block: impl FnOnce(&Path) -> bool,
) -> Option<String> {
put(cwd, "d/sub/b.mds", "B\n");
put(cwd, "vars.json", r#"{"name": "one"}"#);
let (child, tap, _) = common::spawn_watch_ready(
mds_bin()
.current_dir(cwd)
.args(["watch", "d", "--out-dir", "o"])
.args(extra)
.stdout(Stdio::null()),
);
let mut child = common::ChildGuard(child);
if !block(&cwd.join("o").join("sub")) {
return None;
}
std::fs::remove_file(cwd.join(native("d/sub/b.mds"))).expect("delete the source");
if edit_vars {
write_atomic(&cwd.join("vars.json"), r#"{"name": "two"}"#);
}
common::wait_for_tap(&tap, "could not remove", WATCH_STEP);
Some(tap.finish_text(&mut child))
}
#[test]
fn an_output_that_cannot_be_removed_is_named_as_typed() {
let dir = scratch();
let root = dir.path();
let shown = native("o/sub/b.md");
for (fixture, extra, edit_vars) in [
("alone", &["--debounce", "0"][..], false),
(
"with-vars",
&["--vars", "vars.json", "--debounce", "500"][..],
true,
),
] {
let cwd = root.join(fixture).join("directory");
let stderr = retire_a_kept_output(&cwd, extra, edit_vars, |sub| {
std::fs::remove_file(sub.join("b.md")).expect("remove the session's output");
std::fs::create_dir(sub.join("b.md")).expect("put a directory in its place");
true
})
.expect("a directory always keeps the output");
let warning = format!("warning: could not remove {shown}: not a regular file\n");
assert!(
stderr.contains(&warning),
"{fixture}: the output is named below the out-dir as typed, {warning:?}; \
stderr: {stderr}"
);
assert!(
cwd.join(&shown).is_dir(),
"{fixture}: the directory is still there"
);
assert_eq!(leak(&stderr, root), None, "{fixture}: stderr: {stderr}");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
let cwd = root.join(fixture).join("read-only");
let _writable = Writable(cwd.join("o").join("sub"));
let stderr = retire_a_kept_output(&cwd, extra, edit_vars, |sub| {
std::fs::set_permissions(sub, std::fs::Permissions::from_mode(0o555)).unwrap();
if std::fs::write(sub.join(".write-probe"), b"").is_ok() {
let _ = std::fs::remove_file(sub.join(".write-probe"));
eprintln!(
"skipped the read-only arm: {} is writable at mode 0o555 (running as \
root?)",
sub.display()
);
return false;
}
true
});
if let Some(stderr) = stderr {
assert!(
stderr.contains(&format!("warning: could not remove {shown}: ")),
"{fixture}: the output is named below the out-dir as typed; stderr: {stderr}"
);
assert!(
cwd.join(&shown).is_file(),
"{fixture}: the output is still there"
);
assert_eq!(leak(&stderr, root), None, "{fixture}: stderr: {stderr}");
}
}
}
}
fn announce_skip(test: &str, reason: &str) {
eprintln!("SKIPPED {test}: {reason}");
if let Some(path) = std::env::var_os("GITHUB_STEP_SUMMARY") {
if let Ok(mut summary) = std::fs::OpenOptions::new()
.append(true)
.create(true)
.open(path)
{
let _ = writeln!(summary, ":warning: path labels: skipped {test}: {reason}");
}
}
}
#[test]
fn an_entry_typed_in_another_case_names_its_output_by_the_name_on_disk() {
let dir = scratch();
let root = dir.path();
put(root, "b/page.mds", "Page\n");
if !root.join(native("b/PAGE.mds")).exists() {
announce_skip(
"an_entry_typed_in_another_case_names_its_output_by_the_name_on_disk",
"the volume of the temporary directory tells case apart",
);
return;
}
let build = run(&root.join("b"), &["build", "PAGE.mds"]);
let built = String::from_utf8_lossy(&build.stderr);
let control = format!("Compiled to {}", native("./PAGE.md"));
assert!(
built.lines().any(|line| line == control),
"control: mds build names the output by the case typed, {control:?}; stderr: {built}"
);
for (cwd, args, output, written) in [
("w", &["watch", "PAGE.mds"][..], "./page.md", "w"),
(
"o",
&["watch", "PAGE.mds", "--out-dir", "out"],
"out/page.md",
"o/out",
),
] {
let output = native(output);
put(root, &format!("{cwd}/page.mds"), "Page\n");
let (mut child, tap, _) = watch_live(&root.join(cwd), args, false);
write_atomic(&root.join(cwd).join("page.mds"), "Edited\n");
common::wait_for_tap(&tap, "Recompiled ", WATCH_STEP);
let stderr = tap.finish_text(&mut child);
assert_eq!(
stderr.lines().next(),
Some("Watching PAGE.mds"),
"{args:?}: stderr: {stderr}"
);
assert_eq!(
lines_starting(&stderr, "Compiled to "),
[format!("Compiled to {output}")],
"{args:?}: stderr: {stderr}"
);
let recompiled = lines_starting(&stderr, "Recompiled ");
assert!(
!recompiled.is_empty()
&& recompiled
.iter()
.all(|line| line.starts_with(&format!("Recompiled {output} ("))),
"{args:?}: every rebuild names the output by the name on disk; stderr: {stderr}"
);
let names: Vec<String> = std::fs::read_dir(root.join(native(written)))
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
.filter(|name| name.ends_with(".md"))
.collect();
assert_eq!(names, ["page.md"], "{args:?}: written under that name");
assert_eq!(leak(&stderr, root), None, "{args:?}: stderr: {stderr}");
}
}
#[test]
fn a_vars_directory_that_cannot_be_watched_is_named_as_typed() {
let dir = scratch();
let root = dir.path();
put(root, "d/a.mds", "A\n");
let mut cases = vec![("nodir/v.json", "nodir")];
if cfg!(target_os = "macos") {
cases.push(("v.json", "."));
}
for (vars, shown) in cases {
let args = typed_args(&["watch", "d", "--vars", vars]);
let label = format!("mds {}", args.join(" "));
let (child, tap, _) = common::spawn_watch_unsynchronized(
mds_bin()
.current_dir(root)
.args(&args)
.stdout(Stdio::null()),
);
let mut child = common::ChildGuard(child);
let deadline = Instant::now() + WATCH_STEP;
let status = loop {
if let Some(status) = child.0.try_wait().expect("wait for mds watch") {
break status;
}
assert!(
Instant::now() < deadline,
"{label}: the session did not end within {WATCH_STEP:?}"
);
std::thread::sleep(Duration::from_millis(10));
};
let stderr = tap.finish_text(&mut child);
assert!(
!status.success(),
"{label}: a missing --vars file ends the session at startup; stderr: {stderr}"
);
let warning = format!(
"warning: failed to watch vars directory {}: {WATCHER_REFUSAL}\n",
native(shown)
);
assert!(
stderr.contains(&warning),
"{label}: the directory is named as the --vars file was typed, and the watcher's \
cause names no path; stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "{label}: stderr: {stderr}");
}
}
const WATCHER_REFUSAL: &str = if cfg!(windows) {
"Input watch path is neither a file nor a directory."
} else {
"No path was found."
};
fn refused(path: &str) -> String {
format!("cannot write {path}: refusing to replace a symlink")
}
#[must_use]
fn plant_symlink(root: &Path, rel: &str) -> bool {
let target = root.join("target.md");
if !target.exists() {
put(root, "target.md", "T\n");
}
let at = root.join(native(rel));
std::fs::create_dir_all(at.parent().expect("a path below the root")).unwrap();
common::make_symlink(&target, &at)
}
#[cfg(unix)]
#[test]
fn an_error_writing_an_output_names_it_as_its_status_line_does() {
use std::os::unix::fs::PermissionsExt as _;
let dir = scratch();
let root = dir.path();
put(root, "src/a.mds", "A\n");
assert!(plant_symlink(root, "out/a.md"));
put(root, "proj/mds.json", r#"{"build":{"output_dir":"dist"}}"#);
put(root, "proj/p.mds", "P\n");
put(root, "proj/src/q.mds", "Q\n");
assert!(plant_symlink(root, "proj/dist/p.md"));
assert!(plant_symlink(root, "proj/dist/q.md"));
put(root, "nts/n.mds", "N\n");
assert!(plant_symlink(root, "nts/n.md"));
assert!(plant_symlink(root, "o4/a.md"));
assert!(plant_symlink(root, "o5/y.md"));
for (cwd, args, shown) in [
(".", &["build", "src", "--out-dir", "out"][..], "out/a.md"),
("proj", &["build", "p.mds"][..], "./dist/p.md"),
("proj", &["build", "src"][..], "src/../dist/q.md"),
(".", &["build", "src/a.mds", "-o", "o5/y.md"][..], "o5/y.md"),
(
".",
&["build", "src/a.mds", "--out-dir", "o4"][..],
"o4/a.md",
),
(".", &["build", "nts/n.mds"][..], "nts/n.md"),
(".", &["build", "nts"][..], "nts/n.md"),
] {
let out = run(&root.join(cwd), args);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(2), "{args:?}: stderr: {stderr}");
assert!(
squash(&stderr).contains(&squash(&refused(shown))),
"{args:?}: the error names the output as {shown:?}; stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "{args:?}: stderr: {stderr}");
}
put(root, "msrc/x.mds", "@message user:\nHi\n@end\n");
put(root, "stale/x.md/keep", "keep\n");
put(root, "proj/msrc/x.mds", "@message user:\nHi\n@end\n");
put(root, "proj/dist/x.md/keep", "keep\n");
for (cwd, args, shown) in [
(".", &["build", "msrc", "--out-dir", "stale"][..], "stale/x"),
("proj", &["build", "msrc"][..], "msrc/../dist/x"),
] {
let out = run(&root.join(cwd), args);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(0), "{args:?}: stderr: {stderr}");
assert!(
stderr.contains(&format!("Compiled to {shown}.json\n")),
"{args:?}: the status line names the output as typed; stderr: {stderr}"
);
let warning =
format!("warning: kept stale output {shown}.md: mds never removes a Markdown file\n");
assert!(
stderr.contains(&warning),
"{args:?}: the warning names the stale output as typed, {warning:?}; stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "{args:?}: stderr: {stderr}");
}
std::fs::create_dir(root.join("ro")).unwrap();
std::fs::set_permissions(root.join("ro"), std::fs::Permissions::from_mode(0o555)).unwrap();
let _writable = Writable(root.join("ro"));
if std::fs::write(root.join("ro/.write-probe"), b"").is_ok() {
let _ = std::fs::remove_file(root.join("ro/.write-probe"));
eprintln!("skipped the read-only arm: ro is writable at mode 0o555 (running as root?)");
return;
}
let out = run(root, &["build", "src/a.mds", "-o", "ro/y.md"]);
let stderr = text(&out.stderr);
assert_eq!(out.status.code(), Some(2), "stderr: {stderr}");
let error = format!(
"cannot write ro/y.md: {}",
std::io::Error::from_raw_os_error(libc::EACCES)
);
assert!(
squash(&stderr).contains(&squash(&error)),
"{error:?}; stderr: {stderr}"
);
assert!(
!stderr.contains(".mds-tmp-") && !stderr.contains("at path"),
"no temporary file is named; stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "stderr: {stderr}");
}
#[test]
fn watch_names_an_output_as_its_status_line_does_in_an_error_writing_it() {
let dir = scratch();
let root = dir.path();
let cases: [(&str, &[&str], &str); 4] = [
("d1/a.mds", &["watch", "d1"], "d1/a.md"),
("w1/page.mds", &["watch", "w1/page.mds"], "w1/page.md"),
(
"w2/page.mds",
&["watch", "w2/page.mds", "-o", "o2/y.md"],
"o2/y.md",
),
(
"w3/page.mds",
&["watch", "w3/page.mds", "--out-dir", "o3"],
"o3/page.md",
),
];
for link in [false, true] {
let cwd = root.join(if link { "link" } else { "directory" });
let mut planted = true;
for (source, _, output) in cases {
put(&cwd, source, "Page\n");
if link {
planted = planted && plant_symlink(&cwd, output);
} else {
put(&cwd, &format!("{output}/keep"), "keep\n");
}
}
if !planted {
continue;
}
for (_, args, output) in cases {
let args = typed_args(args);
let label = format!("(link: {link}) mds {}", args.join(" "));
let shown = native(output);
let (child, tap, _) = common::spawn_watch_unsynchronized(
mds_bin()
.current_dir(&cwd)
.args(&args)
.args(["--debounce", "0"])
.stdout(Stdio::null()),
);
let mut child = common::ChildGuard(child);
let error = if link {
refused(&shown)
} else {
format!("cannot write {shown}: ")
};
let printed = common::poll_tap_until(&tap, WATCH_STEP, |text| {
squash(text).contains(&squash(&error))
});
let stderr = tap.finish_text(&mut child);
assert!(printed.is_ok(), "{label}: {error:?}; stderr: {stderr}");
assert!(
!stderr.contains(".mds-tmp-"),
"{label}: no temporary file is named; stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "{label}: stderr: {stderr}");
if !link {
assert!(
cwd.join(&shown).join("keep").is_file(),
"{label}: the directory is left as it was"
);
}
}
}
put(root, "d5/a.mds", "A\n");
let (mut child, tap, _) = watch_live(root, &["watch", "d5", "--out-dir", "o5"], false);
std::fs::remove_file(root.join(native("o5/a.md"))).expect("remove the startup output");
put(root, "o5/a.md/keep", "keep\n");
write_atomic(&root.join(native("d5/a.mds")), "@message user:\nHi\n@end\n");
common::wait_for_tap(&tap, "stale", WATCH_STEP);
let stderr = tap.finish_text(&mut child);
let warning = format!(
"warning: could not remove stale output {}: ",
native("o5/a.md")
);
assert!(
squash(&stderr).contains(&squash(&warning)),
"{warning:?}; stderr: {stderr}"
);
let recompiled = format!("Recompiled {} (", native("o5/a.json"));
assert!(
stderr.contains(&recompiled),
"the rebuild names the output as typed, {recompiled:?}; stderr: {stderr}"
);
assert_eq!(leak(&stderr, root), None, "stderr: {stderr}");
}
#[cfg(windows)]
#[test]
fn compiled_to_and_map_lines_under_an_existing_out_dir_carry_no_verbatim_prefix() {
let dir = scratch();
put(dir.path(), "src/sub/page.mds", "Page\n");
std::fs::create_dir(dir.path().join("out")).unwrap();
assert!(
dir.path()
.join("out")
.canonicalize()
.unwrap()
.to_string_lossy()
.starts_with(r"\\?\"),
"test assumption: canonicalize must yield a verbatim path on Windows"
);
let out = run(
dir.path(),
&["build", "src", "--out-dir", "out", "--source-map"],
);
let (stdout, stderr) = (text(&out.stdout), text(&out.stderr));
assert_eq!(out.status.code(), Some(0), "stderr: {stderr}");
assert_eq!(
stderr,
format!(
"Compiled to {}\nSource map written to {}\n1 built, 0 failed\n",
native("out/sub/page.md"),
native("out/sub/page.md.map")
),
);
assert!(!stdout.contains(r"\\?\"), "stdout: {stdout}");
assert!(!stderr.contains(r"\\?\"), "stderr: {stderr}");
assert_eq!(leak(&stderr, dir.path()), None, "stderr: {stderr}");
}
#[cfg(windows)]
#[test]
fn watch_banner_and_recompiled_lines_carry_no_verbatim_prefix() {
let dir = scratch();
put(dir.path(), "src/sub/page.mds", "Page\n");
std::fs::create_dir(dir.path().join("out")).unwrap();
assert!(
dir.path()
.join("src")
.canonicalize()
.unwrap()
.to_string_lossy()
.starts_with(r"\\?\"),
"test assumption: canonicalize must yield a verbatim path on Windows"
);
let cases: [(&[&str], &str, &str); 2] = [
(
&["watch", "src", "--out-dir", "out"],
"Watching directory src",
"out/sub/page.md",
),
(
&["watch", "src/sub/page.mds"],
"Watching src/sub/page.mds",
"src/sub/page.md",
),
];
for (args, banner, output) in cases {
put(dir.path(), "src/sub/page.mds", "Page\n");
let args = typed_args(args);
let (mut child, tap, _) = watch_live(dir.path(), &args, false);
write_atomic(&dir.path().join(native("src/sub/page.mds")), "Edited\n");
common::wait_for_tap(&tap, "Recompiled ", WATCH_STEP);
let stderr = tap.finish_text(&mut child);
assert_eq!(
stderr.lines().next(),
Some(native(banner).as_str()),
"{args:?}: stderr: {stderr}"
);
let prefix = format!("Recompiled {} (", native(output));
let recompiled = lines_starting(&stderr, "Recompiled ");
assert!(
!recompiled.is_empty() && recompiled.iter().all(|line| line.starts_with(&prefix)),
"{args:?}: every rebuild names the output as {prefix:?}; stderr: {stderr}"
);
assert!(!stderr.contains(r"\\?\"), "{args:?}: stderr: {stderr}");
assert_eq!(
leak(&stderr, dir.path()),
None,
"{args:?}: stderr: {stderr}"
);
}
}
const LABEL_SECTION: &str = "### 7.10 Path labels";
const LABEL_COLUMNS: [&str; 4] = ["Command", "Line", "Form", "Pinned by"];
const SWEEP: &str = "no_listed_build_check_or_lint_run_names_the_working_directory";
const LABEL_TABLE: &[&[&str]] = &[
&["auto_detection_names_the_file_as_found_in_the_working_directory"],
&[SWEEP],
&[SWEEP],
&[SWEEP],
&[SWEEP],
&[
"compiled_to_under_a_relative_out_dir_names_the_out_dir_as_typed",
"compiled_to_under_a_symlinked_out_dir_names_the_link_not_its_target",
"dir_build_out_dir_status_line_names_the_out_dir_as_typed",
],
&["a_config_output_dir_is_named_through_the_directory_mds_json_was_reached_by"],
&[
"source_map_lines_under_an_out_dir_name_the_out_dir_as_typed",
"a_config_output_dir_names_the_map_written_and_the_stale_map_removed",
SWEEP,
],
&["a_config_output_dir_names_the_map_written_and_the_stale_map_removed"],
&["an_output_directory_that_cannot_be_created_is_named_as_its_output_is"],
&["the_config_source_map_warning_names_mds_json_as_reached"],
&["r3_error_frame_names_root_relative_path_for_subdir_file"],
&["an_error_writing_an_output_names_it_as_its_status_line_does"],
&[SWEEP],
&["fmt_names_a_file_argument_as_typed_and_a_directory_s_entries_below_it"],
&["fmt_names_a_file_argument_as_typed_and_a_directory_s_entries_below_it"],
&["clean_names_a_file_argument_as_typed", SWEEP],
&[
"lint_names_a_file_argument_as_typed_and_its_findings_by_its_file_name",
"a_partial_fix_of_a_file_is_announced_after_the_findings_it_leaves",
SWEEP,
],
&["lint_names_a_file_argument_as_typed_and_its_findings_by_its_file_name"],
&[
"lint_names_a_file_argument_as_typed_and_its_findings_by_its_file_name",
SWEEP,
],
&["unknown_rule_warning_names_the_mds_json_as_the_input_reached_it"],
&[
"watching_names_the_entry_and_the_directory_as_typed",
"watching_names_a_path_reached_through_a_symlink_by_the_link",
],
&[
"watching_names_the_entry_and_the_directory_as_typed",
"watching_names_a_path_reached_through_a_symlink_by_the_link",
"watch_dot_forms_watch_the_canonical_directory",
],
&[
"watch_startup_names_an_out_dir_and_a_config_output_dir_as_build_does",
"recompiled_and_removed_name_each_output_as_typed",
],
&[
"recompiled_and_removed_name_each_output_as_typed",
"watching_names_a_path_reached_through_a_symlink_by_the_link",
],
&["an_emptied_output_is_named_as_its_recompiled_line_names_it"],
&[
"recompiled_and_removed_name_each_output_as_typed",
"removed_names_the_output_as_typed_when_the_vars_file_changes_in_the_same_batch",
],
&["an_output_that_cannot_be_removed_is_named_as_typed"],
&[
"watch_keeps_the_hand_written_siblings_of_a_deleted_source",
"watch_keeps_an_output_edited_since_the_session_wrote_it",
"watch_removes_the_old_output_of_a_changed_kind_only_when_it_wrote_it",
"watch_writes_a_file_whose_kind_changed_to_that_kinds_output",
"watch_never_writes_over_a_file_it_did_not_write_when_the_kind_changes",
"watch_never_writes_over_a_file_that_appears_while_a_change_of_kind_is_written",
"watch_never_removes_the_output_of_a_source_that_is_still_there",
"watch_never_replaces_the_output_of_another_source_when_the_kind_changes",
"watch_names_a_file_it_wrote_then_the_user_changed_as_changed",
"watch_replaces_its_own_file_once_the_user_restores_it_when_the_kind_changes",
"watch_tells_an_old_kind_output_in_a_remade_out_dir_is_kept",
],
&["an_entry_typed_in_another_case_names_its_output_by_the_name_on_disk"],
&["a_watched_directory_is_named_as_the_user_typed_it"],
&[
"a_vars_directory_that_cannot_be_watched_is_named_as_typed",
"a_watched_directory_is_named_as_the_user_typed_it",
],
&["a_watched_directory_is_named_as_the_user_typed_it"],
&["watch_names_an_output_as_its_status_line_does_in_an_error_writing_it"],
&["init_names_the_file_it_creates_as_typed"],
&[
"a_relative_output_location_is_refused_where_the_working_directory_is_gone",
"watch_with_a_relative_out_dir_stops_at_startup_where_the_working_directory_is_gone",
],
&["auto_detection_in_a_working_directory_it_cannot_list_names_it_as_a_dot"],
&[
"compiled_to_and_map_lines_under_an_existing_out_dir_carry_no_verbatim_prefix",
"dir_build_out_dir_status_line_has_no_verbatim_prefix_on_windows",
],
&["watch_banner_and_recompiled_lines_carry_no_verbatim_prefix"],
];
#[derive(Debug, PartialEq)]
struct LabelRow {
line: usize,
tests: Vec<String>,
}
fn table_cells(line: &str) -> Vec<String> {
let inner = line.trim();
let inner = inner.strip_prefix('|').unwrap_or(inner);
let inner = inner.strip_suffix('|').unwrap_or(inner);
let mut cells = Vec::new();
let mut cell = String::new();
let mut escaped = false;
for c in inner.chars() {
if c == '|' && !escaped {
cells.push(cell.trim().to_owned());
cell.clear();
} else {
cell.push(c);
}
escaped = c == '\\' && !escaped;
}
cells.push(cell.trim().to_owned());
cells
}
fn is_test_name(name: &str) -> bool {
name.starts_with(|c: char| c.is_ascii_lowercase())
&& name
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
}
fn cited_tests(cell: &str) -> Result<Vec<String>, String> {
if cell.is_empty() {
return Err("the Pinned by cell cites no test".to_owned());
}
cell.split(", ")
.map(|item| {
item.strip_prefix('`')
.and_then(|rest| rest.strip_suffix('`'))
.filter(|name| is_test_name(name))
.map(str::to_owned)
.ok_or_else(|| format!("{item:?} is not a test name in backticks"))
})
.collect()
}
fn ends_label_section(line: &str) -> bool {
let level = |text: &str| text.bytes().take_while(|&b| b == b'#').count();
let heading = level(line);
line == "---"
|| ((1..=level(LABEL_SECTION)).contains(&heading)
&& matches!(line.as_bytes().get(heading), None | Some(b' ')))
}
fn label_table(spec: &str) -> Result<Vec<LabelRow>, String> {
let lines: Vec<&str> = spec.lines().map(str::trim_end).collect();
let starts: Vec<usize> = lines
.iter()
.enumerate()
.filter(|(_, line)| **line == LABEL_SECTION)
.map(|(at, _)| at)
.collect();
let [start] = starts.as_slice() else {
return Err(format!(
"expected one {LABEL_SECTION:?} heading, found {}",
starts.len()
));
};
let section: Vec<(usize, &str)> = lines[start + 1..]
.iter()
.enumerate()
.map(|(at, line)| (start + 2 + at, *line))
.take_while(|(_, line)| !ends_label_section(line))
.collect();
let table: Vec<(usize, &str)> = section
.iter()
.copied()
.filter(|(_, line)| line.starts_with('|'))
.collect();
let (Some(&(first, header)), Some(&(last, _))) = (table.first(), table.last()) else {
return Err("the section holds no table".to_owned());
};
if table.len() != last - first + 1 {
return Err(format!(
"the table lines {first}..={last} are not one table: other lines lie between them"
));
}
if let Some(&(after, _)) = section
.iter()
.find(|&&(line, text)| line == last + 1 && !text.is_empty())
{
return Err(format!("line {after}: the table must end at a blank line"));
}
if table_cells(header) != LABEL_COLUMNS {
return Err(format!(
"line {first}: the header is {:?}, not {LABEL_COLUMNS:?}",
table_cells(header)
));
}
let is_rule = |cell: &String| {
let dashes = cell.trim_start_matches(':').trim_end_matches(':');
dashes.len() >= 3 && dashes.chars().all(|c| c == '-')
};
match table.get(1) {
Some(&(_, separator))
if table_cells(separator).len() == LABEL_COLUMNS.len()
&& table_cells(separator).iter().all(is_rule) => {}
_ => return Err(format!("line {}: no separator row", first + 1)),
}
let rows = table[2..]
.iter()
.map(|&(line, text)| {
let cells = table_cells(text);
if cells.len() != LABEL_COLUMNS.len() {
return Err(format!(
"line {line}: {} cells, not {}",
cells.len(),
LABEL_COLUMNS.len()
));
}
if let Some(empty) = cells[..3].iter().position(String::is_empty) {
return Err(format!(
"line {line}: the {} cell is empty",
LABEL_COLUMNS[empty]
));
}
let tests = cited_tests(&cells[3]).map_err(|e| format!("line {line}: {e}"))?;
Ok(LabelRow { line, tests })
})
.collect::<Result<Vec<_>, String>>()?;
if rows.is_empty() {
return Err(format!("line {first}: the table has no rows"));
}
Ok(rows)
}
fn cli_rust_sources() -> Vec<(PathBuf, String)> {
let crate_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let mut sources = Vec::new();
for sub in ["tests", "src"] {
for entry in std::fs::read_dir(crate_dir.join(sub)).expect("list a source directory") {
let path = entry.expect("read a directory entry").path();
if path.extension().is_some_and(|ext| ext == "rs") {
let text = std::fs::read_to_string(&path).expect("read a source file");
sources.push((path, text));
}
}
}
sources.sort();
sources
}
fn test_definitions<'a>(name: &str, sources: &'a [(PathBuf, String)]) -> Vec<&'a Path> {
let signature = format!("fn {name}() {{");
sources
.iter()
.filter(|(_, text)| {
let lines: Vec<&str> = text.lines().map(str::trim).collect();
lines
.iter()
.enumerate()
.filter(|(_, line)| **line == signature)
.any(|(at, _)| {
let attributes: Vec<&str> = lines[..at]
.iter()
.rev()
.take_while(|line| line.starts_with("#[") || line.starts_with("///"))
.copied()
.collect();
attributes.contains(&"#[test]")
&& !attributes.iter().any(|line| line.starts_with("#[ignore"))
})
})
.map(|(path, _)| path.as_path())
.collect()
}
#[test]
fn the_spec_s_path_label_table_cites_exactly_these_tests() {
let spec_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../spec.md");
let spec = std::fs::read_to_string(&spec_path).expect("read spec.md");
let rows = label_table(&spec).unwrap_or_else(|e| panic!("spec.md §7.10: {e}"));
let cited: BTreeSet<&str> = rows
.iter()
.flat_map(|row| row.tests.iter().map(String::as_str))
.collect();
let listed: BTreeSet<&str> = LABEL_TABLE
.iter()
.flat_map(|row| row.iter().copied())
.collect();
let unlisted: Vec<&str> = cited.difference(&listed).copied().collect();
let uncited: Vec<&str> = listed.difference(&cited).copied().collect();
assert!(
unlisted.is_empty() && uncited.is_empty(),
"spec.md §7.10 cites tests LABEL_TABLE does not list: {unlisted:?}; \
LABEL_TABLE lists tests spec.md §7.10 does not cite: {uncited:?}"
);
let table: Vec<Vec<&str>> = rows
.iter()
.map(|row| row.tests.iter().map(String::as_str).collect())
.collect();
let lines: Vec<usize> = rows.iter().map(|row| row.line).collect();
assert_eq!(
table, LABEL_TABLE,
"spec.md §7.10's rows (lines {lines:?}) and LABEL_TABLE differ row by row"
);
let sources = cli_rust_sources();
let undefined: Vec<String> = listed
.iter()
.filter_map(|name| {
let defined = test_definitions(name, &sources);
(defined.len() != 1).then(|| format!("{name}: defined in {defined:?}"))
})
.collect();
assert!(
undefined.is_empty(),
"each test spec.md §7.10 cites must be a #[test] fn, not ignored, defined in exactly \
one file of crates/mds-cli/tests/*.rs and crates/mds-cli/src/*.rs: {undefined:#?}"
);
}
#[test]
fn the_table_check_refuses_a_row_without_a_test_and_finds_only_defined_tests() {
let spec = |rows: &str| {
format!(
"## 7. CLI\n\n{LABEL_SECTION}\n\nProse.\n\n| Command | Line | Form | Pinned by |\n\
|---|---|---|---|\n{rows}\n\n---\n\n## 8. Next\n\n| a | b |\n|---|---|\n"
)
};
let good = spec("| `mds x` | `X` | as typed | `a_test`, `b_test` |");
assert_eq!(
label_table(&good),
Ok(vec![LabelRow {
line: 9,
tests: vec!["a_test".to_owned(), "b_test".to_owned()],
}])
);
assert_eq!(
label_table(&good.replace('\n', "\r\n")),
label_table(&good),
"a CRLF spec reads as the LF one"
);
assert!(
label_table(&spec("| `mds x` | `a \\| b` | as typed | `a_test` |")).is_ok(),
"an escaped pipe stays inside its cell"
);
for (rows, what) in [
("| `mds x` | `X` | as typed | — |", "a dash"),
("| `mds x` | `X` | as typed | |", "an empty Pinned by cell"),
("| `mds x` | `X` | as typed | a_test |", "a name without backticks"),
("| `mds x` | `X` | as typed | `a_test` and prose |", "prose"),
("| `mds x` | `X` | as typed | `a_test`,`b_test` |", "another separator"),
("| `mds x` | `X` | as typed | `A_Test` |", "a name no test has"),
("| `mds x` | `X` | `a_test` |", "three cells"),
("| `mds x` | | as typed | `a_test` |", "an empty Line cell"),
(
"| `mds x` | `X` | as typed | `a_test` |\n\nProse.\n\n| `mds y` | `Y` | as typed | `b_test` |",
"a second table",
),
(
"| `mds x` | `X` | as typed | `a_test` |\n`mds y` | `Y` | as typed | — |",
"a row without a leading pipe, which still continues the table",
),
(
"| `mds x` | `X` | as typed | `a_test` |\n | `mds y` | `Y` | as typed | — |",
"an indented row",
),
(
"| `mds x` | `X` | as typed | `a_test` |\n\n#### Sub\n\n\
| Command | Line | Form | Pinned by |\n|---|---|---|---|\n\
| `mds y` | `Y` | as typed | — |",
"a second table below a subsection's heading",
),
] {
assert!(
label_table(&spec(rows)).is_err(),
"{what} must be refused: {rows:?}"
);
}
assert!(label_table("# spec\n").is_err(), "no section");
assert!(
label_table(&format!("{good}{good}")).is_err(),
"the section twice"
);
assert!(
label_table(&good.replace("| Pinned by |", "| Tests |")).is_err(),
"another header"
);
assert!(label_table(&spec("")).is_err(), "a table with no rows");
assert_eq!(
label_table(&good.replace("---\n\n## 8. Next", "### 7.11 Next")),
label_table(&good),
"a heading of the section's own level ends it, before the next table"
);
let planted = vec![
(
PathBuf::from("a.rs"),
"/// Doc.\n#[cfg(unix)]\n#[test]\nfn planted_test() {\n}\n\nfn planted_helper() {\n}\n"
.to_owned(),
),
(PathBuf::from("b.rs"), "#[test]\nfn twice() {\n}\n".to_owned()),
(PathBuf::from("c.rs"), "#[test]\nfn twice() {\n}\n".to_owned()),
(
PathBuf::from("d.rs"),
"#[cfg(unix)]\n#[test]\nfn per_platform() {\n}\n\n\
#[cfg(windows)]\n#[test]\nfn per_platform() {\n}\n\n\
#[ignore]\n#[test]\nfn ignored() {\n}\n\n\
#[test]\n#[ignore = \"slow\"]\nfn ignored_too() {\n}\n\n\
#[cfg_attr(not(unix), ignore = \"unix only\")]\n#[test]\nfn ignored_elsewhere() {\n}\n"
.to_owned(),
),
];
assert_eq!(
test_definitions("planted_test", &planted),
[Path::new("a.rs")]
);
assert!(
test_definitions("planted_helper", &planted).is_empty(),
"a function without #[test] is no test"
);
assert!(
test_definitions("planted", &planted).is_empty(),
"no prefix match"
);
assert_eq!(
test_definitions("twice", &planted).len(),
2,
"a test defined twice is found twice, which the table check refuses"
);
assert_eq!(
test_definitions("per_platform", &planted),
[Path::new("d.rs")],
"a test defined per platform in one file is found in that file"
);
for ignored in ["ignored", "ignored_too"] {
assert!(
test_definitions(ignored, &planted).is_empty(),
"an ignored test runs nowhere, so it pins nothing: {ignored}"
);
}
assert_eq!(
test_definitions("ignored_elsewhere", &planted),
[Path::new("d.rs")],
"a test ignored only on other platforms runs here"
);
let sources = cli_rust_sources();
let here = test_definitions("clean_names_a_file_argument_as_typed", &sources);
assert!(
here.len() == 1 && here[0].ends_with("path_labels.rs"),
"a real test is found where it is defined: {here:?}"
);
assert!(
test_definitions("scratch", &sources).is_empty(),
"a real helper is no test"
);
}