Skip to content

feat(test): bean-price compat harness — symbol-set differential (refs #967) - #978

Merged
robcohen merged 8 commits into
mainfrom
feat/bean-price-compat-harness
May 3, 2026
Merged

robcohen merged 8 commits into
mainfrom
feat/bean-price-compat-harness

Conversation

@robcohen

@robcohen robcohen commented May 3, 2026

Copy link
Copy Markdown
Member

Summary

Lands the keystone deliverable from #967's audit: a Rust integration test that drives both `rledger price` and `bean-price -n` against a fixture beancount file and asserts they discover the same set of `(symbol, quote_currency)` pairs to fetch.

Once this is in, every per-candidate audit item from #967 (`--expressive`, `--clobber`, cache semantics, `--date`, source-selection precedence, logging) can add its own fixture + assertion against the same harness instead of inventing its own scaffolding.

How it works

  • Fixture written to a temp `.beancount` file with two declared commodities carrying `price: "USD:yahoo/SYMBOL"` metadata.
  • bean-price invoked as `bean-price -n ` (dry-run); tabular output parsed for `(symbol, currency)` tuples.
  • rledger price invoked with `-f --source-cmd `. The stub script just `echo 1.00`s, so no network, no fetching, no flaky external dependency. The `AAPL: 1.00 USD` lines on stdout get parsed.
  • Symbol-sets are compared with `assert_eq!`. On mismatch the assertion message includes both sets.

Constraints handled

  • `bean-price` may not be on PATH for contributors running `cargo test` outside `nix develop`. Test skips (with a notice) rather than fails. CI runs in the dev shell (`bean-price` added in chore(nix): add beanprice to dev shell for bean-price differential tests #976), so it runs there.
  • Stub script ETXTBSY: writing the stub via `NamedTempFile` keeps an open write handle, so Linux refuses to `exec` it. Use a plain file inside a `TempDir` instead.

Test plan

  • `cargo test -p rustledger --test bean_price_compat` passes inside `nix develop`.
  • `cargo clippy -p rustledger --all-features --tests -- -D warnings` clean.
  • `cargo fmt --all -- --check` clean.
  • Test skips gracefully when `bean-price` is missing from PATH.

Out of scope

This PR does one comparison (a basic `commodity` + `price:` fixture). Per-candidate audits will add more fixtures (with `quote_currency:`, fallback chains, undeclared/inactive flags, etc.) in their own dedicated PRs — that was the recommendation in my last comment on #967.

Refs #967.

…967)

Lands the keystone deliverable from issue #967's audit: a Rust integration
test that runs both `rledger price` and `bean-price -n` against a fixture
beancount file and asserts they discover the same set of (symbol,
quote_currency) pairs to fetch.

How it works:
- Fixture is written to a temp file with two declared commodities carrying
  `price: "USD:yahoo/SYMBOL"` metadata.
- `bean-price -n` (dry-run) is parsed from its tabular output.
- `rledger price -f <fixture> --source-cmd <stub>` is run with a no-op stub
  that returns "1.00" for any symbol — no network, no fetching.
- Both symbol-sets are compared; mismatch fails the test with a useful
  diff in the assertion message.

The harness skips (with a notice) when `bean-price` is not on PATH so
contributors running `cargo test` outside `nix develop` aren't blocked.
CI uses the dev shell (where #976 added beanprice), so this runs there.

Note on the stub source: written into a `TempDir` with a plain file (not
`NamedTempFile`) — the latter holds an open write handle and Linux refuses
to exec it (ETXTBSY).

Once landed, every per-candidate audit item from #967 (#967 items 1-6) can
add its own fixture + assertion against this harness.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 3, 2026 04:21
@robcohen
robcohen requested a review from a team as a code owner May 3, 2026 04:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Rust integration test in crates/rustledger to differentially compare rledger price against upstream bean-price -n for symbol discovery from a beancount fixture. This supports the compatibility audit tracked in #967 by establishing a reusable test harness inside the existing CLI/test suite.

Changes:

  • Introduces a bean_price_compat integration test that writes a temporary beancount fixture, runs both CLIs, and compares discovered (symbol, quote_currency) pairs.
  • Adds helper functions to detect bean-price, create a stub external source, invoke both commands, and parse their outputs into comparable sets.
  • Seeds the harness with one basic fixture covering declared commodities with price: metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented May 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 30.1ms ± 0.7ms 21.8 MB
beancount 913.4ms ± 6.1ms 50.5 MB
Comparison 30.3x faster 2.3x less

vs main branch (2026-05-03)

Metric main this PR Change
Time 36.4ms 30.1ms -17.3% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

github-actions Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 99%
BQL Compatibility 91%
Full AST Match 99%
Total Files 782
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen and others added 2 commits May 2, 2026 22:39
…nd fixture

Self-review fixes for PR #978:

1. Scope correction. Rename "symbol-set" -> "commodity discovery". The
   harness uses --source-cmd, which bypasses rledger's source-resolution
   layer — so it cannot validate source-precedence (audit item 5),
   per-spec ticker preservation (#970), or fallback chains. Module
   docstring now states this explicitly. Validating those needs an HTTP
   mock or a future --dry-run flag on rledger.

2. Switch rledger output extraction to --beancount format. Stable,
   documented format (`<date> price <SYM> <amt> <CCY>`); decouples
   the parser from whatever the stub source happens to print.

3. #![cfg(unix)]. The set_mode(0o755) call needs unix::fs::PermissionsExt;
   without the gate, Windows builds would fail to compile.

4. Tighten extract_bean_price_jobs: require uppercase ticker shape +
   uppercase currency. Previously a stray log line beginning with an
   identifier could be wrongly interpreted as a job.

5. New CI sanity test bean_price_must_be_on_path_in_dev_shell. Without
   it, removing beanprice from the flake would silently turn every
   compat test into a no-op (skip path) and we wouldn't notice. Gated
   on IN_NIX_SHELL or CI env vars to avoid false failures locally.

6. New fixture FIXTURE_MIXED_CURRENCIES (USD AAPL + EUR SAP). On its
   first run it found a real bug — see #979: parse_simple_format
   hardcodes "USD" when --source-cmd output omits a currency. The
   stub now echoes the requested --currency back as a workaround so
   the harness can land independently; #979 tracks the underlying fix.

7. Drop noise docstrings on private helpers; consolidate via
   assert_same_commodities helper.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
rledger parses --source-cmd through shell_words::split, so a temp directory
containing whitespace would cause the path to be word-split into separate
tokens and the exec to fail before discovery could be compared.

Quote the path with shell_words::quote before passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen enabled auto-merge (squash) May 3, 2026 04:43
robcohen and others added 3 commits May 3, 2026 07:03
…shell

The bean_price_must_be_on_path_in_dev_shell sanity test was gated on
\`IN_NIX_SHELL OR CI\`, but CI doesn't currently run inside \`nix develop\`
— so on CI the gate fired but bean-price wasn't on PATH, causing the
test to fail.

Drop the CI env-var trigger; only assert when actually inside the dev
shell. Local devs running tests via \`nix develop\` still get the
flake-regression guard; making CI also run under nix is a separate
workflow change.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The harness in crates/rustledger/tests/bean_price_compat.rs depends on
bean-price being on PATH, which is only true inside the nix dev shell
(added to the flake in #976). The main Test matrix uses dtolnay/rust-toolchain
+ cargo nextest directly, with no nix involved, so the harness silently
skipped there.

Add a dedicated bean-price-compat job that uses cachix/install-nix-action
and runs `cargo test -p rustledger --test bean_price_compat` inside
`nix develop`. Wire it into the build aggregator's required-results list.

This ensures CI actually executes the differential tests against bean-price
so a flake regression (or a real divergence between rledger and bean-price
discovery) breaks the PR loudly instead of silently.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen disabled auto-merge May 3, 2026 13:06
robcohen and others added 2 commits May 3, 2026 07:15
Reverts the CI workflow approach (commit 919fee1, reverted in adad0b28)
in favor of a local pre-push hook gated on relevant file changes.

Why local instead of CI: running the harness in CI requires nix shell
(for bean-price), which adds noticeable runtime to every PR. The harness
mostly catches regressions in the price command's discovery layer or in
the flake's beanprice packaging — both areas are touched rarely. Running
the check pre-push when those specific files change concentrates the
cost on the contributor making the relevant change.

The hook fires when any of these change:
- crates/rustledger/src/cmd/price/**, price_cmd.rs (discovery / source layer)
- crates/rustledger/tests/bean_price_compat.rs (the harness itself)
- flake.nix, flake.lock (could drop or break beanprice)

Uses `nix develop --command cargo test ...` so bean-price is reliably on
PATH; if nix isn't installed, the push fails loudly with a clear error
rather than silently skipping.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen merged commit 220aedb into main May 3, 2026
30 checks passed
@robcohen
robcohen deleted the feat/bean-price-compat-harness branch May 3, 2026 13:32
robcohen added a commit that referenced this pull request May 3, 2026
Two follow-ups to PR #982 / issue #967.

(item 4) --date as-of for the active-commodity filter
=====================================================

Before this change, `rledger price -f file.beancount --date 2020-01-01`
parsed the date and passed it to source requests, but the active filter
that decides which commodities to include used the LATEST balances —
so a commodity bought before 2020 and sold afterward was excluded from
the historical fetch. Bean-price walks the file as-of `--date` for
both balance computation and price lookup; this aligns rledger.

Implementation:
- discover_symbols and active_commodities now take Option<NaiveDate>.
- Transactions with date > as_of are skipped from the balance roll-up.
- Close directives after as_of are ignored (the account WAS open then).
- price_cmd.rs parses args.date once, early, and threads it into
  discovery (was previously parsed only later for the source request).

Tests:
- as_of_filter_includes_holdings_at_that_date_only
- as_of_ignores_close_directive_after_cutoff

(harness extension) Source-resolution differential via --dry-run
================================================================

The compat harness (#978) previously compared only commodity discovery
because rledger had no equivalent of `bean-price -n`. With #982's
`rledger price -n` flag now landed, the harness can compare full
(symbol, currency, source, ticker) resolution between the two binaries
— covering source precedence (audit item 5), per-spec ticker
preservation (#970), and fallback chains (#963/#970).

Adds two new tests:
- rledger_and_bean_price_resolve_same_attempts_basic
- rledger_and_bean_price_resolve_same_attempts_mixed_currencies

Both pass against the existing FIXTURE_BASIC and
FIXTURE_MIXED_CURRENCIES fixtures.

Docs:
- Drop the --date row from the "Differences from bean-price" table —
  it's no longer a divergence.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
robcohen added a commit that referenced this pull request May 3, 2026
Two follow-ups to PR #982 / issue #967.

(item 4) --date as-of for the active-commodity filter
=====================================================

Before this change, `rledger price -f file.beancount --date 2020-01-01`
parsed the date and passed it to source requests, but the active filter
that decides which commodities to include used the LATEST balances —
so a commodity bought before 2020 and sold afterward was excluded from
the historical fetch. Bean-price walks the file as-of `--date` for
both balance computation and price lookup; this aligns rledger.

Implementation:
- discover_symbols and active_commodities now take Option<NaiveDate>.
- Transactions with date > as_of are skipped from the balance roll-up.
- Close directives after as_of are ignored (the account WAS open then).
- price_cmd.rs parses args.date once, early, and threads it into
  discovery (was previously parsed only later for the source request).

Tests:
- as_of_filter_includes_holdings_at_that_date_only
- as_of_ignores_close_directive_after_cutoff

(harness extension) Source-resolution differential via --dry-run
================================================================

The compat harness (#978) previously compared only commodity discovery
because rledger had no equivalent of `bean-price -n`. With #982's
`rledger price -n` flag now landed, the harness can compare full
(symbol, currency, source, ticker) resolution between the two binaries
— covering source precedence (audit item 5), per-spec ticker
preservation (#970), and fallback chains (#963/#970).

Adds two new tests:
- rledger_and_bean_price_resolve_same_attempts_basic
- rledger_and_bean_price_resolve_same_attempts_mixed_currencies

Both pass against the existing FIXTURE_BASIC and
FIXTURE_MIXED_CURRENCIES fixtures.

Docs:
- Drop the --date row from the "Differences from bean-price" table —
  it's no longer a divergence.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
robcohen added a commit that referenced this pull request May 3, 2026
#983)

* feat(price): --date as-of for active filter + extend compat harness

Two follow-ups to PR #982 / issue #967.

(item 4) --date as-of for the active-commodity filter
=====================================================

Before this change, `rledger price -f file.beancount --date 2020-01-01`
parsed the date and passed it to source requests, but the active filter
that decides which commodities to include used the LATEST balances —
so a commodity bought before 2020 and sold afterward was excluded from
the historical fetch. Bean-price walks the file as-of `--date` for
both balance computation and price lookup; this aligns rledger.

Implementation:
- discover_symbols and active_commodities now take Option<NaiveDate>.
- Transactions with date > as_of are skipped from the balance roll-up.
- Close directives after as_of are ignored (the account WAS open then).
- price_cmd.rs parses args.date once, early, and threads it into
  discovery (was previously parsed only later for the source request).

Tests:
- as_of_filter_includes_holdings_at_that_date_only
- as_of_ignores_close_directive_after_cutoff

(harness extension) Source-resolution differential via --dry-run
================================================================

The compat harness (#978) previously compared only commodity discovery
because rledger had no equivalent of `bean-price -n`. With #982's
`rledger price -n` flag now landed, the harness can compare full
(symbol, currency, source, ticker) resolution between the two binaries
— covering source precedence (audit item 5), per-spec ticker
preservation (#970), and fallback chains (#963/#970).

Adds two new tests:
- rledger_and_bean_price_resolve_same_attempts_basic
- rledger_and_bean_price_resolve_same_attempts_mixed_currencies

Both pass against the existing FIXTURE_BASIC and
FIXTURE_MIXED_CURRENCIES fixtures.

Docs:
- Drop the --date row from the "Differences from bean-price" table —
  it's no longer a divergence.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* test(price): self-review on PR #983 — boundary date assertions

Add boundary-date assertions for the as_of filter:
- as_of exactly on buy date → buy applies, SHIB active
- as_of exactly on sell date → both apply, SHIB inactive
- as_of exactly on close date → close applies, DOGE inactive

Pins down the inclusive `<=` comparison so a future change to `<` would
break loudly with a focused test rather than only via integration.

Also: comment on the harness's two-space skip-annotation strip noting
it's coupled to dump_fetch_plan's exact output format.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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.

2 participants