Skip to content

Commit d358fad

Browse files
authored
in tests, force terminal width to be 80 (#104)
Ensures that snapshots are deterministic even under `cargo nextest run --nocapture`.
1 parent 84e08c0 commit d358fad

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎crates/dropshot-api-manager/src/output.rs‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ pub fn display_resolution(
393393
"\n{}\n",
394394
textwrap::fill(
395395
&n.to_string(),
396-
textwrap::Options::with_termwidth()
396+
textwrap::Options::new(term_width())
397397
.initial_indent(&initial_indent)
398398
.subsequent_indent(&more_indent)
399399
)
@@ -539,7 +539,7 @@ where
539539
"{}",
540540
textwrap::fill(
541541
&issue.to_string(),
542-
textwrap::Options::with_termwidth()
542+
textwrap::Options::new(term_width())
543543
.initial_indent(&nested_first_indent)
544544
.subsequent_indent(&nested_more_indent)
545545
)
@@ -705,7 +705,7 @@ fn write_problem_header(
705705
"{}",
706706
textwrap::fill(
707707
&InlineErrorChain::new(error).to_string(),
708-
textwrap::Options::with_termwidth()
708+
textwrap::Options::new(term_width())
709709
.initial_indent(&first_indent)
710710
.subsequent_indent(&more_indent)
711711
)
@@ -731,7 +731,7 @@ fn write_fix_summary(
731731
"{}",
732732
textwrap::fill(
733733
&format!("will {}", s),
734-
textwrap::Options::with_termwidth()
734+
textwrap::Options::new(term_width())
735735
.initial_indent(&first_indent)
736736
.subsequent_indent(&more_indent)
737737
)
@@ -762,6 +762,28 @@ impl fmt::Display for InlineErrorChain<'_> {
762762
}
763763
}
764764

765+
/// Returns the wrap width to use for terminal output.
766+
///
767+
/// Honors the `OPENAPI_MGR_TERM_WIDTH` environment variable as an override.
768+
/// Otherwise falls back to [`textwrap::termwidth`], which queries the terminal
769+
/// connected to stdout, or returns 80 when stdout isn't a tty.
770+
///
771+
/// The override exists for snapshot determinism. Under `cargo nextest run` by
772+
/// default, stdout is captured, so `termwidth` returns 80 and snapshots are
773+
/// deterministic. Under `cargo nextest run --no-capture` (or `cargo test`),
774+
/// however, stdout may be the developer's tty, and width is wherever the window
775+
/// happens to be sized. Setting `OPENAPI_MGR_TERM_WIDTH=80` explicitly, as we
776+
/// do in our tests, ensures that snapshots are deterministic in this scenario
777+
/// as well.
778+
pub(crate) fn term_width() -> usize {
779+
match std::env::var("OPENAPI_MGR_TERM_WIDTH") {
780+
Ok(s) => s.parse().unwrap_or_else(|err| {
781+
panic!("OPENAPI_MGR_TERM_WIDTH={s:?} is not a valid width: {err}")
782+
}),
783+
Err(_) => textwrap::termwidth(),
784+
}
785+
}
786+
765787
/// Output headers.
766788
pub(crate) mod headers {
767789
// Same width as Cargo's output.

‎crates/integration-tests/src/environment.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,21 @@ pub struct TestEnvironment {
113113
vcs_mode: VcsMode,
114114
}
115115

116+
/// Set the wrap width used by `output.rs::term_width` to ensure that snapshots
117+
/// are deterministic.
118+
fn set_test_term_width() {
119+
// SAFETY:
120+
// https://nexte.st/docs/configuration/env-vars/#altering-the-environment-within-tests
121+
unsafe {
122+
std::env::set_var("OPENAPI_MGR_TERM_WIDTH", "80");
123+
}
124+
}
125+
116126
impl TestEnvironment {
117127
/// Create a new test environment with temporary directories, backed by a
118128
/// Git repository.
119129
pub fn new_git() -> Result<Self> {
130+
set_test_term_width();
120131
let temp_dir =
121132
Utf8TempDir::with_prefix("dropshot-api-manager-integration-")
122133
.context("failed to create temporary directory")?;
@@ -177,6 +188,7 @@ impl TestEnvironment {
177188
/// The non-colocated nature of the Jujutsu repository means that operations
178189
/// are forced to go through jj.
179190
pub fn new_jj() -> Result<Self> {
191+
set_test_term_width();
180192
let temp_dir = Utf8TempDir::with_prefix("dropshot-api-manager-jj-")
181193
.context("failed to create temporary directory")?;
182194

0 commit comments

Comments
 (0)