Skip to content

Commit 96d96e7

Browse files
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>
1 parent 40694c5 commit 96d96e7

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

src/uu/more/src/more.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use crossterm::{
2525

2626
use unicode_segmentation::UnicodeSegmentation;
2727
use unicode_width::UnicodeWidthStr;
28-
use uucore::display::Quotable;
2928
use uucore::error::{UResult, USimpleError, UUsageError};
29+
use uucore::{display::Quotable, show};
3030
use uucore::{format_usage, help_about, help_usage};
3131

3232
const ABOUT: &str = help_about!("more.md");
@@ -113,25 +113,31 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
113113
let file = Path::new(file);
114114
if file.is_dir() {
115115
terminal::disable_raw_mode().unwrap();
116-
return Err(UUsageError::new(
117-
1,
116+
show!(UUsageError::new(
117+
0,
118118
format!("{} is a directory.", file.quote()),
119119
));
120+
terminal::enable_raw_mode().unwrap();
121+
continue;
120122
}
121123
if !file.exists() {
122124
terminal::disable_raw_mode().unwrap();
123-
return Err(USimpleError::new(
124-
1,
125+
show!(USimpleError::new(
126+
0,
125127
format!("cannot open {}: No such file or directory", file.quote()),
126128
));
129+
terminal::enable_raw_mode().unwrap();
130+
continue;
127131
}
128132
let opened_file = match File::open(file) {
129133
Err(why) => {
130134
terminal::disable_raw_mode().unwrap();
131-
return Err(USimpleError::new(
132-
1,
135+
show!(USimpleError::new(
136+
0,
133137
format!("cannot open {}: {}", file.quote(), why.kind()),
134138
));
139+
terminal::enable_raw_mode().unwrap();
140+
continue;
135141
}
136142
Ok(opened_file) => opened_file,
137143
};

tests/by-util/test_more.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ fn test_more_dir_arg() {
9191
if std::io::stdout().is_terminal() {
9292
new_ucmd!()
9393
.arg(".")
94-
.fails()
95-
.usage_error("'.' is a directory.");
94+
.succeeds()
95+
.stderr_contains("'.' is a directory.");
9696
}
9797
}
9898

@@ -108,8 +108,46 @@ fn test_more_invalid_file_perms() {
108108
at.make_file("invalid-perms.txt");
109109
set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap();
110110
ucmd.arg("invalid-perms.txt")
111-
.fails()
112-
.code_is(1)
111+
.succeeds()
113112
.stderr_contains("permission denied");
114113
}
115114
}
115+
116+
#[test]
117+
fn test_more_error_on_single_arg() {
118+
if std::io::stdout().is_terminal() {
119+
let ts = TestScenario::new("more");
120+
ts.fixtures.mkdir_all("folder");
121+
ts.ucmd()
122+
.arg("folder")
123+
.succeeds()
124+
.stderr_contains("is a directory");
125+
ts.ucmd()
126+
.arg("file1")
127+
.succeeds()
128+
.stderr_contains("No such file or directory");
129+
}
130+
}
131+
132+
#[test]
133+
fn test_more_error_on_multiple_files() {
134+
if std::io::stdout().is_terminal() {
135+
let ts = TestScenario::new("more");
136+
ts.fixtures.mkdir_all("folder");
137+
ts.fixtures.make_file("file1");
138+
ts.ucmd()
139+
.arg("folder")
140+
.arg("file2")
141+
.arg("file1")
142+
.succeeds()
143+
.stderr_contains("folder")
144+
.stderr_contains("file2")
145+
.stdout_contains("file1");
146+
ts.ucmd()
147+
.arg("file2")
148+
.arg("file3")
149+
.succeeds()
150+
.stderr_contains("file2")
151+
.stderr_contains("file3");
152+
}
153+
}

0 commit comments

Comments
 (0)