Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more handle errors with multiple files #4997

Merged
merged 6 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/uu/more/src/more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,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 @@ -105,25 +105,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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually know why it stays in raw mode, but it seems unrelated to this change, so let's merge it!

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