diff --git a/tests/integration.rs b/tests/integration.rs index 32c6d51..6009736 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -54,7 +54,11 @@ fn sweep(args: &[&str]) -> Command { cmd } -fn run(mut cmd: impl BorrowMut) -> Assert { +/// Sets the target folder and runs the given command +fn run<'a>(mut cmd: impl BorrowMut, target: impl Into>) -> Assert { + if let Some(target) = target.into() { + cmd.borrow_mut().env("CARGO_TARGET_DIR", target); + } let assert = cmd.borrow_mut().assert().success(); let out = assert.get_output(); let str = |s| std::str::from_utf8(s).unwrap(); @@ -67,9 +71,7 @@ fn run(mut cmd: impl BorrowMut) -> Assert { fn build(project: &str) -> Result<(u64, TempDir)> { let target = tempdir()?; let old_size = get_size(target.path())?; - run(cargo(project) - .arg("build") - .env("CARGO_TARGET_DIR", target.path())); + run(cargo(project).arg("build"), target.path()); let new_size = get_size(target.path())?; assert!(new_size > old_size, "cargo didn't build anything"); Ok((new_size, target)) @@ -83,8 +85,8 @@ fn clean_and_parse(target: &TempDir, args: &[&str]) -> Result { } else { ("Successfully removed", "Cleaned ") }; - let assertion = run(sweep(args).env("CARGO_TARGET_DIR", target.path())) - .stdout(contains(remove_msg).and(contains(clean_msg))); + let assertion = + run(sweep(args), target.path()).stdout(contains(remove_msg).and(contains(clean_msg))); let output = assertion.get_output(); assert!(output.stderr.is_empty()); @@ -167,7 +169,7 @@ fn stamp_file() -> TestResult { let (size, target) = build("sample-project")?; // Create a stamp file for --file. - let assert = run(sweep(&["--stamp"])); + let assert = run(sweep(&["--stamp"]), target.path()); println!( "{}", std::str::from_utf8(&assert.get_output().stdout).unwrap() @@ -182,7 +184,7 @@ fn stamp_file() -> TestResult { // For some reason, we delete the stamp file after `--file` :( // Recreate it. - run(sweep(&["--stamp"])); + run(sweep(&["--stamp"]), target.path()); let actual_cleaned = count_cleaned(&target, args, size)?; assert_eq!(actual_cleaned, expected_cleaned); @@ -190,15 +192,51 @@ 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(); + + assert_eq!( + output, + r#" +[DEBUG] cleaning: "/tmp/.tmpYzB6xH" with remove_older_until_fits +[DEBUG] size_to_remove: 46978 +[DEBUG] Sizing: "/tmp/.tmpYzB6xH/debug" with total_disk_space_in_a_profile +[DEBUG] Hashs by time: [ + ( + 25.123342ms, + "261b05c2354104eb", + ), +] +[DEBUG] cleaning: "/tmp/.tmpYzB6xH/debug" with remove_not_built_with_in_a_profile +[DEBUG] Successfully removed: "/tmp/.tmpYzB6xH/debug/deps/libsample_project-261b05c2354104eb.rlib" +[DEBUG] Successfully removed: "/tmp/.tmpYzB6xH/debug/deps/libsample_project-261b05c2354104eb.rmeta" +[DEBUG] Successfully removed: "/tmp/.tmpYzB6xH/debug/deps/sample_project-261b05c2354104eb.d" +[DEBUG] Successfully removed: "/tmp/.tmpYzB6xH/debug/.fingerprint/sample-project-261b05c2354104eb" +[INFO] Cleaned 9.43 KiB from "/tmp/.tmpYzB6xH" + "# + ); + + Ok(()) +} + #[test] fn hidden() -> TestResult { // This path is so strange because we use CARGO_TARGET_DIR to set the target to a temporary directory. // So we can't let cargo-sweep discover any other projects, or it will think they share the same directory as this hidden project. let (size, target) = build("fresh-prefix/.hidden/hidden-project")?; let run = |args| { - run(sweep(args) - .current_dir(test_dir().join("fresh-prefix")) - .env("CARGO_TARGET_DIR", target.path())) + run( + sweep(args).current_dir(test_dir().join("fresh-prefix")), + target.path(), + ) }; run(&["--maxsize", "0", "-r"]); @@ -230,9 +268,10 @@ fn error_output() -> TestResult { } let (_, tempdir) = build("sample-project")?; - let assert = run(sweep(&["--installed"]) - .env("PATH", test_dir()) - .env("CARGO_TARGET_DIR", tempdir.path())); + let assert = run( + sweep(&["--installed"]).env("PATH", test_dir()), + tempdir.path(), + ); assert.stdout(contains("oh no an error")); Ok(()) @@ -250,7 +289,7 @@ fn error_status() -> TestResult { fn golden_reference(args: &[&str], file: &str) -> TestResult { let mut cmd = Command::new(cargo_bin("cargo-sweep")); - let mut assert = run(cmd.args(args)); + let mut assert = run(cmd.args(args), None); assert = assert.stderr(is_empty()); let actual = std::str::from_utf8(&assert.get_output().stdout)?; @@ -273,9 +312,7 @@ fn path() -> TestResult { cmd.arg("sweep").arg("--installed").current_dir(temp_dir()); // Pass `path` as an argument, instead of `current_dir` like it normally is. - let assert = run(cmd - .arg(project_dir("sample-project")) - .env("CARGO_TARGET_DIR", target.path())); + let assert = run(cmd.arg(project_dir("sample-project")), target.path()); assert.stdout(contains("Cleaned")); Ok(())