Skip to content

Commit 9b4fb7c

Browse files
committed
Auto merge of rust-lang#17105 - kpreid:nocapture, r=Veykril
Make test harness arguments configurable and not `--nocapture`. * Added config `runnables.extraTestBinaryArgs` to control the args. * The default is `--show-output` rather than `--nocapture` to prevent unreadable output when 2 or more tests fail or print output at once. * Renamed variables in `CargoTargetSpec::runnable_args()` for clarity. Fixes rust-lang#12737. Suggested changelog info: > New Features > > * add `rust-analyzer.runnables.extraTestBinaryArgs` to configure test harness options when running tests; replaces a previously hard-coded `--nocapture` option. I'm not sure I made the right choices in vocabulary, between “binary”, “executable”, and “runnable”, and “launch” vs. “execute”, so let me know if I missed something to be consistent with in the naming and documentation.
2 parents af1fd88 + db292bd commit 9b4fb7c

File tree

5 files changed

+70
-29
lines changed

5 files changed

+70
-29
lines changed

crates/rust-analyzer/src/cargo_target_spec.rs

+29-27
Original file line numberDiff line numberDiff line change
@@ -33,53 +33,55 @@ impl CargoTargetSpec {
3333
kind: &RunnableKind,
3434
cfg: &Option<CfgExpr>,
3535
) -> (Vec<String>, Vec<String>) {
36-
let mut args = Vec::new();
37-
let mut extra_args = Vec::new();
36+
let extra_test_binary_args = snap.config.runnables().extra_test_binary_args;
37+
38+
let mut cargo_args = Vec::new();
39+
let mut executable_args = Vec::new();
3840

3941
match kind {
4042
RunnableKind::Test { test_id, attr } => {
41-
args.push("test".to_owned());
42-
extra_args.push(test_id.to_string());
43+
cargo_args.push("test".to_owned());
44+
executable_args.push(test_id.to_string());
4345
if let TestId::Path(_) = test_id {
44-
extra_args.push("--exact".to_owned());
46+
executable_args.push("--exact".to_owned());
4547
}
46-
extra_args.push("--nocapture".to_owned());
48+
executable_args.extend(extra_test_binary_args);
4749
if attr.ignore {
48-
extra_args.push("--ignored".to_owned());
50+
executable_args.push("--ignored".to_owned());
4951
}
5052
}
5153
RunnableKind::TestMod { path } => {
52-
args.push("test".to_owned());
53-
extra_args.push(path.clone());
54-
extra_args.push("--nocapture".to_owned());
54+
cargo_args.push("test".to_owned());
55+
executable_args.push(path.clone());
56+
executable_args.extend(extra_test_binary_args);
5557
}
5658
RunnableKind::Bench { test_id } => {
57-
args.push("bench".to_owned());
58-
extra_args.push(test_id.to_string());
59+
cargo_args.push("bench".to_owned());
60+
executable_args.push(test_id.to_string());
5961
if let TestId::Path(_) = test_id {
60-
extra_args.push("--exact".to_owned());
62+
executable_args.push("--exact".to_owned());
6163
}
62-
extra_args.push("--nocapture".to_owned());
64+
executable_args.extend(extra_test_binary_args);
6365
}
6466
RunnableKind::DocTest { test_id } => {
65-
args.push("test".to_owned());
66-
args.push("--doc".to_owned());
67-
extra_args.push(test_id.to_string());
68-
extra_args.push("--nocapture".to_owned());
67+
cargo_args.push("test".to_owned());
68+
cargo_args.push("--doc".to_owned());
69+
executable_args.push(test_id.to_string());
70+
executable_args.extend(extra_test_binary_args);
6971
}
7072
RunnableKind::Bin => {
7173
let subcommand = match spec {
7274
Some(CargoTargetSpec { target_kind: TargetKind::Test, .. }) => "test",
7375
_ => "run",
7476
};
75-
args.push(subcommand.to_owned());
77+
cargo_args.push(subcommand.to_owned());
7678
}
7779
}
7880

7981
let (allowed_features, target_required_features) = if let Some(mut spec) = spec {
8082
let allowed_features = mem::take(&mut spec.features);
8183
let required_features = mem::take(&mut spec.required_features);
82-
spec.push_to(&mut args, kind);
84+
spec.push_to(&mut cargo_args, kind);
8385
(allowed_features, required_features)
8486
} else {
8587
(Default::default(), Default::default())
@@ -89,10 +91,10 @@ impl CargoTargetSpec {
8991

9092
match &cargo_config.features {
9193
CargoFeatures::All => {
92-
args.push("--all-features".to_owned());
94+
cargo_args.push("--all-features".to_owned());
9395
for feature in target_required_features {
94-
args.push("--features".to_owned());
95-
args.push(feature);
96+
cargo_args.push("--features".to_owned());
97+
cargo_args.push(feature);
9698
}
9799
}
98100
CargoFeatures::Selected { features, no_default_features } => {
@@ -108,16 +110,16 @@ impl CargoTargetSpec {
108110

109111
feats.dedup();
110112
for feature in feats {
111-
args.push("--features".to_owned());
112-
args.push(feature);
113+
cargo_args.push("--features".to_owned());
114+
cargo_args.push(feature);
113115
}
114116

115117
if *no_default_features {
116-
args.push("--no-default-features".to_owned());
118+
cargo_args.push("--no-default-features".to_owned());
117119
}
118120
}
119121
}
120-
(args, extra_args)
122+
(cargo_args, executable_args)
121123
}
122124

123125
pub(crate) fn for_file(

crates/rust-analyzer/src/config.rs

+11
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@ config_data! {
392392
/// Additional arguments to be passed to cargo for runnables such as
393393
/// tests or binaries. For example, it may be `--release`.
394394
runnables_extraArgs: Vec<String> = vec![],
395+
/// Additional arguments to be passed through Cargo to launched tests, benchmarks, or
396+
/// doc-tests.
397+
///
398+
/// Unless the launched target uses a
399+
/// [custom test harness](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-harness-field),
400+
/// they will end up being interpreted as options to
401+
/// [`rustc`’s built-in test harness (“libtest”)](https://doc.rust-lang.org/rustc/tests/index.html#cli-arguments).
402+
runnables_extraTestBinaryArgs: Vec<String> = vec!["--show-output".to_owned()],
395403

396404
/// Path to the Cargo.toml of the rust compiler workspace, for usage in rustc_private
397405
/// projects, or "discover" to try to automatically find it if the `rustc-dev` component
@@ -823,6 +831,8 @@ pub struct RunnablesConfig {
823831
pub override_cargo: Option<String>,
824832
/// Additional arguments for the `cargo`, e.g. `--release`.
825833
pub cargo_extra_args: Vec<String>,
834+
/// Additional arguments for the binary being run, if it is a test or benchmark.
835+
pub extra_test_binary_args: Vec<String>,
826836
}
827837

828838
/// Configuration for workspace symbol search requests.
@@ -1749,6 +1759,7 @@ impl Config {
17491759
RunnablesConfig {
17501760
override_cargo: self.runnables_command().clone(),
17511761
cargo_extra_args: self.runnables_extraArgs().clone(),
1762+
extra_test_binary_args: self.runnables_extraTestBinaryArgs().clone(),
17521763
}
17531764
}
17541765

crates/rust-analyzer/tests/slow-tests/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn main() {}
157157
{
158158
"args": {
159159
"cargoArgs": ["test", "--package", "foo", "--test", "spam"],
160-
"executableArgs": ["test_eggs", "--exact", "--nocapture"],
160+
"executableArgs": ["test_eggs", "--exact", "--show-output"],
161161
"cargoExtraArgs": [],
162162
"overrideCargo": null,
163163
"workspaceRoot": server.path().join("foo")
@@ -190,7 +190,7 @@ fn main() {}
190190
"cargoExtraArgs": [],
191191
"executableArgs": [
192192
"",
193-
"--nocapture"
193+
"--show-output"
194194
]
195195
},
196196
"kind": "cargo",

docs/user/generated_config.adoc

+18
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,24 @@ Command to be executed instead of 'cargo' for runnables.
851851
--
852852
Additional arguments to be passed to cargo for runnables such as
853853
tests or binaries. For example, it may be `--release`.
854+
--
855+
[[rust-analyzer.runnables.extraTestBinaryArgs]]rust-analyzer.runnables.extraTestBinaryArgs::
856+
+
857+
--
858+
Default:
859+
----
860+
[
861+
"--show-output"
862+
]
863+
----
864+
Additional arguments to be passed through Cargo to launched tests, benchmarks, or
865+
doc-tests.
866+
867+
Unless the launched target uses a
868+
[custom test harness](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-harness-field),
869+
they will end up being interpreted as options to
870+
[`rustc`’s built-in test harness (“libtest”)](https://doc.rust-lang.org/rustc/tests/index.html#cli-arguments).
871+
854872
--
855873
[[rust-analyzer.rustc.source]]rust-analyzer.rustc.source (default: `null`)::
856874
+

editors/code/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,16 @@
15941594
"type": "string"
15951595
}
15961596
},
1597+
"rust-analyzer.runnables.extraTestBinaryArgs": {
1598+
"markdownDescription": "Additional arguments to be passed through Cargo to launched tests, benchmarks, or\ndoc-tests.\n\nUnless the launched target uses a\n[custom test harness](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-harness-field),\nthey will end up being interpreted as options to\n[`rustc`’s built-in test harness (“libtest”)](https://doc.rust-lang.org/rustc/tests/index.html#cli-arguments).",
1599+
"default": [
1600+
"--show-output"
1601+
],
1602+
"type": "array",
1603+
"items": {
1604+
"type": "string"
1605+
}
1606+
},
15971607
"rust-analyzer.rustc.source": {
15981608
"markdownDescription": "Path to the Cargo.toml of the rust compiler workspace, for usage in rustc_private\nprojects, or \"discover\" to try to automatically find it if the `rustc-dev` component\nis installed.\n\nAny project which uses rust-analyzer with the rustcPrivate\ncrates must set `[package.metadata.rust-analyzer] rustc_private=true` to use it.\n\nThis option does not take effect until rust-analyzer is restarted.",
15991609
"default": null,

0 commit comments

Comments
 (0)