Skip to content

Parse the output while parsing the baseline file. #528

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
Sep 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
32 changes: 17 additions & 15 deletions git-date/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,29 @@ mod relative {
}

pub(crate) fn parse(input: &str, now: Option<SystemTime>) -> Option<Result<OffsetDateTime, Error>> {
parse_inner(input).map(|offset| {
let offset = std::time::Duration::from_secs(offset.whole_seconds().try_into().expect("positive value"));
now.ok_or(Error::MissingCurrentTime).map(|now| {
now.checked_sub(offset)
.expect("BUG: values can't be large enough to cause underflow")
.into()
})
let offset = parse_inner(input).map(|offset| {
let secs = offset.whole_seconds().try_into().expect("positive value");
return std::time::Duration::from_secs(secs);
})?;
now.ok_or(Error::MissingCurrentTime).map(|now| {
now.checked_sub(offset)
.expect("BUG: values can't be large enough to cause underflow")
.into()
})
}

fn duration(period: &str, multiplier: i64) -> Option<Duration> {
let period = period.strip_suffix('s').unwrap_or(period);
Some(match period {
"second" => Duration::seconds(multiplier),
"minute" => Duration::minutes(multiplier),
"hour" => Duration::hours(multiplier),
"day" => Duration::days(multiplier),
"week" => Duration::weeks(multiplier),
// TODO months & years
let seconds: i64 = match period {
"second" => 1,
"minute" => 60,
"hour" => 3_600,
"day" => 86_400,
"week" => 604_800,
// TODO months & years?
_ => return None,
})
};
Some(Duration::seconds(seconds.checked_mul(multiplier)?))
}

#[cfg(test)]
Expand Down
5 changes: 0 additions & 5 deletions git-date/tests/fixtures/generate_git_date_baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,3 @@ baseline '123456789'
# raw
baseline '1660874655 +0800'

# failing

# empty_input
baseline ""

Binary file not shown.
15 changes: 10 additions & 5 deletions git-date/tests/time/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use once_cell::sync::Lazy;

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

static BASELINE: Lazy<HashMap<BString, (usize, BString)>> = Lazy::new(|| {
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<_> {
Expand All @@ -15,7 +15,14 @@ static BASELINE: Lazy<HashMap<BString, (usize, BString)>> = Lazy::new(|| {
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 = lines.next().expect("three lines per baseline").into();
let output = u32::from_str(
lines
.next()
.expect("three lines per baseline")
.to_str()
.expect("valid utf"),
)
.expect("valid epoch value");
map.insert(date_str.into(), (exit_code, output));
}
Ok(map)
Expand All @@ -34,8 +41,7 @@ fn baseline() {
);
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");
assert_eq!(actual, expected, "{pattern:?} disagrees with baseline: {actual:?}")
assert_eq!(actual, *output, "{pattern:?} disagrees with baseline: {actual:?}")
}
}
}
Expand Down Expand Up @@ -93,7 +99,6 @@ mod relative {
use time::{Duration, OffsetDateTime};

#[test]
#[should_panic] // TODO: fix
fn large_offsets_can_panic() {
git_date::parse("999999999999999 weeks ago", Some(std::time::UNIX_EPOCH)).ok();
}
Expand Down
4 changes: 2 additions & 2 deletions git-prompt/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub(crate) mod imp {
};

use nix::sys::{termios, termios::Termios};
use parking_lot::{lock_api::MutexGuard, Mutex, RawMutex};
use parking_lot::{const_mutex, lock_api::MutexGuard, Mutex, RawMutex};

use crate::{unix::TTY_PATH, Error, Mode, Options};

static TERM_STATE: Mutex<Option<Termios>> = Mutex::new(None);
static TERM_STATE: Mutex<Option<Termios>> = const_mutex(None);

/// Ask the user given a `prompt`, returning the result.
pub(crate) fn ask(prompt: &str, Options { mode, .. }: &Options<'_>) -> Result<String, Error> {
Expand Down