Skip to content

Commit

Permalink
fix: check input path exists when parsing cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhall88 committed Dec 14, 2023
1 parent c8e0a05 commit 1ce7150
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod download;

use serde::Deserialize;
use std::ffi::OsStr;
use std::io::{self, Write};
use std::path::PathBuf;
use std::process::Command;

#[derive(Deserialize)]
Expand Down Expand Up @@ -51,7 +53,16 @@ impl CommandRunner {
}
}

// add tests for each of the CommandRunner methods
/// A utility function that allows the CLI to error if a path doesn't exist
pub fn check_path_exists<S: AsRef<OsStr> + ?Sized>(s: &S) -> Result<PathBuf, String> {
let path = PathBuf::from(s);
if path.exists() {
Ok(path)
} else {
Err(format!("{:?} does not exist", path))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -87,4 +98,17 @@ mod tests {
let command = CommandRunner::new("not-a-real-command");
assert!(!command.is_executable());
}

#[test]
fn check_path_exists_it_doesnt() {
let result = check_path_exists(OsStr::new("fake.path"));
assert!(result.is_err())
}

#[test]
fn check_path_it_does() {
let actual = check_path_exists(OsStr::new("Cargo.toml")).unwrap();
let expected = PathBuf::from("Cargo.toml");
assert_eq!(actual, expected)
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::Parser;
use env_logger::Builder;
use lazy_static::lazy_static;
use log::{debug, error, info, LevelFilter};
use nohuman::{download::download_database, CommandRunner};
use nohuman::{check_path_exists, download::download_database, CommandRunner};

lazy_static! {
static ref DEFAULT_DB_LOCATION: String = {
Expand All @@ -22,7 +22,7 @@ lazy_static! {
#[command(author, version, about, long_about = None)]
struct Args {
/// Input file(s) to remove human reads from
#[arg(name = "INPUT", required_unless_present_any = &["check", "download"])]
#[arg(name = "INPUT", required_unless_present_any = &["check", "download"], value_parser = check_path_exists)]
input: Option<Vec<PathBuf>>,

/// Check that all required dependencies are available
Expand Down

0 comments on commit 1ce7150

Please sign in to comment.