mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
fix(head): check the whether file exists before unwrap (#1858)
closes https://github.com/uutils/coreutils/issues/1800
This commit is contained in:
parent
734368bc92
commit
6c98415340
2 changed files with 19 additions and 0 deletions
|
@ -149,6 +149,13 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
first_time = false;
|
||||
|
||||
let path = Path::new(file);
|
||||
if path.is_dir() || !path.metadata().is_ok() {
|
||||
eprintln!(
|
||||
"cannot open '{}' for reading: No such file or directory",
|
||||
&path.to_str().unwrap()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
let reader = File::open(&path).unwrap();
|
||||
let mut buffer = BufReader::new(reader);
|
||||
if !head(&mut buffer, &settings) {
|
||||
|
|
|
@ -167,3 +167,15 @@ fn test_bug_in_negative_zero_lines() {
|
|||
//GNU Head returns "a\nb\n"
|
||||
.stdout_is("");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_such_file_or_directory() {
|
||||
let result = new_ucmd!().arg("no_such_file.toml").run();
|
||||
|
||||
assert_eq!(
|
||||
true,
|
||||
result
|
||||
.stderr
|
||||
.contains("cannot open 'no_such_file.toml' for reading: No such file or directory")
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue