Skip to content

Commit

Permalink
fix: panic if default config also doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Apr 7, 2021
1 parent ab7efbf commit fb84831
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ fn deserialize(content: &str) -> Result<Config, DigsError> {
}

pub fn get(path: &Path) -> Result<Config, DigsError> {
// 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)
}
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
9 changes: 9 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fb84831

Please sign in to comment.