Skip to content

Extend git-date tests and fix bug discovered in the process. #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions git-date/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ pub(crate) mod function {
if offset.len() != 5 {
return None;
}
let sign = if &offset[..1] == "-" { Sign::Plus } else { Sign::Minus };
let sign = if &offset[..1] == "-" { Sign::Minus } else { Sign::Plus };
let hours: i32 = offset[1..3].parse().ok()?;
let minutes: i32 = offset[3..5].parse().ok()?;
let offset_in_seconds = hours * 3600 + minutes * 60;
let mut offset_in_seconds = hours * 3600 + minutes * 60;
if sign == Sign::Minus {
offset_in_seconds *= -1;
};
let time = Time {
seconds_since_unix_epoch,
offset_in_seconds,
Expand Down
15 changes: 9 additions & 6 deletions git-date/tests/fixtures/generate_git_date_baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ git init;

function baseline() {
local test_date=$1 # first argument is the date to test
local test_name=$2 # second argument is the format name for re-formatting

git -c section.key="$test_date" config --type=expiry-date section.key && status=0 || status=$?
{
echo "$test_date"
echo "$test_name"
echo "$status"
if [ $status == 0 ]
then
Expand All @@ -27,15 +29,16 @@ function baseline() {
# ODO
#baseline '2022-08-22'
# rfc2822
baseline 'Thu, 18 Aug 2022 12:45:06 +0800'
baseline 'Thu, 18 Aug 2022 12:45:06 +0800' 'RFC2822'
# iso8601
baseline '2022-08-17 22:04:58 +0200'
baseline '2022-08-17 22:04:58 +0200' 'ISO8601'
# iso8601_strict
baseline '2022-08-17T21:43:13+08:00'
baseline '2022-08-17T21:43:13+08:00' 'ISO8601_STRICT'
# default
baseline 'Thu Sep 04 2022 10:45:06 -0400'
baseline 'Thu Sep 04 2022 10:45:06 -0400' '' # cannot round-trip, incorrect day-of-week
baseline 'Sun Sep 04 2022 10:45:06 -0400' 'DEFAULT'
# unix
baseline '123456789'
baseline '123456789' 'UNIX'
# raw
baseline '1660874655 +0800'
baseline '1660874655 +0800' 'RAW'

63 changes: 63 additions & 0 deletions git-date/tests/time/baseline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::{collections::HashMap, time::SystemTime};

use git_date::time::{format, Format};
use once_cell::sync::Lazy;

type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;

static BASELINE: Lazy<HashMap<String, (String, usize, u32)>> = Lazy::new(|| {
let base = git_testtools::scripted_fixture_repo_read_only("generate_git_date_baseline.sh").unwrap();

(|| -> Result<_> {
let mut map = HashMap::new();
let file = std::fs::read(base.join("baseline.git"))?;
let baseline = std::str::from_utf8(&file).expect("valid utf");
let mut lines = baseline.lines();
while let Some(date_str) = lines.next() {
let name = lines.next().expect("four lines per baseline").to_string();
let exit_code = lines.next().expect("four lines per baseline").parse()?;
let output: u32 = lines
.next()
.expect("four lines per baseline")
.parse()
.expect("valid epoch value");
map.insert(date_str.into(), (name, exit_code, output));
}
Ok(map)
})()
.unwrap()
});

#[test]
fn baseline() {
for (pattern, (name, exit_code, output)) in BASELINE.iter() {
let res = git_date::parse(pattern.as_str(), Some(SystemTime::now()));
assert_eq!(
res.is_ok(),
*exit_code == 0,
"{pattern:?} disagrees with baseline: {res:?}"
);
if *exit_code == 0 {
let t = res.unwrap();
let actual = t.seconds_since_unix_epoch;
assert_eq!(actual, *output, "{pattern:?} disagrees with baseline: {actual:?}");
if name == "" {
// This test is not appropriate for round-trip, as the input is malformed.
continue;
}
let reformatted = t.format(match name.as_str() {
"RFC2822" => Format::Custom(format::RFC2822),
"ISO8601" => Format::Custom(format::ISO8601),
"ISO8601_STRICT" => Format::Custom(format::ISO8601_STRICT),
"DEFAULT" => Format::Custom(format::DEFAULT),
"UNIX" => Format::Unix,
"RAW" => Format::Raw,
&_ => Format::Raw,
});
assert_eq!(
reformatted, *pattern,
"{reformatted:?} disagrees with baseline: {pattern:?}"
);
}
}
}
1 change: 1 addition & 0 deletions git-date/tests/time/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bstr::ByteSlice;
use git_date::{time::Sign, Time};

mod baseline;
mod format;
mod parse;

Expand Down
68 changes: 24 additions & 44 deletions git-date/tests/time/parse.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,6 @@
use std::{collections::HashMap, time::SystemTime};
use std::time::SystemTime;

use bstr::{BString, ByteSlice};
use git_date::{time::Sign, Time};
use once_cell::sync::Lazy;

type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;

static BASELINE: Lazy<HashMap<BString, (usize, u32)>> = Lazy::new(|| {
let base = git_testtools::scripted_fixture_repo_read_only("generate_git_date_baseline.sh").unwrap();

(|| -> Result<_> {
let mut map = HashMap::new();
let baseline = std::fs::read(base.join("baseline.git"))?;
let mut lines = baseline.lines();
while let Some(date_str) = lines.next() {
let exit_code = lines.next().expect("three lines per baseline").to_str()?.parse()?;
let output: u32 = lines
.next()
.expect("three lines per baseline")
.to_str()
.expect("valid utf")
.parse()
.expect("valid epoch value");
map.insert(date_str.into(), (exit_code, output));
}
Ok(map)
})()
.unwrap()
});

#[test]
fn baseline() {
for (pattern, (exit_code, output)) in BASELINE.iter() {
let res = git_date::parse(pattern.to_str().expect("valid pattern"), Some(SystemTime::now()));
assert_eq!(
res.is_ok(),
*exit_code == 0,
"{pattern:?} disagrees with baseline: {res:?}"
);
if *exit_code == 0 {
let actual = res.unwrap().seconds_since_unix_epoch;
assert_eq!(actual, *output, "{pattern:?} disagrees with baseline: {actual:?}")
}
}
}

#[test]
fn special_time_is_ok_for_now() {
Expand Down Expand Up @@ -83,6 +40,29 @@ fn rfc2822() {
);
}

#[test]
fn raw() {
assert_eq!(
git_date::parse("1660874655 +0800", None).expect("parsed raw string"),
Time {
seconds_since_unix_epoch: 1660874655,
offset_in_seconds: 28800,
sign: Sign::Plus,
},
"could not parse with raw format"
);

assert_eq!(
git_date::parse("1660874655 -0800", None).expect("parsed raw string"),
Time {
seconds_since_unix_epoch: 1660874655,
offset_in_seconds: -28800,
sign: Sign::Minus,
},
"could not parse with raw format"
);
}

#[test]
fn invalid_dates_can_be_produced_without_current_time() {
assert!(matches!(
Expand Down