mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
A better handler for ./coreutils date -f aze
(#4482)
This commit is contained in:
parent
882a35c3b6
commit
89b83c2f6a
2 changed files with 31 additions and 9 deletions
|
@ -203,9 +203,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
|
|
||||||
return set_system_datetime(date);
|
return set_system_datetime(date);
|
||||||
} else {
|
} else {
|
||||||
// Declare a file here because it needs to outlive the `dates` iterator.
|
|
||||||
let file: File;
|
|
||||||
|
|
||||||
// Get the current time, either in the local time zone or UTC.
|
// Get the current time, either in the local time zone or UTC.
|
||||||
let now: DateTime<FixedOffset> = if settings.utc {
|
let now: DateTime<FixedOffset> = if settings.utc {
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
|
@ -222,12 +219,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let iter = std::iter::once(date);
|
let iter = std::iter::once(date);
|
||||||
Box::new(iter)
|
Box::new(iter)
|
||||||
}
|
}
|
||||||
DateSource::File(ref path) => {
|
DateSource::File(ref path) => match File::open(path) {
|
||||||
file = File::open(path).unwrap();
|
Ok(file) => {
|
||||||
let lines = BufReader::new(file).lines();
|
let lines = BufReader::new(file).lines();
|
||||||
let iter = lines.filter_map(Result::ok).map(parse_date);
|
let iter = lines.filter_map(Result::ok).map(parse_date);
|
||||||
Box::new(iter)
|
Box::new(iter)
|
||||||
}
|
}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(USimpleError::new(
|
||||||
|
2,
|
||||||
|
format!("{}: No such file or directory", path.display()),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
},
|
||||||
DateSource::Now => {
|
DateSource::Now => {
|
||||||
let iter = std::iter::once(Ok(now));
|
let iter = std::iter::once(Ok(now));
|
||||||
Box::new(iter)
|
Box::new(iter)
|
||||||
|
|
|
@ -266,6 +266,24 @@ fn test_date_set_valid_2() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_date_for_invalid_file() {
|
||||||
|
let result = new_ucmd!().arg("--file").arg("invalid_file").fails();
|
||||||
|
result.no_stdout();
|
||||||
|
assert_eq!(
|
||||||
|
result.stderr_str().trim(),
|
||||||
|
"date: invalid_file: No such file or directory",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_date_for_file() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
let file = "test_date_for_file";
|
||||||
|
at.touch(file);
|
||||||
|
ucmd.arg("--file").arg(file).succeeds();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(all(unix, not(target_os = "macos")))]
|
#[cfg(all(unix, not(target_os = "macos")))]
|
||||||
/// TODO: expected to fail currently; change to succeeds() when required.
|
/// TODO: expected to fail currently; change to succeeds() when required.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue