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

date: show error when reading dirs for -f arg (#4572)

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
Surya Teja K 2023-03-29 11:19:14 +05:30 committed by GitHub
parent d7bc324979
commit b14d19770f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 10 deletions

View file

@ -19,7 +19,7 @@ use std::fs::File;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::path::PathBuf; use std::path::PathBuf;
use uucore::display::Quotable; use uucore::display::Quotable;
#[cfg(not(any(target_os = "macos", target_os = "redox")))] #[cfg(not(any(target_os = "redox")))]
use uucore::error::FromIo; use uucore::error::FromIo;
use uucore::error::{UResult, USimpleError}; use uucore::error::{UResult, USimpleError};
use uucore::{format_usage, help_about, help_usage, show}; use uucore::{format_usage, help_about, help_usage, show};
@ -219,19 +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) => match File::open(path) { DateSource::File(ref path) => {
Ok(file) => { if path.is_dir() {
let lines = BufReader::new(file).lines();
let iter = lines.filter_map(Result::ok).map(parse_date);
Box::new(iter)
}
Err(_err) => {
return Err(USimpleError::new( return Err(USimpleError::new(
2, 2,
format!("{}: No such file or directory", path.display()), format!("expected file, got directory {}", path.quote()),
)); ));
} }
}, let file = File::open(path)
.map_err_context(|| path.as_os_str().to_string_lossy().to_string())?;
let lines = BufReader::new(file).lines();
let iter = lines.map_while(Result::ok).map(parse_date);
Box::new(iter)
}
DateSource::Now => { DateSource::Now => {
let iter = std::iter::once(Ok(now)); let iter = std::iter::once(Ok(now));
Box::new(iter) Box::new(iter)

View file

@ -283,6 +283,16 @@ fn test_date_for_invalid_file() {
); );
} }
#[test]
fn test_date_for_dir_as_file() {
let result = new_ucmd!().arg("--file").arg("/").fails();
result.no_stdout();
assert_eq!(
result.stderr_str().trim(),
"date: expected file, got directory '/'",
);
}
#[test] #[test]
fn test_date_for_file() { fn test_date_for_file() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();