Skip to content

Commit 6438b59

Browse files
committed
test(tui): run the tests that set the theme in their own process
The theme (theme::cache) is process-wide. 43 tests set it through two helpers, with_theme_test_env and with_test_env of the theme command, and every test that renders text reads it. The pin_theme() helper of the project takes a lock, but only the tests that call it exclude each other. In a pair run of 100, a mermaid test failed 96 times and a ghost-text test failed 92 times. The two helpers now run their closure in its own process, so each of the 43 tests has the theme to itself.
1 parent 042a1ea commit 6438b59

2 files changed

Lines changed: 34 additions & 25 deletions

File tree

  • tui/crates/codegen/pi-pager/src

‎tui/crates/codegen/pi-pager/src/app/dispatch/tests/mod.rs‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -509,20 +509,25 @@ fn agent_toast(app: &AppView) -> Option<String> {
509509
.as_ref()
510510
.map(|(s, _)| s.clone())
511511
}
512-
/// Use the `theme_cache::test_lock` to serialize tests that touch
513-
/// the in-memory theme state (single mutable global). Mirrors the
514-
/// pattern used by `theme::cache::tests`.
512+
/// Run a test body that changes the in-memory theme state (a single mutable
513+
/// global) in a process of its own. The tests that read the theme do not all
514+
/// take the theme test lock, so a lock cannot keep them away from a body that
515+
/// switches the theme; a child process can (see
516+
/// `test_util::in_own_process`). The lock stays for the case that the test
517+
/// does not run on a thread of its own.
515518
fn with_theme_test_env(f: impl FnOnce()) {
516-
let _guard = crate::theme::cache::test_lock()
517-
.lock()
518-
.unwrap_or_else(|e| e.into_inner());
519-
crate::theme::cache::reset_for_test();
520-
crate::theme::cache::seed_auto_theme_defaults_for_test();
521-
crate::theme::cache::set(crate::theme::ThemeKind::GrokNight);
522-
crate::theme::system_appearance::clear_mock();
523-
f();
524-
crate::theme::system_appearance::clear_mock();
525-
crate::theme::cache::reset_for_test();
519+
crate::test_util::in_own_process(|| {
520+
let _guard = crate::theme::cache::test_lock()
521+
.lock()
522+
.unwrap_or_else(|e| e.into_inner());
523+
crate::theme::cache::reset_for_test();
524+
crate::theme::cache::seed_auto_theme_defaults_for_test();
525+
crate::theme::cache::set(crate::theme::ThemeKind::GrokNight);
526+
crate::theme::system_appearance::clear_mock();
527+
f();
528+
crate::theme::system_appearance::clear_mock();
529+
crate::theme::cache::reset_for_test();
530+
});
526531
}
527532
fn agent_scrollback_len(app: &AppView) -> usize {
528533
app.agents.get(&AgentId(0)).unwrap().scrollback.len()

‎tui/crates/codegen/pi-pager/src/slash/commands/theme.rs‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,23 @@ mod tests {
146146
use crate::theme::{cache as theme_cache, system_appearance};
147147

148148
/// Run a test with a clean in-memory state. Prevents disk reads by
149-
/// pre-loading the theme state.
149+
/// pre-loading the theme state. The body runs in a process of its own: it
150+
/// switches the theme, and the tests that read the theme do not all take
151+
/// the theme test lock (see `test_util::in_own_process`).
150152
fn with_test_env(f: impl FnOnce()) {
151-
let _guard = theme_cache::test_lock()
152-
.lock()
153-
.unwrap_or_else(|e| e.into_inner());
154-
theme_cache::reset_for_test();
155-
theme_cache::seed_auto_theme_defaults_for_test();
156-
system_appearance::clear_mock();
157-
// Set LOADED=true so current_kind() doesn't try to read from disk.
158-
theme_cache::set(ThemeKind::GrokNight);
159-
f();
160-
system_appearance::clear_mock();
161-
theme_cache::reset_for_test();
153+
crate::test_util::in_own_process(|| {
154+
let _guard = theme_cache::test_lock()
155+
.lock()
156+
.unwrap_or_else(|e| e.into_inner());
157+
theme_cache::reset_for_test();
158+
theme_cache::seed_auto_theme_defaults_for_test();
159+
system_appearance::clear_mock();
160+
// Set LOADED=true so current_kind() doesn't try to read from disk.
161+
theme_cache::set(ThemeKind::GrokNight);
162+
f();
163+
system_appearance::clear_mock();
164+
theme_cache::reset_for_test();
165+
});
162166
}
163167

164168
// -- suggest_args ---------------------------------------------------------

0 commit comments

Comments
 (0)