use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, Output, Stdio};
use std::time::{SystemTime, UNIX_EPOCH};
fn profmt(arguments: &[&str], stdin: &[u8]) -> Output {
run_profmt(arguments, stdin, None, true)
}
fn run_profmt(arguments: &[&str], stdin: &[u8], directory: Option<&std::path::Path>, no_config: bool) -> Output {
let mut command = Command::new(env!("CARGO_BIN_EXE_profmt"));
if no_config {
command.arg("--no-config");
}
command
.args(arguments)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
if let Some(directory) = directory {
command.current_dir(directory);
}
let mut child = command.spawn().expect("start profmt");
child
.stdin
.take()
.expect("stdin pipe")
.write_all(stdin)
.expect("write stdin");
child.wait_with_output().expect("wait for profmt")
}
fn temporary_directory(test: &str) -> PathBuf {
let nonce = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("clock after epoch")
.as_nanos();
let path = std::env::temp_dir().join(format!("profmt-cli-{test}-{}-{nonce}", std::process::id()));
fs::create_dir(&path).expect("create test directory");
path
}
const SINGLE_QUOTED: &[u8] = b"import 'a.proto';\n";
const DOUBLE_QUOTED: &[u8] = b"import \"a.proto\";\n";
#[test]
fn discovers_configuration_and_applies_cli_overrides_last() {
let root = temporary_directory("config");
fs::write(root.join(".profmt.toml"), "quote_style = \"double\"\n").expect("write config");
let configured = run_profmt(&["-"], SINGLE_QUOTED, Some(&root), false);
let overridden = run_profmt(&["--quote-style", "single", "-"], DOUBLE_QUOTED, Some(&root), false);
assert!(
configured.status.success(),
"{}",
String::from_utf8_lossy(&configured.stderr)
);
assert_eq!(configured.stdout, DOUBLE_QUOTED);
assert!(
overridden.status.success(),
"{}",
String::from_utf8_lossy(&overridden.stderr)
);
assert_eq!(overridden.stdout, SINGLE_QUOTED);
fs::remove_dir_all(root).expect("remove test directory");
}
#[test]
fn formats_standard_input_and_honours_style_flags() {
let output = profmt(&["--quote-style", "preserve", "-"], b"import 'a.proto' ;\n");
assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
assert_eq!(output.stdout, SINGLE_QUOTED);
assert!(output.stderr.is_empty());
}
#[test]
fn check_on_standard_input_uses_documented_exit_statuses() {
let changed = profmt(&["--check", "--quiet", "-"], b"message A{}\n");
let clean = profmt(&["--check", "--quiet", "-"], b"message A {}\n");
assert_eq!(changed.status.code(), Some(1));
assert!(changed.stdout.is_empty());
assert!(changed.stderr.is_empty());
assert_eq!(clean.status.code(), Some(0));
}
#[test]
fn check_with_diff_prints_a_unified_diff() {
let output = profmt(&["--check", "--diff", "-"], b"message A{}\n");
assert_eq!(output.status.code(), Some(1));
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.starts_with("changed: stdin\n--- a/stdin\n+++ b/stdin\n"),
"{stdout}"
);
assert!(stdout.contains("-message A{}\n+message A {}\n"), "{stdout}");
}
#[test]
fn write_walks_directories_and_respects_gitignore() {
let root = temporary_directory("walk");
fs::create_dir(root.join(".git")).expect("create repository marker");
fs::write(root.join(".gitignore"), b"ignored.proto\n").expect("write ignore file");
fs::write(root.join("keep.proto"), b"message A{}\n").expect("write included proto file");
fs::write(root.join("ignored.proto"), b"message B{}\n").expect("write ignored proto file");
fs::write(root.join("notes.txt"), b"message C{}\n").expect("write unrelated file");
let output = profmt(&["--write", "--quiet", root.to_str().expect("UTF-8 path")], b"");
assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
assert_eq!(
fs::read(root.join("keep.proto")).expect("read included file"),
b"message A {}\n"
);
assert_eq!(
fs::read(root.join("ignored.proto")).expect("read ignored file"),
b"message B{}\n"
);
assert_eq!(
fs::read(root.join("notes.txt")).expect("read unrelated file"),
b"message C{}\n"
);
fs::remove_dir_all(root).expect("remove test directory");
}
#[test]
fn check_reports_each_changed_file_and_a_summary() {
let root = temporary_directory("summary");
fs::write(root.join("a.proto"), b"message A{}\n").expect("write changed file");
fs::write(root.join("b.proto"), b"message B {}\n").expect("write clean file");
let output = profmt(&["--check", root.to_str().expect("UTF-8 path")], b"");
assert_eq!(output.status.code(), Some(1));
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("changed: ") && stdout.contains("a.proto"), "{stdout}");
assert!(!stdout.contains("b.proto"), "{stdout}");
assert_eq!(
String::from_utf8_lossy(&output.stderr),
"profmt: 1 unchanged, 1 changed, 0 failed (2 total)\n"
);
fs::remove_dir_all(root).expect("remove test directory");
}
#[test]
fn refuses_ambiguous_multi_file_stdout() {
let root = temporary_directory("multiple");
let first = root.join("a.proto");
let second = root.join("b.proto");
fs::write(&first, b"message A {}\n").expect("write first file");
fs::write(&second, b"message B {}\n").expect("write second file");
let output = profmt(
&[
first.to_str().expect("UTF-8 path"),
second.to_str().expect("UTF-8 path"),
],
b"",
);
assert_eq!(output.status.code(), Some(2));
assert!(String::from_utf8_lossy(&output.stderr).contains("printing requires exactly one proto file"));
fs::remove_dir_all(root).expect("remove test directory");
}
#[test]
fn parse_errors_fail_the_file_and_the_run() {
let root = temporary_directory("parse-error");
fs::write(root.join("bad.proto"), b"message {\n").expect("write invalid file");
fs::write(root.join("good.proto"), b"message A {}\n").expect("write valid file");
let output = profmt(&["--check", root.to_str().expect("UTF-8 path")], b"");
assert_eq!(output.status.code(), Some(2));
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("failed: ") && stderr.contains("bad.proto") && stderr.contains("parse error at line 1"),
"{stderr}"
);
assert!(
stderr.contains("profmt: 1 unchanged, 0 changed, 1 failed (2 total)"),
"{stderr}"
);
assert_eq!(
fs::read(root.join("bad.proto")).expect("read invalid file"),
b"message {\n"
);
fs::remove_dir_all(root).expect("remove test directory");
}
#[test]
fn rejects_misspelled_configuration_keys() {
let root = temporary_directory("bad-config");
fs::write(root.join(".profmt.toml"), "line_wdith = 100\n").expect("write config");
let output = run_profmt(&["-"], b"message A {}\n", Some(&root), false);
assert_eq!(output.status.code(), Some(2));
assert!(String::from_utf8_lossy(&output.stderr).contains("unknown field `line_wdith`"));
fs::remove_dir_all(root).expect("remove test directory");
}
#[test]
fn quiet_requires_a_mode() {
let output = profmt(&["--quiet", "-"], b"message A {}\n");
assert_eq!(output.status.code(), Some(2));
assert!(String::from_utf8_lossy(&output.stderr).contains("--quiet requires --check or --write"));
}