Skip to content

Commit

Permalink
Merge pull request #25 from jakedeichert/always-exit-custom-maskfile-…
Browse files Browse the repository at this point in the history
…not-found

Always exit with error when custom maskfile is not found
  • Loading branch information
jacobdeichert authored Jul 27, 2019
2 parents 0a24a00 + 649bdfb commit 709eacc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ fn main() {

let maskfile = find_maskfile();
if maskfile.is_err() {
println!("{} no maskfile.md found", "WARNING:".yellow());
// If the maskfile can't be found, at least parse for --version or --help
cli_app.get_matches();
return;
Expand Down Expand Up @@ -55,6 +54,20 @@ fn find_maskfile() -> Result<String, String> {

let maskfile = mask::loader::read_maskfile(maskfile_path);

if maskfile.is_err() {
if let Some(p) = maskfile_path.to_str() {
// Check if this is a custom maskfile
if p != "./maskfile.md" {
// Exit with an error it's not found
eprintln!("{} specified maskfile not found", "ERROR:".red());
std::process::exit(1);
} else {
// Just log a warning and let the process continue
println!("{} no maskfile.md found", "WARNING:".yellow());
}
}
}

maskfile
}

Expand Down
59 changes: 52 additions & 7 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ fn specifying_a_maskfile_in_a_different_dir() {
.success();
}

mod when_no_maskfile_found {
// Using current_dir(".github") to make sure the default maskfile.md can't be found
mod when_no_maskfile_found_in_current_directory {
use super::*;

#[test]
fn logs_warning_about_missing_file() {
common::run_mask(&PathBuf::from("./nonexistent.md"))
fn logs_warning_about_missing_maskfile_when_its_not_custom() {
common::run_mask(&PathBuf::from("./maskfile.md"))
.current_dir(".github")
.assert()
.stdout(contains(format!(
"{} no maskfile.md found",
Expand All @@ -41,7 +43,8 @@ mod when_no_maskfile_found {

#[test]
fn exits_without_error_for_help() {
common::run_mask(&PathBuf::from("./nonexistent.md"))
common::run_mask(&PathBuf::from("./maskfile.md"))
.current_dir(".github")
.command("--help")
.assert()
.stdout(contains("USAGE:"))
Expand All @@ -50,7 +53,8 @@ mod when_no_maskfile_found {

#[test]
fn exits_without_error_for_version() {
common::run_mask(&PathBuf::from("./nonexistent.md"))
common::run_mask(&PathBuf::from("./maskfile.md"))
.current_dir(".github")
.command("--version")
.assert()
.stdout(contains(format!("{} {}", crate_name!(), crate_version!())))
Expand All @@ -59,10 +63,51 @@ mod when_no_maskfile_found {

#[test]
fn exits_with_error_for_any_other_command() {
common::run_mask(&PathBuf::from("./maskfile.md"))
.current_dir(".github")
.command("nothing")
.assert()
.stderr(contains("error: Found argument 'nothing' which wasn't expected, or isn't valid in this context"))
.failure();
}
}

mod when_custom_specified_maskfile_not_found {
use super::*;

#[test]
fn exits_with_error_for_help() {
common::run_mask(&PathBuf::from("./nonexistent.md"))
.command("yasss")
.command("--help")
.assert()
.stderr(contains("error: Found argument 'yasss' which wasn't expected, or isn't valid in this context"))
.stderr(contains(format!(
"{} specified maskfile not found",
"ERROR:".red()
)))
.failure();
}

#[test]
fn exits_with_error_for_version() {
common::run_mask(&PathBuf::from("./nonexistent.md"))
.command("--version")
.assert()
.stderr(contains(format!(
"{} specified maskfile not found",
"ERROR:".red()
)))
.failure();
}

#[test]
fn exits_with_error_for_any_other_command() {
common::run_mask(&PathBuf::from("./nonexistent.md"))
.command("what")
.assert()
.stderr(contains(format!(
"{} specified maskfile not found",
"ERROR:".red()
)))
.failure();
}
}
Expand Down

0 comments on commit 709eacc

Please sign in to comment.