use std::io::Write;
use std::process::{Command, Stdio};
fn comex(args: &[&str], stdin: &str) -> (String, String, bool) {
let mut child = Command::new(env!("CARGO_BIN_EXE_comex"))
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("failed to run comex");
child
.stdin
.take()
.unwrap()
.write_all(stdin.as_bytes())
.unwrap();
let out = child.wait_with_output().unwrap();
(
String::from_utf8_lossy(&out.stdout).into_owned(),
String::from_utf8_lossy(&out.stderr).into_owned(),
out.status.success(),
)
}
const SRC: &str = "#!/bin/sh\n# deploy\nset -e # strict\necho \"go\"\n";
#[test]
fn strips_from_stdin_by_default() {
let (out, _, ok) = comex(&[], SRC);
assert!(ok);
assert_eq!(out, "#!/bin/sh\nset -e\necho \"go\"\n");
}
#[test]
fn comments_mode_keeps_only_comments() {
let (out, _, ok) = comex(&["-c"], SRC);
assert!(ok);
assert_eq!(out, "deploy\nstrict\n");
}
#[test]
fn bundled_short_flags_work() {
let (out, _, ok) = comex(&["-cm"], SRC);
assert!(ok, "-cm should be -c -m");
assert_eq!(out, "# deploy\n# strict\n");
}
#[test]
fn keep_lines_preserves_numbering() {
let (out, _, ok) = comex(&["-k"], SRC);
assert!(ok);
assert_eq!(out.lines().count(), SRC.lines().count());
}
#[test]
fn detect_reports_syntax_and_scores() {
let (out, _, ok) = comex(&["-d"], SRC);
assert!(ok);
assert!(out.starts_with("hash (confidence "), "{out}");
assert!(out.contains("scores: hash="), "{out}");
}
#[test]
fn forced_syntax_overrides_detection() {
let (out, _, ok) = comex(&["-s", "c"], "x = 1; // c\n");
assert!(ok);
assert_eq!(out, "x = 1;\n");
let (out, _, _) = comex(&["--syntax=c"], "x = 1; // c\n");
assert_eq!(out, "x = 1;\n");
let (out, _, _) = comex(&[], "x = 1; // c\n");
assert_eq!(out, "x = 1; // c\n", "undetectable input must pass through");
}
#[test]
fn list_names_every_dialect() {
let (out, _, ok) = comex(&["--list"], "");
assert!(ok);
for name in ["c", "hash", "sql", "lua", "markup", "unknown"] {
assert!(out.contains(name), "--list missing {name}");
}
}
#[test]
fn help_and_version() {
let (out, _, ok) = comex(&["--help"], "");
assert!(ok && out.contains("USAGE:"));
let (out, _, ok) = comex(&["-V"], "");
assert!(ok && out.starts_with("comex "));
}
#[test]
fn bad_input_is_an_error_not_a_panic() {
let (_, err, ok) = comex(&["--frobnicate"], "");
assert!(!ok && err.contains("unknown option"), "{err}");
let (_, err, ok) = comex(&["-s", "klingon"], "");
assert!(!ok && err.contains("unknown syntax"), "{err}");
let (_, err, ok) = comex(&["/nonexistent/path/xyz"], "");
assert!(!ok && err.contains("comex:"), "{err}");
let (_, err, ok) = comex(&["--in-place"], "");
assert!(!ok && err.contains("needs at least one FILE"), "{err}");
}
#[test]
fn in_place_rewrites_the_file() {
let dir = std::env::temp_dir().join(format!("comex-cli-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("script.sh");
std::fs::write(&path, SRC).unwrap();
let (out, _, ok) = comex(&["-i", path.to_str().unwrap()], "");
assert!(ok);
assert!(out.is_empty(), "--in-place should not write to stdout");
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
"#!/bin/sh\nset -e\necho \"go\"\n"
);
std::fs::write(&path, SRC).unwrap();
let (_, err, ok) = comex(&["--stats", "-i", path.to_str().unwrap()], "");
assert!(ok);
assert!(
err.contains("syntax=hash") && err.contains("comments=2"),
"{err}"
);
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn multiple_files_concatenate() {
let dir = std::env::temp_dir().join(format!("comex-cli-multi-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let a = dir.join("a.c");
let b = dir.join("b.c");
std::fs::write(&a, "int a = 1; // one\n").unwrap();
std::fs::write(&b, "int b = 2; // two\n").unwrap();
let (out, _, ok) = comex(&["-s", "c", a.to_str().unwrap(), b.to_str().unwrap()], "");
assert!(ok);
assert_eq!(out, "int a = 1;\nint b = 2;\n");
let (out, _, ok) = comex(&["-d", a.to_str().unwrap(), b.to_str().unwrap()], "");
assert!(ok);
assert!(out.contains("a.c: ") && out.contains("b.c: "), "{out}");
std::fs::remove_dir_all(&dir).ok();
}