mod common;
use common::{closed_pipe, mds_bin};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
const RUN_TIMEOUT: Duration = Duration::from_secs(60);
const POLL: Duration = Duration::from_millis(5);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Stream {
Stdout,
Stderr,
}
impl Stream {
fn other(self) -> Self {
match self {
Self::Stdout => Self::Stderr,
Self::Stderr => Self::Stdout,
}
}
}
struct Run {
code: Option<i32>,
stdout: String,
stderr: String,
}
impl Run {
fn text(&self, stream: Stream) -> &str {
match stream {
Stream::Stdout => &self.stdout,
Stream::Stderr => &self.stderr,
}
}
}
fn drain<R: Read + Send + 'static>(pipe: Option<R>) -> Option<JoinHandle<Vec<u8>>> {
pipe.map(|mut pipe| {
std::thread::spawn(move || {
let mut bytes = Vec::new();
let _ = pipe.read_to_end(&mut bytes);
bytes
})
})
}
fn joined(handle: Option<JoinHandle<Vec<u8>>>) -> String {
let bytes = handle
.map(|h| h.join().expect("pipe drain thread"))
.unwrap_or_default();
String::from_utf8_lossy(&bytes).into_owned()
}
fn wait_bounded(child: &mut Child, what: &str) -> Option<i32> {
let deadline = Instant::now() + RUN_TIMEOUT;
loop {
if let Some(status) = child.try_wait().expect("poll the mds child") {
return status.code();
}
if Instant::now() >= deadline {
let _ = child.kill();
let _ = child.wait();
panic!("`mds {what}` did not exit within {RUN_TIMEOUT:?}");
}
std::thread::sleep(POLL);
}
}
enum Input {
Bytes(Vec<u8>),
Handle(Stdio),
}
fn run(dir: &Path, args: &[&str], stdin: &str, closed: Option<Stream>) -> Run {
let stdout = match closed {
Some(Stream::Stdout) => Stdio::from(closed_pipe()),
_ => Stdio::piped(),
};
let stderr = match closed {
Some(Stream::Stderr) => Stdio::from(closed_pipe()),
_ => Stdio::piped(),
};
run_with(
dir,
args,
Input::Bytes(stdin.as_bytes().to_vec()),
stdout,
stderr,
)
}
fn run_with(dir: &Path, args: &[&str], stdin: Input, stdout: Stdio, stderr: Stdio) -> Run {
run_command(mds_bin(), dir, args, stdin, stdout, stderr)
}
fn run_command(
mut cmd: Command,
dir: &Path,
args: &[&str],
stdin: Input,
stdout: Stdio,
stderr: Stdio,
) -> Run {
let what = args.join(" ");
cmd.args(args)
.current_dir(dir)
.stdout(stdout)
.stderr(stderr);
let bytes = match stdin {
Input::Bytes(bytes) => {
cmd.stdin(Stdio::piped());
Some(bytes)
}
Input::Handle(handle) => {
cmd.stdin(handle);
None
}
};
let mut child = cmd.spawn().expect("spawn mds");
let writer = bytes.map(|bytes| {
let mut child_stdin = child.stdin.take().expect("stdin is piped");
std::thread::spawn(move || {
let _ = child_stdin.write_all(&bytes);
})
});
let out = drain(child.stdout.take());
let err = drain(child.stderr.take());
let code = wait_bounded(&mut child, &what);
if let Some(writer) = writer {
writer.join().expect("stdin writer thread");
}
Run {
code,
stdout: joined(out),
stderr: joined(err),
}
}
const LINT_WARN_SOURCE: &str =
"---\ngreeting: Hello\nunused_key: never referenced in the body\n---\n\n{{greeting}}, world!\n";
const LINT_FIXABLE_SOURCE: &str = "@if \"x\" == \"y\":\nhidden\n@end\nHello\n";
fn fixture_dir() -> tempfile::TempDir {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
let files: [(&str, &str); 15] = [
("ok.mds", "Hello\n"),
("bad.mds", "@if flag:\nunterminated\n"),
("d/ok.mds", "Hello\n"),
("d/bad.mds", "@if flag:\nunterminated\n"),
("good/a.mds", "A\n"),
("good/b.mds", "B\n"),
("messy.mds", "Hello"),
("two/a.mds", "A"),
("two/b.mds", "B"),
("warn.mds", LINT_WARN_SOURCE),
("fix.mds", LINT_FIXABLE_SOURCE),
("lints/ok.mds", "Hello\n"),
("lints/warn.mds", LINT_WARN_SOURCE),
("fixes/a.mds", LINT_FIXABLE_SOURCE),
("fixes/b.mds", LINT_FIXABLE_SOURCE),
];
for dir_name in ["d", "good", "m", "two", "lints", "fixes"] {
std::fs::create_dir(root.join(dir_name)).expect("create a fixture dir");
}
for (name, text) in files {
std::fs::write(root.join(name), text).expect("write a fixture file");
}
std::fs::write(root.join("m/messy.mds"), "Hello").expect("write m/messy.mds");
dir
}
fn files_under(dir: &Path) -> Vec<(PathBuf, Vec<u8>)> {
let mut out = Vec::new();
let mut stack = vec![dir.to_path_buf()];
for _ in 0..64 {
let Some(d) = stack.pop() else { break };
for entry in std::fs::read_dir(&d).expect("read fixture dir").flatten() {
let path = entry.path();
let file_type = entry.file_type().expect("file type");
if file_type.is_dir() {
stack.push(path);
} else {
let bytes = std::fs::read(&path).expect("read fixture file");
let rel = path.strip_prefix(dir).expect("under dir").to_path_buf();
out.push((rel, bytes));
}
}
}
assert!(stack.is_empty(), "fixture tree deeper than the walk bound");
out.sort();
out
}
fn native(path: &str) -> String {
path.replace('/', std::path::MAIN_SEPARATOR_STR)
}
fn diff_header(path: &str) -> String {
format!("+++ {}\n", native(path))
}
struct Row<'a> {
args: &'static [&'static str],
stdin: &'static str,
verdict: i32,
closed: Stream,
open_contains: &'a str,
}
fn assert_closing_keeps_the_verdict(row: &Row<'_>) -> Vec<(PathBuf, Vec<u8>)> {
let what = row.args.join(" ");
let closed_name = format!("{:?}", row.closed).to_lowercase();
let open_dir = fixture_dir();
let open = run(open_dir.path(), row.args, row.stdin, None);
assert_eq!(
open.code,
Some(row.verdict),
"control: `mds {what}` with open streams must exit with its verdict; stderr: {:?}",
open.stderr
);
assert!(
open.text(row.closed).contains(row.open_contains),
"control: `mds {what}` must print {:?} on {closed_name}, or the closed-{closed_name} \
run would prove nothing; {closed_name}: {:?}",
row.open_contains,
open.text(row.closed)
);
assert_the_closed_run_matches(row, &open, open_dir.path())
}
fn assert_closing_an_unused_stream_changes_nothing(
args: &'static [&'static str],
verdict: i32,
closed: Stream,
) -> Vec<(PathBuf, Vec<u8>)> {
let what = args.join(" ");
let open_dir = fixture_dir();
let open = run(open_dir.path(), args, "", None);
assert_eq!(
open.code,
Some(verdict),
"control: `mds {what}` with open streams must exit with its verdict; stderr: {:?}",
open.stderr
);
assert_eq!(
open.text(closed),
"",
"control: `mds {what}` must write nothing to {closed:?} for this row"
);
let row = Row {
args,
stdin: "",
verdict,
closed,
open_contains: "",
};
assert_the_closed_run_matches(&row, &open, open_dir.path())
}
fn assert_the_closed_run_matches(
row: &Row<'_>,
open: &Run,
open_dir: &Path,
) -> Vec<(PathBuf, Vec<u8>)> {
let what = row.args.join(" ");
let closed_name = format!("{:?}", row.closed).to_lowercase();
let kept = row.closed.other();
let closed_dir = fixture_dir();
let closed = run(closed_dir.path(), row.args, row.stdin, Some(row.closed));
assert_eq!(
closed.code,
Some(row.verdict),
"`mds {what}` with {closed_name} closed must exit with the verdict it has with open \
streams ({}), not a panic (101) or a signal (None); open-run stderr: {:?}; \
closed-run stdout: {:?}; closed-run stderr: {:?}",
row.verdict,
open.stderr,
closed.stdout,
closed.stderr
);
assert_eq!(
closed.text(kept),
open.text(kept),
"`mds {what}`: closing {closed_name} must not change {:?}",
kept
);
let open_files = files_under(open_dir);
let closed_files = files_under(closed_dir.path());
assert_eq!(
closed_files, open_files,
"`mds {what}`: closing {closed_name} must not change what is written to disk"
);
closed_files
}
fn written<'a>(files: &'a [(PathBuf, Vec<u8>)], path: &str) -> Option<&'a [u8]> {
files
.iter()
.find(|(p, _)| p == Path::new(path))
.map(|(_, bytes)| bytes.as_slice())
}
#[test]
fn check_a_file_with_stderr_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["check", "ok.mds"],
stdin: "",
verdict: 0,
closed: Stream::Stderr,
open_contains: "OK: ok.mds\n",
});
}
#[test]
fn check_a_broken_file_with_stderr_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["check", "bad.mds"],
stdin: "",
verdict: 1,
closed: Stream::Stderr,
open_contains: "mds::syntax",
});
}
#[test]
fn check_stdin_with_stderr_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["check", "-"],
stdin: "Hello\n",
verdict: 0,
closed: Stream::Stderr,
open_contains: "OK: <stdin>\n",
});
}
#[test]
fn check_a_directory_with_stderr_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["check", "d"],
stdin: "",
verdict: 1,
closed: Stream::Stderr,
open_contains: "1 passed, 1 failed\n",
});
}
#[test]
fn init_with_stderr_closed_exits_0_and_writes_the_file() {
let files = assert_closing_keeps_the_verdict(&Row {
args: &["init"],
stdin: "",
verdict: 0,
closed: Stream::Stderr,
open_contains: "Created hello.mds",
});
let starter = written(&files, "hello.mds").map(String::from_utf8_lossy);
assert!(
starter
.as_deref()
.is_some_and(|s| s.contains("Hello {{name}}!")),
"`mds init` with stderr closed must still write the starter file; got {starter:?}"
);
}
#[test]
fn check_a_file_with_stdout_closed_exits_0() {
assert_closing_an_unused_stream_changes_nothing(&["check", "ok.mds"], 0, Stream::Stdout);
}
#[test]
fn init_with_stdout_closed_exits_0_and_writes_the_file() {
let files = assert_closing_an_unused_stream_changes_nothing(&["init"], 0, Stream::Stdout);
assert!(
written(&files, "hello.mds").is_some(),
"`mds init` with stdout closed must still write the starter file"
);
}
#[test]
fn lint_with_an_unknown_format_and_stderr_closed_exits_2() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--format", "yaml", "ok.mds"],
stdin: "",
verdict: 2,
closed: Stream::Stderr,
open_contains: "unknown --format value",
});
}
#[test]
fn build_to_stdout_with_stdout_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["build", "ok.mds", "-o", "-"],
stdin: "",
verdict: 0,
closed: Stream::Stdout,
open_contains: "Hello\n",
});
}
#[test]
fn build_stdin_to_stdout_with_stdout_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["build", "-"],
stdin: "Hello\n",
verdict: 0,
closed: Stream::Stdout,
open_contains: "Hello\n",
});
}
#[test]
fn build_a_file_with_stderr_closed_exits_0_and_writes_the_output() {
let files = assert_closing_keeps_the_verdict(&Row {
args: &["build", "ok.mds"],
stdin: "",
verdict: 0,
closed: Stream::Stderr,
open_contains: &format!("Compiled to {}\n", native("./ok.md")),
});
assert_eq!(
written(&files, "ok.md"),
Some(&b"Hello\n"[..]),
"`mds build ok.mds` with stderr closed must still write ok.md"
);
}
#[test]
fn build_with_a_source_map_and_stderr_closed_exits_0_and_writes_both_files() {
let files = assert_closing_keeps_the_verdict(&Row {
args: &["build", "ok.mds", "--source-map"],
stdin: "",
verdict: 0,
closed: Stream::Stderr,
open_contains: &format!("Source map written to {}\n", native("./ok.md.map")),
});
assert!(
written(&files, "ok.md").is_some() && written(&files, "ok.md.map").is_some(),
"`mds build ok.mds --source-map` with stderr closed must still write the output \
and its map; files: {:?}",
files.iter().map(|(p, _)| p).collect::<Vec<_>>()
);
}
#[test]
fn build_a_directory_with_stderr_closed_exits_0_and_writes_every_output() {
let files = assert_closing_keeps_the_verdict(&Row {
args: &["build", "good"],
stdin: "",
verdict: 0,
closed: Stream::Stderr,
open_contains: "2 built, 0 failed\n",
});
assert_eq!(
(written(&files, "good/a.md"), written(&files, "good/b.md")),
(Some(&b"A\n"[..]), Some(&b"B\n"[..])),
"`mds build good` with stderr closed must still write both outputs"
);
}
#[test]
fn build_a_directory_with_a_broken_file_and_stderr_closed_exits_1() {
let files = assert_closing_keeps_the_verdict(&Row {
args: &["build", "d"],
stdin: "",
verdict: 1,
closed: Stream::Stderr,
open_contains: "1 built, 1 failed\n",
});
assert_eq!(
written(&files, "d/ok.md"),
Some(&b"Hello\n"[..]),
"`mds build d` with stderr closed must still write the file that compiles"
);
}
#[test]
fn fmt_a_file_with_stderr_closed_exits_0_and_rewrites_it() {
let files = assert_closing_keeps_the_verdict(&Row {
args: &["fmt", "messy.mds"],
stdin: "",
verdict: 0,
closed: Stream::Stderr,
open_contains: "Formatted: messy.mds\n",
});
assert_eq!(
written(&files, "messy.mds"),
Some(&b"Hello\n"[..]),
"`mds fmt messy.mds` with stderr closed must still rewrite the file"
);
}
#[test]
fn fmt_check_with_stderr_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["fmt", "--check", "messy.mds"],
stdin: "",
verdict: 1,
closed: Stream::Stderr,
open_contains: "Would reformat: messy.mds\n",
});
}
#[test]
fn fmt_stdin_with_stdout_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["fmt", "-"],
stdin: "Hello",
verdict: 0,
closed: Stream::Stdout,
open_contains: "Hello\n",
});
}
#[test]
fn fmt_diff_with_stdout_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["fmt", "--diff", "messy.mds"],
stdin: "",
verdict: 0,
closed: Stream::Stdout,
open_contains: "+++ messy.mds\n",
});
}
#[test]
fn fmt_check_diff_with_stdout_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["fmt", "--check", "--diff", "messy.mds"],
stdin: "",
verdict: 1,
closed: Stream::Stdout,
open_contains: "+++ messy.mds\n",
});
}
#[test]
fn fmt_check_diff_on_a_directory_with_stdout_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["fmt", "--check", "--diff", "m"],
stdin: "",
verdict: 1,
closed: Stream::Stdout,
open_contains: &diff_header("m/messy.mds"),
});
}
#[test]
fn lint_a_file_with_warnings_and_stderr_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "warn.mds"],
stdin: "",
verdict: 1,
closed: Stream::Stderr,
open_contains: "[unused-variable]",
});
}
#[test]
fn lint_a_clean_file_with_stderr_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "ok.mds"],
stdin: "",
verdict: 0,
closed: Stream::Stderr,
open_contains: "Clean: ok.mds\n",
});
}
#[test]
fn lint_a_directory_with_warnings_and_stderr_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "lints"],
stdin: "",
verdict: 1,
closed: Stream::Stderr,
open_contains: "1 clean, 1 with warnings, 0 with errors, 0 resource-limited\n",
});
}
#[test]
fn lint_stdin_with_warnings_and_stderr_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "-"],
stdin: LINT_WARN_SOURCE,
verdict: 1,
closed: Stream::Stderr,
open_contains: "[unused-variable]",
});
}
#[test]
fn lint_fix_check_of_stdin_with_stderr_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--fix", "--check", "-"],
stdin: LINT_FIXABLE_SOURCE,
verdict: 1,
closed: Stream::Stderr,
open_contains: "Would fix: <stdin>\n",
});
}
#[test]
fn lint_fix_of_a_file_with_stderr_closed_exits_0_and_rewrites_it() {
let files = assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--fix", "fix.mds"],
stdin: "",
verdict: 0,
closed: Stream::Stderr,
open_contains: "Fixed: fix.mds\n",
});
assert_eq!(
written(&files, "fix.mds"),
Some(&b"Hello\n"[..]),
"`mds lint --fix fix.mds` with stderr closed must still rewrite the file"
);
}
#[test]
fn lint_json_on_a_file_with_stdout_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--format", "json", "warn.mds"],
stdin: "",
verdict: 1,
closed: Stream::Stdout,
open_contains: "\"file\":\"warn.mds\"",
});
}
#[test]
fn lint_json_on_a_directory_with_stdout_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--format", "json", "lints"],
stdin: "",
verdict: 1,
closed: Stream::Stdout,
open_contains: "\"file\":\"warn.mds\"",
});
}
#[test]
fn lint_json_on_stdin_with_stdout_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--format", "json", "-"],
stdin: LINT_WARN_SOURCE,
verdict: 1,
closed: Stream::Stdout,
open_contains: "\"file\":\"<stdin>\"",
});
}
#[test]
fn lint_fix_of_stdin_with_stdout_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--fix", "-"],
stdin: LINT_FIXABLE_SOURCE,
verdict: 0,
closed: Stream::Stdout,
open_contains: "Hello\n",
});
}
#[test]
fn lint_fix_diff_with_stdout_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--fix", "--diff", "fix.mds"],
stdin: "",
verdict: 1,
closed: Stream::Stdout,
open_contains: "+++ fix.mds\n",
});
}
#[test]
fn lint_fix_diff_of_a_directory_with_stdout_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--fix", "--diff", "fixes"],
stdin: "",
verdict: 1,
closed: Stream::Stdout,
open_contains: &diff_header("fixes/b.mds"),
});
}
#[test]
fn lint_json_fix_diff_of_a_directory_with_stdout_closed_exits_1() {
assert_closing_keeps_the_verdict(&Row {
args: &["lint", "--format", "json", "--fix", "--diff", "fixes"],
stdin: "",
verdict: 1,
closed: Stream::Stdout,
open_contains: &diff_header("fixes/b.mds"),
});
}
struct Cell {
subcommand: &'static str,
stream: Stream,
file: &'static str,
test: &'static str,
}
const TABLE: &[Cell] = &[
Cell {
subcommand: "build",
stream: Stream::Stdout,
file: "broken_pipe.rs",
test: "build_to_stdout_with_stdout_closed_exits_0",
},
Cell {
subcommand: "build",
stream: Stream::Stderr,
file: "broken_pipe.rs",
test: "build_a_file_with_stderr_closed_exits_0_and_writes_the_output",
},
Cell {
subcommand: "check",
stream: Stream::Stdout,
file: "broken_pipe.rs",
test: "check_a_file_with_stdout_closed_exits_0",
},
Cell {
subcommand: "check",
stream: Stream::Stderr,
file: "broken_pipe.rs",
test: "check_a_file_with_stderr_closed_exits_0",
},
Cell {
subcommand: "fmt",
stream: Stream::Stdout,
file: "broken_pipe.rs",
test: "fmt_stdin_with_stdout_closed_exits_0",
},
Cell {
subcommand: "fmt",
stream: Stream::Stderr,
file: "broken_pipe.rs",
test: "fmt_a_file_with_stderr_closed_exits_0_and_rewrites_it",
},
Cell {
subcommand: "lint",
stream: Stream::Stdout,
file: "broken_pipe.rs",
test: "lint_json_on_a_file_with_stdout_closed_exits_1",
},
Cell {
subcommand: "lint",
stream: Stream::Stderr,
file: "broken_pipe.rs",
test: "lint_a_file_with_warnings_and_stderr_closed_exits_1",
},
Cell {
subcommand: "init",
stream: Stream::Stdout,
file: "broken_pipe.rs",
test: "init_with_stdout_closed_exits_0_and_writes_the_file",
},
Cell {
subcommand: "init",
stream: Stream::Stderr,
file: "broken_pipe.rs",
test: "init_with_stderr_closed_exits_0_and_writes_the_file",
},
Cell {
subcommand: "watch",
stream: Stream::Stdout,
file: "cli_watch.rs",
test: "watch_to_stdout_whose_reader_is_gone_stops_and_exits_0",
},
Cell {
subcommand: "watch",
stream: Stream::Stderr,
file: "cli_watch.rs",
test: "watch_with_stderr_closed_keeps_watching",
},
];
fn subcommands() -> Vec<String> {
let mut cmd = mds_bin();
cmd.arg("--help")
.stdout(Stdio::piped())
.stderr(Stdio::null());
let mut child = cmd.spawn().expect("spawn mds --help");
let out = drain(child.stdout.take());
let code = wait_bounded(&mut child, "--help");
assert_eq!(code, Some(0), "`mds --help` exits 0");
commands_in_help(&joined(out))
}
fn commands_in_help(help: &str) -> Vec<String> {
help.lines()
.skip_while(|line| line.trim() != "Commands:")
.skip(1)
.take_while(|line| line.starts_with(' '))
.filter_map(|line| line.split_whitespace().next())
.filter(|name| *name != "help")
.map(str::to_string)
.collect()
}
fn table_gaps(subcommands: &[String], table: &[Cell]) -> Vec<String> {
let mut gaps = Vec::new();
for subcommand in subcommands {
for stream in [Stream::Stdout, Stream::Stderr] {
if !table
.iter()
.any(|cell| cell.subcommand == subcommand && cell.stream == stream)
{
gaps.push(format!("mds {subcommand} × {stream:?} closed: no row"));
}
}
}
for cell in table {
if !subcommands.iter().any(|s| s == cell.subcommand) {
gaps.push(format!(
"mds {} is not a subcommand, but has a row: {}",
cell.subcommand, cell.test
));
}
}
gaps
}
fn defines_test(file: &str, test: &str) -> bool {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join(file);
let src = std::fs::read_to_string(&path).expect("read a test file");
let lines: Vec<&str> = src.lines().map(str::trim).collect();
let signature = format!("fn {test}() {{");
lines
.iter()
.enumerate()
.filter(|(_, line)| **line == signature)
.any(|(at, _)| {
lines[..at]
.iter()
.rev()
.take_while(|line| line.starts_with("#[") || line.starts_with("///"))
.any(|line| *line == "#[test]")
})
}
#[test]
fn every_subcommand_has_a_row_for_each_closed_stream() {
let subcommands = subcommands();
assert!(
subcommands.len() >= 6,
"non-vacuity: expected at least 6 subcommands in `mds --help`, found {subcommands:?}"
);
let gaps = table_gaps(&subcommands, TABLE);
assert!(
gaps.is_empty(),
"the closed-stream table has gaps:\n{gaps:#?}"
);
let missing: Vec<&str> = TABLE
.iter()
.filter(|cell| !defines_test(cell.file, cell.test))
.map(|cell| cell.test)
.collect();
assert!(
missing.is_empty(),
"these rows name no #[test] fn in their file: {missing:?}"
);
}
#[test]
fn the_table_check_reports_a_missing_row_a_stale_cell_and_an_unknown_test() {
let help = "Usage: mds <COMMAND>\n\nCommands:\n build Compile\n serve Serve\n \
help Print this message\n\nOptions:\n -q Quiet\n";
let subcommands = commands_in_help(help);
assert_eq!(subcommands, ["build", "serve"]);
let gaps = table_gaps(&subcommands, TABLE);
for want in [
"mds serve × Stdout closed: no row",
"mds serve × Stderr closed: no row",
"mds watch is not a subcommand, but has a row: watch_with_stderr_closed_keeps_watching",
] {
assert!(
gaps.iter().any(|gap| gap == want),
"want {want:?} among {gaps:#?}"
);
}
assert!(
!gaps.iter().any(|gap| gap.starts_with("mds build ×")),
"build has both rows; gaps: {gaps:#?}"
);
assert!(defines_test(
"broken_pipe.rs",
"check_a_file_with_stderr_closed_exits_0"
));
assert!(!defines_test(
"broken_pipe.rs",
"check_a_file_with_stdout_closed_exits_42"
));
}
#[cfg(target_os = "linux")]
fn run_into_dev_full_in(dir: &Path, args: &[&str], stdin: &str) -> Run {
let full = std::fs::OpenOptions::new()
.write(true)
.open("/dev/full")
.expect("open /dev/full");
run_with(
dir,
args,
Input::Bytes(stdin.as_bytes().to_vec()),
Stdio::from(full),
Stdio::piped(),
)
}
#[cfg(unix)]
fn run_into_a_file_it_may_not_grow_in(dir: &Path, args: &[&str], stdin: &str) -> Run {
let out = tempfile::tempfile().expect("create the stdout file");
let mut cmd = mds_bin();
common::limit_file_growth(&mut cmd, 0);
run_command(
cmd,
dir,
args,
Input::Bytes(stdin.as_bytes().to_vec()),
Stdio::from(out),
Stdio::piped(),
)
}
#[cfg(windows)]
fn run_into_a_read_only_handle_in(dir: &Path, args: &[&str], stdin: &str) -> Run {
let holder = tempfile::tempdir().expect("create the stdout file's directory");
let path = holder.path().join("stdout");
std::fs::write(&path, "").expect("create the stdout file");
let read_only = std::fs::File::open(&path).expect("open the stdout file to read");
run_with(
dir,
args,
Input::Bytes(stdin.as_bytes().to_vec()),
Stdio::from(read_only),
Stdio::piped(),
)
}
struct FailingStdout {
how: &'static str,
cause: &'static str,
run: fn(&Path, &[&str], &str) -> Run,
}
fn failing_stdouts() -> Vec<FailingStdout> {
[
#[cfg(target_os = "linux")]
FailingStdout {
how: "/dev/full",
cause: "(os error 28)",
run: run_into_dev_full_in,
},
#[cfg(unix)]
FailingStdout {
how: "a file it may not grow",
cause: "(os error 27)",
run: run_into_a_file_it_may_not_grow_in,
},
#[cfg(windows)]
FailingStdout {
how: "a handle opened to read alone",
cause: "(os error 5)",
run: run_into_a_read_only_handle_in,
},
]
.into_iter()
.collect()
}
struct FullRow<'a> {
args: &'static [&'static str],
stdin: &'static str,
verdict: i32,
open_contains: &'a [&'a str],
prepare: fn(&Path),
}
fn assert_a_failing_stdout_lifts_the_verdict(row: &FullRow<'_>) -> (Run, Vec<Run>) {
let what = row.args.join(" ");
let ways = failing_stdouts();
assert!(
!ways.is_empty(),
"no way to fail stdout on this platform: ignore the row here"
);
let open_dir = fixture_dir();
(row.prepare)(open_dir.path());
let open = run(open_dir.path(), row.args, row.stdin, None);
assert_eq!(
open.code,
Some(row.verdict),
"control: `mds {what}` into an open pipe must exit with its verdict; stderr: {:?}",
open.stderr
);
assert!(
!open.stderr.contains("cannot write to stdout"),
"control: `mds {what}` into an open pipe must not report stdout; stderr: {:?}",
open.stderr
);
for text in row.open_contains {
assert!(
open.stdout.contains(text),
"control: `mds {what}` must print {text:?} on stdout, or a failing stdout would \
lose nothing; stdout: {:?}",
open.stdout
);
}
let want = row.verdict.max(2);
let mut failing = Vec::with_capacity(ways.len());
for FailingStdout {
how,
cause,
run: run_failing,
} in ways
{
let dir = fixture_dir();
(row.prepare)(dir.path());
let run = run_failing(dir.path(), row.args, row.stdin);
assert_eq!(
run.code,
Some(want),
"`mds {what}` into {how} must exit {want}, its verdict {} lifted to at least 2; \
stderr: {:?}",
row.verdict,
run.stderr
);
assert_eq!(
(
run.stderr.matches("mds::io").count(),
run.stderr.matches("cannot write to stdout").count()
),
(1, 1),
"`mds {what}` into {how} must report the failure exactly once, as one mds::io \
error naming stdout; stderr: {:?}",
run.stderr
);
assert!(
run.stderr.contains(cause),
"`mds {what}` into {how} must fail for that reason, {cause}; stderr: {:?}",
run.stderr
);
failing.push(run);
}
(open, failing)
}
fn write_an_oversized_source(dir: &Path) {
std::fs::write(dir.join("big.mds"), vec![b'x'; STDIN_CAP + 1]).expect("write big.mds");
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn build_to_stdout_into_a_failing_stdout_exits_2() {
assert_a_failing_stdout_lifts_the_verdict(&FullRow {
args: &["build", "ok.mds", "-o", "-"],
stdin: "",
verdict: 0,
open_contains: &["Hello\n"],
prepare: |_| {},
});
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn fmt_stdin_into_a_failing_stdout_exits_2() {
assert_a_failing_stdout_lifts_the_verdict(&FullRow {
args: &["fmt", "-"],
stdin: "Hello",
verdict: 0,
open_contains: &["Hello\n"],
prepare: |_| {},
});
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn help_into_a_failing_stdout_exits_2() {
assert_a_failing_stdout_lifts_the_verdict(&FullRow {
args: &["--help"],
stdin: "",
verdict: 0,
open_contains: &["Usage: mds"],
prepare: |_| {},
});
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn lint_json_on_a_clean_file_into_a_failing_stdout_exits_2() {
assert_a_failing_stdout_lifts_the_verdict(&FullRow {
args: &["lint", "--format", "json", "ok.mds"],
stdin: "",
verdict: 0,
open_contains: &["{\"files\":[],\"truncated\":false,\"version\":1}\n"],
prepare: |_| {},
});
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn lint_json_on_a_clean_directory_into_a_failing_stdout_exits_2() {
assert_a_failing_stdout_lifts_the_verdict(&FullRow {
args: &["lint", "--format", "json", "good"],
stdin: "",
verdict: 0,
open_contains: &["{\"files\":[],\"truncated\":false,\"version\":1}\n"],
prepare: |_| {},
});
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn lint_fix_of_stdin_without_a_final_newline_into_a_failing_stdout_exits_2() {
let (open, _) = assert_a_failing_stdout_lifts_the_verdict(&FullRow {
args: &["lint", "--fix", "-"],
stdin: "Hello",
verdict: 0,
open_contains: &["Hello"],
prepare: |_| {},
});
assert_eq!(
open.stdout, "Hello",
"control: the source must be written back without a final newline"
);
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn lint_json_on_a_file_over_the_size_limit_into_a_failing_stdout_exits_3() {
assert_a_failing_stdout_lifts_the_verdict(&FullRow {
args: &["lint", "--format", "json", "big.mds"],
stdin: "",
verdict: 3,
open_contains: &["\"code\":\"mds::resource_limit\""],
prepare: write_an_oversized_source,
});
}
fn assert_each_lost_diff_counts_as_failed(
row: &FullRow<'_>,
open_summary: &str,
lost_summary: &str,
) {
let what = row.args.join(" ");
let (open, failing) = assert_a_failing_stdout_lifts_the_verdict(row);
assert!(
open.stderr.contains(open_summary),
"control: `mds {what}` into an open pipe must print {open_summary:?}; stderr: {:?}",
open.stderr
);
for run in &failing {
assert!(
run.stderr.contains(lost_summary),
"`mds {what}` must count each file whose diff a failing stdout lost as failed, \
{lost_summary:?}; stderr: {:?}",
run.stderr
);
}
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn fmt_diff_of_a_directory_into_a_failing_stdout_counts_each_lost_diff_as_failed() {
assert_each_lost_diff_counts_as_failed(
&FullRow {
args: &["fmt", "--diff", "two"],
stdin: "",
verdict: 0,
open_contains: &[&diff_header("two/a.mds"), &diff_header("two/b.mds")],
prepare: |_| {},
},
"2 would reformat, 0 unchanged, 0 failed\n",
"0 would reformat, 0 unchanged, 2 failed\n",
);
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn lint_fix_diff_of_a_directory_into_a_failing_stdout_counts_each_lost_diff_under_errors() {
assert_each_lost_diff_counts_as_failed(
&FullRow {
args: &["lint", "--fix", "--diff", "fixes"],
stdin: "",
verdict: 1,
open_contains: &[&diff_header("fixes/a.mds"), &diff_header("fixes/b.mds")],
prepare: |_| {},
},
"2 clean, 0 with warnings, 0 with errors, 0 resource-limited\n",
"0 clean, 0 with warnings, 2 with errors, 0 resource-limited\n",
);
}
#[test]
#[cfg_attr(not(any(unix, windows)), ignore = "needs a way to fail stdout")]
fn lint_json_fix_diff_of_a_directory_into_a_failing_stdout_counts_each_lost_diff_under_errors() {
assert_each_lost_diff_counts_as_failed(
&FullRow {
args: &["lint", "--format", "json", "--fix", "--diff", "fixes"],
stdin: "",
verdict: 1,
open_contains: &[
&diff_header("fixes/a.mds"),
&diff_header("fixes/b.mds"),
"\"version\":1}\n",
],
prepare: |_| {},
},
"2 clean, 0 with warnings, 0 with errors, 0 resource-limited\n",
"0 clean, 0 with warnings, 2 with errors, 0 resource-limited\n",
);
}
#[test]
fn help_with_stdout_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["--help"],
stdin: "",
verdict: 0,
closed: Stream::Stdout,
open_contains: "Usage: mds",
});
}
#[test]
fn version_with_stdout_closed_exits_0() {
assert_closing_keeps_the_verdict(&Row {
args: &["--version"],
stdin: "",
verdict: 0,
closed: Stream::Stdout,
open_contains: "mds ",
});
}
#[test]
fn a_usage_error_with_stderr_closed_exits_2() {
assert_closing_keeps_the_verdict(&Row {
args: &["build", "--no-such-flag"],
stdin: "",
verdict: 2,
closed: Stream::Stderr,
open_contains: "unexpected argument",
});
}
fn assert_exit_and_code(run: &Run, what: &str, code: i32, mds_code: &str) {
assert_eq!(
run.code,
Some(code),
"`mds {what}` must exit {code}; stderr: {:?}",
run.stderr
);
assert!(
run.stderr.contains(mds_code),
"`mds {what}` must report {mds_code}; stderr: {:?}",
run.stderr
);
}
#[cfg(unix)]
struct ReadOnlyDir(PathBuf);
#[cfg(unix)]
impl ReadOnlyDir {
fn new(path: &Path) -> Option<Self> {
use std::os::unix::fs::PermissionsExt as _;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o555))
.expect("chmod 0o555");
let guard = Self(path.to_path_buf());
let probe = path.join(".write-probe");
if std::fs::write(&probe, b"").is_ok() {
let _ = std::fs::remove_file(&probe);
eprintln!(
"skipped: a file can be created in {} at mode 0o555 (running as root?)",
path.display()
);
return None;
}
Some(guard)
}
}
#[cfg(unix)]
impl Drop for ReadOnlyDir {
fn drop(&mut self) {
use std::os::unix::fs::PermissionsExt as _;
let _ = std::fs::set_permissions(&self.0, std::fs::Permissions::from_mode(0o755));
}
}
#[cfg(unix)]
fn fixture_with_read_only_dir(files: &[(&str, &str)]) -> Option<(tempfile::TempDir, ReadOnlyDir)> {
let dir = fixture_dir();
let ro = dir.path().join("ro");
std::fs::create_dir(&ro).expect("create ro");
for (name, text) in files {
std::fs::write(ro.join(name), text).expect("write a file in ro");
}
let guard = ReadOnlyDir::new(&ro)?;
Some((dir, guard))
}
#[cfg(unix)]
fn run_in(dir: &Path, args: &[&str]) -> Run {
run(dir, args, "", None)
}
#[cfg(unix)]
#[test]
fn a_build_whose_output_cannot_be_written_exits_2() {
let Some((dir, _ro)) = fixture_with_read_only_dir(&[]) else {
return;
};
let run = run_in(dir.path(), &["build", "ok.mds", "-o", "ro/out.md"]);
assert_exit_and_code(&run, "build ok.mds -o ro/out.md", 2, "mds::io");
assert!(
!dir.path().join("ro/out.md").exists(),
"nothing may be written"
);
}
#[cfg(unix)]
#[test]
fn a_directory_build_with_one_unwritable_output_exits_2_and_writes_the_others() {
let dir = fixture_dir();
let w = dir.path().join("w");
std::fs::create_dir_all(w.join("ro")).expect("create w/ro");
std::fs::write(w.join("a.mds"), "A\n").expect("write w/a.mds");
std::fs::write(w.join("ro/b.mds"), "B\n").expect("write w/ro/b.mds");
let Some(_ro) = ReadOnlyDir::new(&w.join("ro")) else {
return;
};
let run = run_in(dir.path(), &["build", "w"]);
assert_exit_and_code(&run, "build w", 2, "mds::io");
assert!(
run.stderr.contains("1 built, 1 failed"),
"the summary must count the write failure; stderr: {:?}",
run.stderr
);
assert_eq!(
std::fs::read_to_string(w.join("a.md")).ok().as_deref(),
Some("A\n"),
"the output that can be written must still be written"
);
}
#[cfg(unix)]
#[test]
fn fmt_of_a_file_it_cannot_rewrite_exits_2() {
let Some((dir, _ro)) = fixture_with_read_only_dir(&[("messy.mds", "Hello")]) else {
return;
};
let run = run_in(dir.path(), &["fmt", "ro/messy.mds"]);
assert_exit_and_code(&run, "fmt ro/messy.mds", 2, "mds::io");
assert_eq!(
std::fs::read_to_string(dir.path().join("ro/messy.mds"))
.ok()
.as_deref(),
Some("Hello"),
"the file must be left as it was"
);
}
#[cfg(unix)]
#[test]
fn fmt_of_a_directory_with_a_file_it_cannot_rewrite_exits_2() {
let Some((dir, _ro)) = fixture_with_read_only_dir(&[("messy.mds", "Hello")]) else {
return;
};
let run = run_in(dir.path(), &["fmt", "ro"]);
assert_exit_and_code(&run, "fmt ro", 2, "mds::io");
assert!(
run.stderr.contains("0 formatted, 0 unchanged, 1 failed"),
"the summary must count the write failure; stderr: {:?}",
run.stderr
);
}
#[cfg(unix)]
#[test]
fn lint_fix_of_a_file_it_cannot_rewrite_exits_2_with_the_io_code() {
let fixable = "@if \"x\" == \"y\":\nhidden\n@end\nHello\n";
let Some((dir, _ro)) = fixture_with_read_only_dir(&[("fix.mds", fixable)]) else {
return;
};
let run = run_in(dir.path(), &["lint", "--fix", "ro/fix.mds"]);
assert_exit_and_code(&run, "lint --fix ro/fix.mds", 2, "mds::io");
}
#[cfg(unix)]
#[test]
fn init_in_a_directory_it_cannot_write_exits_2() {
let Some((dir, _ro)) = fixture_with_read_only_dir(&[]) else {
return;
};
let run = run_in(dir.path(), &["init", "ro/hello.mds"]);
assert_exit_and_code(&run, "init ro/hello.mds", 2, "mds::io");
assert!(
!dir.path().join("ro/hello.mds").exists(),
"nothing may be written"
);
}
#[cfg(unix)]
fn fixture_with_unreadable_source() -> Option<tempfile::TempDir> {
use std::os::unix::fs::PermissionsExt as _;
let dir = fixture_dir();
let u = dir.path().join("u");
std::fs::create_dir(&u).expect("create u");
std::fs::write(u.join("a.mds"), "A").expect("write u/a.mds");
let b = u.join("b.mds");
std::fs::write(&b, "B\n").expect("write u/b.mds");
std::fs::set_permissions(&b, std::fs::Permissions::from_mode(0o000)).expect("chmod 0o000");
if std::fs::read(&b).is_ok() {
eprintln!(
"skipped: {} can be read at mode 0o000 (running as root?)",
b.display()
);
return None;
}
Some(dir)
}
#[cfg(unix)]
fn assert_unreadable_source_exits_2(run: &Run, what: &str, summary: &str) {
assert_exit_and_code(run, what, 2, "mds::io");
assert!(
run.stderr.contains("b.mds"),
"`mds {what}` must name the file it could not read; stderr: {:?}",
run.stderr
);
assert!(
run.stderr.contains(summary),
"`mds {what}` must process the other file and count the failed one: {summary:?}; \
stderr: {:?}",
run.stderr
);
}
#[cfg(unix)]
#[test]
fn a_directory_build_with_an_unreadable_source_exits_2() {
let Some(dir) = fixture_with_unreadable_source() else {
return;
};
let run = run_in(dir.path(), &["build", "u"]);
assert_unreadable_source_exits_2(&run, "build u", "1 built, 1 failed");
assert_eq!(
std::fs::read_to_string(dir.path().join("u/a.md"))
.ok()
.as_deref(),
Some("A\n"),
"the source that can be read must still be built"
);
}
#[cfg(unix)]
#[test]
fn a_directory_check_with_an_unreadable_source_exits_2() {
let Some(dir) = fixture_with_unreadable_source() else {
return;
};
let run = run_in(dir.path(), &["check", "u"]);
assert_unreadable_source_exits_2(&run, "check u", "1 passed, 1 failed");
}
#[cfg(unix)]
#[test]
fn a_directory_fmt_with_an_unreadable_source_exits_2() {
let Some(dir) = fixture_with_unreadable_source() else {
return;
};
let run = run_in(dir.path(), &["fmt", "u"]);
assert_unreadable_source_exits_2(&run, "fmt u", "1 formatted, 0 unchanged, 1 failed");
assert_eq!(
std::fs::read_to_string(dir.path().join("u/a.mds"))
.ok()
.as_deref(),
Some("A\n"),
"the source that can be read must still be formatted"
);
}
#[test]
fn a_directory_build_or_check_with_a_missing_import_exits_2() {
for (args, summary) in [
(&["build", "i"][..], "1 built, 1 failed"),
(&["check", "i"][..], "1 passed, 1 failed"),
] {
let what = args.join(" ");
let dir = fixture_dir();
let i = dir.path().join("i");
std::fs::create_dir(&i).expect("create i");
std::fs::write(i.join("a.mds"), "@import \"./nope.mds\" as n\n\nHi\n")
.expect("write i/a.mds");
std::fs::write(i.join("b.mds"), "Ok\n").expect("write i/b.mds");
let run = run(dir.path(), args, "", None);
assert_exit_and_code(&run, &what, 2, "mds::file_not_found");
assert!(
run.stderr.contains(summary),
"`mds {what}` must process the other file and count the failed one: \
{summary:?}; stderr: {:?}",
run.stderr
);
}
}
#[test]
fn a_directory_run_whose_failures_are_a_template_error_and_a_resource_limit_exits_3() {
let over_cap = "x".repeat(STDIN_CAP + 1);
for (args, summary) in [
(&["build", "t"][..], "1 built, 2 failed"),
(&["check", "t"][..], "1 passed, 2 failed"),
(&["fmt", "t"][..], "0 formatted, 1 unchanged, 2 failed"),
] {
let what = args.join(" ");
let dir = fixture_dir();
let t = dir.path().join("t");
std::fs::create_dir(&t).expect("create t");
std::fs::write(t.join("ok.mds"), "Hello\n").expect("write t/ok.mds");
std::fs::write(t.join("bad.mds"), "@if flag:\nunterminated\n").expect("write t/bad.mds");
std::fs::write(t.join("big.mds"), &over_cap).expect("write t/big.mds");
let run = run(dir.path(), args, "", None);
assert_exit_and_code(&run, &what, 3, "mds::resource_limit");
assert!(
run.stderr.contains("mds::syntax") && !run.stderr.contains("mds::io"),
"`mds {what}` must report the template error and no I/O failure; stderr: {:?}",
run.stderr
);
assert!(
run.stderr.contains(summary),
"`mds {what}` must count both failures: {summary:?}; stderr: {:?}",
run.stderr
);
}
}
const STDIN_COMMANDS: [&[&str]; 4] = [
&["build", "-"],
&["check", "-"],
&["fmt", "-"],
&["lint", "-"],
];
fn unreadable_stdin() -> (tempfile::TempDir, Stdio) {
let dir = tempfile::tempdir().expect("tempdir");
#[cfg(unix)]
let handle = std::fs::File::open(dir.path()).expect("open a directory handle");
#[cfg(windows)]
let handle = std::fs::File::create(dir.path().join("stdin")).expect("create a write-only file");
(dir, Stdio::from(handle))
}
#[test]
fn stdin_that_cannot_be_read_exits_2() {
for args in STDIN_COMMANDS {
let what = args.join(" ");
let dir = fixture_dir();
let (_keep, stdin) = unreadable_stdin();
let run = run_with(
dir.path(),
args,
Input::Handle(stdin),
Stdio::piped(),
Stdio::piped(),
);
assert_exit_and_code(&run, &what, 2, "mds::io");
assert!(
run.stderr.contains("cannot read stdin"),
"`mds {what}` must say stdin could not be read; stderr: {:?}",
run.stderr
);
}
}
#[test]
fn stdin_that_is_not_utf8_exits_2() {
for args in STDIN_COMMANDS {
let what = args.join(" ");
let dir = fixture_dir();
let run = run_with(
dir.path(),
args,
Input::Bytes(b"Hello \xff\n".to_vec()),
Stdio::piped(),
Stdio::piped(),
);
assert_exit_and_code(&run, &what, 2, "mds::io");
assert!(
run.stderr.contains("valid UTF-8"),
"`mds {what}` must say stdin was not UTF-8; stderr: {:?}",
run.stderr
);
}
}
const STDIN_CAP: usize = 10 * 1024 * 1024;
#[test]
fn stdin_over_the_cap_exits_3() {
let lint_json: &[&str] = &["lint", "--format", "json", "-"];
for args in STDIN_COMMANDS.into_iter().chain([lint_json]) {
let what = args.join(" ");
let dir = fixture_dir();
let run = run_with(
dir.path(),
args,
Input::Bytes(vec![b'x'; STDIN_CAP + 1]),
Stdio::piped(),
Stdio::piped(),
);
if args == lint_json {
assert_eq!(run.code, Some(3), "`mds {what}`; stderr: {:?}", run.stderr);
assert!(
run.stdout.contains("\"code\":\"mds::resource_limit\""),
"`mds {what}` must report mds::resource_limit; stdout: {:?}",
run.stdout
);
} else {
assert_exit_and_code(&run, &what, 3, "mds::resource_limit");
}
}
}
#[test]
fn stdin_of_exactly_the_cap_passes_the_size_gate() {
let mut at_cap = vec![b'x'; STDIN_CAP - 1];
at_cap.push(b'\n');
for args in STDIN_COMMANDS {
let what = args.join(" ");
let dir = fixture_dir();
let run = run_with(
dir.path(),
args,
Input::Bytes(at_cap.clone()),
Stdio::piped(),
Stdio::piped(),
);
assert_eq!(
run.code,
Some(0),
"`mds {what}` must accept exactly the cap; stderr: {:?}",
run.stderr
);
}
}