main.rs split into args/walk/diff modules, all std-only. --diff emits a patch-applicable unified diff (bounded-resync hunks, GNU-style missing-newline marker); --exclude is a small component-glob matcher that prunes directory walks; --stdin-filename names stdin buffers in messages; per-file catch_unwind turns formatter panics into ordinary errors. Exit-code contract (0/1/2) frozen and documented in --help and README. 12 CLI integration tests + 18 unit tests. Known gap found by QA, queued in TODO §1: deep nesting overflows the parser stack (uncatchable), needs an explicit recursion-depth limit. Dev: Opus agent; QA: Sonnet agent; review in docs/features/cli-polish/README.md (includes one lead fix: the no-final-newline marker is now emitted the way patch(1) expects). Co-Authored-By: Claude Fable 5 <[email protected]>
262 lines
9.3 KiB
Rust
262 lines
9.3 KiB
Rust
//! End-to-end tests that drive the real `vfmt` binary.
|
|
//!
|
|
//! Fixtures use the most trivial Verilog possible so these tests stay
|
|
//! stable while the formatter's layout rules evolve.
|
|
|
|
use std::io::Write;
|
|
use std::path::PathBuf;
|
|
use std::process::{Command, Output, Stdio};
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
const VFMT: &str = env!("CARGO_BIN_EXE_vfmt");
|
|
/// Formats to `FORMATTED`: the only change is the newline before `endmodule`.
|
|
const UNFORMATTED: &str = "module m;endmodule\n";
|
|
const FORMATTED: &str = "module m;\nendmodule\n";
|
|
/// Truncated header — the parser cannot recover from this.
|
|
const UNPARSEABLE: &str = "module\n";
|
|
|
|
/// A throwaway directory under the system temp dir, removed on drop.
|
|
struct TempDir {
|
|
path: PathBuf,
|
|
}
|
|
|
|
impl TempDir {
|
|
fn new(tag: &str) -> TempDir {
|
|
static COUNTER: AtomicUsize = AtomicUsize::new(0);
|
|
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
|
|
let path = std::env::temp_dir().join(format!("vfmt-cli-{}-{tag}-{n}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&path);
|
|
std::fs::create_dir_all(&path).expect("create temp dir");
|
|
TempDir { path }
|
|
}
|
|
|
|
fn write(&self, rel: &str, contents: &str) -> String {
|
|
let path = self.path.join(rel);
|
|
if let Some(parent) = path.parent() {
|
|
std::fs::create_dir_all(parent).expect("create parent dir");
|
|
}
|
|
std::fs::write(&path, contents).expect("write fixture");
|
|
path.to_string_lossy().into_owned()
|
|
}
|
|
|
|
fn read(&self, rel: &str) -> String {
|
|
std::fs::read_to_string(self.path.join(rel)).expect("read fixture")
|
|
}
|
|
|
|
fn root(&self) -> String {
|
|
self.path.to_string_lossy().into_owned()
|
|
}
|
|
}
|
|
|
|
impl Drop for TempDir {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_dir_all(&self.path);
|
|
}
|
|
}
|
|
|
|
fn run(args: &[&str]) -> Output {
|
|
Command::new(VFMT).args(args).output().expect("run vfmt")
|
|
}
|
|
|
|
fn run_with_stdin(args: &[&str], input: &str) -> Output {
|
|
let mut child = Command::new(VFMT)
|
|
.args(args)
|
|
.stdin(Stdio::piped())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::piped())
|
|
.spawn()
|
|
.expect("spawn vfmt");
|
|
child.stdin.as_mut().expect("stdin").write_all(input.as_bytes()).expect("write stdin");
|
|
child.wait_with_output().expect("run vfmt")
|
|
}
|
|
|
|
fn code(out: &Output) -> i32 {
|
|
out.status.code().expect("exit code")
|
|
}
|
|
|
|
fn stdout(out: &Output) -> String {
|
|
String::from_utf8_lossy(&out.stdout).into_owned()
|
|
}
|
|
|
|
fn stderr(out: &Output) -> String {
|
|
String::from_utf8_lossy(&out.stderr).into_owned()
|
|
}
|
|
|
|
#[test]
|
|
fn formats_files_in_place() {
|
|
let dir = TempDir::new("inplace");
|
|
dir.write("a.sv", UNFORMATTED);
|
|
dir.write("nested/b.v", UNFORMATTED);
|
|
dir.write("notes.txt", "not verilog\n");
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("a.sv"), FORMATTED);
|
|
assert_eq!(dir.read("nested/b.v"), FORMATTED);
|
|
assert_eq!(dir.read("notes.txt"), "not verilog\n", "non-Verilog files are not touched");
|
|
assert!(stderr(&out).contains("2 files reformatted"), "{}", stderr(&out));
|
|
|
|
// Second run is a no-op: nothing left to change.
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 0);
|
|
assert!(stderr(&out).contains("2 files left unchanged"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn check_reports_without_writing() {
|
|
let dir = TempDir::new("check");
|
|
let path = dir.write("a.sv", UNFORMATTED);
|
|
|
|
let out = run(&["--check", &path]);
|
|
assert_eq!(code(&out), 1, "would-change must exit 1");
|
|
assert!(stdout(&out).contains("would reformat:"), "{}", stdout(&out));
|
|
assert_eq!(dir.read("a.sv"), UNFORMATTED, "--check must not write");
|
|
|
|
dir.write("a.sv", FORMATTED);
|
|
let out = run(&["--check", &path]);
|
|
assert_eq!(code(&out), 0, "nothing to change must exit 0");
|
|
assert_eq!(stdout(&out), "");
|
|
}
|
|
|
|
#[test]
|
|
fn diff_prints_a_unified_diff_without_writing() {
|
|
let dir = TempDir::new("diff");
|
|
let path = dir.write("a.sv", UNFORMATTED);
|
|
|
|
let out = run(&["--diff", &path]);
|
|
assert_eq!(code(&out), 1, "would-change must exit 1");
|
|
assert_eq!(dir.read("a.sv"), UNFORMATTED, "--diff must not write");
|
|
|
|
let text = stdout(&out);
|
|
let lines: Vec<&str> = text.lines().collect();
|
|
assert_eq!(lines[0], format!("--- {path}"), "{text}");
|
|
assert_eq!(lines[1], format!("+++ {path}"), "{text}");
|
|
assert!(lines[2].starts_with("@@ ") && lines[2].ends_with(" @@"), "{text}");
|
|
assert!(lines.contains(&"-module m;endmodule"), "{text}");
|
|
assert!(lines.contains(&"+module m;"), "{text}");
|
|
assert!(lines.contains(&"+endmodule"), "{text}");
|
|
// The summary goes to stderr so stdout is a clean patch.
|
|
assert!(stderr(&out).contains("would be reformatted"), "{}", stderr(&out));
|
|
|
|
dir.write("a.sv", FORMATTED);
|
|
let out = run(&["--diff", &path]);
|
|
assert_eq!(code(&out), 0);
|
|
assert_eq!(stdout(&out), "", "an unchanged file prints no diff");
|
|
}
|
|
|
|
#[test]
|
|
fn diff_conflicts_with_stdout() {
|
|
let dir = TempDir::new("diffconflict");
|
|
let path = dir.write("a.sv", UNFORMATTED);
|
|
let out = run(&["--diff", "--stdout", &path]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("--diff cannot be combined with --stdout"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn stdin_mode_uses_stdin_filename_in_errors() {
|
|
let out = run_with_stdin(&["-"], UNPARSEABLE);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).starts_with("error: <stdin>: "), "{}", stderr(&out));
|
|
|
|
let out = run_with_stdin(&["--stdin-filename", "rtl/top.sv", "-"], UNPARSEABLE);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).starts_with("error: rtl/top.sv: "), "{}", stderr(&out));
|
|
assert!(!stderr(&out).contains("<stdin>"), "{}", stderr(&out));
|
|
assert!(!std::path::Path::new("rtl/top.sv").exists(), "--stdin-filename must not touch disk");
|
|
|
|
// It also names the buffer in --diff and --check output.
|
|
let out = run_with_stdin(&["--diff", "--stdin-filename", "rtl/top.sv", "-"], UNFORMATTED);
|
|
assert_eq!(code(&out), 1);
|
|
assert!(stdout(&out).starts_with("--- rtl/top.sv\n+++ rtl/top.sv\n"), "{}", stdout(&out));
|
|
|
|
let out = run_with_stdin(&["--check", "--stdin-filename", "rtl/top.sv", "-"], UNFORMATTED);
|
|
assert_eq!(code(&out), 1);
|
|
assert_eq!(stdout(&out), "would reformat: rtl/top.sv\n");
|
|
}
|
|
|
|
#[test]
|
|
fn stdin_mode_formats_to_stdout() {
|
|
let out = run_with_stdin(&["-"], UNFORMATTED);
|
|
assert_eq!(code(&out), 0);
|
|
assert_eq!(stdout(&out), FORMATTED);
|
|
}
|
|
|
|
#[test]
|
|
fn exclude_skips_files_and_directories() {
|
|
let dir = TempDir::new("exclude");
|
|
dir.write("keep.sv", UNFORMATTED);
|
|
dir.write("skip_gen.sv", UNFORMATTED);
|
|
dir.write("build/inner.sv", UNFORMATTED);
|
|
dir.write("sub/keep.sv", UNFORMATTED);
|
|
|
|
let out = run(&["--exclude", "*_gen.sv", "--exclude", "build", &dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("keep.sv"), FORMATTED);
|
|
assert_eq!(dir.read("sub/keep.sv"), FORMATTED);
|
|
assert_eq!(dir.read("skip_gen.sv"), UNFORMATTED, "name glob must be excluded");
|
|
assert_eq!(dir.read("build/inner.sv"), UNFORMATTED, "excluded dir is not descended into");
|
|
assert!(stderr(&out).contains("2 files reformatted"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn exclude_matches_a_path_suffix_when_it_contains_a_slash() {
|
|
let dir = TempDir::new("excludepath");
|
|
dir.write("gen/rtl/a.sv", UNFORMATTED);
|
|
dir.write("gen/hand/b.sv", UNFORMATTED);
|
|
|
|
let out = run(&["--exclude", "gen/rtl", &dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("gen/rtl/a.sv"), UNFORMATTED);
|
|
assert_eq!(dir.read("gen/hand/b.sv"), FORMATTED);
|
|
}
|
|
|
|
#[test]
|
|
fn exclude_does_not_apply_to_explicitly_named_files() {
|
|
let dir = TempDir::new("excludeexplicit");
|
|
let path = dir.write("skip_gen.sv", UNFORMATTED);
|
|
|
|
let out = run(&["--exclude", "*_gen.sv", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("skip_gen.sv"), FORMATTED);
|
|
}
|
|
|
|
#[test]
|
|
fn unformattable_file_exits_2_and_is_left_untouched() {
|
|
let dir = TempDir::new("badparse");
|
|
dir.write("bad.sv", UNPARSEABLE);
|
|
dir.write("good.sv", UNFORMATTED);
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 2, "an error must exit 2");
|
|
assert_eq!(dir.read("bad.sv"), UNPARSEABLE, "a file that fails to format is not rewritten");
|
|
assert_eq!(dir.read("good.sv"), FORMATTED, "other files are still formatted");
|
|
let err = stderr(&out);
|
|
assert!(err.contains("bad.sv: parse error"), "{err}");
|
|
assert!(err.contains("1 file failed to format"), "{err}");
|
|
|
|
// An error outranks "would reformat" in --check and --diff too.
|
|
dir.write("good.sv", UNFORMATTED);
|
|
assert_eq!(code(&run(&["--check", &dir.root()])), 2);
|
|
assert_eq!(code(&run(&["--diff", &dir.root()])), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn usage_errors_exit_2() {
|
|
assert_eq!(code(&run(&[])), 2, "no inputs");
|
|
assert_eq!(code(&run(&["--nope", "x.sv"])), 2, "unknown option");
|
|
assert_eq!(code(&run(&["--exclude"])), 2, "missing option value");
|
|
assert_eq!(code(&run(&["/definitely/not/here.sv"])), 2, "missing input file");
|
|
}
|
|
|
|
#[test]
|
|
fn help_documents_the_exit_codes() {
|
|
let out = run(&["--help"]);
|
|
assert_eq!(code(&out), 0);
|
|
let text = stdout(&out);
|
|
assert!(text.contains("EXIT CODES:"), "{text}");
|
|
for flag in ["--check", "--diff", "--exclude", "--stdin-filename"] {
|
|
assert!(text.contains(flag), "help is missing {flag}");
|
|
}
|
|
}
|