tests/tui_spec: remove the NO_COLOR env race behind the flaky suite
`alert_line_prefixes_when_no_color` flipped the process-global `NO_COLOR`
without the `ASCII_ENV` mutex the other env-touching tests in this binary
hold, so a concurrent `remove_var("NO_COLOR")` could win and make
`alert_line()` resolve to Rgb/Ansi16 — the `! ` assertion then failed on an
otherwise green run (measured 1/12 runs at cc1201d).
Inject the colour mode instead of reading the environment in the test:
* `tui::alert_line_for_mode(mode, msg, width)` — `alert_line()` is now a
one-line delegate passing `color_mode()`, so production behaviour is
unchanged and the no-colour path is asserted without touching the env.
* The test now pins all three shapes of that path: `None` prefixes `! `,
Rgb leaves the message verbatim, and an already-prefixed alert is not
double-prefixed.
Also fix the second, same-class race the 20x repeat surfaced:
`wallets_settings_menu_frame_shows_the_shipped_footer` compares a rendered
frame against `status_footer_phased` computed moments earlier, and both read
`ascii_chrome()`/`color_mode()` live — a concurrent `ONIONWIRE_ASCII=1` made
the two sides disagree (✓/… vs ok/...). It now takes the same `ASCII_ENV`
lock and forces the unicode mode.
Evidence (TMPDIR=/home/lancelot/.hermes/tmp/onionwire-tui):
* cargo test --locked -> exit 0, 35 suites, 472 passed, 0 failed, 4 ignored
* cargo clippy --locked --all-targets -- -D warnings -> exit 0
* cargo test --locked --test tui_spec x60 -> 60 ok, 0 failures
* teeth: with the `! ` prefix removed from the None path, the test fails
("expected '! ' prefix in None mode: \"peer down\"") — reverted.
* ONIONWIRE_ASCII=1 cargo test --locked --test tui_spec -> 37 passed (no
unlocked test's outcome depends on the ambient value)
* rustfmt --edition 2024 --check on both files: only the inherited hunks
(src/tui.rs 16 == HEAD 16; tests/tui_spec.rs 3 vs HEAD 4, one inherited
hunk reformatted away by this edit)
This commit is contained in:
parent
a1f5c878d3
commit
a9cb0d5758
2 changed files with 34 additions and 13 deletions
11
src/tui.rs
11
src/tui.rs
|
|
@ -56,8 +56,17 @@ fn ellipsis() -> &'static str {
|
|||
/// Cells, not chars: the message can carry a peer-supplied transport error, and
|
||||
/// a CJK/emoji byte there must not push the alert into the next pane.
|
||||
pub fn alert_line(msg: &str, width: u16) -> String {
|
||||
alert_line_for_mode(color_mode(), msg, width)
|
||||
}
|
||||
|
||||
/// [`alert_line`] with the colour mode supplied by the caller.
|
||||
///
|
||||
/// Tests pin the `! ` ornament of the no-colour path through this entry point
|
||||
/// rather than flipping `NO_COLOR`, which is process-global and mutated
|
||||
/// concurrently by other tests in the same binary.
|
||||
pub fn alert_line_for_mode(mode: ColorMode, msg: &str, width: u16) -> String {
|
||||
let mut text = msg.to_string();
|
||||
if color_mode() == ColorMode::None && !text.starts_with("! ") {
|
||||
if mode == ColorMode::None && !text.starts_with("! ") {
|
||||
text = format!("! {text}");
|
||||
}
|
||||
fit_cells(&text, width as usize)
|
||||
|
|
|
|||
|
|
@ -833,7 +833,7 @@ async fn renders_a_queued_row_from_a_real_outbox_send() {
|
|||
// Colour token set (card t_49b7c2b1)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
use onionwire::tui::{ColorMode, tokens_for};
|
||||
use onionwire::tui::{ColorMode, alert_line_for_mode, tokens_for};
|
||||
use ratatui::style::{Color, Modifier};
|
||||
|
||||
#[test]
|
||||
|
|
@ -910,17 +910,24 @@ fn tokens_for_is_deterministic_across_calls() {
|
|||
|
||||
#[test]
|
||||
fn alert_line_prefixes_when_no_color() {
|
||||
// NO_COLOR → ColorMode::None → alert_line prepends "! "
|
||||
let _guard = std::env::var_os("NO_COLOR");
|
||||
unsafe { std::env::set_var("NO_COLOR", "1") };
|
||||
let line = alert_line("peer down", 80);
|
||||
assert!(line.starts_with("! "), "expected '! ' prefix in None mode: {line:?}");
|
||||
unsafe {
|
||||
match _guard {
|
||||
Some(v) => std::env::set_var("NO_COLOR", v),
|
||||
None => std::env::remove_var("NO_COLOR"),
|
||||
}
|
||||
};
|
||||
// ColorMode::None (NO_COLOR) → alert_line prepends "! ". The mode is
|
||||
// injected rather than read from the process env: other tests in this
|
||||
// binary flip NO_COLOR concurrently, which made the env version flaky.
|
||||
let plain = alert_line_for_mode(ColorMode::None, "peer down", 80);
|
||||
assert!(
|
||||
plain.starts_with("! "),
|
||||
"expected '! ' prefix in None mode: {plain:?}"
|
||||
);
|
||||
// The ornament belongs to the None path only; RGB keeps the message verbatim.
|
||||
assert_eq!(
|
||||
alert_line_for_mode(ColorMode::Rgb, "peer down", 80),
|
||||
"peer down"
|
||||
);
|
||||
// An alert that already carries the ornament is not double-prefixed.
|
||||
assert_eq!(
|
||||
alert_line_for_mode(ColorMode::None, "! peer down", 80),
|
||||
"! peer down"
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
@ -974,6 +981,11 @@ fn render_menu(w: u16, h: u16, st: &WalletMenuState) -> String {
|
|||
|
||||
#[test]
|
||||
fn wallets_settings_menu_frame_shows_the_shipped_footer() {
|
||||
// Both sides of the assertion read the process environment through
|
||||
// `ascii_chrome()` / `color_mode()` (glyphs and alert ornaments), so this
|
||||
// test must serialise with the other env-mutating tests in this binary.
|
||||
let _guard = ASCII_ENV.lock().expect("ascii env lock");
|
||||
force_unicode_mode();
|
||||
// The app renders this screen's one status line with `status_footer_phased`
|
||||
// (the chrome redesign put the Tor phase in it) and the hint from
|
||||
// `footer_hints`; the fixture has to do the same or every menu frame in the
|
||||
|
|
|
|||
Loading…
Reference in a new issue