From 3669264ee65e3c7b6f0e48b151be6dffc50f94b6 Mon Sep 17 00:00:00 2001 From: Jose Garcia Crosta Date: Fri, 13 Dec 2024 15:39:52 -0300 Subject: [PATCH] Fix tests --- apps/cargo-scout-audit/Cargo.lock | 10 ++++++++++ apps/cargo-scout-audit/Cargo.toml | 3 +++ apps/cargo-scout-audit/tests/main.rs | 21 ++++++++++++++------- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/apps/cargo-scout-audit/Cargo.lock b/apps/cargo-scout-audit/Cargo.lock index 33f322c6..33ceade8 100644 --- a/apps/cargo-scout-audit/Cargo.lock +++ b/apps/cargo-scout-audit/Cargo.lock @@ -438,6 +438,7 @@ dependencies = [ "tracing-bunyan-formatter", "tracing-log 0.2.0", "tracing-subscriber", + "uuid", "walkdir", "webbrowser", "windows", @@ -4318,6 +4319,15 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" +dependencies = [ + "getrandom", +] + [[package]] name = "valuable" version = "0.1.0" diff --git a/apps/cargo-scout-audit/Cargo.toml b/apps/cargo-scout-audit/Cargo.toml index c94e85a9..57d3546b 100644 --- a/apps/cargo-scout-audit/Cargo.toml +++ b/apps/cargo-scout-audit/Cargo.toml @@ -65,6 +65,9 @@ tracing-log = "=0.2.0" tracing-subscriber = { version = "=0.3.18", features = ["env-filter", "registry"] } webbrowser = "=1.0.1" +[dev-dependencies] +uuid = { version = "1.3.1", features = ["v4"] } + [build-dependencies] regex = "*" sha2 = "0.10.8" diff --git a/apps/cargo-scout-audit/tests/main.rs b/apps/cargo-scout-audit/tests/main.rs index 7ed4acb7..7a9d3eaa 100644 --- a/apps/cargo-scout-audit/tests/main.rs +++ b/apps/cargo-scout-audit/tests/main.rs @@ -1,12 +1,18 @@ #[cfg(test)] mod tests { - use anyhow::{Context, Result}; + use anyhow::Result; use cargo_scout_audit::{ cli::{OutputFormat, Scout}, startup::run_scout, }; use lazy_static::lazy_static; use std::{collections::HashMap, fs, path::PathBuf}; + use tempfile::TempDir; + use uuid::Uuid; + + lazy_static! { + static ref TEST_DIR: TempDir = TempDir::new().expect("Failed to create temp directory"); + } lazy_static! { static ref DETECTORS_DIR: PathBuf = { @@ -112,13 +118,14 @@ mod tests { } } - fn test_output_fn(file: &str, format: OutputFormat) -> Result<()> { - test_output_format(file, &format) - .with_context(|| format!("Failed to test {:?} format", &format))?; - fs::remove_file(file) - .unwrap_or_else(|_| panic!("Should be able to delete the file: {}", file)); + fn test_output_fn(base_name: &str, format: OutputFormat) -> Result<()> { + let unique_name = format!("{}_{}", Uuid::new_v4(), base_name); + let file_path = TEST_DIR.path().join(unique_name); - Ok(()) + let result = test_output_format(&file_path.to_string_lossy(), &format); + let _ = fs::remove_file(&file_path); + + result } #[test]