Skip to content

Commit fe0cd8c

Browse files
committed
Configure cargo args
1 parent fb68a42 commit fe0cd8c

File tree

11 files changed

+146
-5
lines changed

11 files changed

+146
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ exclude = [
8585
"testdata/tree/dependency",
8686
"testdata/tree/everything_skipped",
8787
"testdata/tree/factorial",
88+
"testdata/tree/fails_without_feature",
8889
"testdata/tree/hang_avoided_by_attr/",
8990
"testdata/tree/hang_when_mutated",
9091
"testdata/tree/insta",

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
- Fixed: `--exclude-re` and `--re` can match against the return type as shown in
88
`--list`.
99

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

1313
## 1.1.1
1414

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ flags the function for cargo-mutants.
218218

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

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

233234
exclude_re = ["impl Debug"] # same as -E
234235
examine_re = ["impl Serialize", "impl Deserialize"] # same as -F, test *only* matches
236+
237+
additional_cargo_args = ["--all-features"]
238+
additional_cargo_test_args = ["--jobs=1"]
235239
```
236240

237241
### Exit codes

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ pub struct Config {
3333
pub exclude_re: Vec<String>,
3434
/// Examine only mutants matching these regexps.
3535
pub examine_re: Vec<String>,
36+
/// Pass extra args to every cargo invocation.
37+
pub additional_cargo_args: Vec<String>,
38+
/// Pass extra args to cargo test.
39+
pub additional_cargo_test_args: Vec<String>,
3640
}
3741

3842
impl Config {

src/options.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,18 @@ impl Options {
7474
}
7575

7676
Ok(Options {
77-
additional_cargo_args: args.cargo_arg.clone(),
78-
additional_cargo_test_args: args.cargo_test_args.clone(),
77+
additional_cargo_args: args
78+
.cargo_arg
79+
.iter()
80+
.cloned()
81+
.chain(config.additional_cargo_args.iter().cloned())
82+
.collect(),
83+
additional_cargo_test_args: args
84+
.cargo_test_args
85+
.iter()
86+
.cloned()
87+
.chain(config.additional_cargo_test_args.iter().cloned())
88+
.collect(),
7989
check_only: args.check,
8090
examine_names: Some(
8191
RegexSet::new(args.examine_re.iter().chain(config.examine_re.iter()))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "cargo-mutants-testdata-fails-without-feature"
3+
version = "0.0.0"
4+
edition = "2021"
5+
authors = ["Martin Pool"]
6+
publish = false
7+
8+
[dependencies.mutants]
9+
version = "0.0.3"
10+
11+
[features]
12+
needed = []
13+
14+
[[bin]]
15+
name = "factorial"
16+
doctest = false
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# `fails_without_feature`
2+
3+
The crates for this test fail unless a feature is turned on.
4+
5+
(Not a good style perhaps, but a good way to test that Cargo features can be passed through.)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#[mutants::skip]
2+
fn main() {
3+
for i in 1..=6 {
4+
println!("{}! = {}", i, factorial(i));
5+
}
6+
}
7+
8+
#[cfg(feature = "needed")]
9+
fn factorial(n: u32) -> u32 {
10+
let mut a = 1;
11+
for i in 2..=n {
12+
a *= i;
13+
}
14+
a
15+
}
16+
17+
#[cfg(not(feature = "needed"))]
18+
#[mutants::skip]
19+
fn factorial(_n: u32) -> u32 {
20+
panic!("needed feature is not enabled");
21+
}
22+
23+
#[test]
24+
fn test_factorial() {
25+
println!("factorial({}) = {}", 6, factorial(6)); // This line is here so we can see it in --nocapture
26+
assert_eq!(factorial(6), 720);
27+
}

tests/cli/config.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,56 @@ fn list_with_config_file_regexps() {
119119
"src/simple_fns.rs:17: replace divisible_by_three -> bool with false\n",
120120
));
121121
}
122+
123+
#[test]
124+
fn tree_fails_without_needed_feature() {
125+
// The point of this tree is to check that Cargo features can be turned on,
126+
// but let's make sure it does fail as intended if they're not.
127+
let testdata = copy_of_testdata("fails_without_feature");
128+
run_assert_cmd()
129+
.args(["mutants", "-d"])
130+
.arg(testdata.path())
131+
.assert()
132+
.failure()
133+
.stdout(predicates::str::contains(
134+
"test failed in an unmutated tree",
135+
));
136+
}
137+
138+
#[test]
139+
fn additional_cargo_args() {
140+
// The point of this tree is to check that Cargo features can be turned on,
141+
// but let's make sure it does fail as intended if they're not.
142+
let testdata = copy_of_testdata("fails_without_feature");
143+
write_config_file(
144+
&testdata,
145+
r#"
146+
additional_cargo_args = ["--features", "needed"]
147+
"#,
148+
);
149+
run_assert_cmd()
150+
.args(["mutants", "-d"])
151+
.arg(testdata.path())
152+
.assert()
153+
.success()
154+
.stdout(predicates::str::contains("1 caught"));
155+
}
156+
157+
#[test]
158+
fn additional_cargo_test_args() {
159+
// The point of this tree is to check that Cargo features can be turned on,
160+
// but let's make sure it does fail as intended if they're not.
161+
let testdata = copy_of_testdata("fails_without_feature");
162+
write_config_file(
163+
&testdata,
164+
r#"
165+
additional_cargo_test_args = ["--all-features", ]
166+
"#,
167+
);
168+
run_assert_cmd()
169+
.args(["mutants", "-d"])
170+
.arg(testdata.path())
171+
.assert()
172+
.success()
173+
.stdout(predicates::str::contains("1 caught"));
174+
}

tests/cli/snapshots/cli__list_mutants_in_all_trees_as_json.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ expression: buf
127127
]
128128
```
129129

130+
## testdata/tree/fails_without_feature
131+
132+
```json
133+
[
134+
{
135+
"package": "cargo-mutants-testdata-fails-without-feature",
136+
"file": "src/bin/factorial.rs",
137+
"line": 9,
138+
"function": "factorial",
139+
"return_type": "-> u32",
140+
"replacement": "Default::default()"
141+
}
142+
]
143+
```
144+
130145
## testdata/tree/hang_avoided_by_attr
131146

132147
```json

0 commit comments

Comments
 (0)