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 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 @@
let file = Path::new(file);
if file.is_dir() {
terminal::disable_raw_mode().unwrap();
return Err(UUsageError::new(
1,
show!(UUsageError::new(

Check warning on line 108 in src/uu/more/src/more.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/more/src/more.rs#L108

Added line #L108 was not covered by tests
0,
format!("{} is a directory.", file.quote()),
));
terminal::enable_raw_mode().unwrap();

Check warning on line 112 in src/uu/more/src/more.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/more/src/more.rs#L112

Added line #L112 was not covered by tests
continue;
}
if !file.exists() {
terminal::disable_raw_mode().unwrap();
return Err(USimpleError::new(
1,
show!(USimpleError::new(

Check warning on line 117 in src/uu/more/src/more.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/more/src/more.rs#L117

Added line #L117 was not covered by tests
0,
format!("cannot open {}: No such file or directory", file.quote()),
));
terminal::enable_raw_mode().unwrap();

Check warning on line 121 in src/uu/more/src/more.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/more/src/more.rs#L121

Added line #L121 was not covered by tests
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(

Check warning on line 127 in src/uu/more/src/more.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/more/src/more.rs#L127

Added line #L127 was not covered by tests
0,
format!("cannot open {}: {}", file.quote(), why.kind()),
));
terminal::enable_raw_mode().unwrap();

Check warning on line 131 in src/uu/more/src/more.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/more/src/more.rs#L131

Added line #L131 was not covered by tests
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 @@
if std::io::stdout().is_terminal() {
new_ucmd!()
.arg(".")
.fails()
.usage_error("'.' is a directory.");
.succeeds()
.stderr_contains("'.' is a directory.");

Check warning on line 95 in tests/by-util/test_more.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_more.rs#L95

Added line #L95 was not covered by tests
}
}

Expand All @@ -108,8 +108,46 @@
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()

Check warning on line 121 in tests/by-util/test_more.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_more.rs#L119-L121

Added lines #L119 - L121 were not covered by tests
.arg("folder")
.succeeds()
.stderr_contains("is a directory");
ts.ucmd()

Check warning on line 125 in tests/by-util/test_more.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_more.rs#L124-L125

Added lines #L124 - L125 were not covered by tests
.arg("file1")
.succeeds()
.stderr_contains("No such file or directory");
}

Check warning on line 129 in tests/by-util/test_more.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_more.rs#L128-L129

Added lines #L128 - L129 were not covered by tests
}

#[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()

Check warning on line 138 in tests/by-util/test_more.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_more.rs#L135-L138

Added lines #L135 - L138 were not covered by tests
.arg("folder")
.arg("file2")
.arg("file1")
.succeeds()
.stderr_contains("folder")
.stderr_contains("file2")
.stdout_contains("file1");
ts.ucmd()

Check warning on line 146 in tests/by-util/test_more.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_more.rs#L145-L146

Added lines #L145 - L146 were not covered by tests
.arg("file2")
.arg("file3")
.succeeds()
.stderr_contains("file2")
.stderr_contains("file3");
}

Check warning on line 152 in tests/by-util/test_more.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_more.rs#L151-L152

Added lines #L151 - L152 were not covered by tests
}
Loading