1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

pr: use chrono instead of time in tests #5972

This commit is contained in:
biplab5464 2024-02-14 23:08:34 +05:30
parent 6adaf31d49
commit 04c821ca78

View file

@ -5,13 +5,10 @@
// spell-checker:ignore (ToDO) Sdivide // spell-checker:ignore (ToDO) Sdivide
use crate::common::util::{TestScenario, UCommand}; use crate::common::util::{TestScenario, UCommand};
use chrono::{DateTime, Duration, Utc};
use std::fs::metadata; use std::fs::metadata;
use time::macros::format_description;
use time::Duration;
use time::OffsetDateTime;
const DATE_TIME_FORMAT: &[time::format_description::FormatItem] = const DATE_TIME_FORMAT: &str = "%b %d %H:%M %Y";
format_description!("[month repr:short] [day] [hour]:[minute] [year]");
fn file_last_modified_time(ucmd: &UCommand, path: &str) -> String { fn file_last_modified_time(ucmd: &UCommand, path: &str) -> String {
let tmp_dir_path = ucmd.get_full_fixture_path(path); let tmp_dir_path = ucmd.get_full_fixture_path(path);
@ -20,28 +17,28 @@ fn file_last_modified_time(ucmd: &UCommand, path: &str) -> String {
.map(|i| { .map(|i| {
i.modified() i.modified()
.map(|x| { .map(|x| {
let date_time: OffsetDateTime = x.into(); let date_time: DateTime<Utc> = x.into();
date_time.format(&DATE_TIME_FORMAT).unwrap() date_time.format(DATE_TIME_FORMAT).to_string()
}) })
.unwrap_or_default() .unwrap_or_default()
}) })
.unwrap_or_default() .unwrap_or_default()
} }
fn all_minutes(from: OffsetDateTime, to: OffsetDateTime) -> Vec<String> { fn all_minutes(from: DateTime<Utc>, to: DateTime<Utc>) -> Vec<String> {
let to = to + Duration::minutes(1); let to = to + Duration::minutes(1);
// const FORMAT: &str = "%b %d %H:%M %Y"; // const FORMAT: &str = "%b %d %H:%M %Y";
let mut vec = vec![]; let mut vec = vec![];
let mut current = from; let mut current = from;
while current < to { while current < to {
vec.push(current.format(&DATE_TIME_FORMAT).unwrap()); vec.push(current.format(DATE_TIME_FORMAT).to_string());
current += Duration::minutes(1); current += Duration::minutes(1);
} }
vec vec
} }
fn valid_last_modified_template_vars(from: OffsetDateTime) -> Vec<Vec<(String, String)>> { fn valid_last_modified_template_vars(from: DateTime<Utc>) -> Vec<Vec<(String, String)>> {
all_minutes(from, OffsetDateTime::now_utc()) all_minutes(from, Utc::now())
.into_iter() .into_iter()
.map(|time| vec![("{last_modified_time}".to_string(), time)]) .map(|time| vec![("{last_modified_time}".to_string(), time)])
.collect() .collect()
@ -257,7 +254,7 @@ fn test_with_suppress_error_option() {
fn test_with_stdin() { fn test_with_stdin() {
let expected_file_path = "stdin.log.expected"; let expected_file_path = "stdin.log.expected";
let mut scenario = new_ucmd!(); let mut scenario = new_ucmd!();
let start = OffsetDateTime::now_utc(); let start = Utc::now();
scenario scenario
.pipe_in_fixture("stdin.log") .pipe_in_fixture("stdin.log")
.args(&["--pages=1:2", "-n", "-"]) .args(&["--pages=1:2", "-n", "-"])
@ -320,7 +317,7 @@ fn test_with_mpr() {
let expected_test_file_path = "mpr.log.expected"; let expected_test_file_path = "mpr.log.expected";
let expected_test_file_path1 = "mpr1.log.expected"; let expected_test_file_path1 = "mpr1.log.expected";
let expected_test_file_path2 = "mpr2.log.expected"; let expected_test_file_path2 = "mpr2.log.expected";
let start = OffsetDateTime::now_utc(); let start = Utc::now();
new_ucmd!() new_ucmd!()
.args(&["--pages=1:2", "-m", "-n", test_file_path, test_file_path1]) .args(&["--pages=1:2", "-m", "-n", test_file_path, test_file_path1])
.succeeds() .succeeds()
@ -329,7 +326,7 @@ fn test_with_mpr() {
&valid_last_modified_template_vars(start), &valid_last_modified_template_vars(start),
); );
let start = OffsetDateTime::now_utc(); let start = Utc::now();
new_ucmd!() new_ucmd!()
.args(&["--pages=2:4", "-m", "-n", test_file_path, test_file_path1]) .args(&["--pages=2:4", "-m", "-n", test_file_path, test_file_path1])
.succeeds() .succeeds()
@ -338,7 +335,7 @@ fn test_with_mpr() {
&valid_last_modified_template_vars(start), &valid_last_modified_template_vars(start),
); );
let start = OffsetDateTime::now_utc(); let start = Utc::now();
new_ucmd!() new_ucmd!()
.args(&[ .args(&[
"--pages=1:2", "--pages=1:2",
@ -445,7 +442,7 @@ fn test_with_join_lines_option() {
let test_file_2 = "test.log"; let test_file_2 = "test.log";
let expected_file_path = "joined.log.expected"; let expected_file_path = "joined.log.expected";
let mut scenario = new_ucmd!(); let mut scenario = new_ucmd!();
let start = OffsetDateTime::now_utc(); let start = Utc::now();
scenario scenario
.args(&["+1:2", "-J", "-m", test_file_1, test_file_2]) .args(&["+1:2", "-J", "-m", test_file_1, test_file_2])
.run() .run()