state/mod.rs 986 -> 63 production lines: document.rs (one analyzed buffer), buffers.rs (open buffers), views.rs (closed-file view cache), scope.rs (roots, settings, scope ladder), rescan.rs (scan and widen protocol, 411), incremental.rs (one saved file re-indexed), sources.rs (file sets for refactorings, 170). The 3 944-line inline test module moved to state/tests/ (14 topic files). No function body changed; only forced pub(super) widenings. Lead's line-multiset check: no code line lost or added. CLAUDE.md measurement refreshed (2026-10-14); a stale test path in server/tests/folders.rs corrected. Gates on the merged tree: fmt and clippy -D warnings clean; lucisv_server 1 823 passed, 0 failed, 38 ignored (= before); the lane's workspace run 80 suites, 5 078 passed. Refactor dev Claude Fable 5.1. Co-Authored-By: Claude Opus 5.5 <[email protected]>
284 lines
9.3 KiB
Rust
284 lines
9.3 KiB
Rust
//! The `Workspace` tests: one file per topic, every one driven through the
|
|
//! real `Workspace` against a scratch tree on disk.
|
|
//!
|
|
//! This file is the harness — `Tree`, a workspace root removed on drop —
|
|
//! followed by the helpers more than one file uses: settings as a client
|
|
//! would push them, a buffer opened from disk, and the ways a scan's
|
|
//! outcome is read back. The sibling files are the sections the one big
|
|
//! test module used to have, under the names they had; a helper used by
|
|
//! one section only stayed with it.
|
|
|
|
use std::fs;
|
|
use std::slice::from_ref;
|
|
use std::str::FromStr;
|
|
|
|
use super::*;
|
|
use crate::settings::IndexScope;
|
|
|
|
mod document;
|
|
mod filelist;
|
|
mod folder_scope;
|
|
mod folders;
|
|
mod incremental;
|
|
mod interleave;
|
|
mod large_file;
|
|
mod lazy_read;
|
|
mod open_buffers;
|
|
mod persistence;
|
|
mod settings;
|
|
mod size_cap;
|
|
mod views;
|
|
mod widen;
|
|
|
|
fn uri(s: &str) -> Uri {
|
|
Uri::from_str(s).expect("test uri parses")
|
|
}
|
|
|
|
/// A scratch workspace root. Removed on drop.
|
|
struct Tree {
|
|
path: PathBuf,
|
|
}
|
|
|
|
impl Tree {
|
|
fn new(name: &str) -> Tree {
|
|
let path =
|
|
std::env::temp_dir().join(format!("lucisv_server_scan_{}_{name}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&path);
|
|
fs::create_dir_all(&path).expect("temp dir");
|
|
Tree { path }
|
|
}
|
|
|
|
fn write(&self, rel: &str, text: &str) {
|
|
let path = self.path.join(rel);
|
|
if let Some(parent) = path.parent() {
|
|
fs::create_dir_all(parent).expect("parent");
|
|
}
|
|
fs::write(&path, text).expect("write");
|
|
}
|
|
|
|
/// A package declaring one localparam, so the index can be asked
|
|
/// whether this file was scanned at all.
|
|
fn package(&self, rel: &str, name: &str) {
|
|
self.write(rel, &format!("package {name};\n localparam K = 1;\nendpackage\n"));
|
|
}
|
|
}
|
|
|
|
impl Drop for Tree {
|
|
fn drop(&mut self) {
|
|
let _ = fs::remove_dir_all(&self.path);
|
|
}
|
|
}
|
|
|
|
/// Open `rel` in `ws` as the buffer it is on disk.
|
|
fn open(ws: &mut Workspace, tree: &Tree, rel: &str) {
|
|
let path = tree.path.join(rel);
|
|
let text = fs::read_to_string(&path).expect("the fixture is on disk");
|
|
let uri = crate::uri::path_to_uri(&path).expect("uri");
|
|
ws.set_document(uri, text);
|
|
}
|
|
|
|
/// A buffer of exactly `bytes` bytes that is also real Verilog.
|
|
fn filler(bytes: usize) -> String {
|
|
let mut text = String::from("module m;\n");
|
|
while text.len() + 9 <= bytes {
|
|
text.push_str(" wire a;\n");
|
|
}
|
|
while text.len() < bytes {
|
|
text.push(' ');
|
|
}
|
|
text
|
|
}
|
|
|
|
/// A tree of three files that declare each other's names and include each
|
|
/// other, so a half-published index has something to get wrong.
|
|
fn tangled(tag: &str) -> Tree {
|
|
let tree = Tree::new(tag);
|
|
tree.write("defs.svh", "`define WIDE 32\n");
|
|
tree.write(
|
|
"a.sv",
|
|
"`include \"defs.svh\"\npackage a;\n localparam K = 1;\n \
|
|
typedef logic [`WIDE-1:0] word_t;\nendpackage\n",
|
|
);
|
|
tree.write(
|
|
"b.sv",
|
|
"`include \"defs.svh\"\nmodule b;\n import a::*;\n word_t w;\n \
|
|
localparam K2 = a::K;\nendmodule\n",
|
|
);
|
|
tree.write("c.sv", "module c;\n b inner ();\nendmodule\n");
|
|
tree
|
|
}
|
|
|
|
/// `settings` with the pre-2026-08-27 whole-workspace scope asked for —
|
|
/// the push-door twin of [`Workspace::open_whole_workspace`], needed
|
|
/// wherever a test pushes settings *after* opening its roots, since a push
|
|
/// replaces every field including the scope.
|
|
fn wide(settings: ClientSettings) -> ClientSettings {
|
|
ClientSettings { index_scope: IndexScope::Workspace, ..settings }
|
|
}
|
|
|
|
/// `lucisv.exclude` as a client would push it.
|
|
fn excluding(patterns: &[&str]) -> ClientSettings {
|
|
ClientSettings {
|
|
exclude: patterns.iter().map(|p| p.to_string()).collect(),
|
|
index_scope_depth: 0,
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// The folder rung at `scopeDepth: 0` — the open file's own folder and
|
|
/// nothing above it.
|
|
///
|
|
/// Said out loud since 2026-08-28, when the shipped default became
|
|
/// [`crate::settings::DEFAULT_SCOPE_DEPTH`]: a test about *this* rung
|
|
/// names the depth it is about rather than riding whatever the default is
|
|
/// this month. The behaviour it selects is unchanged.
|
|
fn own_folder_only(ws: &mut Workspace) {
|
|
ws.set_client_settings(ClientSettings { index_scope_depth: 0, ..Default::default() });
|
|
}
|
|
|
|
/// `lucisv.filelist` as a client would push it.
|
|
fn using_filelist(path: &str) -> ClientSettings {
|
|
ClientSettings { filelist: Some(path.to_string()), ..Default::default() }
|
|
}
|
|
|
|
/// A `ClientSettings` with the size cap set to `bytes`.
|
|
fn capping(bytes: u64) -> ClientSettings {
|
|
ClientSettings { max_file_size: bytes, ..Default::default() }
|
|
}
|
|
|
|
/// A file of `bytes` bytes that would declare `name` if anything read it.
|
|
/// Padded with a trailing comment, so what makes it big is not what makes
|
|
/// it a package.
|
|
fn fat_package(tree: &Tree, rel: &str, name: &str, bytes: usize) {
|
|
let head = format!("package {name};\n localparam K = 1;\nendpackage\n// ");
|
|
let mut text = String::with_capacity(bytes + head.len());
|
|
text.push_str(&head);
|
|
while text.len() < bytes {
|
|
text.push('x');
|
|
}
|
|
text.push('\n');
|
|
tree.write(rel, &text);
|
|
}
|
|
|
|
/// A client that keeps its index cache in `dir`.
|
|
fn caching(dir: &Path) -> ClientSettings {
|
|
ClientSettings {
|
|
index_cache_path: Some(dir.to_string_lossy().into_owned()),
|
|
index_scope_depth: 0,
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// What the index holds, as the names of the packages it found.
|
|
fn indexed(ws: &Workspace, names: &[&str]) -> Vec<String> {
|
|
names.iter().filter(|n| ws.index().member(n, "K").len() == 1).map(|n| n.to_string()).collect()
|
|
}
|
|
|
|
/// Everything a scan produces, rendered as one comparable value: the
|
|
/// index's own witness, the completion tables and the include graph.
|
|
///
|
|
/// The index's fingerprint alone would not do — this round publishes all
|
|
/// three tables together, so all three have to be shown to come out the
|
|
/// same however many publishes happened on the way.
|
|
fn everything(ws: &Workspace) -> String {
|
|
let mut out = ws.index().fingerprint();
|
|
out.push_str(&format!("words {:?}\n", ws.candidates().words("")));
|
|
out.push_str(&format!(
|
|
"units {:?}\n",
|
|
ws.candidates().units("").iter().map(|c| &c.name).collect::<Vec<_>>()
|
|
));
|
|
out.push_str(&format!(
|
|
"macros {:?}\n",
|
|
ws.candidates().macros("").iter().map(|m| &m.name).collect::<Vec<_>>()
|
|
));
|
|
let mut reachable: Vec<String> = ws
|
|
.index()
|
|
.files()
|
|
.iter()
|
|
.map(|path| {
|
|
let mut hops: Vec<String> = ws
|
|
.includes()
|
|
.reachable(vec![path.clone()])
|
|
.iter()
|
|
.filter_map(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
|
|
.collect();
|
|
hops.sort();
|
|
format!("reaches {} {hops:?}", path.display())
|
|
})
|
|
.collect();
|
|
reachable.sort();
|
|
out.push_str(&reachable.join("\n"));
|
|
out
|
|
}
|
|
|
|
/// One [`IndexEvent`] as the word and the numbers a caller would report.
|
|
fn seen(event: &IndexEvent) -> String {
|
|
match event {
|
|
IndexEvent::Parsing { done, total, path } => {
|
|
format!("indexing {done}/{total} {}", path.display())
|
|
}
|
|
IndexEvent::Cached { done, total, path } => {
|
|
format!("cached {done}/{total} {}", path.display())
|
|
}
|
|
IndexEvent::Analyzed { done, total, path } => {
|
|
format!("analyzed {done}/{total} {}", path.display())
|
|
}
|
|
IndexEvent::Skipped { path, size, cap } => {
|
|
format!("skipped {} {size} {cap}", path.display())
|
|
}
|
|
}
|
|
}
|
|
|
|
/// What one whole scan did, file by file: what it parsed, what it took
|
|
/// from the cache instead, and what came from an open buffer's own
|
|
/// analysis. The three together are the work list.
|
|
#[derive(Debug, Default, PartialEq, Eq)]
|
|
struct Scan {
|
|
parsed: Vec<String>,
|
|
cached: Vec<String>,
|
|
analyzed: Vec<String>,
|
|
}
|
|
|
|
/// Scan `roots` (or rescan what is already open, with `None`) and record
|
|
/// which files were parsed and which came out of the cache.
|
|
fn scan(ws: &mut Workspace, roots: Option<Vec<PathBuf>>) -> Scan {
|
|
let mut out = Scan::default();
|
|
{
|
|
let mut see = |event: IndexEvent| match event {
|
|
IndexEvent::Parsing { path, .. } => out.parsed.push(file_name(path)),
|
|
IndexEvent::Cached { path, .. } => out.cached.push(file_name(path)),
|
|
IndexEvent::Analyzed { path, .. } => out.analyzed.push(file_name(path)),
|
|
IndexEvent::Skipped { .. } => {}
|
|
};
|
|
match roots {
|
|
Some(roots) => ws.open_whole_workspace_with(roots, &mut see),
|
|
None => ws.rebuild_index_with(&mut see),
|
|
};
|
|
}
|
|
out
|
|
}
|
|
|
|
fn file_name(path: &Path) -> String {
|
|
path.file_name().expect("a file").to_string_lossy().into_owned()
|
|
}
|
|
|
|
/// Every source file under `dir`, for the corpus sweep above.
|
|
fn walk_sources(dir: &Path) -> Vec<PathBuf> {
|
|
let mut out = Vec::new();
|
|
let mut stack = vec![dir.to_path_buf()];
|
|
while let Some(next) = stack.pop() {
|
|
let Ok(entries) = fs::read_dir(&next) else { continue };
|
|
for entry in entries.flatten() {
|
|
let path = entry.path();
|
|
if path.is_dir() {
|
|
stack.push(path);
|
|
} else if matches!(path.extension().and_then(|e| e.to_str()), Some("sv" | "v" | "svh"))
|
|
{
|
|
out.push(path);
|
|
}
|
|
}
|
|
}
|
|
out.sort();
|
|
out
|
|
}
|