Skip to content

Commit

Permalink
fix: return error before opening pager if directory is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed May 3, 2024
1 parent e62600c commit 931525c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [".", "crate/encstr"]
[workspace.package]
repository = "https://github.com/pamburus/hl"
authors = ["Pavel Ivanov <mr.pavel.ivanov@gmail.com>"]
version = "0.29.0-alpha.7"
version = "0.29.0-alpha.8"
edition = "2021"
license = "MIT"

Expand Down
33 changes: 33 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ impl InputReference {
match self {
InputReference::Stdin => None,
InputReference::File(path) => {
let meta = std::fs::metadata(path).map_err(|e| {
io::Error::new(
e.kind(),
format!("failed to get information on {}: {}", self.description(), e),
)
})?;
if meta.is_dir() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("{} is a directory", self.description()),
));
}
Some(Box::new(File::open(path).map_err(|e| {
io::Error::new(e.kind(), format!("failed to open {}: {}", self.description(), e))
})?))
Expand Down Expand Up @@ -531,6 +543,27 @@ mod tests {
assert_eq!(err.to_string().contains("test.log"), true);
}

#[test]
fn test_input_hold_error_is_dir() {
let reference = InputReference::File(PathBuf::from("."));
let result = reference.hold();
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::InvalidInput);
assert_eq!(err.to_string().contains("is a directory"), true);
}

#[test]
fn test_input_hold_error_not_found() {
let filename = "?????????????";
let reference = InputReference::File(PathBuf::from(filename));
let result = reference.hold();
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::NotFound);
assert_eq!(err.to_string().contains(filename), true);
}

struct FailingReader;

impl Read for FailingReader {
Expand Down

0 comments on commit 931525c

Please sign in to comment.