Skip to content

Commit

Permalink
Configure cargo args
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Nov 25, 2022
1 parent fb68a42 commit fe0cd8c
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ exclude = [
"testdata/tree/dependency",
"testdata/tree/everything_skipped",
"testdata/tree/factorial",
"testdata/tree/fails_without_feature",
"testdata/tree/hang_avoided_by_attr/",
"testdata/tree/hang_when_mutated",
"testdata/tree/insta",
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
- Fixed: `--exclude-re` and `--re` can match against the return type as shown in
`--list`.

- New: a `.cargo/mutants.toml` file can be used to configure standard filters
for a project.
- New: A `.cargo/mutants.toml` file can be used to configure standard filters
and cargo args for a project.

## 1.1.1

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ flags the function for cargo-mutants.

cargo-mutants looks for a `.cargo/mutants.toml` file in the root of the source
directory. If a config file exists, the values are appended to the corresponding
command-line arguments.
command-line arguments. (This may cause problems if you use `--` twice on the
command line to pass arguments to the inner test binary.)

Configured exclusions may be particularly important when there are modules that
are inherently hard to test, and the project has made a decision to accept lower
Expand All @@ -232,6 +233,9 @@ examine_globs = ["src/important/*.rs"] # same as -f, test *only* these files

exclude_re = ["impl Debug"] # same as -E
examine_re = ["impl Serialize", "impl Deserialize"] # same as -F, test *only* matches

additional_cargo_args = ["--all-features"]
additional_cargo_test_args = ["--jobs=1"]
```

### Exit codes
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub struct Config {
pub exclude_re: Vec<String>,
/// Examine only mutants matching these regexps.
pub examine_re: Vec<String>,
/// Pass extra args to every cargo invocation.
pub additional_cargo_args: Vec<String>,
/// Pass extra args to cargo test.
pub additional_cargo_test_args: Vec<String>,
}

impl Config {
Expand Down
14 changes: 12 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ impl Options {
}

Ok(Options {
additional_cargo_args: args.cargo_arg.clone(),
additional_cargo_test_args: args.cargo_test_args.clone(),
additional_cargo_args: args
.cargo_arg
.iter()
.cloned()
.chain(config.additional_cargo_args.iter().cloned())
.collect(),
additional_cargo_test_args: args
.cargo_test_args
.iter()
.cloned()
.chain(config.additional_cargo_test_args.iter().cloned())
.collect(),
check_only: args.check,
examine_names: Some(
RegexSet::new(args.examine_re.iter().chain(config.examine_re.iter()))
Expand Down
16 changes: 16 additions & 0 deletions testdata/tree/fails_without_feature/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "cargo-mutants-testdata-fails-without-feature"
version = "0.0.0"
edition = "2021"
authors = ["Martin Pool"]
publish = false

[dependencies.mutants]
version = "0.0.3"

[features]
needed = []

[[bin]]
name = "factorial"
doctest = false
5 changes: 5 additions & 0 deletions testdata/tree/fails_without_feature/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `fails_without_feature`

The crates for this test fail unless a feature is turned on.

(Not a good style perhaps, but a good way to test that Cargo features can be passed through.)
27 changes: 27 additions & 0 deletions testdata/tree/fails_without_feature/src/bin/factorial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#[mutants::skip]
fn main() {
for i in 1..=6 {
println!("{}! = {}", i, factorial(i));
}
}

#[cfg(feature = "needed")]
fn factorial(n: u32) -> u32 {
let mut a = 1;
for i in 2..=n {
a *= i;
}
a
}

#[cfg(not(feature = "needed"))]
#[mutants::skip]
fn factorial(_n: u32) -> u32 {
panic!("needed feature is not enabled");
}

#[test]
fn test_factorial() {
println!("factorial({}) = {}", 6, factorial(6)); // This line is here so we can see it in --nocapture
assert_eq!(factorial(6), 720);
}
53 changes: 53 additions & 0 deletions tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,56 @@ fn list_with_config_file_regexps() {
"src/simple_fns.rs:17: replace divisible_by_three -> bool with false\n",
));
}

#[test]
fn tree_fails_without_needed_feature() {
// The point of this tree is to check that Cargo features can be turned on,
// but let's make sure it does fail as intended if they're not.
let testdata = copy_of_testdata("fails_without_feature");
run_assert_cmd()
.args(["mutants", "-d"])
.arg(testdata.path())
.assert()
.failure()
.stdout(predicates::str::contains(
"test failed in an unmutated tree",
));
}

#[test]
fn additional_cargo_args() {
// The point of this tree is to check that Cargo features can be turned on,
// but let's make sure it does fail as intended if they're not.
let testdata = copy_of_testdata("fails_without_feature");
write_config_file(
&testdata,
r#"
additional_cargo_args = ["--features", "needed"]
"#,
);
run_assert_cmd()
.args(["mutants", "-d"])
.arg(testdata.path())
.assert()
.success()
.stdout(predicates::str::contains("1 caught"));
}

#[test]
fn additional_cargo_test_args() {
// The point of this tree is to check that Cargo features can be turned on,
// but let's make sure it does fail as intended if they're not.
let testdata = copy_of_testdata("fails_without_feature");
write_config_file(
&testdata,
r#"
additional_cargo_test_args = ["--all-features", ]
"#,
);
run_assert_cmd()
.args(["mutants", "-d"])
.arg(testdata.path())
.assert()
.success()
.stdout(predicates::str::contains("1 caught"));
}
15 changes: 15 additions & 0 deletions tests/cli/snapshots/cli__list_mutants_in_all_trees_as_json.snap
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ expression: buf
]
```

## testdata/tree/fails_without_feature

```json
[
{
"package": "cargo-mutants-testdata-fails-without-feature",
"file": "src/bin/factorial.rs",
"line": 9,
"function": "factorial",
"return_type": "-> u32",
"replacement": "Default::default()"
}
]
```

## testdata/tree/hang_avoided_by_attr

```json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ src/bin/factorial.rs:1: replace main with ()
src/bin/factorial.rs:7: replace factorial -> u32 with Default::default()
```

## testdata/tree/fails_without_feature

```
src/bin/factorial.rs:9: replace factorial -> u32 with Default::default()
```

## testdata/tree/hang_avoided_by_attr

```
Expand Down

0 comments on commit fe0cd8c

Please sign in to comment.