class-semantics: classes are first-class in vlog_semantic -- class
scopes and symbols, member/method/prototype binding, this.x
(nearest-class-only), super.x, cls::x with lexical unit heads,
out-of-body cls::method bound into the class's scope with the extern
prototype as the definition (rule 10), and the honest
InheritedCandidate{base, paths} shape: a member miss in an extending
class records what the unknown base might supply (import paths
included) and is never chased, not even in-file -- the future
inheritance round's worklist, measured at 1303 candidates. Unit-name
misses stay ExternalCandidate (QA-found, lead-ruled: bases supply
members, not package names; +170 recovered navigations, pinned).
Workspace index gains classes as units (246) and their members
(11399). scan.rs migrated to parser name-spans: 26 call sites down
to the 2 documented consumers. 114044 spans, 0 unplaced, 0 panics;
UVM code now has hover/definition/references/outline.
lexer-cleanup: the three filed gaps -- @(* ) lexes via a
starts_attribute backoff (an attribute cannot begin with `)`; real
attributes pinned, no misfire on legal expressions), a leading BOM
is accepted as a named losslessness exception (mid-file BOM errors
clearly; 20k-case position fuzz clean), and non-ASCII lex errors now
decode the full character AND surface through vfmt check's error
slot (exit 2, matching format, text-rule findings still reported --
the silent-pass hole closed). The macro space-before-paren item is
parked with a QA-hardened analysis: the reading depends on a
definition vfmt cannot see, any fixed answer is provably wrong for
one population, and today's behavior fails safe.
827 workspace tests. Dev: 2 Fable agents; QA: 2 Opus agents; reviews
in docs/features/{class-semantics,lexer-cleanup}/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
1514 lines
61 KiB
Rust
1514 lines
61 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";
|
|
/// Formats to `INDENT_2` or `INDENT_4` depending on the `indent` setting.
|
|
const UNINDENTED: &str = "module m;\nwire x;\nendmodule\n";
|
|
const INDENT_2: &str = "module m;\n wire x;\nendmodule\n";
|
|
const INDENT_4: &str = "module m;\n wire x;\nendmodule\n";
|
|
/// 44 columns: one line under the default 100, split under `line-length = 40`.
|
|
const WIDE: &str = "module m (input wire aaaa, input wire bbbb);\nendmodule\n";
|
|
const WIDE_SPLIT: &str = "module m (\n input wire aaaa,\n input wire bbbb\n);\nendmodule\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);
|
|
}
|
|
}
|
|
|
|
/// Runs vfmt with the cache switched off. Every test here shares one
|
|
/// working directory (the crate's), so a default-on cache would put a
|
|
/// `.vfmt_cache/` in the source tree *and* let one test's run change what
|
|
/// another observes. The cache tests opt back in with `run_raw` and their
|
|
/// own `--cache-dir`.
|
|
fn run(args: &[&str]) -> Output {
|
|
run_raw(&with_no_cache(args))
|
|
}
|
|
|
|
/// `args` with `--no-cache` inserted after the subcommand, if there is one,
|
|
/// so that everything the caller wrote keeps its exact position (a test that
|
|
/// checks `--exclude` with no value must still find no value).
|
|
fn with_no_cache<'a>(args: &[&'a str]) -> Vec<&'a str> {
|
|
let mut args = args.to_vec();
|
|
let at = usize::from(matches!(args.first(), Some(&"check" | &"format")));
|
|
args.insert(at, "--no-cache");
|
|
args
|
|
}
|
|
|
|
/// Runs vfmt exactly as given, cache and all.
|
|
fn run_raw(args: &[&str]) -> Output {
|
|
Command::new(VFMT).args(args).output().expect("run vfmt")
|
|
}
|
|
|
|
/// Runs vfmt exactly as given, from `dir` — the cache's default location
|
|
/// is relative to the working directory.
|
|
fn run_in(dir: &TempDir, args: &[&str]) -> Output {
|
|
Command::new(VFMT).args(args).current_dir(&dir.path).output().expect("run vfmt")
|
|
}
|
|
|
|
fn run_with_stdin(args: &[&str], input: &str) -> Output {
|
|
let mut child = Command::new(VFMT)
|
|
.args(with_no_cache(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 deeply_nested_file_does_not_kill_the_batch() {
|
|
// Before `vlog_parser`'s depth limit this input overflowed the stack and
|
|
// aborted the process, so files sorted after it were never processed —
|
|
// and a stack overflow is not a panic, so `catch_unwind` cannot help.
|
|
let deep = format!(
|
|
"module m;\nalways @(posedge clk) begin\n{}x <= 1;\n{}end\nendmodule\n",
|
|
"if (clk) begin\n".repeat(150),
|
|
"end\n".repeat(150)
|
|
);
|
|
// A postfix chain is built by a loop, not by recursion: it costs the
|
|
// parser no depth but hands the formatter an equally deep AST, which
|
|
// crashed it the same way until chains got a budget of their own.
|
|
let chain = format!("module m;\nassign x = a{};\nendmodule\n", ".b".repeat(1500));
|
|
let dir = TempDir::new("deep");
|
|
dir.write("a_before.sv", UNFORMATTED);
|
|
dir.write("b_deep.sv", &deep);
|
|
dir.write("c_chain.sv", &chain);
|
|
dir.write("d_after.sv", UNFORMATTED);
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 2, "a file that cannot be parsed exits 2");
|
|
assert_eq!(dir.read("b_deep.sv"), deep, "the offending file is left untouched");
|
|
assert_eq!(dir.read("c_chain.sv"), chain, "the offending file is left untouched");
|
|
assert_eq!(dir.read("a_before.sv"), FORMATTED);
|
|
assert_eq!(dir.read("d_after.sv"), FORMATTED, "files after the bad ones are still processed");
|
|
let err = stderr(&out);
|
|
assert!(err.contains("b_deep.sv: parse error"), "{err}");
|
|
assert!(err.contains("nesting too deep"), "{err}");
|
|
assert!(err.contains("c_chain.sv: parse error"), "{err}");
|
|
assert!(err.contains("expression chain too long"), "{err}");
|
|
assert!(err.contains("2 files reformatted"), "{err}");
|
|
assert!(err.contains("2 files failed to format"), "{err}");
|
|
}
|
|
|
|
/// A tree wide enough that the pool actually splits it up, with a mix of
|
|
/// formatted, unformatted and broken files so every counter is exercised.
|
|
fn mixed_tree(dir: &TempDir) {
|
|
for n in 0..24 {
|
|
dir.write(&format!("wide/f{n:02}.sv"), UNFORMATTED);
|
|
dir.write(&format!("deep/a/b/c/g{n:02}.v"), FORMATTED);
|
|
}
|
|
dir.write("bad/x.sv", UNPARSEABLE);
|
|
dir.write("bad/y.sv", UNPARSEABLE);
|
|
}
|
|
|
|
#[test]
|
|
fn thread_count_changes_nothing_but_speed() {
|
|
// The determinism bar: the same invocation must produce byte-identical
|
|
// stdout, stderr and exit code however many workers ran it.
|
|
for mode in [&["--check"][..], &["--diff"][..]] {
|
|
let dir = TempDir::new("threads");
|
|
mixed_tree(&dir);
|
|
let root = dir.root();
|
|
|
|
let mut args = mode.to_vec();
|
|
args.push(&root);
|
|
let parallel = run(&args);
|
|
let mut serial_args = vec!["--threads", "1"];
|
|
serial_args.extend_from_slice(mode);
|
|
serial_args.push(&root);
|
|
let serial = run(&serial_args);
|
|
|
|
assert_eq!(code(¶llel), code(&serial), "{mode:?}");
|
|
assert_eq!(parallel.stdout, serial.stdout, "{mode:?}: stdout must not depend on threads");
|
|
assert_eq!(parallel.stderr, serial.stderr, "{mode:?}: stderr must not depend on threads");
|
|
assert_eq!(code(¶llel), 2, "the broken files win over would-reformat");
|
|
assert!(stderr(¶llel).contains("2 files failed to format"), "{}", stderr(¶llel));
|
|
|
|
// And a second parallel run of the same thing is byte-identical too.
|
|
let again = run(&args);
|
|
assert_eq!(parallel.stdout, again.stdout, "{mode:?}: two parallel runs must agree");
|
|
assert_eq!(parallel.stderr, again.stderr, "{mode:?}: two parallel runs must agree");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn writing_in_parallel_matches_writing_serially() {
|
|
let parallel = TempDir::new("writepar");
|
|
let serial = TempDir::new("writeser");
|
|
mixed_tree(¶llel);
|
|
mixed_tree(&serial);
|
|
|
|
let a = run(&[¶llel.root()]);
|
|
let b = run(&["--threads", "1", &serial.root()]);
|
|
assert_eq!(code(&a), 2);
|
|
assert_eq!(code(&b), 2);
|
|
// Only the summary line can be compared verbatim: the error lines name
|
|
// the two (necessarily different) temporary directories.
|
|
let summary = |out: &Output| stderr(out).lines().last().unwrap_or_default().to_string();
|
|
assert_eq!(summary(&a), summary(&b), "the summary must not depend on the thread count");
|
|
assert_eq!(
|
|
summary(&a),
|
|
"24 files reformatted, 24 files left unchanged, 2 files failed to format"
|
|
);
|
|
for n in 0..24 {
|
|
let f = format!("wide/f{n:02}.sv");
|
|
assert_eq!(parallel.read(&f), FORMATTED);
|
|
assert_eq!(parallel.read(&f), serial.read(&f));
|
|
}
|
|
assert_eq!(parallel.read("bad/x.sv"), UNPARSEABLE, "a broken file is left untouched");
|
|
}
|
|
|
|
#[test]
|
|
fn diff_over_many_files_comes_out_in_input_order() {
|
|
let dir = TempDir::new("difforder");
|
|
let names = ["a.sv", "b.sv", "c.sv", "d/e.sv", "d/f.sv", "g.sv"];
|
|
for name in names {
|
|
dir.write(name, UNFORMATTED);
|
|
}
|
|
|
|
let out = run(&["--diff", &dir.root()]);
|
|
assert_eq!(code(&out), 1, "{}", stderr(&out));
|
|
let text = stdout(&out);
|
|
let headers: Vec<&str> = text.lines().filter(|l| l.starts_with("--- ")).collect();
|
|
let expected: Vec<String> =
|
|
names.iter().map(|n| format!("--- {}/{n}", dir.root())).collect::<Vec<_>>();
|
|
// `d/` sorts after `c.sv` and before `g.sv`, matching the walk order.
|
|
let mut expected: Vec<&str> = expected.iter().map(String::as_str).collect();
|
|
expected.sort();
|
|
assert_eq!(headers, expected, "{text}");
|
|
// Each file's hunks stay together: nothing interleaves.
|
|
assert_eq!(text.matches("@@ ").count(), names.len(), "{text}");
|
|
assert_eq!(stdout(&run(&["--threads", "1", "--diff", &dir.root()])), text);
|
|
}
|
|
|
|
#[test]
|
|
fn a_deeply_nested_file_formats_on_a_worker_thread() {
|
|
// Just under `vlog_parser::MAX_NESTING_DEPTH` (a nested `if … begin`
|
|
// costs two levels), so it parses — and needs several MiB of stack to
|
|
// do it in a debug build: without the depth limit this family died at
|
|
// 15 levels on a 2 MiB thread, which is exactly rayon's default worker
|
|
// stack. This test crashes the process if the pool's 8 MiB is dropped.
|
|
let deep = format!(
|
|
"module m;\nalways @(posedge clk) begin\n{}x <= 1;\n{}end\nendmodule\n",
|
|
"if (clk) begin\n".repeat(28),
|
|
"end\n".repeat(28)
|
|
);
|
|
let dir = TempDir::new("deeppool");
|
|
dir.write("deep.sv", &deep);
|
|
dir.write("plain.sv", UNFORMATTED);
|
|
|
|
let out = run(&["--check", &dir.root()]);
|
|
assert_eq!(code(&out), 1, "{}", stderr(&out));
|
|
let err = stderr(&out);
|
|
assert!(!err.contains("parse error"), "the input must be under the limit: {err}");
|
|
assert!(!err.contains("panicked"), "{err}");
|
|
assert!(stdout(&out).contains("deep.sv"), "{}", stdout(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn every_file_keeps_its_own_config_when_formatted_in_parallel() {
|
|
let dir = TempDir::new("configpar");
|
|
dir.write("vfmt.toml", "indent = 4\n");
|
|
dir.write("sub/vfmt.toml", "indent = 8\n");
|
|
dir.write("sub/deeper/vfmt.toml", "indent = 2\n");
|
|
for n in 0..12 {
|
|
dir.write(&format!("root{n:02}.sv"), UNINDENTED);
|
|
dir.write(&format!("sub/mid{n:02}.sv"), UNINDENTED);
|
|
dir.write(&format!("sub/deeper/low{n:02}.sv"), UNINDENTED);
|
|
}
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
for n in 0..12 {
|
|
assert_eq!(dir.read(&format!("root{n:02}.sv")), INDENT_4);
|
|
assert_eq!(
|
|
dir.read(&format!("sub/mid{n:02}.sv")),
|
|
"module m;\n wire x;\nendmodule\n"
|
|
);
|
|
assert_eq!(dir.read(&format!("sub/deeper/low{n:02}.sv")), INDENT_2);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn gitignore_is_honored_while_walking() {
|
|
let dir = TempDir::new("gitignore");
|
|
dir.write(".gitignore", "# generated\nbuild/\n*_gen.sv\n");
|
|
dir.write("keep.sv", UNFORMATTED);
|
|
dir.write("skip_gen.sv", UNFORMATTED);
|
|
dir.write("build/inner.sv", UNFORMATTED);
|
|
dir.write("sub/deep_gen.sv", UNFORMATTED);
|
|
dir.write("sub/keep.sv", UNFORMATTED);
|
|
|
|
let out = run(&[&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, "a name pattern ignores at any depth");
|
|
assert_eq!(dir.read("sub/deep_gen.sv"), UNFORMATTED);
|
|
assert_eq!(dir.read("build/inner.sv"), UNFORMATTED, "an ignored directory is not entered");
|
|
assert!(stderr(&out).contains("2 files reformatted"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_nested_gitignore_overrides_the_one_above_it() {
|
|
let dir = TempDir::new("gitignorenested");
|
|
dir.write(".gitignore", "*_gen.sv\n/anchored.sv\n");
|
|
dir.write("sub/.gitignore", "!keep_gen.sv\ndrop.sv\n");
|
|
dir.write("anchored.sv", UNFORMATTED);
|
|
dir.write("sub/anchored.sv", UNFORMATTED);
|
|
dir.write("sub/keep_gen.sv", UNFORMATTED);
|
|
dir.write("sub/other_gen.sv", UNFORMATTED);
|
|
dir.write("sub/drop.sv", UNFORMATTED);
|
|
dir.write("drop.sv", UNFORMATTED);
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("anchored.sv"), UNFORMATTED, "a leading slash anchors to the root");
|
|
assert_eq!(dir.read("sub/anchored.sv"), FORMATTED, "…and does not reach into a subdirectory");
|
|
assert_eq!(dir.read("sub/keep_gen.sv"), FORMATTED, "the deeper `!` rule wins");
|
|
assert_eq!(dir.read("sub/other_gen.sv"), UNFORMATTED, "the outer rule still applies");
|
|
assert_eq!(dir.read("sub/drop.sv"), UNFORMATTED, "the deeper file's own rule applies");
|
|
assert_eq!(dir.read("drop.sv"), FORMATTED, "a deeper rule does not apply above itself");
|
|
}
|
|
|
|
#[test]
|
|
fn no_gitignore_switches_the_whole_thing_off() {
|
|
let dir = TempDir::new("nogitignore");
|
|
dir.write(".gitignore", "build/\n*_gen.sv\n");
|
|
dir.write("keep.sv", UNFORMATTED);
|
|
dir.write("skip_gen.sv", UNFORMATTED);
|
|
dir.write("build/inner.sv", UNFORMATTED);
|
|
|
|
let out = run(&["--no-gitignore", &dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("keep.sv"), FORMATTED);
|
|
assert_eq!(dir.read("skip_gen.sv"), FORMATTED);
|
|
assert_eq!(dir.read("build/inner.sv"), FORMATTED);
|
|
}
|
|
|
|
#[test]
|
|
fn gitignore_does_not_apply_to_explicitly_named_paths() {
|
|
let dir = TempDir::new("gitignoreexplicit");
|
|
dir.write(".gitignore", "*_gen.sv\nbuild/\n");
|
|
let named = dir.write("skip_gen.sv", UNFORMATTED);
|
|
let inside = dir.write("build/inner.sv", UNFORMATTED);
|
|
|
|
let out = run(&[&named, &inside]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("skip_gen.sv"), FORMATTED, "a path on the command line is never ignored");
|
|
assert_eq!(dir.read("build/inner.sv"), FORMATTED);
|
|
}
|
|
|
|
#[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}");
|
|
assert!(text.contains("CONFIGURATION:"), "{text}");
|
|
assert!(text.contains("IGNORE FILES:"), "{text}");
|
|
for flag in [
|
|
"--check",
|
|
"--diff",
|
|
"--exclude",
|
|
"--stdin-filename",
|
|
"--no-config",
|
|
"--no-gitignore",
|
|
"--threads",
|
|
] {
|
|
assert!(text.contains(flag), "help is missing {flag}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_config_file_is_found_by_searching_upward() {
|
|
let dir = TempDir::new("configup");
|
|
dir.write("vfmt.toml", "indent = 4\nline-length = 40\n");
|
|
dir.write("a/b/x.sv", UNINDENTED);
|
|
dir.write("a/b/wide.sv", WIDE);
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("a/b/x.sv"), INDENT_4);
|
|
// Both keys took effect: split by `line-length`, indented by `indent`.
|
|
assert_eq!(
|
|
dir.read("a/b/wide.sv"),
|
|
"module m (\n input wire aaaa,\n input wire bbbb\n);\nendmodule\n"
|
|
);
|
|
// Both names work; `.vfmt.toml` is the fallback within one directory.
|
|
dir.write("a/b/x.sv", UNINDENTED);
|
|
std::fs::rename(dir.path.join("vfmt.toml"), dir.path.join(".vfmt.toml")).expect("rename");
|
|
assert_eq!(code(&run(&[&dir.root()])), 0);
|
|
assert_eq!(dir.read("a/b/x.sv"), INDENT_4);
|
|
}
|
|
|
|
#[test]
|
|
fn each_file_gets_the_config_of_its_own_directory() {
|
|
let dir = TempDir::new("configper");
|
|
dir.write("vfmt.toml", "indent = 4\n");
|
|
dir.write("root.sv", UNINDENTED);
|
|
dir.write("sub/vfmt.toml", "indent = 8\n");
|
|
dir.write("sub/deep/inner.sv", UNINDENTED);
|
|
dir.write("other/plain.sv", UNINDENTED);
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("root.sv"), INDENT_4);
|
|
assert_eq!(dir.read("sub/deep/inner.sv"), "module m;\n wire x;\nendmodule\n");
|
|
assert_eq!(dir.read("other/plain.sv"), INDENT_4, "the nearest config is the root's");
|
|
|
|
// --check uses the same per-file settings and its summary is unchanged:
|
|
// everything is now formatted to its own directory's config.
|
|
let out = run(&["--check", &dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert!(stderr(&out).contains("3 files left unchanged"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_cli_flag_overrides_the_config_file() {
|
|
let dir = TempDir::new("configcli");
|
|
dir.write("vfmt.toml", "indent = 4\nline-length = 40\n");
|
|
dir.write("x.sv", UNINDENTED);
|
|
dir.write("wide.sv", WIDE);
|
|
|
|
let out = run(&["--indent", "2", &dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("x.sv"), INDENT_2, "--indent wins over the file");
|
|
assert_eq!(dir.read("wide.sv"), WIDE_SPLIT, "line-length is still the file's");
|
|
}
|
|
|
|
#[test]
|
|
fn no_config_ignores_config_files_entirely() {
|
|
let dir = TempDir::new("noconfig");
|
|
// Not even parsed: --no-config reads nothing from disk.
|
|
dir.write("vfmt.toml", "this file is not valid at all\n");
|
|
dir.write("x.sv", UNINDENTED);
|
|
|
|
let out = run(&["--no-config", &dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("x.sv"), INDENT_2, "defaults apply");
|
|
}
|
|
|
|
#[test]
|
|
fn a_broken_config_exits_2_before_writing_anything() {
|
|
let dir = TempDir::new("configbad");
|
|
dir.write("vfmt.toml", "indent = 4\nline_length = 80\n");
|
|
dir.write("x.sv", UNINDENTED);
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 2, "a config error is an error, not a warning");
|
|
assert_eq!(dir.read("x.sv"), UNINDENTED, "nothing is written when the config is broken");
|
|
let err = stderr(&out);
|
|
assert!(err.contains("vfmt.toml:2:"), "the error names the file and line: {err}");
|
|
assert!(err.contains("unknown key `line_length`"), "{err}");
|
|
|
|
for (contents, expected) in [
|
|
("indent = 99\n", "between 1 and 8"),
|
|
("line-length = wide\n", "expects an integer"),
|
|
("[tool.vfmt]\nindent = 2\n", "tables"),
|
|
("exclude = [\n \"a\",\n]\n", "multi-line arrays are unsupported"),
|
|
] {
|
|
dir.write("vfmt.toml", contents);
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 2, "{contents}");
|
|
assert!(stderr(&out).contains(expected), "{contents}: {}", stderr(&out));
|
|
assert_eq!(dir.read("x.sv"), UNINDENTED);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn config_excludes_are_added_to_cli_excludes() {
|
|
let dir = TempDir::new("configexclude");
|
|
dir.write("vfmt.toml", "exclude = [ \"*_gen.sv\", \"build\" ] # generated\n");
|
|
dir.write("keep.sv", UNINDENTED);
|
|
dir.write("skip_gen.sv", UNINDENTED);
|
|
dir.write("build/inner.sv", UNINDENTED);
|
|
dir.write("vendor/v.sv", UNINDENTED);
|
|
|
|
let out = run(&["--exclude", "vendor", &dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("keep.sv"), INDENT_2);
|
|
assert_eq!(dir.read("skip_gen.sv"), UNINDENTED, "config exclude applies");
|
|
assert_eq!(dir.read("build/inner.sv"), UNINDENTED, "config exclude applies to dirs");
|
|
assert_eq!(dir.read("vendor/v.sv"), UNINDENTED, "--exclude still applies");
|
|
}
|
|
|
|
#[test]
|
|
fn stdin_discovers_config_from_stdin_filename() {
|
|
let dir = TempDir::new("configstdin");
|
|
dir.write("vfmt.toml", "indent = 4\n");
|
|
let buffer = format!("{}/rtl/buffer.sv", dir.root());
|
|
|
|
let out = run_with_stdin(&["--stdin-filename", &buffer, "-"], UNINDENTED);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(stdout(&out), INDENT_4);
|
|
assert!(!std::path::Path::new(&buffer).exists(), "--stdin-filename must not touch disk");
|
|
|
|
// Without the flag the anchor is the cwd, which has no config here.
|
|
let out = run_with_stdin(&["-"], UNINDENTED);
|
|
assert_eq!(code(&out), 0);
|
|
assert_eq!(stdout(&out), INDENT_2);
|
|
|
|
let out = run_with_stdin(&["--no-config", "--stdin-filename", &buffer, "-"], UNINDENTED);
|
|
assert_eq!(code(&out), 0);
|
|
assert_eq!(stdout(&out), INDENT_2, "--no-config applies to stdin too");
|
|
|
|
// A broken config is an error in stdin mode too, and nothing is echoed.
|
|
dir.write("vfmt.toml", "indent = 4\nbogus = 1\n");
|
|
let out = run_with_stdin(&["--stdin-filename", &buffer, "-"], UNINDENTED);
|
|
assert_eq!(code(&out), 2);
|
|
assert_eq!(stdout(&out), "", "no output when the config is broken");
|
|
assert!(stderr(&out).contains("unknown key `bogus`"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn the_search_stops_at_a_git_directory_after_checking_it() {
|
|
let dir = TempDir::new("configgit");
|
|
dir.write("vfmt.toml", "indent = 4\n");
|
|
std::fs::create_dir_all(dir.path.join("repo/.git")).expect("create .git");
|
|
dir.write("repo/rtl/x.sv", UNINDENTED);
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("repo/rtl/x.sv"), INDENT_2, "the walk stops at the repo root");
|
|
|
|
// A config file *in* the repo root is still found: `.git` only stops
|
|
// the walk after its own directory has been checked.
|
|
dir.write("repo/rtl/x.sv", UNINDENTED);
|
|
dir.write("repo/vfmt.toml", "indent = 8\n");
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("repo/rtl/x.sv"), "module m;\n wire x;\nendmodule\n");
|
|
}
|
|
|
|
// -------------------------------------------------------------- vfmt check
|
|
|
|
/// Two findings: a tab on line 2 and a module/file-name mismatch.
|
|
const LINTY: &str = "module foo;\n\twire a;\nendmodule\n";
|
|
/// The same file with the tab expanded — `no-tabs` clean.
|
|
const LINTY_FIXED: &str = "module foo;\n wire a;\nendmodule\n";
|
|
|
|
#[test]
|
|
fn check_reports_violations_and_exits_1() {
|
|
let dir = TempDir::new("check-lint");
|
|
let path = dir.write("bar.sv", LINTY);
|
|
|
|
let out = run(&["check", &path]);
|
|
assert_eq!(code(&out), 1, "violations must exit 1: {}", stderr(&out));
|
|
let text = stdout(&out);
|
|
assert!(text.contains(&format!("{path}:2:1: tab character")), "{text}");
|
|
assert!(text.contains("[no-tabs]"), "{text}");
|
|
assert!(text.contains("[module-filename]"), "{text}");
|
|
assert!(stderr(&out).contains("found 2 violations"), "{}", stderr(&out));
|
|
assert_eq!(dir.read("bar.sv"), LINTY, "check must not write");
|
|
}
|
|
|
|
#[test]
|
|
fn check_exits_0_on_a_clean_file() {
|
|
let dir = TempDir::new("check-clean");
|
|
let path = dir.write("foo.sv", LINTY_FIXED);
|
|
|
|
let out = run(&["check", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(stdout(&out), "");
|
|
}
|
|
|
|
#[test]
|
|
fn check_diagnostics_are_sorted_by_file_then_position() {
|
|
let dir = TempDir::new("check-order");
|
|
dir.write("b.sv", LINTY);
|
|
dir.write("a.sv", LINTY);
|
|
|
|
let out = run(&["check", &dir.root()]);
|
|
let text = stdout(&out);
|
|
let lines: Vec<&str> = text.lines().collect();
|
|
assert_eq!(lines.len(), 4, "{lines:?}");
|
|
assert!(lines[0].contains("a.sv:1:"), "{lines:?}");
|
|
assert!(lines[1].contains("a.sv:2:"), "{lines:?}");
|
|
assert!(lines[2].contains("b.sv:1:"), "{lines:?}");
|
|
assert!(lines[3].contains("b.sv:2:"), "{lines:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn check_json_is_one_array_with_positions_and_fixes() {
|
|
let dir = TempDir::new("check-json");
|
|
let path = dir.write("foo.sv", LINTY);
|
|
|
|
let out = run(&["check", "--output-format", "json", "--select", "no-tabs", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
let text = stdout(&out);
|
|
assert!(text.starts_with('['), "{text}");
|
|
assert!(text.trim_end().ends_with(']'), "{text}");
|
|
assert_eq!(text.matches("\"rule\":\"no-tabs\"").count(), 1, "{text}");
|
|
assert!(text.contains("\"location\":{\"line\":2,\"column\":1}"), "{text}");
|
|
assert!(text.contains("\"applicability\":\"safe\""), "{text}");
|
|
assert!(text.contains("\"content\":\" \""), "{text}");
|
|
|
|
// A clean run still emits a parsable, empty array.
|
|
dir.write("foo.sv", LINTY_FIXED);
|
|
let out = run(&["check", "--output-format", "json", "--select", "no-tabs", &path]);
|
|
assert_eq!(code(&out), 0);
|
|
assert_eq!(stdout(&out).trim_end(), "[]");
|
|
}
|
|
|
|
#[test]
|
|
fn check_fix_rewrites_the_file_and_the_result_relints_clean() {
|
|
let dir = TempDir::new("check-fix");
|
|
let path = dir.write("foo.sv", "module foo;\n\twire a; \nendmodule\n");
|
|
|
|
let out = run(&["check", "--fix", &path]);
|
|
assert_eq!(code(&out), 0, "everything fixable was fixed: {}", stderr(&out));
|
|
assert_eq!(dir.read("foo.sv"), LINTY_FIXED, "the tab and the trailing run are both gone");
|
|
assert!(stderr(&out).contains("fixed 2 violations"), "{}", stderr(&out));
|
|
|
|
// The fixed file is clean for the rules that had fixes, and a second
|
|
// --fix run has nothing left to do.
|
|
let out = run(&["check", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stdout(&out));
|
|
let out = run(&["check", "--fix", &path]);
|
|
assert_eq!(code(&out), 0);
|
|
assert_eq!(dir.read("foo.sv"), LINTY_FIXED);
|
|
}
|
|
|
|
#[test]
|
|
fn check_fix_leaves_unfixable_findings_reported() {
|
|
let dir = TempDir::new("check-fix-left");
|
|
let path = dir.write("bar.sv", LINTY);
|
|
|
|
let out = run(&["check", "--fix", &path]);
|
|
assert_eq!(code(&out), 1, "module-filename has no fix, so something is left");
|
|
assert_eq!(dir.read("bar.sv"), LINTY_FIXED);
|
|
assert!(stdout(&out).contains("[module-filename]"), "{}", stdout(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn check_reports_a_file_that_does_not_lex_instead_of_passing_it() {
|
|
// A non-ASCII identifier fails to lex, so token- and AST-level rules see
|
|
// nothing. That must be a loud error (exit 2), never a silent exit 0 —
|
|
// the file was not actually checked.
|
|
let dir = TempDir::new("check-nolex");
|
|
let path = dir.write("bad.sv", "module bad;\n parameter int élan = 1;\nendmodule\n");
|
|
|
|
let out = run(&["check", &path]);
|
|
assert_eq!(code(&out), 2, "a file that does not lex is an error: {}", stderr(&out));
|
|
let err = stderr(&out);
|
|
assert!(err.contains("lex error"), "{err}");
|
|
assert!(err.contains("character 'é' (U+00E9) is not valid in Verilog source"), "{err}");
|
|
assert!(err.contains("2:17"), "the position names the character: {err}");
|
|
assert!(err.contains("could not be checked"), "{err}");
|
|
|
|
// Text-level findings on the same file are still reported alongside.
|
|
dir.write("bad.sv", "module bad;\n\tparameter int élan = 1;\nendmodule\n");
|
|
let out = run(&["check", "--select", "no-tabs", &path]);
|
|
assert_eq!(code(&out), 2, "the error outranks the violation exit");
|
|
assert!(stdout(&out).contains("[no-tabs]"), "{}", stdout(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_bom_prefixed_file_formats_and_checks_like_a_plain_one() {
|
|
// The BOM is accepted like leading whitespace and not preserved: the
|
|
// formatter writes the file back without it.
|
|
let dir = TempDir::new("bom");
|
|
let path = dir.write("m.sv", "\u{feff}module m;endmodule\n");
|
|
|
|
let out = run(&["check", &path]);
|
|
assert_eq!(code(&out), 0, "a BOM'd file checks clean: {}", stderr(&out));
|
|
assert_eq!(stdout(&out), "", "{}", stdout(&out));
|
|
|
|
let out = run(&["format", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("m.sv"), FORMATTED, "formatted, BOM dropped");
|
|
}
|
|
|
|
#[test]
|
|
fn check_select_and_ignore_pick_the_rule_set() {
|
|
let dir = TempDir::new("check-select");
|
|
let path = dir.write("bar.sv", LINTY);
|
|
|
|
let out = run(&["check", "--select", "no-tabs", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
assert_eq!(stdout(&out).lines().count(), 1, "{}", stdout(&out));
|
|
assert!(stdout(&out).contains("[no-tabs]"));
|
|
|
|
let out = run(&["check", "--ignore", "no-tabs,module-filename", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stdout(&out));
|
|
|
|
let out = run(&["check", "--select", "no-such-rule", &path]);
|
|
assert_eq!(code(&out), 2, "an unknown rule is an error, not a silent no-op");
|
|
assert!(stderr(&out).contains("unknown rule"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn check_honors_inline_waivers_and_reports_dead_ones() {
|
|
let dir = TempDir::new("check-waive");
|
|
let path = dir.write("foo.sv", "module foo;\n\twire a; // vfmt: waive no-tabs\nendmodule\n");
|
|
let out = run(&["check", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stdout(&out));
|
|
|
|
dir.write("foo.sv", "module foo;\n wire a; // verilog_lint: waive no-tabs\nendmodule\n");
|
|
let out = run(&["check", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
assert!(stdout(&out).contains("[unused-waiver]"), "{}", stdout(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn check_reads_stdin_and_names_it() {
|
|
let out = run_with_stdin(&["check", "--stdin-filename", "rtl/z.sv", "-"], LINTY);
|
|
assert_eq!(code(&out), 1);
|
|
assert!(stdout(&out).contains("rtl/z.sv:2:1:"), "{}", stdout(&out));
|
|
|
|
let out = run_with_stdin(&["check", "-"], LINTY);
|
|
assert_eq!(code(&out), 1);
|
|
assert!(stdout(&out).contains("<stdin>:2:1:"), "{}", stdout(&out));
|
|
assert!(
|
|
!stdout(&out).contains("module-filename"),
|
|
"a rule that needs a file name stays quiet: {}",
|
|
stdout(&out)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn check_reuses_the_walk_and_its_exclusions() {
|
|
let dir = TempDir::new("check-walk");
|
|
dir.write("keep.sv", LINTY);
|
|
dir.write("skip_gen.sv", LINTY);
|
|
dir.write("notes.txt", "\tnot verilog\n");
|
|
|
|
let out = run(&["check", "--exclude", "*_gen.sv", &dir.root()]);
|
|
assert_eq!(code(&out), 1);
|
|
let text = stdout(&out);
|
|
assert!(text.contains("keep.sv"), "{text}");
|
|
assert!(!text.contains("skip_gen.sv"), "--exclude applies to check: {text}");
|
|
assert!(!text.contains("notes.txt"), "only Verilog files are linted: {text}");
|
|
}
|
|
|
|
#[test]
|
|
fn check_expands_tabs_to_the_configured_indent() {
|
|
let dir = TempDir::new("check-indent");
|
|
dir.write("vfmt.toml", "indent = 4\n");
|
|
let path = dir.write("foo.sv", "module foo;\n\twire a;\nendmodule\n");
|
|
|
|
let out = run(&["check", "--fix", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("foo.sv"), "module foo;\n wire a;\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn check_reports_an_unreadable_file_as_an_error() {
|
|
let dir = TempDir::new("check-missing");
|
|
let missing = format!("{}/nope.sv", dir.root());
|
|
let out = run(&["check", &missing]);
|
|
assert_eq!(code(&out), 2);
|
|
}
|
|
|
|
// ------------------------------------------------- subcommand compatibility
|
|
|
|
#[test]
|
|
fn a_bare_invocation_still_formats() {
|
|
let dir = TempDir::new("compat-bare");
|
|
let path = dir.write("a.sv", UNFORMATTED);
|
|
|
|
let out = run(&[&path]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("a.sv"), FORMATTED);
|
|
|
|
// And every pre-subcommand flag combination behaves as it always did.
|
|
dir.write("a.sv", UNFORMATTED);
|
|
let out = run(&["--check", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
assert!(stdout(&out).contains("would reformat:"));
|
|
assert_eq!(dir.read("a.sv"), UNFORMATTED);
|
|
}
|
|
|
|
#[test]
|
|
fn the_format_subcommand_is_an_alias_for_the_bare_invocation() {
|
|
let dir = TempDir::new("compat-alias");
|
|
let path = dir.write("a.sv", UNFORMATTED);
|
|
|
|
let bare = run(&["--check", &path]);
|
|
let alias = run(&["format", "--check", &path]);
|
|
assert_eq!(code(&bare), code(&alias));
|
|
assert_eq!(stdout(&bare), stdout(&alias));
|
|
assert_eq!(stderr(&bare), stderr(&alias));
|
|
|
|
let out = run(&["format", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("a.sv"), FORMATTED);
|
|
}
|
|
|
|
#[test]
|
|
fn a_subcommand_is_only_read_as_the_first_argument() {
|
|
let dir = TempDir::new("compat-first");
|
|
// A file literally named `check`, formatted by naming it after `format`.
|
|
let path = dir.write("check", UNFORMATTED);
|
|
let out = run(&["format", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
|
|
// `--check check` is the old spelling and must still be the formatter.
|
|
dir.write("check", UNFORMATTED);
|
|
let out = run(&["--check", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
assert!(stdout(&out).contains("would reformat:"), "{}", stdout(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn flags_of_the_other_tool_are_rejected() {
|
|
let dir = TempDir::new("compat-flags");
|
|
let path = dir.write("a.sv", UNFORMATTED);
|
|
|
|
for args in [
|
|
vec!["--fix", &path],
|
|
vec!["--select", "no-tabs", &path],
|
|
vec!["--output-format", "json", &path],
|
|
] {
|
|
let out = run(&args);
|
|
assert_eq!(code(&out), 2, "{args:?} must not be accepted by the formatter");
|
|
assert!(stderr(&out).contains("only valid for `vfmt check`"), "{}", stderr(&out));
|
|
}
|
|
for flag in ["--check", "--diff", "--stdout"] {
|
|
let out = run(&["check", flag, &path]);
|
|
assert_eq!(code(&out), 2, "{flag} must not be accepted by the linter");
|
|
assert!(stderr(&out).contains("not valid for `vfmt check`"), "{}", stderr(&out));
|
|
}
|
|
let out = run(&["check", "--unsafe-fixes", &path]);
|
|
assert_eq!(code(&out), 2, "--unsafe-fixes without --fix is a mistake");
|
|
}
|
|
|
|
/// A fenced region (tab + trailing whitespace, deliberately hand-aligned) and
|
|
/// an unfenced one with the same problems. `vfmt format` emits the fenced
|
|
/// bytes verbatim; `vfmt check --fix` must do the same.
|
|
const FENCED: &str = "module foo;\n\
|
|
// vfmt: off\n\
|
|
\tlocalparam int A = 1; \n\
|
|
// vfmt: on\n\
|
|
\tlocalparam int B = 2; \n\
|
|
endmodule\n";
|
|
|
|
#[test]
|
|
fn check_reports_inside_a_fence_but_never_fixes_there() {
|
|
let dir = TempDir::new("check-fence");
|
|
let path = dir.write("foo.sv", FENCED);
|
|
|
|
// Reporting is unaffected: both lines violate both text rules.
|
|
let out = run(&["check", "--select", "no-tabs,no-trailing-whitespace", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
assert_eq!(stdout(&out).lines().count(), 4, "{}", stdout(&out));
|
|
|
|
let out = run(&["check", "--fix", "--select", "no-tabs,no-trailing-whitespace", &path]);
|
|
assert_eq!(code(&out), 1, "the fenced violations remain, so this is not a clean run");
|
|
|
|
let after = dir.read("foo.sv");
|
|
let (before_lines, after_lines): (Vec<&str>, Vec<&str>) =
|
|
(FENCED.lines().collect(), after.lines().collect());
|
|
assert_eq!(after_lines[2], before_lines[2], "the fenced line must be byte-identical");
|
|
assert!(after_lines[2].starts_with('\t'), "including its tab: {:?}", after_lines[2]);
|
|
assert!(after_lines[2].ends_with(" "), "and its trailing run: {:?}", after_lines[2]);
|
|
assert_eq!(after_lines[4], " localparam int B = 2;", "the unfenced line is still fixed");
|
|
|
|
// What is left over is exactly the two findings inside the fence.
|
|
let out = run(&["check", "--select", "no-tabs,no-trailing-whitespace", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
let text = stdout(&out);
|
|
assert_eq!(text.lines().count(), 2, "{text}");
|
|
assert!(text.lines().all(|l| l.contains(":3:")), "both point into the fence: {text}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_fence_holds_even_in_a_file_that_does_not_parse() {
|
|
let dir = TempDir::new("check-fence-broken");
|
|
let src = "module foo;\n @@@ not verilog;\n // vfmt: off\n\tlocalparam int A = 1; \n // vfmt: on\nendmodule\n";
|
|
let path = dir.write("foo.sv", src);
|
|
|
|
let out = run(&["check", "--fix", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
assert_eq!(dir.read("foo.sv"), src, "nothing inside the fence may be rewritten");
|
|
|
|
// And the formatter refuses this file outright, as it always has.
|
|
let out = run(&["--check", &path]);
|
|
assert_eq!(code(&out), 2);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The "already formatted" cache. See docs/features/format-cache/README.md.
|
|
// These tests drive the cache on purpose, so they use `run_raw`/`run_in`
|
|
// rather than the `--no-cache` helper the rest of the file uses.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// The version stamped into every cache entry: the workspace version, which
|
|
/// `vfmt_core` reads with the same `env!`.
|
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|
|
|
impl TempDir {
|
|
/// A cache directory of this test's own, so tests cannot share entries.
|
|
fn cache(&self) -> String {
|
|
self.path.join("cache-dir").to_string_lossy().into_owned()
|
|
}
|
|
|
|
fn cache_file(&self) -> PathBuf {
|
|
self.path.join("cache-dir").join("cache")
|
|
}
|
|
}
|
|
|
|
/// One hand-written cache line, for the tests that forge entries.
|
|
fn cache_line(
|
|
path: &str,
|
|
content: &str,
|
|
line_length: usize,
|
|
indent: usize,
|
|
version: &str,
|
|
) -> String {
|
|
let hash = vfmt_core::cache::content_hash(content.as_bytes());
|
|
format!("{hash:016x}\t{line_length}\t{indent}\t{version}\t{path}\n")
|
|
}
|
|
|
|
#[test]
|
|
fn a_second_run_over_a_formatted_tree_comes_entirely_from_the_cache() {
|
|
let dir = TempDir::new("cache-warm");
|
|
dir.write("a.sv", FORMATTED);
|
|
dir.write("sub/b.sv", FORMATTED);
|
|
let cache = dir.cache();
|
|
|
|
let cold = run_raw(&["--check", &dir.root(), "--cache-dir", &cache]);
|
|
assert_eq!(code(&cold), 0, "{}", stderr(&cold));
|
|
assert!(stderr(&cold).contains("2 files left unchanged"), "{}", stderr(&cold));
|
|
assert!(!stderr(&cold).contains("from cache"), "a cold run hits nothing: {}", stderr(&cold));
|
|
|
|
let warm = run_raw(&["--check", &dir.root(), "--cache-dir", &cache]);
|
|
assert_eq!(code(&warm), 0, "{}", stderr(&warm));
|
|
assert!(stderr(&warm).contains("2 files from cache"), "{}", stderr(&warm));
|
|
// A hit is still an unchanged file: no CI grep changes meaning.
|
|
assert!(stderr(&warm).contains("2 files left unchanged"), "{}", stderr(&warm));
|
|
assert_eq!(stdout(&warm), "", "a hit prints nothing");
|
|
}
|
|
|
|
#[test]
|
|
fn formatting_a_file_caches_what_was_written_not_what_was_read() {
|
|
let dir = TempDir::new("cache-write");
|
|
let path = dir.write("a.sv", UNFORMATTED);
|
|
let cache = dir.cache();
|
|
|
|
let first = run_raw(&[&path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&first), 0, "{}", stderr(&first));
|
|
assert_eq!(dir.read("a.sv"), FORMATTED);
|
|
assert!(!stderr(&first).contains("from cache"), "{}", stderr(&first));
|
|
|
|
// The *output*'s hash went in, so the very next run is a hit.
|
|
let second = run_raw(&[&path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&second), 0, "{}", stderr(&second));
|
|
assert!(stderr(&second).contains("1 file from cache"), "{}", stderr(&second));
|
|
}
|
|
|
|
#[test]
|
|
fn changing_the_content_invalidates_the_entry() {
|
|
let dir = TempDir::new("cache-content");
|
|
let path = dir.write("a.sv", FORMATTED);
|
|
let cache = dir.cache();
|
|
|
|
assert_eq!(code(&run_raw(&["--check", &path, "--cache-dir", &cache])), 0);
|
|
dir.write("a.sv", UNFORMATTED);
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 1, "the edited file must be re-checked: {}", stderr(&out));
|
|
assert!(stdout(&out).contains("would reformat:"), "{}", stdout(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn changing_the_indent_invalidates_the_entry() {
|
|
let dir = TempDir::new("cache-config");
|
|
let path = dir.write("a.sv", INDENT_2);
|
|
let cache = dir.cache();
|
|
|
|
// Clean at the default indent of 2…
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
|
|
// …and the settings are part of the key, so indent 4 is a miss.
|
|
let out = run_raw(&["--check", "--indent", "4", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 1, "indent 4 must not hit the indent 2 entry: {}", stderr(&out));
|
|
|
|
// Same for the width.
|
|
let path = dir.write("wide.sv", WIDE);
|
|
assert_eq!(code(&run_raw(&["--check", &path, "--cache-dir", &cache])), 0);
|
|
let out = run_raw(&["--check", "--line-length", "40", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 1, "line-length 40 must not hit the default entry: {}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_hit_skips_the_work_entirely() {
|
|
// A forged entry for a file the formatter cannot even parse: if the
|
|
// run comes back clean, the whole pipeline really was skipped rather
|
|
// than run and found to agree.
|
|
let dir = TempDir::new("cache-skips");
|
|
let path = dir.write("a.sv", UNPARSEABLE);
|
|
let cache = dir.cache();
|
|
std::fs::create_dir_all(dir.path.join("cache-dir")).unwrap();
|
|
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 2, "without an entry this file is an error");
|
|
|
|
std::fs::write(dir.cache_file(), cache_line(&path, UNPARSEABLE, 100, 2, VERSION)).unwrap();
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 0, "a hit does no work at all: {}", stderr(&out));
|
|
assert!(stderr(&out).contains("1 file from cache"), "{}", stderr(&out));
|
|
assert_eq!(dir.read("a.sv"), UNPARSEABLE, "and still writes nothing");
|
|
}
|
|
|
|
#[test]
|
|
fn a_config_file_change_invalidates_the_entry_too() {
|
|
let dir = TempDir::new("cache-tomlchange");
|
|
dir.write("vfmt.toml", "indent = 2\n");
|
|
let path = dir.write("a.sv", INDENT_2);
|
|
let cache = dir.cache();
|
|
|
|
assert_eq!(code(&run_raw(&["--check", &path, "--cache-dir", &cache])), 0);
|
|
dir.write("vfmt.toml", "indent = 4\n");
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 1, "the resolved settings are the key: {}", stderr(&out));
|
|
// …and an equivalent config written a different way is still a hit.
|
|
dir.write("vfmt.toml", "indent = 2 # back again\n");
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert!(stderr(&out).contains("1 file from cache"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn an_entry_from_another_vfmt_version_is_ignored() {
|
|
let dir = TempDir::new("cache-version");
|
|
let path = dir.write("a.sv", UNFORMATTED);
|
|
let cache = dir.cache();
|
|
std::fs::create_dir_all(dir.path.join("cache-dir")).unwrap();
|
|
|
|
// A forged entry claiming this unformatted file is clean. With this
|
|
// version's stamp it is believed…
|
|
std::fs::write(dir.cache_file(), cache_line(&path, UNFORMATTED, 100, 2, VERSION)).unwrap();
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 0, "the forged entry proves entries are really read");
|
|
assert!(stderr(&out).contains("1 file from cache"), "{}", stderr(&out));
|
|
|
|
// …and with any other version's stamp it is not.
|
|
std::fs::write(dir.cache_file(), cache_line(&path, UNFORMATTED, 100, 2, "0.0.0-other"))
|
|
.unwrap();
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 1, "another version's entry must not be trusted: {}", stderr(&out));
|
|
assert!(!stderr(&out).contains("from cache"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_corrupt_cache_file_is_tolerated_and_rebuilt() {
|
|
let dir = TempDir::new("cache-corrupt");
|
|
let path = dir.write("a.sv", FORMATTED);
|
|
let cache = dir.cache();
|
|
std::fs::create_dir_all(dir.path.join("cache-dir")).unwrap();
|
|
|
|
for corrupt in ["garbage\n", "\0\0\0\n", "not-a-hash\t100\t2\t0.2.0\t/a.sv\n", ""] {
|
|
std::fs::write(dir.cache_file(), corrupt).unwrap();
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 0, "a corrupt cache is never an error: {}", stderr(&out));
|
|
assert!(!stderr(&out).contains("from cache"), "{corrupt:?}: {}", stderr(&out));
|
|
// …and the run rewrote it, so the next one hits.
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert!(stderr(&out).contains("1 file from cache"), "{corrupt:?}: {}", stderr(&out));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn no_cache_neither_reads_nor_writes() {
|
|
let dir = TempDir::new("cache-off");
|
|
let path = dir.write("a.sv", FORMATTED);
|
|
let cache = dir.cache();
|
|
|
|
// Nothing is created…
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache, "--no-cache"]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert!(!dir.cache_file().exists(), "--no-cache must not write");
|
|
|
|
// …and an existing entry is not consulted.
|
|
assert_eq!(code(&run_raw(&["--check", &path, "--cache-dir", &cache])), 0);
|
|
assert!(dir.cache_file().exists());
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache, "--no-cache"]);
|
|
assert!(!stderr(&out).contains("from cache"), "--no-cache must not read: {}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_would_reformat_verdict_is_never_cached() {
|
|
let dir = TempDir::new("cache-dirty");
|
|
let path = dir.write("a.sv", UNFORMATTED);
|
|
let cache = dir.cache();
|
|
|
|
// Every --check run must re-check a dirty file: the fix is external.
|
|
for _ in 0..3 {
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 1, "{}", stderr(&out));
|
|
assert!(stdout(&out).contains("would reformat:"), "{}", stdout(&out));
|
|
assert!(!stderr(&out).contains("from cache"), "{}", stderr(&out));
|
|
}
|
|
// Same for --diff, which must keep printing the hunk.
|
|
let first = run_raw(&["--diff", &path, "--cache-dir", &cache]);
|
|
let second = run_raw(&["--diff", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&second), 1);
|
|
assert_eq!(first.stdout, second.stdout, "--diff output must not decay with the cache");
|
|
}
|
|
|
|
#[test]
|
|
fn an_error_is_never_cached() {
|
|
let dir = TempDir::new("cache-error");
|
|
let good = dir.write("a.sv", FORMATTED);
|
|
let bad = dir.write("b.sv", UNPARSEABLE);
|
|
let cache = dir.cache();
|
|
|
|
for _ in 0..2 {
|
|
let out = run_raw(&["--check", &dir.root(), "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 2, "the broken file must be reported every run");
|
|
assert!(stderr(&out).contains("1 file failed to format"), "{}", stderr(&out));
|
|
}
|
|
// The good file next to it still got cached.
|
|
let out = run_raw(&["--check", &dir.root(), "--cache-dir", &cache]);
|
|
assert!(stderr(&out).contains("1 file from cache"), "{}", stderr(&out));
|
|
assert_eq!(dir.read("a.sv"), FORMATTED);
|
|
assert_eq!(dir.read("b.sv"), UNPARSEABLE);
|
|
let _ = (good, bad);
|
|
}
|
|
|
|
#[test]
|
|
fn the_cache_does_not_change_output_at_any_thread_count() {
|
|
let dir = TempDir::new("cache-threads");
|
|
mixed_tree(&dir);
|
|
let cache = dir.cache();
|
|
let root = dir.root();
|
|
|
|
// Warm it, then compare a parallel and a serial warm run byte for byte.
|
|
let cold = run_raw(&["--check", &root, "--cache-dir", &cache]);
|
|
let parallel = run_raw(&["--check", &root, "--cache-dir", &cache]);
|
|
let serial = run_raw(&["--check", &root, "--cache-dir", &cache, "--threads", "1"]);
|
|
assert_eq!(code(¶llel), code(&serial));
|
|
assert_eq!(parallel.stdout, serial.stdout, "stdout must not depend on threads");
|
|
assert_eq!(parallel.stderr, serial.stderr, "stderr must not depend on threads");
|
|
assert_eq!(code(¶llel), 2, "the two broken files still win");
|
|
assert!(stderr(¶llel).contains("24 files from cache"), "{}", stderr(¶llel));
|
|
// The cold run's own stdout (the would-reformat lines) is unchanged by
|
|
// the cache: the 24 dirty files are re-reported every time.
|
|
assert_eq!(cold.stdout, parallel.stdout, "a warm run reports the same work");
|
|
|
|
// And the cache file itself is stable across runs.
|
|
let a = std::fs::read(dir.cache_file()).unwrap();
|
|
let _ = run_raw(&["--check", &root, "--cache-dir", &cache]);
|
|
assert_eq!(a, std::fs::read(dir.cache_file()).unwrap(), "the store is deterministic");
|
|
}
|
|
|
|
#[test]
|
|
fn the_default_cache_lives_in_the_working_directory_and_ignores_itself() {
|
|
let dir = TempDir::new("cache-default");
|
|
dir.write("a.sv", FORMATTED);
|
|
|
|
let out = run_in(&dir, &["--check", "."]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
let cache_dir = dir.path.join(".vfmt_cache");
|
|
assert!(cache_dir.join("cache").is_file(), "the default cache is cwd-relative");
|
|
assert_eq!(
|
|
std::fs::read_to_string(cache_dir.join(".gitignore")).unwrap(),
|
|
"*\n",
|
|
"the cache directory ignores itself, the way cargo marks target/"
|
|
);
|
|
|
|
let out = run_in(&dir, &["--check", "."]);
|
|
assert!(stderr(&out).contains("1 file from cache"), "{}", stderr(&out));
|
|
|
|
// Nothing but the cache file and its .gitignore is left behind.
|
|
let mut names: Vec<String> = std::fs::read_dir(&cache_dir)
|
|
.unwrap()
|
|
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
|
|
.collect();
|
|
names.sort();
|
|
assert_eq!(names, vec![".gitignore".to_string(), "cache".to_string()]);
|
|
}
|
|
|
|
#[test]
|
|
fn stdin_never_caches() {
|
|
let dir = TempDir::new("cache-stdin");
|
|
let cache = dir.cache();
|
|
let out = run_with_stdin_raw(&["-", "--cache-dir", &cache], FORMATTED);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(stdout(&out), FORMATTED);
|
|
assert!(!dir.cache_file().exists(), "stdin mode has no path to key on");
|
|
}
|
|
|
|
#[test]
|
|
fn stdout_mode_prints_every_file_even_when_the_cache_is_warm() {
|
|
let dir = TempDir::new("cache-stdout");
|
|
let path = dir.write("a.sv", FORMATTED);
|
|
let cache = dir.cache();
|
|
|
|
assert_eq!(code(&run_raw(&["--check", &path, "--cache-dir", &cache])), 0);
|
|
let out = run_raw(&["--stdout", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(stdout(&out), FORMATTED, "--stdout must print, never skip");
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// --lines (range formatting)
|
|
// ------------------------------------------------------------------
|
|
|
|
/// Three items, each needing a different amount of work, so a range's reach is
|
|
/// visible line by line.
|
|
const THREE: &str = "module m;\n wire a;\n wire b;\nendmodule\n";
|
|
|
|
#[test]
|
|
fn lines_formats_only_the_constructs_it_names() {
|
|
let dir = TempDir::new("lines");
|
|
let path = dir.write("a.sv", THREE);
|
|
|
|
let out = run(&["--stdout", "--lines", "2", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(stdout(&out), "module m;\n wire a;\n wire b;\nendmodule\n");
|
|
|
|
// The comma form and the repeated form are the same thing.
|
|
let both = "module m;\n wire a;\n wire b;\nendmodule\n";
|
|
assert_eq!(stdout(&run(&["--stdout", "--lines", "2,3", &path])), both);
|
|
assert_eq!(stdout(&run(&["--stdout", "--lines", "2", "--lines", "3", &path])), both);
|
|
|
|
// In place, and only the named line moved.
|
|
let out = run(&["--lines", "3", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("a.sv"), "module m;\n wire a;\n wire b;\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn lines_combines_with_check_and_diff() {
|
|
let dir = TempDir::new("lines-check");
|
|
let path = dir.write("a.sv", THREE);
|
|
|
|
// Line 4 is `endmodule`, already as the formatter would write it.
|
|
let out = run(&["--check", "--lines", "4", &path]);
|
|
assert_eq!(code(&out), 0, "nothing to change must exit 0: {}", stdout(&out));
|
|
|
|
let out = run(&["--check", "--lines", "2", &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"), THREE, "--check must not write");
|
|
|
|
let out = run(&["--diff", "--lines", "2", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
let text = stdout(&out);
|
|
assert!(text.contains("- wire a;") && text.contains("+ wire a;"), "{text}");
|
|
// Only the selected line shows up as a change; `wire b` is context only.
|
|
let changed: Vec<&str> = text
|
|
.lines()
|
|
.filter(|l| l.starts_with(['+', '-']) && !l.starts_with("+++") && !l.starts_with("---"))
|
|
.collect();
|
|
assert_eq!(changed, ["- wire a;", "+ wire a;"], "{text}");
|
|
}
|
|
|
|
#[test]
|
|
fn lines_works_on_stdin() {
|
|
let out = run_with_stdin(&["--lines", "3", "--stdin-filename", "rtl/top.sv", "-"], THREE);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(stdout(&out), "module m;\n wire a;\n wire b;\nendmodule\n");
|
|
|
|
let out = run_with_stdin(&["--check", "--lines", "2", "-"], THREE);
|
|
assert_eq!(code(&out), 1);
|
|
assert_eq!(stdout(&out), "would reformat: <stdin>\n");
|
|
}
|
|
|
|
#[test]
|
|
fn lines_is_deterministic_and_settles() {
|
|
let dir = TempDir::new("lines-idem");
|
|
let path = dir.write("a.sv", THREE);
|
|
let once = stdout(&run(&["--stdout", "--lines", "2", &path]));
|
|
assert_eq!(stdout(&run(&["--stdout", "--lines", "2", &path])), once, "same input, same output");
|
|
|
|
// Formatting the output again with the same ranges changes nothing here,
|
|
// and `--check` is how a caller sees that.
|
|
dir.write("a.sv", &once);
|
|
assert_eq!(code(&run(&["--check", "--lines", "2", &path])), 0);
|
|
// A whole-file run afterwards may still have work to do — that is the
|
|
// feature, not a leftover.
|
|
assert_eq!(code(&run(&["--check", &path])), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn lines_refuses_a_directory_and_a_bad_range() {
|
|
let dir = TempDir::new("lines-bad");
|
|
dir.write("a.sv", THREE);
|
|
|
|
let out = run(&["--lines", "2", &dir.root()]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("--lines needs a file, not a directory"), "{}", stderr(&out));
|
|
assert_eq!(dir.read("a.sv"), THREE, "nothing is written when the arguments are wrong");
|
|
|
|
for spec in ["5-1", "0-3", "", "x"] {
|
|
let out = run(&["--lines", spec, &dir.write("b.sv", THREE)]);
|
|
assert_eq!(code(&out), 2, "spec {spec:?}");
|
|
}
|
|
let out = run(&["--lines"]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("--lines requires a value"), "{}", stderr(&out));
|
|
|
|
// The linter has no line ranges.
|
|
let out = run(&["check", "--lines", "2", &dir.write("c.sv", THREE)]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("`vfmt check`"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn lines_neither_reads_nor_writes_the_cache() {
|
|
let dir = TempDir::new("lines-cache");
|
|
let path = dir.write("a.sv", FORMATTED);
|
|
let cache = dir.cache();
|
|
|
|
// Warm the cache with a whole-file run, then confirm a range run does not
|
|
// take the shortcut: a partial format is not the "already formatted"
|
|
// verdict the cache stores.
|
|
assert_eq!(code(&run_raw(&["--check", &path, "--cache-dir", &cache])), 0);
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert!(stderr(&out).contains("from cache"), "the cache is warm: {}", stderr(&out));
|
|
|
|
let out = run_raw(&["--check", "--lines", "1", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert!(!stderr(&out).contains("from cache"), "{}", stderr(&out));
|
|
|
|
// And a range run on a file the cache has never seen writes no entry.
|
|
let fresh = TempDir::new("lines-cache-2");
|
|
let fresh_path = fresh.write("a.sv", FORMATTED);
|
|
let fresh_cache = fresh.cache();
|
|
assert_eq!(code(&run_raw(&["--lines", "1", &fresh_path, "--cache-dir", &fresh_cache])), 0);
|
|
assert!(!fresh.cache_file().exists(), "a range run must not seed the cache");
|
|
}
|
|
|
|
fn run_with_stdin_raw(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")
|
|
}
|