Skip to content

Commit

Permalink
Always test local offset
Browse files Browse the repository at this point in the history
The reproduction requires tens of thousands of environment variables to
be set. Testing sets at most a handful. Let's try our luck.
  • Loading branch information
jhpratt committed Feb 22, 2023
1 parent ec81282 commit eb7b60e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
8 changes: 7 additions & 1 deletion tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(
anonymous_parameters,
clippy::all,
clippy::undocumented_unsafe_blocks,
illegal_floating_point_literal_pattern,
late_bound_lifetime_arguments,
path_statements,
Expand All @@ -9,7 +10,6 @@
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unsafe_code,
unused_extern_crates
)]
#![warn(
Expand Down Expand Up @@ -101,6 +101,12 @@ macro_rules! require_all_features {
}

require_all_features! {
use std::sync::Mutex;

/// A lock to ensure that certain tests don't run in parallel, which could lead to a test
/// unexpectedly failing.
static SOUNDNESS_LOCK: Mutex<()> = Mutex::new(());

/// Construct a non-exhaustive modifier.
macro_rules! modifier {
($name:ident {
Expand Down
15 changes: 10 additions & 5 deletions tests/offset_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ fn now_utc() {
assert_eq!(OffsetDateTime::now_utc().offset(), offset!(UTC));
}

#[cfg_attr(miri, ignore)]
#[test]
fn now_local() {
#[cfg(not(target_family = "unix"))]
assert!(OffsetDateTime::now_local().is_ok());
use time::util::local_offset::*;

let _guard = crate::SOUNDNESS_LOCK.lock().unwrap();

// Include for test coverage.
#[cfg(target_family = "unix")]
let _ = OffsetDateTime::now_local();
// Safety: Technically not sound. However, this is a test, and it's highly improbable that we
// will run into issues with setting an environment variable a few times.
unsafe { set_soundness(Soundness::Unsound) };
assert!(OffsetDateTime::now_local().is_ok());
// Safety: We're setting it back to sound.
unsafe { set_soundness(Soundness::Sound) };
}

#[test]
Expand Down
29 changes: 21 additions & 8 deletions tests/utc_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,37 @@ fn neg() {
assert_eq!(-offset!(-23:59:59), offset!(+23:59:59));
}

#[cfg_attr(miri, ignore)]
#[test]
fn local_offset_at() {
#[cfg(not(target_family = "unix"))]
assert!(UtcOffset::local_offset_at(OffsetDateTime::UNIX_EPOCH).is_ok());
use time::util::local_offset::*;

let _guard = crate::SOUNDNESS_LOCK.lock().unwrap();

#[cfg(target_family = "unix")]
let _ = UtcOffset::local_offset_at(OffsetDateTime::UNIX_EPOCH);
// Safety: Technically not sound. However, this is a test, and it's highly improbable that we
// will run into issues with setting an environment variable a few times.
unsafe { set_soundness(Soundness::Unsound) };
assert!(UtcOffset::local_offset_at(OffsetDateTime::UNIX_EPOCH).is_ok());
// Safety: We're setting it back to sound.
unsafe { set_soundness(Soundness::Sound) };
}

#[cfg_attr(miri, ignore)]
#[test]
fn current_local_offset() {
#[cfg(not(target_family = "unix"))]
assert!(UtcOffset::current_local_offset().is_ok());
use time::util::local_offset::*;

let _guard = crate::SOUNDNESS_LOCK.lock().unwrap();

#[cfg(target_family = "unix")]
let _ = UtcOffset::current_local_offset();
// Safety: Technically not sound. However, this is a test, and it's highly improbable that we
// will run into issues with setting an environment variable a few times.
unsafe { set_soundness(Soundness::Unsound) };
assert!(UtcOffset::current_local_offset().is_ok());
// Safety: We're setting it back to sound.
unsafe { set_soundness(Soundness::Sound) };
}

// Note: This behavior is not guaranteed and will hopefully be changed in the future.
#[test]
#[cfg_attr(
any(
Expand Down
6 changes: 4 additions & 2 deletions tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ fn weeks_in_year() {
}
}

#[allow(unsafe_code)]
#[test]
fn local_offset_soundness() {
use time::util::local_offset::*;

// Safety: cargo does not mutate the environment while tests are running.
let _guard = crate::SOUNDNESS_LOCK.lock().unwrap();

assert_eq!(get_soundness(), Soundness::Sound);
// Safety: Technically not sound. However, this is a test, and it's highly improbable that we
// will run into issues with setting an environment variable a few times.
unsafe { set_soundness(Soundness::Unsound) };
assert_eq!(get_soundness(), Soundness::Unsound);
// Safety: We're setting it back to sound.
unsafe { set_soundness(Soundness::Sound) };
assert_eq!(get_soundness(), Soundness::Sound);
}
7 changes: 6 additions & 1 deletion time/src/sys/local_offset_at/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ use crate::{OffsetDateTime, UtcOffset};
/// Attempt to obtain the system's UTC offset. If the offset cannot be determined, `None` is
/// returned.
pub(crate) fn local_offset_at(datetime: OffsetDateTime) -> Option<UtcOffset> {
imp::local_offset_at(datetime)
// miri does not support tzset()
if cfg!(miri) {
None
} else {
imp::local_offset_at(datetime)
}
}

0 comments on commit eb7b60e

Please sign in to comment.