Skip to content

fix: make pager and config tests hermetic under a TTY - #1747

Merged
robcohen merged 2 commits into
mainfrom
fix/hermetic-tty-config-tests
Jul 10, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/hermetic-tty-config-tests

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

Makes the three tests from #1729 hermetic, so cargo test passes in TTY environments (macOS Terminal, ghostty, tmux) and with real user config files on disk. Two distinct causes, per the triage on the issue:

Pager (the TTY half). create_pager() read process-global io::stdout().is_terminal(); libtest captures the thread-local print handles, not fd 1, so under a real terminal the tests asserting the non-TTY branch failed — and create_pager actually spawned less mid-test-run on the developer's machine. The paging decision's inputs (is_tty, NO_PAGER, $PAGER) now live in a small PagerEnv struct: the public create_pager() fills it from the process, tests construct it directly.

Bonus: the previously-untestable TTY side is now covered — NO_PAGER-wins, empty pager command (via config and via $PAGER), unparsable command, and spawn-failure fallback. None of the tests consult or mutate the real environment (parallel tests race on set_var), and none can spawn a process (spawn-success on a TTY is deliberately untested — it would launch a real pager from the suite; the comment says so).

Config (the filesystem half — not TTY). The None-override assertion in resolve_user_config_path_env_override_wins collided with the macOS XDG fallback (#1616), which probes the real filesystem for ~/.config/rledger/config.toml when the fake /native/... sentinel doesn't exist — so it failed for any macOS user with an actual rledger config, terminal or not. The assertion moved into the already-cfg(not(target_os = "macos")) sibling test, with a comment naming the trap so it isn't reintroduced.

No production behavior changes — create_pager's public signature and decision logic are unchanged; the config resolver is untouched.

Verification

  • Full rustledger lib suite (370 tests) green both normally and under a PTY (script -qec "cargo test …"), the reporter's environment shape.
  • The PTY harness is proven non-vacuous: pre-fix code under the same PTY reproduces the reported failures (2 pager tests fail).
  • clippy --all-features --all-targets -D warnings clean on both 1.96 (nix) and 1.97 (CI's stable); fmt clean.

Closes #1729

🤖 Generated with Claude Code

cargo test failed on any macOS/TTY environment (Terminal, ghostty,
tmux) on three tests, two distinct causes:

- pager: create_pager() read process-global io::stdout()
  .is_terminal(); libtest captures the thread-local print handles,
  not fd 1, so under a real terminal the tests asserting the non-TTY
  branch failed — and create_pager actually SPAWNED `less` mid-test.
  The paging decision's inputs (is_tty, NO_PAGER, $PAGER) now live in
  a PagerEnv struct; create_pager() reads the process, tests construct
  it directly. This also makes the previously-untestable TTY side
  testable: new tests cover NO_PAGER-wins, empty command (config and
  $PAGER), unparsable command, and spawn-failure fallback — none of
  which consult or mutate the real environment (parallel tests race
  on set_var).

- config: the None-override assertion in
  resolve_user_config_path_env_override_wins collided with the macOS
  XDG fallback (#1616), which probes the REAL filesystem for
  ~/.config/rledger/config.toml when the fake native sentinel doesn't
  exist — failing for any macOS user with an actual rledger config.
  Moved into the already-cfg(not(macos)) sibling test with a comment
  naming the trap.

Verified: full lib suite green both normally AND under a PTY
(script -qec), where the pre-fix pager tests reproduce the reported
failures (2 failed); clippy clean on 1.96 and 1.97.

Co-Authored-By: Claude Fable 5 <[email protected]>
Copilot AI review requested due to automatic review settings July 10, 2026 13:25
@robcohen
robcohen requested a review from a team as a code owner July 10, 2026 13:25
@codecov

codecov Bot commented Jul 10, 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 Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 35.8ms ± 7.7ms 23.8 MB
beancount 913.9ms ± 9.8ms 50.6 MB
Comparison 25.5x faster 2.1x less

vs main branch (2026-07-10)

Metric main this PR Change
Time 33.7ms 35.8ms +6.2% 🟡
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

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

Makes pager- and config-path unit tests hermetic so cargo test behaves consistently under real TTY/PTY environments and on machines with real user config files, without changing production behavior.

Changes:

  • Refactors pager decision inputs into a PagerEnv struct and routes create_pager() through an internal create_pager_in() to allow fully hermetic unit tests.
  • Expands pager unit tests to cover additional TTY decision branches without consulting/mutating real process env vars or spawning a real pager.
  • Adjusts config-path unit tests to avoid macOS-specific filesystem probing behavior that depends on the developer’s real ~/.config/... state.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
crates/rustledger/src/pager.rs Introduces PagerEnv + create_pager_in() and updates/extends tests to be TTY-safe and hermetic.
crates/rustledger/src/config.rs Moves a “no override uses native” assertion into a non-macOS-gated test to avoid macOS real-filesystem interference.

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

Comment thread crates/rustledger/src/config.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
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
robcohen merged commit 5e54a46 into main Jul 10, 2026
47 of 48 checks passed
@robcohen
robcohen deleted the fix/hermetic-tty-config-tests branch July 10, 2026 14:00
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.

[Bug]: cargo --test fails on mac terminal or ghostty, or tmux

2 participants