From fb84831eae98a09c21818f3678ad9ec644350ca0 Mon Sep 17 00:00:00 2001 From: azzam Date: Wed, 7 Apr 2021 09:08:39 +0700 Subject: [PATCH] fix: panic if default config also doesn't exist --- src/config.rs | 3 +-- src/error.rs | 4 ++++ src/main.rs | 2 +- tests/integration_test.rs | 9 +++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0c80106..41f7758 100755 --- a/src/config.rs +++ b/src/config.rs @@ -28,7 +28,6 @@ fn deserialize(content: &str) -> Result { } pub fn get(path: &Path) -> Result { - // path must be exists. unwrap is safe here. - let file_content = fs::read_to_string(path).unwrap(); + let file_content = fs::read_to_string(path)?; deserialize(&file_content) } diff --git a/src/error.rs b/src/error.rs index 8fc72bf..a80a752 100755 --- a/src/error.rs +++ b/src/error.rs @@ -26,4 +26,8 @@ pub enum DigsError { // All cases from trust-dns #[error("Error: {0:?}")] ForeignError(#[from] ClientError), + + // All cases of `std::io::Error`. + #[error(transparent)] + IOError(#[from] std::io::Error), } diff --git a/src/main.rs b/src/main.rs index c3a9a7e..f28cd5e 100755 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ fn run() -> Result<()> { // get config file let config_path: PathBuf = match matches.value_of("config") { Some(path) => utils::is_exist(path)?, - None => "digs.toml".into(), + None => utils::is_exist("digs.toml".into())?, }; let config = config::get(&config_path)?; diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 0762b80..d8f6f24 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -11,6 +11,15 @@ fn help() { .stdout(predicate::str::contains("digs \u{25cf} dig many at once")); } +#[test] +fn default_config_not_found() { + let mut cmd = Command::cargo_bin("digs").unwrap(); + cmd.arg("example.net").arg("-f").arg("file/doesnt/exist"); + cmd.assert() + .failure() + .stderr(predicate::str::contains("No such file")); +} + #[test] fn config_not_found() { let mut cmd = Command::cargo_bin("digs").unwrap();