Skip to content

Commit

Permalink
refactor: Options holds values by reference
Browse files Browse the repository at this point in the history
Performance of it doesn't matter but this seems to make construction a bit simpler.
  • Loading branch information
sourcefrog committed Nov 15, 2024
1 parent d94c530 commit 816494a
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 87 deletions.
38 changes: 23 additions & 15 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,14 @@ fn cargo_argv(packages: &PackageSelection, phase: Phase, options: &Options) -> V
.iter()
.map(|f| format!("--features={}", f)),
);
cargo_args.extend(options.additional_cargo_args.iter().cloned());
cargo_args.extend(options.additional_cargo_args.iter().map(|s| s.to_string()));
if phase == Phase::Test {
cargo_args.extend(options.additional_cargo_test_args.iter().cloned());
cargo_args.extend(
options
.additional_cargo_test_args
.iter()
.map(|s| s.to_string()),
);
}
cargo_args
}
Expand Down Expand Up @@ -212,6 +217,7 @@ mod test {
use pretty_assertions::assert_eq;
use rusty_fork::rusty_fork_test;

use crate::config::Config;
use crate::Args;

use super::*;
Expand All @@ -238,9 +244,7 @@ mod test {
let mut options = Options::default();
let package_name = "cargo-mutants-testdata-something";
// let relative_manifest_path = Utf8PathBuf::from("testdata/something/Cargo.toml");
options
.additional_cargo_test_args
.extend(["--lib", "--no-fail-fast"].iter().map(|s| s.to_string()));
options.additional_cargo_test_args = vec!["--lib", "--no-fail-fast"];
// TODO: It wolud be a bit better to use `--manifest-path` here, to get
// the fix for <https://github.com/sourcefrog/cargo-mutants/issues/117>
// but it's temporarily regressed.
Expand Down Expand Up @@ -292,10 +296,8 @@ mod test {
let mut options = Options::default();
options
.additional_cargo_test_args
.extend(["--lib", "--no-fail-fast"].iter().map(|s| s.to_string()));
options
.additional_cargo_args
.extend(["--release".to_owned()]);
.extend(["--lib", "--no-fail-fast"]);
options.additional_cargo_args.extend(["--release"]);
assert_eq!(
cargo_argv(&PackageSelection::All, Phase::Check, &options)[1..],
["check", "--tests", "--verbose", "--workspace", "--release"]
Expand All @@ -320,7 +322,8 @@ mod test {
#[test]
fn no_default_features_args_passed_to_cargo() {
let args = Args::try_parse_from(["mutants", "--no-default-features"].as_slice()).unwrap();
let options = Options::from_args(&args).unwrap();
let config = Config::default();
let options = Options::new(&args, &config).unwrap();
assert_eq!(
cargo_argv(&PackageSelection::All, Phase::Check, &options)[1..],
[
Expand All @@ -336,7 +339,8 @@ mod test {
#[test]
fn all_features_args_passed_to_cargo() {
let args = Args::try_parse_from(["mutants", "--all-features"].as_slice()).unwrap();
let options = Options::from_args(&args).unwrap();
let config = Config::default();
let options = Options::new(&args, &config).unwrap();
assert_eq!(
cargo_argv(&PackageSelection::All, Phase::Check, &options)[1..],
[
Expand All @@ -352,7 +356,8 @@ mod test {
#[test]
fn cap_lints_passed_to_cargo() {
let args = Args::try_parse_from(["mutants", "--cap-lints=true"].as_slice()).unwrap();
let options = Options::from_args(&args).unwrap();
let config = Config::default();
let options = Options::new(&args, &config).unwrap();
assert_eq!(
cargo_argv(&PackageSelection::All, Phase::Check, &options)[1..],
["check", "--tests", "--verbose", "--workspace",]
Expand All @@ -365,7 +370,8 @@ mod test {
["mutants", "--features", "foo", "--features", "bar,baz"].as_slice(),
)
.unwrap();
let options = Options::from_args(&args).unwrap();
let config = Config::default();
let options = Options::new(&args, &config).unwrap();
assert_eq!(
cargo_argv(&PackageSelection::All, Phase::Check, &options)[1..],
[
Expand All @@ -381,8 +387,9 @@ mod test {

#[test]
fn profile_arg_passed_to_cargo() {
let config = Config::default();
let args = Args::try_parse_from(["mutants", "--profile", "mutants"].as_slice()).unwrap();
let options = Options::from_args(&args).unwrap();
let options = Options::new(&args, &config).unwrap();
assert_eq!(
cargo_argv(&PackageSelection::All, Phase::Check, &options)[1..],
[
Expand All @@ -401,7 +408,8 @@ mod test {
["mutants", "--test-tool=nextest", "--profile", "mutants"].as_slice(),
)
.unwrap();
let options = Options::from_args(&args).unwrap();
let config = Config::default();
let options = Options::new(&args, &config).unwrap();
assert_eq!(
cargo_argv(&PackageSelection::All, Phase::Build, &options)[1..],
[
Expand Down
4 changes: 2 additions & 2 deletions src/lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn join_threads(threads: Vec<thread::ScopedJoinHandle<'_, Result<()>>>) -> Resul
struct Lab<'a> {
output_mutex: Mutex<OutputDir>,
jobserver: Option<jobserver::Client>,
options: &'a Options,
options: &'a Options<'a>,
console: &'a Console,
}

Expand Down Expand Up @@ -218,7 +218,7 @@ struct Worker<'a> {
build_dir: &'a BuildDir,
output_mutex: &'a Mutex<OutputDir>,
jobserver: &'a Option<jobserver::Client>,
options: &'a Options,
options: &'a Options<'a>,
console: &'a Console,
}

Expand Down
9 changes: 3 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,12 @@ fn main() -> Result<()> {
PackageFilter::Auto(start_dir.to_owned())
};

let output_parent_dir = options
.output_in_dir
.clone()
.unwrap_or_else(|| workspace.root().to_owned());
let output_parent_dir = options.output_in_dir.unwrap_or_else(|| workspace.root());

let mut discovered = workspace.discover(&package_filter, &options, &console)?;

let previously_caught = if args.iterate {
let previously_caught = load_previously_caught(&output_parent_dir)?;
let previously_caught = load_previously_caught(output_parent_dir)?;
info!(
"Iteration excludes {} previously caught or unviable mutants",
previously_caught.len()
Expand Down Expand Up @@ -477,7 +474,7 @@ fn main() -> Result<()> {
if args.list {
print!("{}", list_mutants(&mutants, &options));
} else {
let output_dir = OutputDir::new(&output_parent_dir)?;
let output_dir = OutputDir::new(output_parent_dir)?;
if let Some(previously_caught) = previously_caught {
output_dir.write_previously_caught(&previously_caught)?;
}
Expand Down
Loading

0 comments on commit 816494a

Please sign in to comment.