Skip to content

test: write test files under a per-test temp dir, and inject the cache settings - #2894

Merged
robcohen merged 2 commits into
mainfrom
fix/test-tempdirs
Nov 8, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/test-tempdirs

Conversation

@robcohen

@robcohen robcohen commented Nov 7, 2026

Copy link
Copy Markdown
Member

What changed

Per-test temporary directories. Several tests wrote fixed file names under the shared system temp directory, so two concurrent cargo test runs deleted each other's files. A run of rustledger::integration_test failed this way while another lane was running the same suite. A test that returned early ("Skipping: ...") also left its file behind. These tests now write under a tempfile::TempDir that is removed on drop, and the remove_file(..).ok() cleanups that swallowed their errors are gone:

File Sites
crates/rustledger/tests/integration_test.rs 13
crates/rustledger/tests/cli_commands_test.rs 2
crates/rustledger-loader/src/cache.rs (unit tests) 5
crates/rustledger-loader/tests/cache_env_var_test.rs 3
crates/rustledger/src/cmd/price/cache.rs (test_save_and_load_round_trip) 1

Injected cache settings. The loader's cache read BEANCOUNT_LOAD_CACHE_FILENAME and BEANCOUNT_DISABLE_LOAD_CACHE straight from the process environment. That left the cache tests two bad options. cache_env_var_test called set_var under a mutex, in a binary that opted out of unsafe_code = deny. The unit tests and cache_roundtrip_properties instead asserted that the developer's environment was clean, which breaks on a machine that sets the variable (AGENTS.md: inject environmental inputs; never set_var in parallel tests).

  • New type: CacheEnv carries the two settings as a value. It is built by CacheEnv::from_process() or by CacheEnv::from_lookup(lookup), which takes an injected lookup.
  • New functions: cache_path_in, load_cache_entry_in, save_cache_entry_in and invalidate_cache_in take a CacheEnv. The existing functions read the process environment and delegate to them, so no caller changes.
  • Tests: they pass their settings in. cache_env_var_test no longer mutates the environment and no longer needs #![allow(unsafe_code)]. The reading rule is still tested through from_lookup: the variable names, an empty pattern counting as unset, and any disable value (including empty) disabling. No test asserts the environment any more.

Why

These are concurrent-run collisions found while several lanes ran the workspace tests together under one TMPDIR. This also follows the hermetic-test rule in AGENTS.md.

How to test

cargo test -p rustledger-loader --all-features
cargo test -p rustledger --all-features --test integration_test --test cli_commands_test

Verification

  • Concurrency: I ran the integration_test binary four times at once and the cli_commands_test binary twice at once, all under one shared TMPDIR. Every run passed (19 and 63 tests).
  • Checks: cargo test --workspace --all-features --no-fail-fast, cargo clippy --all-features --all-targets -- -D warnings, RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps and cargo fmt --check all pass.

Not changed

  • The production cache cleanup (invalidate_cache, the legacy-sidecar removal, and the price cache's clear) still ignores a failed remove_file, on purpose: a missing file is the expected case there.
  • Tests that already name their files by process id or a counter (decimal_overflow_test, the LSP tests, add_cmd, corpus_baseline's determinism check) are left as they are.

🤖 Generated with Claude Code

…e settings

Tests in the CLI (`integration_test.rs`, 13 sites; `cli_commands_test.rs`,
2), the loader cache (unit tests, `cache_roundtrip_properties.rs`,
`cache_env_var_test.rs`) and the price cache wrote fixed names under the
shared system temp dir, so two concurrent `cargo test` runs deleted each
other's files, and an early `return` left a file behind. Each now writes
under a `tempfile::TempDir`, removed on drop, and the `remove_file(..).ok()`
cleanups that swallowed errors are gone.

The loader cache read its two environment variables directly, so the
cache tests either mutated process env (`set_var` under a mutex, in a
binary that opted out of `unsafe_code = deny`) or asserted the
developer's env was clean. `CacheEnv` now carries the two settings as a
value (`from_process`, `from_lookup`), and `cache_path_in`,
`load_cache_entry_in`, `save_cache_entry_in` and `invalidate_cache_in`
take it; the plain functions read the process env and delegate. The
tests pass their settings in: no `set_var`, no `unsafe`, no env
precondition.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 7, 2026 19:13
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 39.7ms ± 3.7ms 555.4ms ± 2.8ms 14.0x 43.0 MB / 51.0 MB
investment 59.8ms ± 0.8ms 2105.0ms ± 4.6ms 35.2x 59.0 MB / 59.0 MB
fifo 57.6ms ± 0.3ms 11673.5ms ± 99.8ms 202.8x 54.9 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 38.6ms ± 0.2ms 39.7ms ± 3.7ms +3.0% ✅ within noise 42.9 MB → 43.0 MB (+0.2%)
investment 59.4ms ± 0.2ms 59.8ms ± 0.8ms +0.7% ✅ within noise 58.9 MB → 59.0 MB (+0.2%)
fifo 57.6ms ± 0.3ms 57.6ms ± 0.3ms -0.0% ✅ within noise 55.0 MB → 54.9 MB (-0.1%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 1.7ms, investment 0.4ms, fifo 0.2ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.80519% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-loader/src/cache.rs 93.10% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 826
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

Review round 1 on the temp-dir change:

- price cache: `test_clear` deleted the fixed `/tmp/test-price-cache-clear.json`;
  it and the seven other tests with a fixed `/tmp/test-price-cache*.json` path
  now use a path in their own `TempDir`.
- cli_commands_test: each of the 16 `NamedTempFile` ledgers is created in its own
  `TempDir`, so the cache `rledger check` writes next to it goes with the
  directory instead of piling up in TMPDIR (a run now leaves TMPDIR empty).
- The loader cache test note no longer says `cache_env_var_test` mutates the
  process environment.
- CHANGELOG: drop the blank line after the new bullet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit 51f4a5b into main Nov 8, 2026
63 of 64 checks passed
@robcohen
robcohen deleted the fix/test-tempdirs branch November 8, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant