From e5750076296d2083c4351d7b21247299ec6e3224 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Mon, 17 Jan 2022 10:18:04 -0500 Subject: [PATCH] tail: improve error handling when file not found --- src/uu/tail/src/tail.rs | 5 +++-- tests/by-util/test_tail.rs | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index 655abcecfe8..1dbdd389b5e 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -30,7 +30,7 @@ use std::path::Path; use std::thread::sleep; use std::time::Duration; use uucore::display::Quotable; -use uucore::error::{UResult, USimpleError}; +use uucore::error::{FromIo, UResult, USimpleError}; use uucore::parse_size::{parse_size, ParseSizeError}; use uucore::ringbuffer::RingBuffer; @@ -220,7 +220,8 @@ fn uu_tail(settings: &Settings) -> UResult<()> { if path.is_dir() { continue; } - let mut file = File::open(&path).unwrap(); + let mut file = File::open(&path) + .map_err_context(|| format!("cannot open {} for reading", filename.quote()))?; let md = file.metadata().unwrap(); if is_seekable(&mut file) && get_block_size(&md) > 0 { bounded_tail(&mut file, settings); diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index a020f623541..721c8a46777 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -3,7 +3,7 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore (ToDO) abcdefghijklmnopqrstuvwxyz efghijklmnopqrstuvwxyz vwxyz emptyfile +// spell-checker:ignore (ToDO) abcdefghijklmnopqrstuvwxyz efghijklmnopqrstuvwxyz vwxyz emptyfile bogusfile extern crate tail; @@ -475,3 +475,12 @@ fn test_tail_bytes_for_funny_files() { .code_is(exp_result.code()); } } + +#[test] +fn test_no_such_file() { + new_ucmd!() + .arg("bogusfile") + .fails() + .no_stdout() + .stderr_contains("cannot open 'bogusfile' for reading: No such file or directory"); +}