use std::path::Path;
use std::process::{Command, Output, Stdio};
use testdir::testdir;
fn vpxtool(dir: &Path, args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_vpxtool"))
.args(args)
.current_dir(dir)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("failed to spawn vpxtool")
.wait_with_output()
.expect("failed to wait for vpxtool")
}
#[test]
fn script_diff_ignores_mixed_line_ending_differences() {
let dir = testdir!();
let vpx_path = dir.join("table.vpx");
let out = vpxtool(&dir, &["new", "table.vpx"]);
assert!(out.status.success(), "vpxtool new failed: {:?}", out);
let out = vpxtool(&dir, &["script", "extract", "table.vpx"]);
assert!(
out.status.success(),
"vpxtool script extract failed: {:?}",
out
);
let vbs_path = vpx_path.with_extension("vbs");
let original = std::fs::read_to_string(&vbs_path).expect("read vbs");
let lines: Vec<&str> = original.lines().collect();
let mut mixed = String::new();
for (i, line) in lines.iter().enumerate() {
mixed.push_str(line);
match i % 3 {
0 => mixed.push_str("\r\n"), 1 => mixed.push('\r'), _ => mixed.push('\n'), }
}
std::fs::write(&vbs_path, &mixed).expect("write vbs");
let out = vpxtool(&dir, &["script", "diff", "table.vpx"]);
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
out.status.success(),
"script diff exited with failure: {:?}\nstderr: {stderr}",
out.status,
);
assert!(
stdout.trim().is_empty(),
"expected no diff output when only line endings differ, got:\n{stdout}"
);
}
#[test]
fn script_diff_reports_real_changes_despite_mixed_endings() {
let dir = testdir!();
let vpx_path = dir.join("table.vpx");
let out = vpxtool(&dir, &["new", "table.vpx"]);
assert!(out.status.success(), "vpxtool new failed: {:?}", out);
let out = vpxtool(&dir, &["script", "extract", "table.vpx"]);
assert!(
out.status.success(),
"vpxtool script extract failed: {:?}",
out
);
let vbs_path = vpx_path.with_extension("vbs");
let original = std::fs::read_to_string(&vbs_path).expect("read vbs");
let mut modified = original.replace("\r\n", "\n").replace('\r', "\n");
modified.push_str("' added comment\n");
std::fs::write(&vbs_path, &modified).expect("write vbs");
let out = vpxtool(&dir, &["script", "diff", "table.vpx"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("added comment"),
"expected diff to contain the real change, got:\n{stdout}"
);
}
#[test]
fn script_diff_handles_latin1_sidecar() {
let dir = testdir!();
let out = vpxtool(&dir, &["new", "table.vpx"]);
assert!(out.status.success(), "vpxtool new failed: {:?}", out);
let out = vpxtool(&dir, &["script", "extract", "table.vpx"]);
assert!(
out.status.success(),
"vpxtool script extract failed: {:?}",
out
);
let vbs_path = dir.join("table.vbs");
let mut bytes = std::fs::read(&vbs_path).expect("read vbs");
bytes.extend_from_slice(b"' caf\xE9 comment\r\n");
std::fs::write(&vbs_path, &bytes).expect("write vbs");
let out = vpxtool(&dir, &["script", "diff", "table.vpx"]);
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
out.status.success(),
"script diff exited with failure: {:?}\nstderr: {stderr}",
out.status,
);
assert!(
stdout.contains("comment"),
"expected diff to contain the added latin1 line, got:\n{stdout}"
);
}