Skip to content

Commit

Permalink
Logging errors after execution (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubiratansoares authored Mar 24, 2024
1 parent 518d7b1 commit 817ccde
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
13 changes: 0 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2024 Dotanuki Labs
// SPDX-License-Identifier: MIT

use crate::validator::ValidationOutcome;
use anyhow::ensure;
use clap::Parser;
use human_panic::setup_panic;

Expand All @@ -16,17 +14,6 @@ impl CommandLineInterface {
ParsedRawPath(parsed)
}

pub fn report(&self, outcomes: &[ValidationOutcome]) -> anyhow::Result<()> {
ensure!(!outcomes.is_empty(), "No validations found!");
ensure!(
outcomes.iter().all(|outcome| outcome.has_valid_wrapper_checksum),
"A Gradle wrapper with invalid checksum was found!"
);

println!("All Gradle wrappers have valid checksums!");
Ok(())
}

pub fn new() -> CommandLineInterface {
setup_panic!();
CommandLineInterface {}
Expand Down
43 changes: 38 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,31 @@ mod cli;
mod validator;

use crate::cli::CommandLineInterface;
use crate::validator::ValidationOutcome;
use std::process::exit;

fn main() -> anyhow::Result<()> {
fn main() {
let cli = CommandLineInterface::new();
let target_path = cli.parse_arguments();
let validations = validator::locate_and_validate(&target_path.0)?;
cli.report(&validations)

match validator::locate_and_validate(&target_path.0) {
Ok(outcomes) => ensure_no_issues(outcomes),
Err(wrapped) => {
eprintln!("{}", &wrapped.to_string());
exit(42)
},
}
}

fn ensure_no_issues(outcomes: Vec<ValidationOutcome>) {
let issues = outcomes.iter().any(|check| !check.has_valid_wrapper_checksum);

if issues {
eprintln!("A Gradle wrapper with invalid checksum was found!");
exit(37)
}

println!("All Gradle wrappers have valid checksums");
}

#[cfg(test)]
Expand All @@ -30,8 +49,22 @@ mod tests {
let arguments = ["-p", &test_data];
let assert = cmd.args(arguments).assert();

let all_ok = "All Gradle wrappers have valid checksums";
assert.success().stdout(contains(all_ok));
assert
.success()
.stdout(contains("All Gradle wrappers have valid checksums"));
}

#[test]
fn should_report_custom_errors() {
let mut cmd = Command::cargo_bin(TOOL).unwrap();

let project_dir = std::env::current_dir().unwrap();
let no_wrappers = format!("{}/scripts", &project_dir.to_string_lossy());

let arguments = ["-p", &no_wrappers];
let assert = cmd.args(arguments).assert();

assert.failure().stderr(contains("No wrappers found"));
}

#[test]
Expand Down

0 comments on commit 817ccde

Please sign in to comment.