mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-04 01:20:34 -06:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
333f5801c4 | ||
|
|
ecfed2acaa |
@@ -278,18 +278,23 @@ fn collect_inputs(inputs: &[String]) -> Discovery {
|
||||
}
|
||||
|
||||
/// Returns false only when the global file budget stops traversal.
|
||||
fn walk(dir: &Path, depth: usize, out: &mut Discovery, seen: &mut std::collections::HashSet<u64>) -> bool {
|
||||
fn walk(dir: &Path, depth: usize, out: &mut Discovery, seen: &mut std::collections::HashSet<(u64, u64)>) -> bool {
|
||||
// A directory is visited once by identity (dev+inode on unix): several links to the same
|
||||
// directory are aliases, not exponential branches of the scan. Other platforms keep the
|
||||
// depth cap only.
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::MetadataExt as _;
|
||||
if let Ok(m) = std::fs::metadata(dir)
|
||||
&& let Some(id) = m.dev().checked_mul(1 << 60).and_then(|d| d.checked_add(m.ino()))
|
||||
&& !seen.insert(id)
|
||||
{
|
||||
return true;
|
||||
if let Ok(m) = std::fs::metadata(dir) {
|
||||
let identity = (m.dev(), m.ino());
|
||||
// Already scanned: an alias. A directory past the depth limit is not scanned, so it is
|
||||
// not recorded: asked for by name later, it is walked from depth 0.
|
||||
if seen.contains(&identity) {
|
||||
return true;
|
||||
}
|
||||
if depth <= MAX_DEPTH {
|
||||
seen.insert(identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
if depth > MAX_DEPTH {
|
||||
|
||||
@@ -205,6 +205,24 @@ fn check_reports_a_depth_limited_scan_while_checking_readable_files() {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_directory_past_the_depth_limit_is_still_scanned_when_named_as_an_input() {
|
||||
let dir = tmpdir("check-depth-named");
|
||||
// The first folder past the limit (33 levels down): the scan from the top reports it unreached.
|
||||
let mut deep = dir.clone();
|
||||
for _ in 0..33 {
|
||||
deep.push("d");
|
||||
}
|
||||
std::fs::create_dir_all(&deep).unwrap();
|
||||
ok(&["run", "--out", deep.join("deep.xlsx").to_str().unwrap(), "--quiet"]);
|
||||
let out = run(&["check", dir.to_str().unwrap(), deep.to_str().unwrap(), "--json"]);
|
||||
let report: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
|
||||
// The first scan reports the subtree it could not reach; the named directory is checked.
|
||||
assert_eq!(report["totals"]["opened"], 1, "{report}");
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
/// A script line whose parameters are under a key `run` does not read used to run the command
|
||||
/// with no parameters at all, and the run still exited 0: three mistaken lines made Sheet2,
|
||||
/// Sheet3 and Sheet4 instead of the named sheets, and nothing said so (#2256).
|
||||
|
||||
@@ -52,19 +52,14 @@ pub(super) fn abs_ref(c: CellRef) -> String {
|
||||
format!("${}${}", gridcraft_core::col_to_letters(c.col), u64::from(c.row) + 1)
|
||||
}
|
||||
|
||||
/// How many cells to the left and above a cell its name is searched for.
|
||||
const NAME_REACH: u32 = 1_000;
|
||||
|
||||
/// A cell's name in the reports: the nearest text to its left and the nearest text above it.
|
||||
pub(super) fn cell_name(sh: &Sheet, c: CellRef) -> String {
|
||||
let text = |c: CellRef| match sh.value(c) {
|
||||
Value::Text(t) if !t.trim().is_empty() => Some(t.to_string()),
|
||||
_ => None,
|
||||
};
|
||||
// Labels are next to their cells: looking further than this on a sparse sheet only costs time
|
||||
// (a thousand cells near the last row would each probe a million empty coordinates).
|
||||
let left = (c.col.saturating_sub(NAME_REACH)..c.col).rev().find_map(|col| text(CellRef::new(c.row, col)));
|
||||
let top = (c.row.saturating_sub(NAME_REACH)..c.row).rev().find_map(|row| text(CellRef::new(row, c.col)));
|
||||
let left = (0..c.col).rev().find_map(|col| text(CellRef::new(c.row, col)));
|
||||
let top = (0..c.row).rev().find_map(|row| text(CellRef::new(row, c.col)));
|
||||
[left, top].into_iter().flatten().collect::<Vec<_>>().join(" ")
|
||||
}
|
||||
|
||||
@@ -443,17 +438,3 @@ pub(super) fn report(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_name_is_searched_only_a_bounded_distance() {
|
||||
let mut sh = Sheet::new("S".to_string());
|
||||
sh.set_cell(CellRef::new(0, 0), gridcraft_model::Cell::value(Value::text("Far")));
|
||||
sh.set_cell(CellRef::new(5, 4), gridcraft_model::Cell::value(Value::text("Near")));
|
||||
assert_eq!(cell_name(&sh, CellRef::new(5, 5)), "Near");
|
||||
assert_eq!(cell_name(&sh, CellRef::new(0, NAME_REACH + 1)), "");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,9 +189,6 @@ pub(super) fn scenario_delete(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
Ok(json!({"deleted": removed.name}))
|
||||
}
|
||||
|
||||
/// Most changing-cell-by-scenario cells one Scenario Summary writes.
|
||||
const MAX_SUMMARY_CELLS: usize = 1_000_000;
|
||||
|
||||
pub(super) fn scenario_summary(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
const CMD: &str = "data.scenarioSummary";
|
||||
let d = s.doc()?;
|
||||
@@ -214,24 +211,17 @@ pub(super) fn scenario_summary(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
// Changing cells of every scenario, first-seen order; per scenario its values.
|
||||
let mut changing: Vec<CellRef> = Vec::new();
|
||||
let mut per: Vec<HashMap<CellRef, Value>> = Vec::new();
|
||||
let mut seen: std::collections::HashSet<CellRef> = Default::default();
|
||||
for sc in &scenarios {
|
||||
let (_, cells) = cell_list(&d.wb, &sc.changing, CMD, MAX_SCENARIO_CELLS)?;
|
||||
let mut m = HashMap::new();
|
||||
for (c, v) in cells.iter().zip(sc.values.iter()) {
|
||||
if seen.insert(*c) {
|
||||
if !changing.contains(c) {
|
||||
changing.push(*c);
|
||||
}
|
||||
m.insert(*c, json_value(v, &d.wb));
|
||||
}
|
||||
per.push(m);
|
||||
}
|
||||
// The summary has a cell for every changing cell in every scenario, filled from the current
|
||||
// value where a scenario doesn't set it, so small disjoint scenarios multiply.
|
||||
// Each result cell is a row of the summary too, with a cell per scenario.
|
||||
if changing.len().saturating_add(results.len()).saturating_mul(per.len()) > MAX_SUMMARY_CELLS {
|
||||
return Err(bad(CMD, "the scenarios together change too many cells for a summary"));
|
||||
}
|
||||
let current: Vec<Value> = changing.iter().map(|c| sh.value(*c)).collect();
|
||||
let current_results: Vec<Value> = results.iter().map(|c| sh.value(*c)).collect();
|
||||
let mut trial = Trial::new(d);
|
||||
|
||||
@@ -297,25 +297,3 @@ fn consolidate_by_position_and_label() {
|
||||
assert!(s.execute("data.consolidate", json!({"sources": []})).is_err());
|
||||
assert!(s.execute("data.consolidate", json!({"sources": ["A1"], "function": "median"})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scenario_summary_refuses_a_matrix_over_its_cell_limit() {
|
||||
let mut s = s();
|
||||
// 200 scenarios of 32 disjoint cells: 6,400 changing cells x 200 scenarios = 1.28 million cells.
|
||||
for i in 0..200 {
|
||||
let (a, b) = (i * 32 + 1, i * 32 + 32);
|
||||
s.execute("data.scenarioAdd", json!({"name": format!("S{i}"), "changing": format!("A{a}:A{b}")})).unwrap();
|
||||
}
|
||||
assert!(s.execute("data.scenarioSummary", json!({})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scenario_summary_counts_result_rows_in_its_cell_limit() {
|
||||
let mut s = s();
|
||||
// 900 scenarios of one distinct cell each are 810,000 cells; 255 result rows take it past the limit.
|
||||
for i in 0..900 {
|
||||
s.execute("data.scenarioAdd", json!({"name": format!("S{i}"), "changing": format!("A{}", i + 1)})).unwrap();
|
||||
}
|
||||
let err = s.execute("data.scenarioSummary", json!({"resultCells": "C1:C255"})).unwrap_err().to_string();
|
||||
assert!(err.contains("too many cells"), "{err}");
|
||||
}
|
||||
|
||||
@@ -237,22 +237,6 @@ const JOURNAL_MAX_ENTRIES: usize = 10_000;
|
||||
pub(crate) const JOURNAL_MAX_BYTES: usize = 64 * 1024 * 1024;
|
||||
const UNDO_BYTES_LIMIT: usize = 128 * 1024 * 1024;
|
||||
|
||||
/// Comment and hyperlink text of a sheet: a changed sheet's snapshot keeps its own copy of these
|
||||
/// strings (an edit deep-copies them), so they count against the history budget with the cells.
|
||||
fn sheet_text_bytes(sheet: &gridcraft_model::Sheet) -> usize {
|
||||
let comments = sheet.comments.values().fold(0usize, |sum, c| {
|
||||
// The vectors' own storage counts too: an empty reply still takes an element.
|
||||
let replies = c.replies.iter().fold(0usize, |n, (a, t)| n.saturating_add(a.len()).saturating_add(t.len()));
|
||||
let replies = replies.saturating_add(c.replies.capacity().saturating_mul(std::mem::size_of::<(String, String)>()));
|
||||
let dates = c.dates.iter().fold(0usize, |n, d| n.saturating_add(d.len()));
|
||||
let dates = dates.saturating_add(c.dates.capacity().saturating_mul(std::mem::size_of::<String>()));
|
||||
sum.saturating_add(std::mem::size_of::<gridcraft_model::Comment>() + c.author.len() + c.text.len() + replies + dates)
|
||||
});
|
||||
sheet.hyperlinks.values().fold(comments, |sum, h| {
|
||||
sum.saturating_add(std::mem::size_of::<gridcraft_model::Hyperlink>() + h.target.len() + h.tooltip.as_ref().map_or(0, String::len))
|
||||
})
|
||||
}
|
||||
|
||||
fn history_retained_bytes(older: &Workbook, newer: &Workbook) -> usize {
|
||||
let mut bytes = std::mem::size_of::<Workbook>();
|
||||
for (index, sheet) in older.sheets.iter().enumerate() {
|
||||
@@ -264,7 +248,6 @@ fn history_retained_bytes(older: &Workbook, newer: &Workbook) -> usize {
|
||||
let empty = gridcraft_model::CellStore::new();
|
||||
let next = newer.sheets.get(index).map_or(&empty, |next| &next.cells);
|
||||
bytes = bytes.saturating_add(sheet.cells.retained_bytes_against(next));
|
||||
bytes = bytes.saturating_add(sheet_text_bytes(sheet));
|
||||
}
|
||||
// Snapshots share the macro list until a change to it; a snapshot with its own list keeps it.
|
||||
if !older.macros.shares(&newer.macros) {
|
||||
|
||||
@@ -5572,26 +5572,3 @@ fn clearing_links_and_notes_keeps_allow_edit_ranges() {
|
||||
let sh = s.doc().unwrap().wb.active().unwrap().clone();
|
||||
assert!(sh.hyperlinks.is_empty() && sh.comments.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_charges_a_changed_sheets_comment_text() {
|
||||
let mut s = s();
|
||||
s.execute("review.newNote", json!({"cell": "A1", "text": "n".repeat(30_000), "author": "Example"})).unwrap();
|
||||
let older = (*s.doc().unwrap().wb).clone();
|
||||
s.execute("cell.set", json!({"cell": "B2", "input": "x"})).unwrap();
|
||||
let newer = (*s.doc().unwrap().wb).clone();
|
||||
// The older snapshot owns the comment text now that the sheet changed under it.
|
||||
assert!(crate::history_retained_bytes(&older, &newer) >= 30_000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_charges_the_storage_of_empty_comment_replies() {
|
||||
let mut s = s();
|
||||
s.execute("review.newComment", json!({"cell": "A1", "text": "c", "author": "Ann"})).unwrap();
|
||||
for _ in 0..500 {
|
||||
s.execute("review.replyComment", json!({"cell": "A1", "text": "", "author": ""})).unwrap();
|
||||
}
|
||||
let sheet = s.doc().unwrap().wb.sheet(0).unwrap().clone();
|
||||
// 500 replies of no text are still 500 elements.
|
||||
assert!(crate::sheet_text_bytes(&sheet) >= 500 * std::mem::size_of::<(String, String)>());
|
||||
}
|
||||
|
||||
+33
-5
@@ -13,7 +13,10 @@ struct Counts {
|
||||
proptests: usize,
|
||||
}
|
||||
|
||||
fn walk(dir: &Path, c: &mut Counts, seen: &mut std::collections::HashSet<u64>, budget: &mut usize) {
|
||||
/// Larger files (or devices behind a symlink) are not source files and are not read.
|
||||
const MAX_SOURCE_BYTES: u64 = 16 << 20;
|
||||
|
||||
fn walk(dir: &Path, c: &mut Counts, seen: &mut std::collections::HashSet<(u64, u64)>, budget: &mut usize) {
|
||||
if *budget == 0 {
|
||||
// Hard stop for pathological trees: two directory symlinks pointing at each other
|
||||
// would otherwise be walked exponentially.
|
||||
@@ -22,9 +25,9 @@ fn walk(dir: &Path, c: &mut Counts, seen: &mut std::collections::HashSet<u64>, b
|
||||
// Directory identities (dev+inode on unix) make a symlink to a visited directory a
|
||||
// no-op, not another exponential branch of the scan.
|
||||
#[cfg(unix)]
|
||||
let id = std::fs::metadata(dir).ok().and_then(|m| m.dev().checked_mul(1 << 60).and_then(|d| d.checked_add(m.ino())));
|
||||
let id = std::fs::metadata(dir).ok().map(|m| (m.dev(), m.ino()));
|
||||
#[cfg(not(unix))]
|
||||
let id: Option<u64> = None;
|
||||
let id: Option<(u64, u64)> = None;
|
||||
if let Some(id) = id
|
||||
&& !seen.insert(id)
|
||||
{
|
||||
@@ -45,7 +48,9 @@ fn walk(dir: &Path, c: &mut Counts, seen: &mut std::collections::HashSet<u64>, b
|
||||
walk(&p, c, seen, budget);
|
||||
}
|
||||
} else if name.ends_with(".rs")
|
||||
&& let Ok(s) = std::fs::read_to_string(&p)
|
||||
// The entry's own metadata does not follow a symlink: only regular files are sources.
|
||||
&& e.metadata().is_ok_and(|m| m.is_file() && m.len() <= MAX_SOURCE_BYTES)
|
||||
&& let Some(s) = read_source(&p)
|
||||
{
|
||||
c.files += 1;
|
||||
c.lines += s.lines().count();
|
||||
@@ -56,6 +61,15 @@ fn walk(dir: &Path, c: &mut Counts, seen: &mut std::collections::HashSet<u64>, b
|
||||
}
|
||||
}
|
||||
|
||||
/// A source file's text, read up to [`MAX_SOURCE_BYTES`]: one that grew or was replaced since its
|
||||
/// size was checked is skipped, not read whole.
|
||||
fn read_source(p: &Path) -> Option<String> {
|
||||
use std::io::Read as _;
|
||||
let mut s = String::new();
|
||||
std::fs::File::open(p).ok()?.take(MAX_SOURCE_BYTES + 1).read_to_string(&mut s).ok()?;
|
||||
(s.len() as u64 <= MAX_SOURCE_BYTES).then_some(s)
|
||||
}
|
||||
|
||||
/// `#[test]` attributes and `proptest!` blocks (each block may hold many
|
||||
/// property tests, and macro-generated tests are only counted once — use
|
||||
/// `--exact` for harness-accurate numbers).
|
||||
@@ -129,11 +143,25 @@ pub fn run(_root: &Path, exact: bool) -> Result<(), String> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::count_tests;
|
||||
use super::{MAX_SOURCE_BYTES, count_tests, read_source};
|
||||
|
||||
#[test]
|
||||
fn counts_attributes_and_proptest_blocks() {
|
||||
let src = "#[test]\nfn a() {}\n// #[test] commented\n #[test]\nfn b() {}\nproptest! {\n}\n";
|
||||
assert_eq!(count_tests(src), (2, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_source_past_the_size_limit_is_not_read() {
|
||||
let dir = std::env::temp_dir().join(format!("xtask-stats-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let (small, big) = (dir.join("a.rs"), dir.join("b.rs"));
|
||||
std::fs::write(&small, "fn a() {}\n").unwrap();
|
||||
let file = std::fs::File::create(&big).unwrap();
|
||||
file.set_len(MAX_SOURCE_BYTES + 1).unwrap(); // sparse: nothing is written
|
||||
drop(file);
|
||||
assert_eq!(read_source(&small).as_deref(), Some("fn a() {}\n"));
|
||||
assert_eq!(read_source(&big), None);
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user