Skip to content

Commit

Permalink
Refactor BuildDir constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Nov 6, 2024
1 parent 751b6e5 commit 206aec8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
71 changes: 62 additions & 9 deletions src/build_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,37 @@ impl Debug for BuildDir {
}

impl BuildDir {
/// Make a new build dir, copying from a source directory, subject to exclusions.
pub fn copy_from(
source: &Utf8Path,
gitignore: bool,
leak_temp_dir: bool,
/// Make the build dir for the baseline.
///
/// Depending on the options, this might be either a copy of the source directory
/// or in-place.
pub fn for_baseline(
workspace: &Workspace,
options: &Options,
console: &Console,
) -> Result<BuildDir> {
if options.in_place {
BuildDir::in_place(workspace.root())
} else {
BuildDir::copy_from(workspace.root(), options, console)
}
}

/// Make a new build dir, copying from a source directory, subject to exclusions.
pub fn copy_from(source: &Utf8Path, options: &Options, console: &Console) -> Result<BuildDir> {
let name_base = format!("cargo-mutants-{}-", source.file_name().unwrap_or("unnamed"));
let source_abs = source
.canonicalize_utf8()
.context("canonicalize source path")?;
let temp_dir = copy_tree(source, &name_base, gitignore, console)?;
let temp_dir = copy_tree(source, &name_base, options.gitignore, console)?;
let path: Utf8PathBuf = temp_dir
.path()
.to_owned()
.try_into()
.context("tempdir path to UTF-8")?;
fix_manifest(&path.join("Cargo.toml"), &source_abs)?;
fix_cargo_config(&path, &source_abs)?;
let temp_dir = if leak_temp_dir {
let temp_dir = if options.leak_dirs {
let _ = temp_dir.into_path();
info!(?path, "Build directory will be leaked for inspection");
None
Expand Down Expand Up @@ -98,8 +109,13 @@ mod test {
fn build_dir_copy_from() {
let tmp = copy_of_testdata("factorial");
let workspace = Workspace::open(tmp.path()).unwrap();
let build_dir =
BuildDir::copy_from(workspace.root(), true, false, &Console::new()).unwrap();
let options = Options {
in_place: false,
gitignore: true,
leak_dirs: false,
..Default::default()
};
let build_dir = BuildDir::copy_from(workspace.root(), &options, &Console::new()).unwrap();
let debug_form = format!("{build_dir:?}");
println!("debug form is {debug_form:?}");
assert!(debug_form.starts_with("BuildDir { path: "));
Expand All @@ -108,6 +124,43 @@ mod test {
assert!(build_dir.path().join("src").is_dir());
}

#[test]
fn for_baseline_in_place() -> Result<()> {
let tmp = copy_of_testdata("factorial");
let workspace = Workspace::open(tmp.path())?;
let options = Options {
in_place: true,
..Default::default()
};
let build_dir = BuildDir::for_baseline(&workspace, &options, &Console::new())?;
assert_eq!(
build_dir.path().canonicalize_utf8()?,
workspace.root().canonicalize_utf8()?
);
assert!(build_dir.temp_dir.is_none());
Ok(())
}

#[test]
fn for_baseline_copied() -> Result<()> {
let tmp = copy_of_testdata("factorial");
let workspace = Workspace::open(tmp.path())?;
let options = Options {
in_place: false,
..Default::default()
};
let build_dir = BuildDir::for_baseline(&workspace, &options, &Console::new())?;
assert!(build_dir.path().is_dir());
assert!(build_dir.path().join("Cargo.toml").is_file());
assert!(build_dir.path().join("src").is_dir());
assert!(build_dir.temp_dir.is_some());
assert_ne!(
build_dir.path().canonicalize_utf8()?,
workspace.root().canonicalize_utf8()?
);
Ok(())
}

#[test]
fn build_dir_in_place() -> Result<()> {
let tmp = copy_of_testdata("factorial");
Expand Down
20 changes: 5 additions & 15 deletions src/lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use tracing::{debug, debug_span, error, trace, warn};

use crate::{
cargo::run_cargo, options::TestPackages, outcome::LabOutcome, output::OutputDir,
package::Package, timeouts::Timeouts, BaselineStrategy, BuildDir, Console, Context, Mutant,
Options, Phase, Result, Scenario, ScenarioOutcome, Utf8Path,
package::Package, timeouts::Timeouts, workspace::Workspace, BaselineStrategy, BuildDir,
Console, Context, Mutant, Options, Phase, Result, Scenario, ScenarioOutcome,
};

/// Run all possible mutation experiments.
Expand All @@ -30,7 +30,7 @@ use crate::{
#[allow(clippy::too_many_lines)] // just for now
pub fn test_mutants(
mut mutants: Vec<Mutant>,
workspace_dir: &Utf8Path,
workspace: &Workspace,
output_dir: OutputDir,
options: &Options,
console: &Console,
Expand All @@ -50,11 +50,7 @@ pub fn test_mutants(
debug!(?mutant_packages);

let output_mutex = Mutex::new(output_dir);
let baseline_build_dir = if options.in_place {
BuildDir::in_place(workspace_dir)?
} else {
BuildDir::copy_from(workspace_dir, options.gitignore, options.leak_dirs, console)?
};
let baseline_build_dir = BuildDir::for_baseline(workspace, options, console)?;

let jobserver = &options
.jobserver
Expand Down Expand Up @@ -109,13 +105,7 @@ pub fn test_mutants(
let build_dir = &if let Some(d) = build_dir_0 {
d
} else {
debug!("copy build dir");
BuildDir::copy_from(
workspace_dir,
options.gitignore,
options.leak_dirs,
console,
)?
BuildDir::copy_from(workspace.root(), options, console)?
};
let worker = Worker {
build_dir,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ fn main() -> Result<()> {
output_dir.write_previously_caught(&previously_caught)?;
}
console.set_debug_log(output_dir.open_debug_log()?);
let lab_outcome = test_mutants(mutants, workspace.root(), output_dir, &options, &console)?;
let lab_outcome = test_mutants(mutants, &workspace, output_dir, &options, &console)?;
exit(lab_outcome.exit_code());
}
Ok(())
Expand Down

0 comments on commit 206aec8

Please sign in to comment.