Skip to content

Commit

Permalink
Let time::parse::relative::various fail more often
Browse files Browse the repository at this point in the history
This tests four inputs representing relative times, instead of one,
in order to:

1. Reveal GitoxideLabs#1696 during more of the year when the tests are run on
   machines set to local time in time zones whose rules include
   daylight savings clock adjustments.

2. Illuminate the nature and limited extent of GitoxideLabs#1696 by trying both
   "weeks ago" and "minutes ago" input.

   (It looks like the "minutes ago" input always passes the test,
   while the "weeks ago" input can fail the test if the interval
   includes a DST adjustment.)

3. Cover a wider range of inputs more generally, which is probably
   a good idea even where GitoxideLabs#1696 is not involved.

Although these change intend to, and appear to succeed at,
triggering more failures due to that but on affected systems set to
local time, they are not expected to produce any new failures on
CI, since all platforms' GitHub-hosted GHA runners are set to use
UTC.

With these changes, the failure, when it occurs, looks like:

    --- STDERR:              gix-date::date time::parse::relative::various ---
    thread 'time::parse::relative::various' panicked at gix-date\tests\time\parse.rs:209:9:
    assertion `left == right` failed: relative times differ
      left: [2024-11-08T21:10:19Z, 2024-11-08T21:10:19Z, 2024-07-05T21:10:19Z, 2024-07-05T21:10:19Z]
     right: [2024-11-08T21:10:19Z, 2024-11-08T21:10:19Z, 2024-07-05T20:10:19Z, 2024-07-05T21:10:19Z]
  • Loading branch information
EliahKagan committed Nov 22, 2024
1 parent 197d31a commit 04c82ca
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions gix-date/tests/time/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,34 @@ mod relative {
#[test]
fn various() {
let now = SystemTime::now();
let two_weeks_ago = gix_date::parse("2 weeks ago", Some(now)).unwrap();
assert_eq!(Sign::Plus, two_weeks_ago.sign);
assert_eq!(0, two_weeks_ago.offset);
let expected = Zoned::try_from(now)
.unwrap()
// account for the loss of precision when creating `Time` with seconds
.round(
jiff::ZonedRound::new()
.smallest(jiff::Unit::Second)
.mode(jiff::RoundMode::Trunc),
)
.unwrap()
.saturating_sub(2.weeks());
assert_eq!(
jiff::Timestamp::from_second(two_weeks_ago.seconds).unwrap(),
expected.timestamp(),
"relative times differ"

let cases = [
("2 weeks ago", 2.weeks()),
("20160 minutes ago", 20_160.minutes()), // 2 weeks
("20 weeks ago", 20.weeks()),
("201600 minutes ago", 201_600.minutes()), // 20 weeks
];

let times = cases.map(|(input, _)| gix_date::parse(input, Some(now)).unwrap());

assert_eq!(times.map(|_| Sign::Plus), times.map(|time| time.sign));
assert_eq!(times.map(|_| 0), times.map(|time| time.offset));

let expected = cases.map(|(_, span)|
Zoned::try_from(now)
.unwrap()
// account for the loss of precision when creating `Time` with seconds
.round(
jiff::ZonedRound::new()
.smallest(jiff::Unit::Second)
.mode(jiff::RoundMode::Trunc),
)
.unwrap()
.saturating_sub(span)
.timestamp()
);
let actual = times.map(|time| jiff::Timestamp::from_second(time.seconds).unwrap());
assert_eq!(actual, expected, "relative times differ");
}
}

Expand Down

0 comments on commit 04c82ca

Please sign in to comment.