diff --git a/src/uu/more/src/more.rs b/src/uu/more/src/more.rs index 7df717218..a03e64a49 100644 --- a/src/uu/more/src/more.rs +++ b/src/uu/more/src/more.rs @@ -25,8 +25,8 @@ use crossterm::{ use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; -use uucore::display::Quotable; use uucore::error::{UResult, USimpleError, UUsageError}; +use uucore::{display::Quotable, show}; use uucore::{format_usage, help_about, help_usage}; const ABOUT: &str = help_about!("more.md"); @@ -113,25 +113,31 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let file = Path::new(file); if file.is_dir() { terminal::disable_raw_mode().unwrap(); - return Err(UUsageError::new( - 1, + show!(UUsageError::new( + 0, format!("{} is a directory.", file.quote()), )); + terminal::enable_raw_mode().unwrap(); + continue; } if !file.exists() { terminal::disable_raw_mode().unwrap(); - return Err(USimpleError::new( - 1, + show!(USimpleError::new( + 0, format!("cannot open {}: No such file or directory", file.quote()), )); + terminal::enable_raw_mode().unwrap(); + continue; } let opened_file = match File::open(file) { Err(why) => { terminal::disable_raw_mode().unwrap(); - return Err(USimpleError::new( - 1, + show!(USimpleError::new( + 0, format!("cannot open {}: {}", file.quote(), why.kind()), )); + terminal::enable_raw_mode().unwrap(); + continue; } Ok(opened_file) => opened_file, }; diff --git a/tests/by-util/test_more.rs b/tests/by-util/test_more.rs index e80020d39..3e9c426ad 100644 --- a/tests/by-util/test_more.rs +++ b/tests/by-util/test_more.rs @@ -91,8 +91,8 @@ fn test_more_dir_arg() { if std::io::stdout().is_terminal() { new_ucmd!() .arg(".") - .fails() - .usage_error("'.' is a directory."); + .succeeds() + .stderr_contains("'.' is a directory."); } } @@ -108,8 +108,46 @@ fn test_more_invalid_file_perms() { at.make_file("invalid-perms.txt"); set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap(); ucmd.arg("invalid-perms.txt") - .fails() - .code_is(1) + .succeeds() .stderr_contains("permission denied"); } } + +#[test] +fn test_more_error_on_single_arg() { + if std::io::stdout().is_terminal() { + let ts = TestScenario::new("more"); + ts.fixtures.mkdir_all("folder"); + ts.ucmd() + .arg("folder") + .succeeds() + .stderr_contains("is a directory"); + ts.ucmd() + .arg("file1") + .succeeds() + .stderr_contains("No such file or directory"); + } +} + +#[test] +fn test_more_error_on_multiple_files() { + if std::io::stdout().is_terminal() { + let ts = TestScenario::new("more"); + ts.fixtures.mkdir_all("folder"); + ts.fixtures.make_file("file1"); + ts.ucmd() + .arg("folder") + .arg("file2") + .arg("file1") + .succeeds() + .stderr_contains("folder") + .stderr_contains("file2") + .stdout_contains("file1"); + ts.ucmd() + .arg("file2") + .arg("file3") + .succeeds() + .stderr_contains("file2") + .stderr_contains("file3"); + } +}