Skip to content

Commit

Permalink
more handle errors with multiple files (#4997)
Browse files Browse the repository at this point in the history
* more handle errors with multiple files

* tests/more test refactor and new case

* tests/more new cases

* more: use show! and change exitstatus and adjust tests to new exitvalue

---------

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
  • Loading branch information
Ludmuterol and sylvestre authored Feb 4, 2024
1 parent 40694c5 commit 96d96e7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/uu/more/src/more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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,
};
Expand Down
46 changes: 42 additions & 4 deletions tests/by-util/test_more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}

Expand All @@ -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");
}
}

0 comments on commit 96d96e7

Please sign in to comment.