use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
fn fixture_dir(name: &str) -> PathBuf {
let stamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system time should be after Unix epoch")
.as_nanos();
let dir = std::env::temp_dir().join(format!(
"mdo-wrapper-test-{name}-{}-{stamp}",
std::process::id()
));
fs::create_dir_all(&dir).expect("failed to create temp fixture dir");
dir
}
#[test]
fn mdo_open_propagates_render_failure_for_missing_file() {
let dir = fixture_dir("missing-file");
let missing_input = dir.join("does-not-exist.md");
let output = Command::new(env!("CARGO_BIN_EXE_mdo-open"))
.arg(&missing_input)
.output()
.expect("failed to run mdo-open");
assert!(
!output.status.success(),
"mdo-open should exit non-zero when the wrapped render fails: {output:?}"
);
fs::remove_dir_all(dir).expect("failed to clean up temp fixture dir");
}
#[cfg(target_os = "linux")]
fn empty_path_dir(dir: &std::path::Path) -> PathBuf {
let empty = dir.join("empty-path");
fs::create_dir_all(&empty).expect("failed to create empty PATH dir");
empty
}
#[cfg(target_os = "linux")]
#[test]
fn mdo_open_forwards_file_arg_and_propagates_browser_launch_failure() {
let dir = fixture_dir("open-launch-failure");
let input = dir.join("sample.md");
fs::write(&input, "# Sample\n\nSome text.\n").expect("failed to write markdown fixture");
let empty_path = empty_path_dir(&dir);
let output = Command::new(env!("CARGO_BIN_EXE_mdo-open"))
.arg(&input)
.env("PATH", &empty_path)
.output()
.expect("failed to run mdo-open");
assert!(
!output.status.success(),
"mdo-open should exit non-zero when the wrapped browser launch fails: {output:?}"
);
let rendered_path =
mdo_cli::temp_output_for(&input).expect("failed to compute expected temp output path");
assert!(
rendered_path.exists(),
"expected mdo-open to have forwarded the file arg and rendered {rendered_path:?}"
);
fs::remove_dir_all(dir).expect("failed to clean up temp fixture dir");
}