bakery: fix confirm() test hang when stdin is a real tty
All checks were successful
dev bakery / build (push) Successful in 39s
All checks were successful
dev bakery / build (push) Successful in 39s
confirm() checked the actual process stdin's is_terminal() state, which is true when cargo test is run from an interactive shell rather than CI/piped input — the two confirm-dependent tests then blocked on a read_line nobody was there to answer. Force stdin_is_terminal() to false in test builds so the tests never touch real stdin at all.
This commit is contained in:
parent
2a23d81e39
commit
1322fc31ac
1 changed files with 18 additions and 1 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
#[cfg(not(test))]
|
||||||
use std::io::IsTerminal;
|
use std::io::IsTerminal;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
@ -23,6 +24,22 @@ fn ensure_safe_component(name: &str, what: &str) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether stdin should be treated as an interactive terminal. Always
|
||||||
|
/// `false` in test builds regardless of the real process stdin — running
|
||||||
|
/// `cargo test` from an actual interactive shell (not CI, not piped) gives
|
||||||
|
/// the test binary a real tty, which previously made `confirm` block on a
|
||||||
|
/// `read_line` nobody was there to answer.
|
||||||
|
fn stdin_is_terminal() -> bool {
|
||||||
|
#[cfg(test)]
|
||||||
|
{
|
||||||
|
false
|
||||||
|
}
|
||||||
|
#[cfg(not(test))]
|
||||||
|
{
|
||||||
|
std::io::stdin().is_terminal()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Prompts `prompt [y/N] ` and returns the answer. `assume_yes` (the global
|
/// Prompts `prompt [y/N] ` and returns the answer. `assume_yes` (the global
|
||||||
/// `--yes` flag) skips the prompt entirely; otherwise, a non-tty stdin
|
/// `--yes` flag) skips the prompt entirely; otherwise, a non-tty stdin
|
||||||
/// (CI, piped input) answers "no" rather than blocking on a read that will
|
/// (CI, piped input) answers "no" rather than blocking on a read that will
|
||||||
|
|
@ -31,7 +48,7 @@ fn confirm(prompt: &str, assume_yes: bool) -> bool {
|
||||||
if assume_yes {
|
if assume_yes {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if !std::io::stdin().is_terminal() {
|
if !stdin_is_terminal() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue