diff --git a/Cargo.lock b/Cargo.lock index 335c06fb..f67c7123 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -600,7 +600,7 @@ checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" [[package]] name = "encstr" -version = "0.29.0-alpha.7" +version = "0.29.0-alpha.8" [[package]] name = "enum-map" @@ -757,7 +757,7 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "hl" -version = "0.29.0-alpha.7" +version = "0.29.0-alpha.8" dependencies = [ "atoi", "bincode", diff --git a/Cargo.toml b/Cargo.toml index f21b05df..80dab6a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = [".", "crate/encstr"] [workspace.package] repository = "https://github.com/pamburus/hl" authors = ["Pavel Ivanov "] -version = "0.29.0-alpha.7" +version = "0.29.0-alpha.8" edition = "2021" license = "MIT" diff --git a/src/input.rs b/src/input.rs index 6fb6713c..10cb7706 100644 --- a/src/input.rs +++ b/src/input.rs @@ -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)) })?)) @@ -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 {