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

more handle errors with multiple files (#4997)

* 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>
This commit is contained in:
Ludmuterol 2024-02-04 16:32:14 +01:00 committed by GitHub
parent 40694c5e75
commit 96d96e7de3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 11 deletions

View file

@ -25,8 +25,8 @@ use crossterm::{
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use uucore::display::Quotable;
use uucore::error::{UResult, USimpleError, UUsageError}; use uucore::error::{UResult, USimpleError, UUsageError};
use uucore::{display::Quotable, show};
use uucore::{format_usage, help_about, help_usage}; use uucore::{format_usage, help_about, help_usage};
const ABOUT: &str = help_about!("more.md"); const ABOUT: &str = help_about!("more.md");
@ -113,25 +113,31 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let file = Path::new(file); let file = Path::new(file);
if file.is_dir() { if file.is_dir() {
terminal::disable_raw_mode().unwrap(); terminal::disable_raw_mode().unwrap();
return Err(UUsageError::new( show!(UUsageError::new(
1, 0,
format!("{} is a directory.", file.quote()), format!("{} is a directory.", file.quote()),
)); ));
terminal::enable_raw_mode().unwrap();
continue;
} }
if !file.exists() { if !file.exists() {
terminal::disable_raw_mode().unwrap(); terminal::disable_raw_mode().unwrap();
return Err(USimpleError::new( show!(USimpleError::new(
1, 0,
format!("cannot open {}: No such file or directory", file.quote()), format!("cannot open {}: No such file or directory", file.quote()),
)); ));
terminal::enable_raw_mode().unwrap();
continue;
} }
let opened_file = match File::open(file) { let opened_file = match File::open(file) {
Err(why) => { Err(why) => {
terminal::disable_raw_mode().unwrap(); terminal::disable_raw_mode().unwrap();
return Err(USimpleError::new( show!(USimpleError::new(
1, 0,
format!("cannot open {}: {}", file.quote(), why.kind()), format!("cannot open {}: {}", file.quote(), why.kind()),
)); ));
terminal::enable_raw_mode().unwrap();
continue;
} }
Ok(opened_file) => opened_file, Ok(opened_file) => opened_file,
}; };

View file

@ -91,8 +91,8 @@ fn test_more_dir_arg() {
if std::io::stdout().is_terminal() { if std::io::stdout().is_terminal() {
new_ucmd!() new_ucmd!()
.arg(".") .arg(".")
.fails() .succeeds()
.usage_error("'.' is a directory."); .stderr_contains("'.' is a directory.");
} }
} }
@ -108,8 +108,46 @@ fn test_more_invalid_file_perms() {
at.make_file("invalid-perms.txt"); at.make_file("invalid-perms.txt");
set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap(); set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap();
ucmd.arg("invalid-perms.txt") ucmd.arg("invalid-perms.txt")
.fails() .succeeds()
.code_is(1)
.stderr_contains("permission denied"); .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");
}
}