Skip to content

Commit 1558789

Browse files
committed
test: force CET-1 timezone for golden tests
Ensured that golden snapshots remained stable across different environments by setting a fixed timezone. This prevented inconsistencies in local time calculations caused by varying host system settings. Signed-off-by: Chmouel Boudjnah <chmouel@chmouel.com>
1 parent 28af7c2 commit 1558789

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

crates/nextmeeting-core/src/format/golden_tests.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! Run with `cargo insta review` to update snapshots after intentional changes.
55
66
use chrono::{DateTime, Local, NaiveDate, TimeZone, Utc};
7+
use std::sync::Once;
78

89
use crate::event::{EventLink, LinkKind, MeetingView, NormalizedEvent};
910
use crate::format::{FormatOptions, OutputFormatter, TimeFormat};
@@ -14,6 +15,28 @@ fn utc(y: i32, m: u32, d: u32, h: u32, min: u32, s: u32) -> DateTime<Utc> {
1415
Utc.with_ymd_and_hms(y, m, d, h, min, s).unwrap()
1516
}
1617

18+
#[cfg(unix)]
19+
unsafe extern "C" {
20+
fn tzset();
21+
}
22+
23+
fn ensure_test_timezone() {
24+
static INIT: Once = Once::new();
25+
26+
INIT.call_once(|| {
27+
// Keep golden snapshots stable on CI and developer machines by forcing
28+
// the same local timezone offset used when the snapshots were recorded.
29+
unsafe {
30+
std::env::set_var("TZ", "CET-1");
31+
}
32+
33+
#[cfg(unix)]
34+
unsafe {
35+
tzset();
36+
}
37+
});
38+
}
39+
1740
/// Create a date for all-day events.
1841
#[allow(dead_code)]
1942
fn date(y: i32, m: u32, d: u32) -> NaiveDate {
@@ -22,6 +45,7 @@ fn date(y: i32, m: u32, d: u32) -> NaiveDate {
2245

2346
/// Convert UTC to local (for test assertions).
2447
fn local_from_utc(dt: DateTime<Utc>) -> DateTime<Local> {
48+
ensure_test_timezone();
2549
dt.with_timezone(&Local)
2650
}
2751

@@ -112,6 +136,7 @@ fn all_day_meeting(now: DateTime<Utc>, title: &str) -> MeetingView {
112136
/// The reference time for all golden tests: 2025-02-05 10:00:00 UTC.
113137
/// Using a fixed time ensures reproducible snapshots.
114138
fn reference_time() -> DateTime<Utc> {
139+
ensure_test_timezone();
115140
utc(2025, 2, 5, 10, 0, 0)
116141
}
117142

0 commit comments

Comments
 (0)