Skip to content

Commit 513cbaf

Browse files
authored
Merge pull request #99 from chrissimpkins/future-date-validation
Add start and end date validations vs. current date
2 parents 798bc04 + 9cf0476 commit 513cbaf

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

src/main.rs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,33 @@ impl Config {
505505
}
506506

507507
fn check_bounds(start: &Option<Bound>, end: &Option<Bound>) -> Result<(), Error> {
508+
// current UTC date
509+
let current = Utc::now().date();
508510
match (&start, &end) {
511+
// start date is after end date
509512
(Some(Bound::Date(start)), Some(Bound::Date(end))) if end < start => {
510513
bail!(
511-
"end should be after start, got start: {:?} and end {:?}",
514+
"end should be after start, got start: {} and end {}",
512515
start,
513516
end
514517
);
515518
}
519+
// start date is after current date
520+
(Some(Bound::Date(start)), Some(Bound::Date(_end))) if start > &current => {
521+
bail!(
522+
"start date should be on or before current date, got start date request: {} and current date is {}",
523+
start,
524+
current
525+
);
526+
}
527+
// end date is after current date
528+
(Some(Bound::Date(_start)), Some(Bound::Date(end))) if end > &current => {
529+
bail!(
530+
"end date should be on or before current date, got start date request: {} and current date is {}",
531+
end,
532+
current
533+
);
534+
}
516535
_ => {}
517536
}
518537

@@ -899,6 +918,11 @@ fn get_end_date(cfg: &Config) -> chrono::Date<Utc> {
899918
}
900919
}
901920

921+
fn date_is_future(test_date: chrono::Date<Utc>) -> bool {
922+
let current_date = chrono::Utc::now().date();
923+
test_date > current_date
924+
}
925+
902926
// nightlies branch of bisect execution
903927
fn bisect_nightlies(cfg: &Config, client: &Client) -> Result<BisectionResult, Error> {
904928
if cfg.args.alt {
@@ -918,6 +942,22 @@ fn bisect_nightlies(cfg: &Config, client: &Client) -> Result<BisectionResult, Er
918942
let mut last_failure = get_end_date(cfg);
919943
let has_start = cfg.args.start.is_some();
920944

945+
// validate start and end dates to confirm that they are not future dates
946+
// start date validation
947+
if has_start && date_is_future(nightly_date) {
948+
bail!(
949+
"start date must be on or before the current date. received start date request {}",
950+
nightly_date
951+
)
952+
}
953+
// end date validation
954+
if date_is_future(last_failure) {
955+
bail!(
956+
"end date must be on or before the current date. received end date request {}",
957+
nightly_date
958+
)
959+
}
960+
921961
let mut nightly_iter = NightlyFinderIter::new(nightly_date);
922962

923963
// this loop tests nightly toolchains to:
@@ -1203,3 +1243,37 @@ fn main() {
12031243
}
12041244
}
12051245
}
1246+
1247+
#[cfg(test)]
1248+
mod tests {
1249+
use super::*;
1250+
1251+
// Start and end date validations
1252+
#[test]
1253+
fn test_check_bounds_valid_bounds() {
1254+
let date1 = chrono::Utc::now().date().pred();
1255+
let date2 = chrono::Utc::now().date().pred();
1256+
assert!(check_bounds(&Some(Bound::Date(date1)), &Some(Bound::Date(date2))).is_ok());
1257+
}
1258+
1259+
#[test]
1260+
fn test_check_bounds_invalid_start_after_end() {
1261+
let start = chrono::Utc::now().date();
1262+
let end = chrono::Utc::now().date().pred();
1263+
assert!(check_bounds(&Some(Bound::Date(start)), &Some(Bound::Date(end))).is_err());
1264+
}
1265+
1266+
#[test]
1267+
fn test_check_bounds_invalid_start_after_current() {
1268+
let start = chrono::Utc::now().date().succ();
1269+
let end = chrono::Utc::now().date();
1270+
assert!(check_bounds(&Some(Bound::Date(start)), &Some(Bound::Date(end))).is_err());
1271+
}
1272+
1273+
#[test]
1274+
fn test_check_bounds_invalid_end_after_current() {
1275+
let start = chrono::Utc::now().date();
1276+
let end = chrono::Utc::now().date().succ();
1277+
assert!(check_bounds(&Some(Bound::Date(start)), &Some(Bound::Date(end))).is_err());
1278+
}
1279+
}

0 commit comments

Comments
 (0)