use std::io::Write as _;
use std::process::Command;
fn hjkl() -> Command {
Command::new(env!("CARGO_BIN_EXE_hjkl"))
}
#[test]
fn headless_substitute_writes_back() {
let mut f = tempfile::NamedTempFile::new().unwrap();
f.write_all(b"foo bar foo\n").unwrap();
let path = f.path().to_path_buf();
let status = hjkl()
.args(["--headless", "+:%s/foo/baz/g", "+:wq"])
.arg(&path)
.status()
.expect("hjkl binary");
assert!(status.success(), "exit code: {status}");
let contents = std::fs::read_to_string(&path).unwrap();
assert_eq!(contents, "baz bar baz\n", "file not updated: {contents:?}");
}
#[test]
fn headless_no_write_without_explicit_save() {
let mut f = tempfile::NamedTempFile::new().unwrap();
f.write_all(b"foo bar foo\n").unwrap();
let path = f.path().to_path_buf();
let status = hjkl()
.args(["--headless", "+:%s/foo/baz/g"])
.arg(&path)
.status()
.expect("hjkl binary");
assert!(status.success(), "exit code: {status}");
let contents = std::fs::read_to_string(&path).unwrap();
assert_eq!(
contents, "foo bar foo\n",
"file was unexpectedly modified: {contents:?}"
);
}
#[test]
fn headless_unknown_command_sets_exit_1() {
let mut f = tempfile::NamedTempFile::new().unwrap();
f.write_all(b"hello\n").unwrap();
let path = f.path().to_path_buf();
let output = hjkl()
.args(["--headless", "+:doesnotexist", "+:q"])
.arg(&path)
.output()
.expect("hjkl binary");
assert_eq!(output.status.code(), Some(1), "expected exit 1");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("doesnotexist"),
"stderr should mention the bad command; got: {stderr:?}"
);
}
#[test]
fn headless_dash_c_and_plus_interleave() {
let mut f = tempfile::NamedTempFile::new().unwrap();
f.write_all(b"a\n").unwrap();
let path = f.path().to_path_buf();
let status = hjkl()
.args(["--headless", "-c", ":%s/a/b/g", "+:%s/b/c/g", "+:wq"])
.arg(&path)
.status()
.expect("hjkl binary");
assert!(status.success(), "exit code: {status}");
let contents = std::fs::read_to_string(&path).unwrap();
assert_eq!(contents, "c\n", "unexpected contents: {contents:?}");
}
#[test]
fn headless_no_files_exits_clean() {
let output = hjkl().arg("--headless").output().expect("hjkl binary");
assert!(output.status.success(), "exit code: {}", output.status);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("no commands or files"),
"expected warning on stderr; got: {stderr:?}"
);
}