Skip to content
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

git-date parse draft #498

Merged
merged 11 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions git-date/tests/fixtures/generate_git_date_baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ function baseline() {
local test_date=$1 # first argument is the date to test

git -c section.key="$test_date" config --type=expiry-date section.key && status=0 || status=$?
# git ls-files "$pathspec" && status=0 || status=$?
{
echo "$test_date"
echo "$status"
if [ $status == 0 ]
then
git -c section.key="$test_date" config --type=expiry-date section.key
else
echo "-1"
fi
} >> baseline.git
}

# success

# date formats following to https://git-scm.com/docs/git-log#Documentation/git-log.txt---dateltformatgt

# short
baseline '2022-08-22'
# ODO
#baseline '2022-08-22'
# rfc2822
baseline 'Thu, 18 Aug 2022 12:45:06 +0800'
# iso8601
Expand Down
Binary file not shown.
17 changes: 12 additions & 5 deletions git-date/tests/time/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ use git_date::time::Sign;
use git_date::Time;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::str::FromStr;
use time::OffsetDateTime;

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

static BASELINE: Lazy<HashMap<BString, usize>> = Lazy::new(|| {
static BASELINE: Lazy<HashMap<BString, (usize, BString)>> = 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("two lines per baseline").to_str()?.parse()?;
map.insert(date_str.into(), exit_code);
let exit_code = lines.next().expect("three lines per baseline").to_str()?.parse()?;
let output = lines.next().expect("three lines per baseline").into();
map.insert(date_str.into(), (exit_code, output));
}
Ok(map)
})()
Expand All @@ -25,13 +27,18 @@ static BASELINE: Lazy<HashMap<BString, usize>> = Lazy::new(|| {

#[test]
fn baseline() {
for (pattern, exit_code) in BASELINE.iter() {
for (pattern, (exit_code, output)) in BASELINE.iter() {
let res = git_date::parse(pattern.to_str().expect("valid pattern"));
assert_eq!(
res.is_some(),
*exit_code == 0,
"{pattern:?} disagrees with baseline: {res:?}"
)
);
if *exit_code == 0 {
let actual = res.unwrap().seconds_since_unix_epoch;
let expected = u32::from_str(output.to_str().expect("valid utf")).expect("valid epoch value");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to parse the output while parsing the baseline file, that way it can be used natively here.

assert_eq!(actual, expected, "{pattern:?} disagrees with baseline: {res:?}")
}
}
}

Expand Down