Skip to content

Commit 3011778

Browse files
Copilotsourcefrog
andcommitted
Add explicit --cargo-test-arg option for test arguments
Co-authored-by: sourcefrog <346355+sourcefrog@users.noreply.github.com>
1 parent 5ded983 commit 3011778

File tree

4 files changed

+139
-4
lines changed

4 files changed

+139
-4
lines changed

book/src/cargo-args.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,26 @@ excludes doctests. (If the doctests are numerous and slow, and not relied upon t
5656
cargo mutants -- --all-targets
5757
```
5858

59+
Alternatively, you can use the explicit `--cargo-test-arg` option, which can be repeated:
60+
61+
```shell
62+
cargo mutants --cargo-test-arg=--all-targets --cargo-test-arg=--no-fail-fast
63+
```
64+
65+
Both formats can be used together if needed:
66+
67+
```shell
68+
cargo mutants --cargo-test-arg=--all-targets -- --nocapture
69+
```
70+
5971
These options can also be configured statically with the `additional_cargo_test_args` key in `.cargo/mutants.toml`:
6072

6173
```toml
6274
additional_cargo_test_args = ["--jobs=1"]
6375
```
6476

77+
When both command-line options and configuration are specified, arguments from `--cargo-test-arg`, then arguments after `--`, and finally configuration file arguments are all combined in that order.
78+
6579
## Arguments to test binaries
6680

6781
You can use a second double-dash to pass options through to the test targets:

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ pub struct Args {
220220
)]
221221
cargo_arg: Vec<String>,
222222

223+
/// Additional args for cargo test.
224+
#[arg(long, allow_hyphen_values = true, help_heading = "Execution")]
225+
cargo_test_arg: Vec<String>,
226+
223227
/// Pass remaining arguments to cargo test after all options and after `--`.
224228
#[arg(last = true, help_heading = "Execution")]
225229
cargo_test_args: Vec<String>,

src/options.rs

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,13 @@ impl Options {
290290

291291
let options = Options {
292292
additional_cargo_args: join_slices(&args.cargo_arg, &config.additional_cargo_args),
293-
additional_cargo_test_args: join_slices(
294-
&args.cargo_test_args,
295-
&config.additional_cargo_test_args,
296-
),
293+
additional_cargo_test_args: args
294+
.cargo_test_arg
295+
.iter()
296+
.chain(&args.cargo_test_args)
297+
.chain(&config.additional_cargo_test_args)
298+
.cloned()
299+
.collect(),
297300
all_features: args.all_features || config.all_features.unwrap_or(false),
298301
annotations: args.annotations.resolve(),
299302
baseline: args.baseline,
@@ -1097,4 +1100,77 @@ mod test {
10971100
let options = Options::new(&args, &config).unwrap();
10981101
assert!(!options.copy_vcs);
10991102
}
1103+
1104+
#[test]
1105+
fn cargo_test_arg_from_command_line() {
1106+
let args = Args::parse_from(["mutants", "--cargo-test-arg=--lib"]);
1107+
let config = Config::default();
1108+
let options = Options::new(&args, &config).unwrap();
1109+
assert_eq!(options.additional_cargo_test_args, vec!["--lib"]);
1110+
}
1111+
1112+
#[test]
1113+
fn cargo_test_arg_multiple_from_command_line() {
1114+
let args = Args::parse_from([
1115+
"mutants",
1116+
"--cargo-test-arg=--lib",
1117+
"--cargo-test-arg=--no-fail-fast",
1118+
]);
1119+
let config = Config::default();
1120+
let options = Options::new(&args, &config).unwrap();
1121+
assert_eq!(
1122+
options.additional_cargo_test_args,
1123+
vec!["--lib", "--no-fail-fast"]
1124+
);
1125+
}
1126+
1127+
#[test]
1128+
fn cargo_test_args_after_double_dash() {
1129+
let args = Args::parse_from(["mutants", "--", "--lib", "--no-fail-fast"]);
1130+
let config = Config::default();
1131+
let options = Options::new(&args, &config).unwrap();
1132+
assert_eq!(
1133+
options.additional_cargo_test_args,
1134+
vec!["--lib", "--no-fail-fast"]
1135+
);
1136+
}
1137+
1138+
#[test]
1139+
fn cargo_test_arg_and_cargo_test_args_combined() {
1140+
let args = Args::parse_from(["mutants", "--cargo-test-arg=--lib", "--", "--no-fail-fast"]);
1141+
let config = Config::default();
1142+
let options = Options::new(&args, &config).unwrap();
1143+
assert_eq!(
1144+
options.additional_cargo_test_args,
1145+
vec!["--lib", "--no-fail-fast"]
1146+
);
1147+
}
1148+
1149+
#[test]
1150+
fn cargo_test_arg_and_config_combined() {
1151+
let args = Args::parse_from(["mutants", "--cargo-test-arg=--lib"]);
1152+
let config = Config::from_str(indoc! { r#"
1153+
additional_cargo_test_args = ["--no-fail-fast"]
1154+
"#})
1155+
.unwrap();
1156+
let options = Options::new(&args, &config).unwrap();
1157+
assert_eq!(
1158+
options.additional_cargo_test_args,
1159+
vec!["--lib", "--no-fail-fast"]
1160+
);
1161+
}
1162+
1163+
#[test]
1164+
fn cargo_test_arg_cargo_test_args_and_config_combined() {
1165+
let args = Args::parse_from(["mutants", "--cargo-test-arg=--all-targets", "--", "--lib"]);
1166+
let config = Config::from_str(indoc! { r#"
1167+
additional_cargo_test_args = ["--no-fail-fast"]
1168+
"#})
1169+
.unwrap();
1170+
let options = Options::new(&args, &config).unwrap();
1171+
assert_eq!(
1172+
options.additional_cargo_test_args,
1173+
vec!["--all-targets", "--lib", "--no-fail-fast"]
1174+
);
1175+
}
11001176
}

tests/config.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,47 @@ fn additional_cargo_test_args() {
269269
.success();
270270
}
271271

272+
#[test]
273+
fn cargo_test_arg_option() {
274+
let testdata = copy_of_testdata("fails_without_feature");
275+
run()
276+
.args(["mutants", "--cargo-test-arg=--all-features", "-d"])
277+
.arg(testdata.path())
278+
.assert()
279+
.success();
280+
}
281+
282+
#[test]
283+
fn cargo_test_arg_multiple_options() {
284+
let testdata = copy_of_testdata("fails_without_feature");
285+
run()
286+
.args([
287+
"mutants",
288+
"--cargo-test-arg=--all-features",
289+
"--cargo-test-arg=--no-fail-fast",
290+
"-d",
291+
])
292+
.arg(testdata.path())
293+
.assert()
294+
.success();
295+
}
296+
297+
#[test]
298+
fn cargo_test_arg_and_additional_cargo_test_args_combined() {
299+
let testdata = copy_of_testdata("fails_without_feature");
300+
write_config_file(
301+
&testdata,
302+
r#"
303+
additional_cargo_test_args = ["--no-fail-fast"]
304+
"#,
305+
);
306+
run()
307+
.args(["mutants", "--cargo-test-arg=--all-features", "-d"])
308+
.arg(testdata.path())
309+
.assert()
310+
.success();
311+
}
312+
272313
#[test]
273314
fn features_config_option() {
274315
let testdata = copy_of_testdata("fails_without_feature");

0 commit comments

Comments
 (0)