Skip to content

Commit

Permalink
add test for new verbose output
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19 committed Dec 26, 2022
1 parent 9120c29 commit dcf7008
Showing 1 changed file with 55 additions and 18 deletions.
73 changes: 55 additions & 18 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ fn sweep(args: &[&str]) -> Command {
cmd
}

fn run(mut cmd: impl BorrowMut<Command>) -> Assert {
/// Sets the target folder and runs the given command
fn run<'a>(mut cmd: impl BorrowMut<Command>, target: impl Into<Option<&'a Path>>) -> 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();
Expand All @@ -67,9 +71,7 @@ fn run(mut cmd: impl BorrowMut<Command>) -> 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))
Expand All @@ -83,8 +85,8 @@ fn clean_and_parse(target: &TempDir, args: &[&str]) -> Result<u64> {
} 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());
Expand Down Expand Up @@ -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()
Expand All @@ -182,23 +184,59 @@ 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);

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"]);
Expand Down Expand Up @@ -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(())
Expand All @@ -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)?;
Expand All @@ -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(())
Expand Down

0 comments on commit dcf7008

Please sign in to comment.