2426 lines
102 KiB
Rust
2426 lines
102 KiB
Rust
//! End-to-end tests that drive the real `lucisv` 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 LUCISV: &str = env!("CARGO_BIN_EXE_lucisv");
|
|
/// 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!("lucisv-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 lucisv with the cache switched off. Every test here shares one
|
|
/// working directory (the crate's), so a default-on cache would put a
|
|
/// `.lucisv_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 lucisv exactly as given, cache and all.
|
|
fn run_raw(args: &[&str]) -> Output {
|
|
Command::new(LUCISV).args(args).output().expect("run lucisv")
|
|
}
|
|
|
|
/// Runs lucisv 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(LUCISV).args(args).current_dir(&dir.path).output().expect("run lucisv")
|
|
}
|
|
|
|
fn run_with_stdin(args: &[&str], input: &str) -> Output {
|
|
let mut child = Command::new(LUCISV)
|
|
.args(with_no_cache(args))
|
|
.stdin(Stdio::piped())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::piped())
|
|
.spawn()
|
|
.expect("spawn lucisv");
|
|
child.stdin.as_mut().expect("stdin").write_all(input.as_bytes()).expect("write stdin");
|
|
child.wait_with_output().expect("run lucisv")
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
/// A directory walk lists a symlink as a file of its own, so `b.sv -> a.sv`
|
|
/// used to put two workers on one file: two report lines for one file, and
|
|
/// two writers racing to replace it. It is one file now, named once.
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn a_symlink_and_its_target_are_one_file() {
|
|
let dir = TempDir::new("alias");
|
|
dir.write("a.sv", UNFORMATTED);
|
|
std::os::unix::fs::symlink(dir.path.join("a.sv"), dir.path.join("b.sv")).expect("symlink");
|
|
|
|
let out = run(&["--check", &dir.root()]);
|
|
assert_eq!(code(&out), 1, "{}", stderr(&out));
|
|
let reported = stdout(&out);
|
|
let named: Vec<&str> = reported.lines().filter(|l| l.contains("would reformat")).collect();
|
|
assert_eq!(named.len(), 1, "one file, one line: {named:?}");
|
|
assert!(named[0].ends_with("a.sv"), "the first spelling walked is the one reported: {named:?}");
|
|
assert!(stderr(&out).contains("1 file would be reformatted"), "{}", stderr(&out));
|
|
|
|
let out = run(&[&dir.root()]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert!(stderr(&out).contains("1 file reformatted"), "{}", stderr(&out));
|
|
assert_eq!(dir.read("a.sv"), FORMATTED);
|
|
assert_eq!(dir.read("b.sv"), FORMATTED, "and the link still reaches it");
|
|
assert!(
|
|
std::fs::symlink_metadata(dir.path.join("b.sv")).expect("stat").file_type().is_symlink(),
|
|
"the link was not replaced by a copy"
|
|
);
|
|
}
|
|
|
|
/// A directory that links back into itself. Before the walk kept a visited
|
|
/// set (`docs/features/walk-cycles/README.md`) this run recursed until the
|
|
/// OS refused the path; now each directory is entered once, so each file is
|
|
/// named once — under the first spelling that reaches it.
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn a_symlink_cycle_terminates_and_names_each_file_once() {
|
|
let dir = TempDir::new("cycle");
|
|
dir.write("rtl/a.sv", UNFORMATTED);
|
|
dir.write("rtl/sub/b.sv", UNFORMATTED);
|
|
// `rtl/self` is `rtl` itself; `rtl/sub/up` is `rtl` from one level down.
|
|
std::os::unix::fs::symlink("../rtl", dir.path.join("rtl/self")).expect("symlink");
|
|
std::os::unix::fs::symlink("..", dir.path.join("rtl/sub/up")).expect("symlink");
|
|
|
|
let out = run(&["--check", &dir.root()]);
|
|
assert_eq!(code(&out), 1, "{}", stderr(&out));
|
|
let reported = stdout(&out);
|
|
let named: Vec<&str> = reported.lines().filter(|l| l.contains("would reformat")).collect();
|
|
assert_eq!(named.len(), 2, "two files, two lines: {named:?}");
|
|
assert!(stderr(&out).contains("2 files would be reformatted"), "{}", 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 `PARSE_STACK_SIZE`
|
|
// 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 a_deeply_nested_buffer_from_stdin_is_a_diagnostic_not_a_crash() {
|
|
// The stdin path never touches the worker pool: it formats on the thread
|
|
// `main` spawned, which is the only stack whose size lucisv gets to
|
|
// state. 500 nested blocks are far past the parser's limit, so the run
|
|
// must end with an ordinary parse error — and not with a SIGABRT, which
|
|
// is what an unstated stack gives instead.
|
|
let deep = format!(
|
|
"module m;\ninitial begin\n{}x <= 1;\n{}end\nendmodule\n",
|
|
"begin\n".repeat(500),
|
|
"end\n".repeat(500)
|
|
);
|
|
let out = run_with_stdin(&["-"], &deep);
|
|
assert_eq!(code(&out), 2, "{}", stderr(&out));
|
|
assert!(stderr(&out).contains("nesting too deep"), "{}", stderr(&out));
|
|
assert!(stdout(&out).is_empty(), "a rejected buffer prints nothing: {}", stdout(&out));
|
|
|
|
// And the deepest *legal* buffer formats on that same stack.
|
|
let legal = format!(
|
|
"module m;\ninitial begin\n{}x <= 1;\n{}end\nendmodule\n",
|
|
"begin\n".repeat(58),
|
|
"end\n".repeat(58)
|
|
);
|
|
let out = run_with_stdin(&["-"], &legal);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert!(stdout(&out).contains("endmodule"), "{}", stdout(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn every_file_keeps_its_own_config_when_formatted_in_parallel() {
|
|
let dir = TempDir::new("configpar");
|
|
dir.write("lucisv.toml", "indent = 4\n");
|
|
dir.write("sub/lucisv.toml", "indent = 8\n");
|
|
dir.write("sub/deeper/lucisv.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("lucisv.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; `.lucisv.toml` is the fallback within one directory.
|
|
dir.write("a/b/x.sv", UNINDENTED);
|
|
std::fs::rename(dir.path.join("lucisv.toml"), dir.path.join(".lucisv.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("lucisv.toml", "indent = 4\n");
|
|
dir.write("root.sv", UNINDENTED);
|
|
dir.write("sub/lucisv.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("lucisv.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("lucisv.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("lucisv.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("lucisv.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.lucisv]\nindent = 2\n", "unknown table"),
|
|
("exclude = [\n \"a\",\n]\n", "multi-line arrays are unsupported"),
|
|
] {
|
|
dir.write("lucisv.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("lucisv.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("lucisv.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("lucisv.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("lucisv.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/lucisv.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");
|
|
}
|
|
|
|
// -------------------------------------------------------------- lucisv 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);
|
|
// `FILE:LINE:COL: severity: message [rule]` — the severity word is part
|
|
// of the line since §33.0, and both of these are style findings.
|
|
assert!(text.contains(&format!("{path}:2:1: style: 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("\"severity\":\"style\""), "{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));
|
|
}
|
|
|
|
/// A severity word stands for its whole class wherever a rule name fits —
|
|
/// §33.0's headline for the command line.
|
|
#[test]
|
|
fn check_select_and_ignore_accept_a_severity_word() {
|
|
let dir = TempDir::new("check-severity");
|
|
// `reg` is an error-class finding, the tab and the name are style ones.
|
|
let path = dir.write("bar.sv", "module foo;\n\treg a;\nendmodule\n");
|
|
|
|
let out = run(&["check", "--select", "error", &path]);
|
|
assert_eq!(code(&out), 1, "{}", stdout(&out));
|
|
let text = stdout(&out);
|
|
assert_eq!(text.lines().count(), 1, "{text}");
|
|
assert!(text.contains(": error: "), "{text}");
|
|
assert!(text.contains("[wire-reg-mixup]"), "{text}");
|
|
|
|
let out = run(&["check", "--ignore", "style", &path]);
|
|
assert_eq!(stdout(&out).lines().count(), 1, "{}", stdout(&out));
|
|
assert!(stdout(&out).contains("[wire-reg-mixup]"), "{}", stdout(&out));
|
|
|
|
// Ignoring every class leaves nothing to report.
|
|
let out = run(&["check", "--ignore", "error,warning,style", &path]);
|
|
assert_eq!(code(&out), 0, "{}", stdout(&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; // lucisv: 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));
|
|
}
|
|
|
|
/// Two tabbed lines, one of which a waiver file's `--line` covers.
|
|
const TABBED: &str = "module foo;\n\twire a;\n\twire b;\nendmodule\n";
|
|
|
|
/// `--waiver-file`: the suppression, the flag repeated, and the dead directive
|
|
/// reported at the waiver file's own line. TODO §39.
|
|
#[test]
|
|
fn check_reads_external_waiver_files() {
|
|
let dir = TempDir::new("check-waiver-file");
|
|
let path = dir.write("foo.sv", TABBED);
|
|
let one = dir.write("one.waiver", "# just line 2\nwaive --rule=no-tabs --line=2\n");
|
|
let out = run(&["check", "--waiver-file", &one, &path]);
|
|
assert_eq!(code(&out), 1, "{}", stdout(&out));
|
|
assert_eq!(stdout(&out).lines().count(), 1, "one tab left: {}", stdout(&out));
|
|
assert!(stdout(&out).contains("foo.sv:3:1"), "{}", stdout(&out));
|
|
|
|
// A second file adds to the first rather than replacing it, and both are
|
|
// then used, so neither is reported.
|
|
let two = dir.write("two.waiver", "waive --rule=no-tabs --line=3 --location=foo.sv\n");
|
|
let out = run(&["check", "--waiver-file", &one, "--waiver-file", &two, &path]);
|
|
assert_eq!(code(&out), 0, "{}", stdout(&out));
|
|
|
|
// A directive that covered nothing is reported against the waiver file.
|
|
let dead = dir.write("dead.waiver", "waive --rule=no-tabs --location=elsewhere.sv\n");
|
|
let out = run(&["check", "--waiver-file", &dead, &path]);
|
|
assert_eq!(code(&out), 1);
|
|
let text = stdout(&out);
|
|
assert!(text.contains("dead.waiver:1:1: style: unused waiver for `no-tabs`"), "{text}");
|
|
assert!(text.contains("[unused-waiver]"), "{text}");
|
|
}
|
|
|
|
/// The `[lint] waivers` key, and that `--waiver-file` adds to it. TODO §39.
|
|
#[test]
|
|
fn check_reads_the_waiver_files_a_config_file_names() {
|
|
let dir = TempDir::new("check-waiver-toml");
|
|
let path = dir.write("rtl/foo.sv", TABBED);
|
|
dir.write("lucisv.toml", "[lint]\nwaivers = [ \"project.waiver\" ]\n");
|
|
dir.write("project.waiver", "waive --rule=no-tabs --line=2\n");
|
|
// Relative to the config file, not to the working directory.
|
|
let out = run_raw(&["check", "--no-cache", &path]);
|
|
assert_eq!(code(&out), 1, "{}", stdout(&out));
|
|
assert!(stdout(&out).contains("foo.sv:3:1"), "only line 3 survives: {}", stdout(&out));
|
|
|
|
let extra = dir.write("extra.waiver", "waive --rule=no-tabs --line=3\n");
|
|
let out = run_raw(&["check", "--no-cache", "--waiver-file", &extra, &path]);
|
|
assert_eq!(code(&out), 0, "the two lists add up: {}", stdout(&out));
|
|
|
|
// `--no-config` drops the config file's list with everything else in it.
|
|
let out = run_raw(&["check", "--no-cache", "--no-config", &path]);
|
|
assert_eq!(code(&out), 1);
|
|
assert_eq!(stdout(&out).lines().count(), 2, "both tabs are back: {}", stdout(&out));
|
|
}
|
|
|
|
/// A waiver file that cannot be read, or that has a line that does not parse,
|
|
/// fails the run with `<file>:<line>: message` and exit 2. TODO §39.
|
|
#[test]
|
|
fn a_broken_waiver_file_fails_the_run() {
|
|
let dir = TempDir::new("check-waiver-bad");
|
|
let path = dir.write("foo.sv", TABBED);
|
|
|
|
let out = run(&["check", "--waiver-file", &dir.write("x.waiver", "hush\n"), &path]);
|
|
assert_eq!(code(&out), 2, "{}", stderr(&out));
|
|
assert!(stderr(&out).contains("x.waiver:1: expected a `waive` directive"), "{}", stderr(&out));
|
|
|
|
let bad = dir.write("y.waiver", "# fine\nwaive --rule=no-such-rule\n");
|
|
let out = run(&["check", "--waiver-file", &bad, &path]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("y.waiver:2: unknown rule"), "{}", stderr(&out));
|
|
|
|
let regexy = dir.write("z.waiver", "waive --rule=no-tabs --location=.*foo.sv\n");
|
|
let out = run(&["check", "--waiver-file", ®exy, &path]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("looks like a regular expression"), "{}", stderr(&out));
|
|
|
|
let out = run(&[
|
|
"check",
|
|
"--waiver-file",
|
|
&dir.path.join("gone.waiver").display().to_string(),
|
|
&path,
|
|
]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("gone.waiver:"), "{}", stderr(&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("lucisv.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 `lucisv 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 `lucisv 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. `lucisv format` emits the fenced
|
|
/// bytes verbatim; `lucisv check --fix` must do the same.
|
|
const FENCED: &str = "module foo;\n\
|
|
// lucisv: off\n\
|
|
\tlocalparam int A = 1; \n\
|
|
// lucisv: 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 // lucisv: off\n\tlocalparam int A = 1; \n // lucisv: 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
|
|
/// `lucisv_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. The
|
|
/// fields are the store's, in order; `uvm` is `[format] uvm-utils-indent`.
|
|
fn cache_line(
|
|
path: &str,
|
|
content: &str,
|
|
line_length: usize,
|
|
indent: usize,
|
|
uvm: bool,
|
|
version: &str,
|
|
) -> String {
|
|
let hash = lucisv_core::cache::content_hash(content.as_bytes());
|
|
format!("{hash:016x}\t{line_length}\t{indent}\t{uvm}\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, false, 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("lucisv.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("lucisv.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("lucisv.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));
|
|
}
|
|
|
|
/// A UVM class whose layout depends on `[format] uvm-utils-indent`: with the
|
|
/// option on, the `` `uvm_field_* `` macro gains a level. Written here as the
|
|
/// formatter leaves it with the option *off*.
|
|
const UVM_CLASS: &str = "class c extends uvm_object;\n `uvm_object_utils_begin(c)\n \
|
|
`uvm_field_int(a, UVM_DEFAULT)\n `uvm_object_utils_end\nendclass\n";
|
|
|
|
#[test]
|
|
fn changing_uvm_utils_indent_invalidates_the_entry() {
|
|
let dir = TempDir::new("cache-uvm");
|
|
dir.write("lucisv.toml", "[format]\nuvm-utils-indent = false\n");
|
|
let path = dir.write("a.sv", UVM_CLASS);
|
|
let cache = dir.cache();
|
|
|
|
// Clean with the option off, and that verdict is cached.
|
|
assert_eq!(code(&run_raw(&[&path, "--cache-dir", &cache])), 0);
|
|
let flat = dir.read("a.sv");
|
|
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));
|
|
|
|
// Turning the option on must not hit the entry the option-off run left.
|
|
dir.write("lucisv.toml", "[format]\nuvm-utils-indent = true\n");
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 1, "uvm-utils-indent is part of the key: {}", stderr(&out));
|
|
assert!(stdout(&out).contains("would reformat:"), "{}", stdout(&out));
|
|
|
|
// Format under the option on, cache *that*, then flip back off.
|
|
assert_eq!(code(&run_raw(&[&path, "--cache-dir", &cache])), 0);
|
|
assert_ne!(dir.read("a.sv"), flat, "the option really does change the output");
|
|
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));
|
|
|
|
dir.write("lucisv.toml", "[format]\nuvm-utils-indent = false\n");
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 1, "and the other direction too: {}", stderr(&out));
|
|
assert!(stdout(&out).contains("would reformat:"), "{}", stdout(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_store_written_before_the_option_joined_the_key_is_rebuilt() {
|
|
let dir = TempDir::new("cache-oldshape");
|
|
let path = dir.write("a.sv", FORMATTED);
|
|
let cache = dir.cache();
|
|
std::fs::create_dir_all(dir.path.join("cache-dir")).unwrap();
|
|
|
|
// The five-field line lucisv wrote before `uvm-utils-indent` was in the
|
|
// key. Not corruption: the line is dropped on its own and the run
|
|
// rebuilds the store in the current shape.
|
|
let hash = lucisv_core::cache::content_hash(FORMATTED.as_bytes());
|
|
std::fs::write(dir.cache_file(), format!("{hash:016x}\t100\t2\t{VERSION}\t{path}\n")).unwrap();
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert_eq!(code(&out), 0, "an older store is never an error: {}", stderr(&out));
|
|
assert!(!stderr(&out).contains("from cache"), "{}", stderr(&out));
|
|
|
|
let out = run_raw(&["--check", &path, "--cache-dir", &cache]);
|
|
assert!(stderr(&out).contains("1 file from cache"), "the store was rebuilt: {}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn an_entry_from_another_lucisv_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, false, 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, false, "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(".lucisv_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("`lucisv 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(LUCISV)
|
|
.args(args)
|
|
.stdin(Stdio::piped())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::piped())
|
|
.spawn()
|
|
.expect("spawn lucisv");
|
|
child.stdin.as_mut().expect("stdin").write_all(input.as_bytes()).expect("write stdin");
|
|
child.wait_with_output().expect("run lucisv")
|
|
}
|
|
|
|
// --- `-f`, the EDA filelist (docs/features/cli-filelist/README.md) ---------
|
|
|
|
/// A tree with a filelist naming one file directly and one through a nested
|
|
/// `-f`, plus a file nothing lists. Returns the temp dir.
|
|
fn filelist_tree(tag: &str) -> TempDir {
|
|
let dir = TempDir::new(tag);
|
|
dir.write("rtl/a.sv", UNFORMATTED);
|
|
dir.write("rtl/b.sv", UNFORMATTED);
|
|
dir.write("rtl/unlisted.sv", UNFORMATTED);
|
|
dir.write("top.f", "# the project\nrtl/a.sv\n-f sub/more.f\n");
|
|
dir.write("sub/more.f", "../rtl/b.sv\n");
|
|
dir
|
|
}
|
|
|
|
#[test]
|
|
fn a_filelist_names_the_files_to_format() {
|
|
let dir = filelist_tree("filelist");
|
|
let list = dir.write("top.f", "# the project\nrtl/a.sv\n-f sub/more.f\n");
|
|
|
|
let out = run(&["-f", &list]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("rtl/a.sv"), FORMATTED);
|
|
assert_eq!(dir.read("rtl/b.sv"), FORMATTED, "a nested -f contributes its files too");
|
|
assert_eq!(dir.read("rtl/unlisted.sv"), UNFORMATTED, "only what the filelist names");
|
|
assert!(stderr(&out).contains("2 files reformatted"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_filelist_and_positional_inputs_add_up_and_each_file_is_processed_once() {
|
|
let dir = filelist_tree("filelist-plus");
|
|
let list = dir.write("top.f", "rtl/a.sv\n-f sub/more.f\n");
|
|
let also = dir.write("rtl/unlisted.sv", UNFORMATTED);
|
|
let already = dir.write("rtl/a.sv", UNFORMATTED);
|
|
|
|
// `rtl/a.sv` is named three times over — by the filelist, by name, and by
|
|
// the directory walk — and must still be one file in the summary.
|
|
let out =
|
|
run(&["--check", "-f", &list, &also, &already, &dir.path.join("rtl").to_string_lossy()]);
|
|
assert_eq!(code(&out), 1, "{}", stderr(&out));
|
|
let text = stdout(&out);
|
|
assert_eq!(text.matches("a.sv").count(), 1, "{text}");
|
|
assert_eq!(text.matches("b.sv").count(), 1, "{text}");
|
|
assert_eq!(text.matches("unlisted.sv").count(), 1, "{text}");
|
|
assert!(stderr(&out).contains("3 files would be reformatted"), "{}", stderr(&out));
|
|
|
|
// Either half works on its own, and repeating `-f` adds a second list.
|
|
let other = dir.write("other.f", "rtl/unlisted.sv\n");
|
|
let out = run(&["--check", "-f", &list, "-f", &other]);
|
|
assert_eq!(code(&out), 1);
|
|
assert!(stderr(&out).contains("3 files would be reformatted"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_missing_listed_file_warns_and_the_rest_are_still_processed() {
|
|
let dir = filelist_tree("filelist-missing");
|
|
let list = dir.write("top.f", "rtl/a.sv\ngone.sv\n-y lib\nrtl/b.sv\n");
|
|
|
|
let out = run(&["-f", &list]);
|
|
assert_eq!(code(&out), 0, "a warning is not an error: {}", stderr(&out));
|
|
assert_eq!(dir.read("rtl/a.sv"), FORMATTED);
|
|
assert_eq!(dir.read("rtl/b.sv"), FORMATTED);
|
|
let err = stderr(&out);
|
|
assert!(err.contains("warning: "), "{err}");
|
|
assert!(err.contains("top.f:2: no such file `gone.sv`"), "{err}");
|
|
assert!(err.contains("top.f:3: ignoring unsupported option `-y`"), "{err}");
|
|
assert!(stdout(&out).is_empty(), "warnings must not pollute stdout: {}", stdout(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn an_unreadable_filelist_exits_2_and_processes_nothing() {
|
|
let dir = filelist_tree("filelist-unreadable");
|
|
let missing = dir.path.join("nope.f").to_string_lossy().into_owned();
|
|
|
|
let out = run(&["-f", &missing, &dir.write("rtl/a.sv", UNFORMATTED)]);
|
|
assert_eq!(code(&out), 2, "an unreadable filelist is the one hard error");
|
|
assert!(stderr(&out).contains("nope.f"), "{}", stderr(&out));
|
|
assert_eq!(dir.read("rtl/a.sv"), UNFORMATTED, "nothing is written when the run aborts");
|
|
}
|
|
|
|
#[test]
|
|
fn check_mode_exits_1_when_a_listed_file_would_be_reformatted() {
|
|
let dir = filelist_tree("filelist-check");
|
|
let list = dir.write("top.f", "rtl/a.sv\n");
|
|
|
|
let out = run(&["--check", "-f", &list]);
|
|
assert_eq!(code(&out), 1);
|
|
assert!(stdout(&out).contains("would reformat"), "{}", stdout(&out));
|
|
assert_eq!(dir.read("rtl/a.sv"), UNFORMATTED, "--check never writes");
|
|
|
|
// Formatted input, same filelist: nothing to do.
|
|
dir.write("rtl/a.sv", FORMATTED);
|
|
assert_eq!(code(&run(&["--check", "-f", &list])), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn the_linter_takes_a_filelist_too_and_json_stays_parsable() {
|
|
let dir = TempDir::new("filelist-lint");
|
|
dir.write("foo.sv", LINTY);
|
|
let list = dir.write("top.f", "foo.sv\ngone.sv\n");
|
|
|
|
let out = run(&["check", "--select", "no-tabs", "-f", &list]);
|
|
assert_eq!(code(&out), 1, "{}", stderr(&out));
|
|
assert!(stdout(&out).contains("no-tabs"), "{}", stdout(&out));
|
|
|
|
// The filelist's warnings go to stderr, so stdout is still one JSON array.
|
|
let out = run(&["check", "--output-format", "json", "--select", "no-tabs", "-f", &list]);
|
|
assert_eq!(code(&out), 1);
|
|
let text = stdout(&out);
|
|
assert!(text.starts_with('['), "{text}");
|
|
assert!(text.trim_end().ends_with(']'), "{text}");
|
|
assert!(!text.contains("warning"), "{text}");
|
|
assert!(stderr(&out).contains("no such file `gone.sv`"), "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
fn a_filelists_paths_are_relative_to_it_not_to_the_working_directory() {
|
|
let dir = filelist_tree("filelist-cwd");
|
|
let sub = TempDir::new("filelist-cwd-elsewhere");
|
|
|
|
// Run from an unrelated directory, naming the filelist relatively from
|
|
// there: every path inside it still resolves against the filelist's
|
|
// own directory, and the nested list's against its own.
|
|
let relative = pathdiff(&sub.path, &dir.path.join("top.f"));
|
|
let out = run_in(&sub, &["--no-cache", "-f", &relative]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("rtl/a.sv"), FORMATTED);
|
|
assert_eq!(dir.read("rtl/b.sv"), FORMATTED);
|
|
}
|
|
|
|
/// `to` spelled relative to `from`, for the temp dirs here: they are always
|
|
/// siblings, so `../<name>/<rest>` is the whole story.
|
|
fn pathdiff(from: &std::path::Path, to: &std::path::Path) -> String {
|
|
let name = to.strip_prefix(from.parent().expect("temp parent")).expect("sibling");
|
|
std::path::Path::new("..").join(name).to_string_lossy().into_owned()
|
|
}
|
|
|
|
#[test]
|
|
fn a_listed_file_is_never_excluded_or_gitignored() {
|
|
let dir = TempDir::new("filelist-exclude");
|
|
dir.write(".gitignore", "*_gen.sv\nbuild/\n");
|
|
dir.write("gen/a_gen.sv", UNFORMATTED);
|
|
dir.write("build/inner.sv", UNFORMATTED);
|
|
let list = dir.write("top.f", "gen/a_gen.sv\nbuild/inner.sv\n");
|
|
|
|
// Same rule as a path named on the command line: it was named, so it is
|
|
// processed, whatever --exclude and .gitignore say.
|
|
let out = run(&["--exclude", "*_gen.sv", "--exclude", "build", "-f", &list]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(dir.read("gen/a_gen.sv"), FORMATTED);
|
|
assert_eq!(dir.read("build/inner.sv"), FORMATTED);
|
|
}
|
|
|
|
#[test]
|
|
fn stdin_and_lines_are_usage_errors_with_a_filelist() {
|
|
let dir = filelist_tree("filelist-usage");
|
|
let list = dir.write("top.f", "rtl/a.sv\n");
|
|
|
|
let out = run_with_stdin(&["-f", &list, "-"], UNFORMATTED);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("`-` cannot be combined with -f"), "{}", stderr(&out));
|
|
|
|
let out = run(&["--lines", "1", "-f", &list]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("--lines needs a file, not a filelist"), "{}", stderr(&out));
|
|
|
|
let out = run(&["-f"]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("-f requires a value"), "{}", stderr(&out));
|
|
|
|
// A filelist that names nothing usable is the ordinary empty-input error.
|
|
let empty = dir.write("empty.f", "# nothing here\n");
|
|
let out = run(&["-f", &empty]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("no Verilog files found"), "{}", stderr(&out));
|
|
|
|
assert_eq!(dir.read("rtl/a.sv"), UNFORMATTED, "no bad command line writes anything");
|
|
}
|
|
|
|
#[test]
|
|
fn help_documents_the_filelist_flag() {
|
|
let text = stdout(&run(&["--help"]));
|
|
assert!(text.contains("FILELISTS:"), "{text}");
|
|
assert!(text.contains("-f <FILELIST>"), "{text}");
|
|
assert!(text.contains("read by lucisv-server only"), "the toml key stays server-only: {text}");
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// The semantic rules and the project census (semantic-lints-on, 2026-09-02)
|
|
// ----------------------------------------------------------------------
|
|
|
|
/// A module whose `dbg_probe` only a *second* file reads, hierarchically.
|
|
const DUT: &str = "module dut;\n logic dbg_probe;\n logic dead;\nendmodule\n";
|
|
const TB: &str = "module tb;\n dut d ();\n initial $display(\"%b\", d.dbg_probe);\nendmodule\n";
|
|
|
|
/// The round's headline: over a project the semantic rules run by default,
|
|
/// and the file that reads `d.dbg_probe` is what keeps `dbg_probe` quiet.
|
|
/// `dead`, which nothing anywhere writes down twice, is still reported.
|
|
#[test]
|
|
fn a_mention_in_another_file_silences_unused_signal_over_a_project() {
|
|
let dir = TempDir::new("census");
|
|
dir.write("dut.sv", DUT);
|
|
dir.write("tb.sv", TB);
|
|
let out = run(&["check", &dir.root()]);
|
|
let text = stdout(&out);
|
|
assert!(text.contains("`dead` is declared but never used"), "{text}");
|
|
assert!(!text.contains("dbg_probe"), "the other file reads it: {text}");
|
|
assert_eq!(code(&out), 1);
|
|
}
|
|
|
|
/// One file is not a project: there is nothing to ask, so the rules that
|
|
/// need a workspace stay off and the run is exactly v0.29.0's.
|
|
#[test]
|
|
fn a_single_file_run_leaves_the_semantic_rules_off() {
|
|
let dir = TempDir::new("alone");
|
|
let dut = dir.write("dut.sv", DUT);
|
|
let out = run(&["check", &dut]);
|
|
assert_eq!(stdout(&out), "");
|
|
assert_eq!(code(&out), 0);
|
|
|
|
// ... and naming the rule still runs it, oracle or no oracle.
|
|
let named = run(&["check", &dut, "--select", "unused-signal"]);
|
|
let text = stdout(&named);
|
|
assert!(text.contains("dbg_probe"), "{text}");
|
|
assert!(text.contains("dead"), "{text}");
|
|
assert_eq!(code(&named), 1);
|
|
}
|
|
|
|
/// A stdin buffer has no project either, and no file to build one from.
|
|
#[test]
|
|
fn a_stdin_buffer_leaves_the_semantic_rules_off() {
|
|
let out = run_with_stdin(&["check", "-"], DUT);
|
|
assert_eq!(stdout(&out), "");
|
|
assert_eq!(code(&out), 0);
|
|
let named = run_with_stdin(&["check", "-", "--select", "unused-signal"], DUT);
|
|
assert!(stdout(&named).contains("dbg_probe"), "{}", stdout(&named));
|
|
}
|
|
|
|
/// The off switch a default-on rule owes its users: `[lint] ignore` in the
|
|
/// `lucisv.toml` governing the files.
|
|
#[test]
|
|
fn a_lint_table_in_the_config_turns_a_rule_off() {
|
|
let dir = TempDir::new("lint-table");
|
|
dir.write("dut.sv", DUT);
|
|
dir.write("tb.sv", TB);
|
|
dir.write("lucisv.toml", "[lint]\nignore = [\"unused-signal\"]\n");
|
|
let out = run(&["check", &dir.root()]);
|
|
assert_eq!(stdout(&out), "", "the config silenced the rule");
|
|
assert_eq!(code(&out), 0);
|
|
}
|
|
|
|
/// `[lint] select` replaces the default set, exactly as `--select` does.
|
|
#[test]
|
|
fn a_lint_table_can_select_instead_of_ignore() {
|
|
let dir = TempDir::new("lint-select");
|
|
dir.write("dut.sv", DUT);
|
|
dir.write("tb.sv", TB);
|
|
dir.write("lucisv.toml", "[lint]\nselect = [\"unused-signal\"]\n");
|
|
let out = run(&["check", &dir.root()]);
|
|
let text = stdout(&out);
|
|
assert!(text.contains("`dead` is declared but never used"), "{text}");
|
|
assert!(!text.contains("dbg_probe"), "{text}");
|
|
}
|
|
|
|
/// Precedence, key by key: the command line beats the config file, and a key
|
|
/// the command line does not mention stays the config file's.
|
|
#[test]
|
|
fn the_command_line_beats_the_lint_table() {
|
|
let dir = TempDir::new("lint-precedence");
|
|
dir.write("dut.sv", DUT);
|
|
dir.write("tb.sv", TB);
|
|
dir.write("lucisv.toml", "[lint]\nignore = [\"unused-signal\"]\n");
|
|
// `--ignore` replaces the config's ignore list, so `unused-signal` is back.
|
|
let out = run(&["check", &dir.root(), "--ignore", "no-tabs"]);
|
|
assert!(stdout(&out).contains("`dead` is declared but never used"), "{}", stdout(&out));
|
|
// `--select` replaces the default set; the config's ignore still applies.
|
|
let selected = run(&["check", &dir.root(), "--select", "unused-signal,no-tabs"]);
|
|
assert_eq!(stdout(&selected), "", "the config's ignore survives a CLI select");
|
|
}
|
|
|
|
/// An unknown rule name in a config file fails the run and says what is
|
|
/// known — the same answer a typo'd `--select` gets, named by file.
|
|
#[test]
|
|
fn an_unknown_rule_in_the_lint_table_is_an_error() {
|
|
let dir = TempDir::new("lint-unknown");
|
|
dir.write("dut.sv", DUT);
|
|
dir.write("tb.sv", TB);
|
|
dir.write("lucisv.toml", "[lint]\nignore = [\"no-such-rule\"]\n");
|
|
let out = run(&["check", &dir.root()]);
|
|
let text = stderr(&out);
|
|
assert!(text.contains("unknown rule: no-such-rule"), "{text}");
|
|
assert!(text.contains("unused-signal"), "the error lists what is known: {text}");
|
|
assert_eq!(code(&out), 2);
|
|
}
|
|
|
|
/// A `[lint.<rule>]` table configures that rule, and the verdict changes
|
|
/// with it — §34.0's headline claim, end to end through the real binary.
|
|
#[test]
|
|
fn a_per_rule_lint_table_changes_the_verdict() {
|
|
let dir = TempDir::new("lint-per-rule");
|
|
let wide = format!("module m;\n wire {};\nendmodule\n", "a".repeat(110));
|
|
dir.write("m.sv", &wide);
|
|
let out = run(&["check", &dir.root()]);
|
|
assert!(stdout(&out).contains("line-length"), "over the default 100: {}", stdout(&out));
|
|
|
|
dir.write("lucisv.toml", "[lint.line-length]\nlength = 130\n");
|
|
let out = run(&["check", &dir.root()]);
|
|
assert_eq!(stdout(&out), "", "the option raised the limit");
|
|
assert_eq!(code(&out), 0);
|
|
}
|
|
|
|
/// A bad entry fails the run naming the config file and the line, which is
|
|
/// what tells a user *where* to fix it.
|
|
#[test]
|
|
fn a_bad_rule_option_fails_the_run_naming_file_and_line() {
|
|
let dir = TempDir::new("lint-bad-option");
|
|
dir.write("m.sv", "module m;\nendmodule\n");
|
|
for (config, wanted) in [
|
|
("[lint.line-length]\nlength = 10\n", "must be between"),
|
|
("[lint.line-length]\nlen = 120\n", "has no option `len`"),
|
|
("[lint.no-such-rule]\nx = 1\n", "unknown rule `no-such-rule`"),
|
|
("[lint.no-tabs]\nwidth = 4\n", "takes no options"),
|
|
("[lint.signal-name-style]\nstyle_regex = \"[a-\"\n", "not a valid pattern"),
|
|
// An escape from another dialect must be refused rather than read
|
|
// as a literal, which would silently judge the wrong names (QA,
|
|
// 2026-09-04).
|
|
("[lint.signal-name-style]\nstyle_regex = \"\\bword\\b\"\n", "unsupported escape `\\b`"),
|
|
("[lint.macro-name-style]\nstyle_regex = \"(A)\\1\"\n", "unsupported escape `\\1`"),
|
|
] {
|
|
dir.write("lucisv.toml", config);
|
|
let out = run(&["check", &dir.root()]);
|
|
let text = stderr(&out);
|
|
assert!(text.contains(wanted), "{config:?}: {text}");
|
|
assert!(text.contains("lucisv.toml:2:"), "file and line: {text}");
|
|
assert_eq!(code(&out), 2, "{config:?}");
|
|
}
|
|
// Every one of them names the rule as well as the option, so an error
|
|
// read out of a CI log stands on its own.
|
|
dir.write("lucisv.toml", "[lint.line-length]\nlength = 10\n");
|
|
let text = stderr(&run(&["check", &dir.root()]));
|
|
assert!(text.contains("rule `line-length`"), "{text}");
|
|
assert!(text.contains("`length` must be between"), "{text}");
|
|
}
|
|
|
|
/// The width `line-length` holds a file to is the width the formatter is
|
|
/// configured with, so the two halves of lucisv cannot disagree.
|
|
#[test]
|
|
fn line_length_follows_the_formatters_width() {
|
|
let dir = TempDir::new("lint-width");
|
|
let wide = format!("module m;\n wire {};\nendmodule\n", "a".repeat(90));
|
|
dir.write("m.sv", &wide);
|
|
assert_eq!(stdout(&run(&["check", &dir.root()])), "", "under the default 100");
|
|
let narrow = run(&["check", &dir.root(), "--line-length", "80"]);
|
|
assert!(stdout(&narrow).contains("line-length"), "{}", stdout(&narrow));
|
|
|
|
dir.write("lucisv.toml", "line-length = 80\n");
|
|
let from_config = run(&["check", &dir.root()]);
|
|
assert!(stdout(&from_config).contains("line-length"), "{}", stdout(&from_config));
|
|
}
|
|
|
|
/// A stdin buffer reads the same `[lint.<rule>]` tables a file does.
|
|
#[test]
|
|
fn a_stdin_buffer_reads_the_rule_options_too() {
|
|
let dir = TempDir::new("lint-stdin-options");
|
|
dir.write("lucisv.toml", "[lint.macro-name-style]\nstyle_regex = \"[a-z_0-9]+\"\n");
|
|
let name = format!("{}/m.sv", dir.root());
|
|
let out = run_with_stdin(&["check", "-", "--stdin-filename", &name], "`define lower_ok 1\n");
|
|
assert_eq!(stdout(&out), "", "the config's pattern accepts it: {}", stdout(&out));
|
|
let plain = run_with_stdin(&["check", "-"], "`define lower_ok 1\n");
|
|
assert!(stdout(&plain).contains("macro-name-style"), "{}", stdout(&plain));
|
|
}
|
|
|
|
/// The QA finding of 2026-09-02, pinned end to end: a testbench that reads an
|
|
/// RTL signal only inside a macro argument — `` `uvm_info(..., $sformatf("%b",
|
|
/// dut.sig), ...) ``, the commonest VPI-probe shape there is — must silence
|
|
/// `unused-signal` for that signal. The lexer hands the whole invocation over
|
|
/// as one token, so this works only because the census splits it.
|
|
#[test]
|
|
fn a_signal_named_only_inside_a_macro_argument_is_not_reported_unused() {
|
|
let dir = TempDir::new("macroarg");
|
|
dir.write("dut.sv", DUT);
|
|
dir.write(
|
|
"tb.sv",
|
|
"module tb;\n dut u_dut ();\n initial begin\n\
|
|
\x20 `uvm_info(\"tb\", $sformatf(\"probe=%b\", u_dut.dbg_probe), UVM_LOW)\n\
|
|
\x20 end\nendmodule\n",
|
|
);
|
|
let out = run(&["check", &dir.root()]);
|
|
let text = stdout(&out);
|
|
assert!(!text.contains("dbg_probe"), "the macro argument names it: {text}");
|
|
assert!(text.contains("`dead` is declared but never used"), "and `dead` still reports: {text}");
|
|
// The format string is a value: a signal called `probe` in another file
|
|
// must not be silenced by `"probe=%b"` here.
|
|
let other = TempDir::new("macroargstring");
|
|
other.write("a.sv", "module a;\n logic probe;\nendmodule\n");
|
|
other.write(
|
|
"b.sv",
|
|
"module b;\n initial `uvm_info(\"t\", $sformatf(\"probe=%b\", 1'b0), UVM_LOW)\nendmodule\n",
|
|
);
|
|
assert!(
|
|
stdout(&run(&["check", &other.root()])).contains("`probe` is declared but never used"),
|
|
"a string's contents are not mentions"
|
|
);
|
|
}
|
|
|
|
/// `lucisv check --rules-doc` prints the generated rule reference and nothing
|
|
/// else. Generated from the registry, so a rule added without documentation
|
|
/// still shows up here — which is the whole point of the flag.
|
|
#[test]
|
|
fn rules_doc_prints_the_generated_reference() {
|
|
let out = run(&["check", "--rules-doc"]);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
let text = stdout(&out);
|
|
assert_eq!(text.lines().next(), Some("# lucisv lint rules"), "{text}");
|
|
for rule in vlog_linter::all_rules() {
|
|
let heading = format!("\n## {}\n", rule.name());
|
|
assert_eq!(
|
|
text.matches(&heading).count(),
|
|
1,
|
|
"`{}` wants exactly one section in the reference",
|
|
rule.name()
|
|
);
|
|
}
|
|
// The flag is the tool now, so it agrees with the library by construction.
|
|
assert_eq!(text, vlog_linter::rules_markdown());
|
|
}
|
|
|
|
/// The flag lints nothing, so a path beside it is a command line that means
|
|
/// two different things — refused rather than silently ignored.
|
|
#[test]
|
|
fn rules_doc_refuses_files_and_belongs_to_check() {
|
|
let dir = TempDir::new("rulesdoc");
|
|
let file = dir.write("m.sv", FORMATTED);
|
|
let out = run(&["check", "--rules-doc", &file]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(stderr(&out).contains("--rules-doc takes no files"), "{}", stderr(&out));
|
|
let out = run(&["format", "--rules-doc"]);
|
|
assert_eq!(code(&out), 2);
|
|
assert!(
|
|
stderr(&out).contains("--rules-doc is only valid for `lucisv check`"),
|
|
"{}",
|
|
stderr(&out)
|
|
);
|
|
}
|
|
|
|
// --- the walk's own warnings (docs/features/small-items-4/README.md) -------
|
|
|
|
/// A tree with the two things the walk can only warn about: a directory it
|
|
/// cannot read, and a source file that is only a broken link.
|
|
#[cfg(unix)]
|
|
fn unwalkable_tree(tag: &str) -> TempDir {
|
|
use std::os::unix::fs::{symlink, PermissionsExt};
|
|
let dir = TempDir::new(tag);
|
|
dir.write("a.sv", UNFORMATTED);
|
|
dir.write("z/late.sv", UNFORMATTED);
|
|
dir.write("locked/hidden.sv", UNFORMATTED);
|
|
symlink("nowhere.sv", dir.path.join("gone.sv")).expect("symlink");
|
|
std::fs::set_permissions(dir.path.join("locked"), std::fs::Permissions::from_mode(0o000))
|
|
.expect("chmod");
|
|
dir
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
fn lock(dir: &TempDir) {
|
|
use std::os::unix::fs::PermissionsExt;
|
|
std::fs::set_permissions(dir.path.join("locked"), std::fs::Permissions::from_mode(0o000))
|
|
.expect("chmod");
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
fn unlock(dir: &TempDir) {
|
|
use std::os::unix::fs::PermissionsExt;
|
|
let _ =
|
|
std::fs::set_permissions(dir.path.join("locked"), std::fs::Permissions::from_mode(0o755));
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(unix)]
|
|
fn an_unreadable_directory_is_a_warning_and_the_rest_of_the_tree_is_formatted() {
|
|
// The finding TODO §45.2 closes: one permission-denied directory used to
|
|
// propagate `read_dir`'s error and fail the whole run.
|
|
let dir = unwalkable_tree("walk-warn");
|
|
let out = run(&[&dir.root()]);
|
|
unlock(&dir);
|
|
|
|
assert_eq!(code(&out), 0, "a warning is not a failure: {}", stderr(&out));
|
|
let said = stderr(&out);
|
|
assert_eq!(said.matches("warning: ").count(), 2, "{said}");
|
|
assert!(said.contains("locked"), "{said}");
|
|
assert!(said.contains("gone.sv: broken symbolic link"), "{said}");
|
|
assert_eq!(dir.read("a.sv"), FORMATTED, "the other files were formatted");
|
|
assert_eq!(dir.read("z/late.sv"), FORMATTED, "including the ones sorted after it");
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(unix)]
|
|
fn the_walks_warnings_leave_check_modes_exit_code_alone() {
|
|
let dir = unwalkable_tree("walk-warn-check");
|
|
let out = run(&["--check", &dir.root()]);
|
|
unlock(&dir);
|
|
|
|
// `--check` answers on formatting alone: two files would be reformatted.
|
|
assert_eq!(code(&out), 1, "{}", stderr(&out));
|
|
assert_eq!(stderr(&out).matches("warning: ").count(), 2, "{}", stderr(&out));
|
|
assert!(!stdout(&out).contains("warning"), "warnings stay off stdout: {}", stdout(&out));
|
|
|
|
// And with nothing left to reformat the run is clean, warnings and all.
|
|
dir.write("a.sv", FORMATTED);
|
|
dir.write("z/late.sv", FORMATTED);
|
|
lock(&dir);
|
|
let out = run(&["--check", &dir.root()]);
|
|
unlock(&dir);
|
|
assert_eq!(code(&out), 0, "{}", stderr(&out));
|
|
assert_eq!(stderr(&out).matches("warning: ").count(), 2, "{}", stderr(&out));
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(unix)]
|
|
fn a_dangling_source_link_named_on_the_command_line_is_still_an_error() {
|
|
// The walk drops the link; a path the user typed is not walked at all, so
|
|
// "no such file or directory" is still what naming one gets.
|
|
let dir = TempDir::new("walk-warn-named");
|
|
std::os::unix::fs::symlink("nowhere.sv", dir.path.join("gone.sv")).expect("symlink");
|
|
let named = dir.path.join("gone.sv").to_string_lossy().into_owned();
|
|
|
|
let out = run(&[&named]);
|
|
assert_eq!(code(&out), 2, "{}", stderr(&out));
|
|
assert!(stderr(&out).contains("no such file or directory"), "{}", stderr(&out));
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// The package half of the census and `unused-import` (perf5-p3, 2026-09-07)
|
|
// ----------------------------------------------------------------------
|
|
|
|
/// A package with two exported names, and the two importers the rule has to
|
|
/// tell apart.
|
|
const PKG: &str = "package p_pkg;\n localparam int A = 1;\n localparam int B = 2;\nendpackage\n";
|
|
const USES_PKG: &str = "module uses_pkg;\n import p_pkg::*;\n localparam int C = A;\nendmodule\n";
|
|
const IGNORES_PKG: &str =
|
|
"module ignores_pkg;\n import p_pkg::*;\n localparam int C = 1;\nendmodule\n";
|
|
|
|
/// The style selection the rule lives in — `unused-import` is `Severity::Style`
|
|
/// and so is off in a default `check`.
|
|
const ALL_STYLE: [&str; 2] = ["--select", "error,warning,style"];
|
|
|
|
/// The headline: over a project, an import nothing in its file uses is
|
|
/// reported, and one the file does use is not — and it is the *package
|
|
/// census* that makes the difference, because the rule can only judge an
|
|
/// import whose package the run can answer for.
|
|
#[test]
|
|
fn an_unused_package_import_is_reported_over_a_project() {
|
|
let dir = TempDir::new("pkg-census");
|
|
dir.write("p_pkg.sv", PKG);
|
|
dir.write("uses_pkg.sv", USES_PKG);
|
|
dir.write("ignores_pkg.sv", IGNORES_PKG);
|
|
let out = run(&["check", &dir.root(), ALL_STYLE[0], ALL_STYLE[1]]);
|
|
let text = stdout(&out);
|
|
assert!(text.contains("unused-import"), "{text}");
|
|
assert!(text.contains("ignores_pkg.sv"), "{text}");
|
|
assert!(
|
|
!text.lines().any(|line| line.contains("uses_pkg.sv") && line.contains("unused-import")),
|
|
"the file that reads `A` keeps its import: {text}"
|
|
);
|
|
assert_eq!(code(&out), 1);
|
|
}
|
|
|
|
/// `unused-import` is a style rule, so a default `check` neither reports it
|
|
/// nor is changed by the presence of a package in the run.
|
|
#[test]
|
|
fn a_default_check_says_nothing_about_imports() {
|
|
let dir = TempDir::new("pkg-default");
|
|
dir.write("p_pkg.sv", PKG);
|
|
dir.write("uses_pkg.sv", USES_PKG);
|
|
dir.write("ignores_pkg.sv", IGNORES_PKG);
|
|
let out = run(&["check", &dir.root()]);
|
|
assert!(!stdout(&out).contains("unused-import"), "{}", stdout(&out));
|
|
}
|
|
|
|
/// A package the run does not contain is a package the run cannot answer
|
|
/// for, and the rule is silent about every import of it. This is the
|
|
/// conservative direction the whole feature is built around: an accusation
|
|
/// needs a known export list.
|
|
#[test]
|
|
fn an_import_of_a_package_outside_the_run_is_not_reported() {
|
|
let dir = TempDir::new("pkg-unknown");
|
|
dir.write("p_pkg.sv", PKG);
|
|
dir.write("uses_pkg.sv", USES_PKG);
|
|
dir.write(
|
|
"stranger.sv",
|
|
"module stranger;\n import elsewhere_pkg::*;\n localparam int C = 1;\nendmodule\n",
|
|
);
|
|
let text = stdout(&run(&["check", &dir.root(), ALL_STYLE[0], ALL_STYLE[1]]));
|
|
assert!(!text.contains("elsewhere_pkg"), "an unknown package is unknowable: {text}");
|
|
}
|
|
|
|
/// A package that re-exports from another one answers `None`: only a whole
|
|
/// project index settles what a re-export chain lets through, and the CLI's
|
|
/// one-file census will not guess. Every import of it goes quiet.
|
|
#[test]
|
|
fn an_import_of_a_re_exporting_package_is_not_reported() {
|
|
let dir = TempDir::new("pkg-reexport");
|
|
dir.write("p_pkg.sv", PKG);
|
|
dir.write(
|
|
"r_pkg.sv",
|
|
"package r_pkg;\n import p_pkg::*;\n export p_pkg::*;\n localparam int D = 1;\n\
|
|
endpackage\n",
|
|
);
|
|
dir.write(
|
|
"ignores_r.sv",
|
|
"module ignores_r;\n import r_pkg::*;\n localparam int C = 1;\nendmodule\n",
|
|
);
|
|
let text = stdout(&run(&["check", &dir.root(), ALL_STYLE[0], ALL_STYLE[1]]));
|
|
assert!(!text.contains("r_pkg"), "a re-export chain is unknowable: {text}");
|
|
}
|
|
|
|
/// Two files declaring one package name is a name the run cannot answer for,
|
|
/// whatever the two say — `Census::of`'s merge turns the second contribution
|
|
/// into `None`, and the rule goes quiet about it.
|
|
#[test]
|
|
fn an_import_of_a_package_two_files_declare_is_not_reported() {
|
|
let dir = TempDir::new("pkg-duplicate");
|
|
dir.write("p_pkg.sv", PKG);
|
|
dir.write("p_pkg_copy.sv", "package p_pkg;\n localparam int Z = 9;\nendpackage\n");
|
|
dir.write("ignores_pkg.sv", IGNORES_PKG);
|
|
let text = stdout(&run(&["check", &dir.root(), ALL_STYLE[0], ALL_STYLE[1]]));
|
|
assert!(!text.contains("unused-import"), "a duplicate package is unknowable: {text}");
|
|
}
|
|
|
|
/// The gate is a claim about *cost*, not about answers, so the answers get
|
|
/// their own end-to-end pin: a run whose only package file is surrounded by
|
|
/// files holding the word `package` in strings and comments reports exactly
|
|
/// what a run without those files reports.
|
|
#[test]
|
|
fn text_that_merely_says_package_changes_no_finding() {
|
|
let bare = TempDir::new("pkg-bare");
|
|
bare.write("p_pkg.sv", PKG);
|
|
bare.write("ignores_pkg.sv", IGNORES_PKG);
|
|
let plain = stdout(&run(&["check", &bare.root(), ALL_STYLE[0], ALL_STYLE[1]]));
|
|
|
|
let noisy = TempDir::new("pkg-noisy");
|
|
noisy.write("p_pkg.sv", PKG);
|
|
noisy.write("ignores_pkg.sv", IGNORES_PKG);
|
|
noisy.write(
|
|
"talker.sv",
|
|
"// package q_pkg; endpackage\n\
|
|
module talker;\n initial $display(\"package q_pkg; endpackage\");\nendmodule\n",
|
|
);
|
|
let with_noise = stdout(&run(&["check", &noisy.root(), ALL_STYLE[0], ALL_STYLE[1]]));
|
|
for finding in plain.lines().filter(|line| line.contains("unused-import")) {
|
|
let tail = finding.rsplit('/').next().unwrap_or(finding);
|
|
assert!(with_noise.contains(tail), "{tail} lost: {with_noise}");
|
|
}
|
|
assert_eq!(
|
|
plain.matches("unused-import").count(),
|
|
with_noise.matches("unused-import").count(),
|
|
"{plain}\n---\n{with_noise}"
|
|
);
|
|
}
|