From d78a8eb9512e17291749df5989ce48e4b3dfc37f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Mon, 26 Dec 2022 14:10:33 -0300 Subject: [PATCH] add test to compare new default output --- Cargo.lock | 12 ++++++++++-- Cargo.toml | 2 ++ tests/integration.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index abb61f0..4b918e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,10 +100,12 @@ dependencies = [ "log", "predicates", "pretty_assertions", + "regex", "serde", "serde_derive", "serde_json", "tempfile", + "unindent", "walkdir", "which", ] @@ -545,9 +547,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" dependencies = [ "aho-corasick", "memchr", @@ -751,6 +753,12 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20" +[[package]] +name = "unindent" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" + [[package]] name = "vec_map" version = "0.8.1" diff --git a/Cargo.toml b/Cargo.toml index 1154cee..b09c7ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,3 +29,5 @@ predicates = "2.1.1" tempfile = "3.3.0" which = { version = "4.3.0", default-features = false } pretty_assertions = "1.3" +regex = "1.7.0" +unindent = "0.1.11" diff --git a/tests/integration.rs b/tests/integration.rs index c9110b4..6e84d25 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -16,7 +16,9 @@ use predicates::{ }; #[allow(unused_imports)] use pretty_assertions::{assert_eq, assert_ne}; +use regex::Regex; use tempfile::{tempdir, TempDir}; +use unindent::unindent; struct AnyhowWithContext(anyhow::Error); impl Debug for AnyhowWithContext { @@ -142,6 +144,11 @@ fn count_cleaned_dry_run(target: &TempDir, args: &[&str], old_size: u64) -> Resu Ok(cleaned) } +fn regex_matches(pattern: &str, text: &str) -> bool { + let pattern = Regex::new(pattern).expect("Failed to compile regex pattern"); + pattern.is_match(text) +} + #[test] fn all_flags() -> TestResult { let all_combos = [ @@ -192,6 +199,45 @@ fn stamp_file() -> TestResult { Ok(()) } +#[test] +fn empty_project_output() -> TestResult { + let (_size, target) = build("sample-project")?; + + let assert = run( + sweep(&["--maxsize", "0"]).current_dir(test_dir().join("sample-project")), + target.path(), + ); + + let output = std::str::from_utf8(&assert.get_output().stdout).unwrap(); + + let regex_skip = r#".+?"#; + + let pattern = unindent(&format!( + r#"\[DEBUG\] cleaning: "/tmp/{regex_skip}" with remove_older_until_fits + \[DEBUG\] size_to_remove: {regex_skip} + \[DEBUG\] Sizing: "/tmp/{regex_skip}/debug" with total_disk_space_in_a_profile + \[DEBUG\] Hashs by time: \[ + \( + {regex_skip}, + "{regex_skip}", + \), + \] + \[DEBUG\] cleaning: "/tmp/{regex_skip}/debug" with remove_not_built_with_in_a_profile + \[DEBUG\] Successfully removed: "/tmp/{regex_skip}/debug/deps/libsample_project-{regex_skip}.rlib" + \[DEBUG\] Successfully removed: "/tmp/{regex_skip}/debug/deps/libsample_project-{regex_skip}.rmeta" + \[DEBUG\] Successfully removed: "/tmp/{regex_skip}/debug/deps/sample_project-{regex_skip}.d" + \[DEBUG\] Successfully removed: "/tmp/{regex_skip}/debug/.fingerprint/sample-project-{regex_skip}" + \[INFO\] Cleaned {regex_skip} from "/tmp/{regex_skip}""#, + )); + + assert!( + regex_matches(&pattern, output), + "failed to match pattern with output" + ); + + Ok(()) +} + #[test] fn hidden() -> TestResult { // This path is so strange because we use CARGO_TARGET_DIR to set the target to a temporary directory.