Skip to content

Commit

Permalink
Improve the recursive_multiple_root_workspaces test
Browse files Browse the repository at this point in the history
This commit improves the recursive_multiple_root_workspaces by applying some
review comments:
* Deduplicating the cargo command by making the cargo function take an
  absolute path instead of a relative one.
* Removing an unnecessary CARGO_INCREMENTAL=0 environment variable
* Renaming the temporary workspace directory variable
  • Loading branch information
bes committed Feb 2, 2023
1 parent 8ca9838 commit 25a73db
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ fn project_dir(project: &str) -> PathBuf {
test_dir().join(project)
}

fn cargo(project: &str) -> Command {
fn cargo(cmd_current_dir: impl AsRef<Path>) -> Command {
let mut cmd = Command::new(env!("CARGO"));
cmd.current_dir(project_dir(project));
cmd.current_dir(cmd_current_dir);
cmd
}

Expand Down Expand Up @@ -75,7 +75,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)
run(cargo(project_dir(project))
.arg("build")
.env("CARGO_TARGET_DIR", target.path()));
let new_size = get_size(target.path())?;
Expand Down Expand Up @@ -395,41 +395,35 @@ fn standalone_usage() -> TestResult {
/// * `CARGO_INCREMENTAL` to `0`, because cargo-sweep doesn't yet clear incremental files.
#[test]
fn recursive_multiple_root_workspaces() -> TestResult {
let target = tempdir()?;
let temp_workspace_dir = tempdir()?;

let nested_workspace_dir = test_dir().join("nested-root-workspace");
let options = CopyOptions::default();

// Copy the whole nested-root-workspace folder (and its content) into the new temp dir,
// and then `cargo build` and run the sweep tests inside that directory.
fs_extra::copy_items(&[&nested_workspace_dir], target.path(), &options)?;
fs_extra::copy_items(&[&nested_workspace_dir], temp_workspace_dir.path(), &options)?;

let old_size = get_size(target.path())?;
let old_size = get_size(temp_workspace_dir.path())?;

// Build bin-crate
run(Command::new(env!("CARGO"))
run(cargo(temp_workspace_dir.path().join("nested-root-workspace/bin-crate"))
// If someone has built & run these tests with CARGO_TARGET_DIR,
// we need to override that.
.env_remove("CARGO_TARGET_DIR")
// Don't output incremental build artifacts
.env("CARGO_INCREMENTAL", "0")
.arg("build")
.current_dir(target.path().join("nested-root-workspace/bin-crate")));
.arg("build"));

let intermediate_build_size = get_size(target.path())?;
let intermediate_build_size = get_size(temp_workspace_dir.path())?;
assert!(intermediate_build_size > old_size);

// Build workspace crates
run(Command::new(env!("CARGO"))
run(cargo(temp_workspace_dir.path().join("nested-root-workspace"))
// If someone has built & run these tests with CARGO_TARGET_DIR,
// we need to override that.
.env_remove("CARGO_TARGET_DIR")
// Don't output incremental build artifacts
.env("CARGO_INCREMENTAL", "0")
.arg("build")
.current_dir(target.path().join("nested-root-workspace")));
.arg("build"));

let final_build_size = get_size(target.path())?;
let final_build_size = get_size(temp_workspace_dir.path())?;
assert!(final_build_size > intermediate_build_size);

// Run a dry-run of cargo-sweep ("clean") in the target directory (recursive)
Expand All @@ -438,10 +432,10 @@ fn recursive_multiple_root_workspaces() -> TestResult {
// If someone has built & run these tests with CARGO_TARGET_DIR,
// we need to override that.
cmd.env_remove("CARGO_TARGET_DIR")
.current_dir(target.path())
.current_dir(temp_workspace_dir.path())
})?;
assert!(expected_cleaned > 0);
let size_after_dry_run_clean = get_size(target.path())?;
let size_after_dry_run_clean = get_size(temp_workspace_dir.path())?;
// Make sure that nothing was actually cleaned
assert_eq!(final_build_size, size_after_dry_run_clean);

Expand All @@ -451,9 +445,9 @@ fn recursive_multiple_root_workspaces() -> TestResult {
// If someone has built & run these tests with CARGO_TARGET_DIR,
// we need to override that.
cmd.env_remove("CARGO_TARGET_DIR")
.current_dir(target.path())
.current_dir(temp_workspace_dir.path())
})?;
assert_sweeped_size(target.path(), actual_cleaned, final_build_size)?;
assert_sweeped_size(temp_workspace_dir.path(), actual_cleaned, final_build_size)?;
assert_eq!(actual_cleaned, expected_cleaned);

Ok(())
Expand Down

0 comments on commit 25a73db

Please sign in to comment.