Skip to content

Make test harness arguments configurable and not --nocapture. #17105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions crates/rust-analyzer/src/cargo_target_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,55 @@ impl CargoTargetSpec {
kind: &RunnableKind,
cfg: &Option<CfgExpr>,
) -> (Vec<String>, Vec<String>) {
let mut args = Vec::new();
let mut extra_args = Vec::new();
let extra_test_binary_args = snap.config.runnables().extra_test_binary_args;

let mut cargo_args = Vec::new();
let mut executable_args = Vec::new();

match kind {
RunnableKind::Test { test_id, attr } => {
args.push("test".to_owned());
extra_args.push(test_id.to_string());
cargo_args.push("test".to_owned());
executable_args.push(test_id.to_string());
if let TestId::Path(_) = test_id {
extra_args.push("--exact".to_owned());
executable_args.push("--exact".to_owned());
}
extra_args.push("--nocapture".to_owned());
executable_args.extend(extra_test_binary_args);
if attr.ignore {
extra_args.push("--ignored".to_owned());
executable_args.push("--ignored".to_owned());
}
}
RunnableKind::TestMod { path } => {
args.push("test".to_owned());
extra_args.push(path.clone());
extra_args.push("--nocapture".to_owned());
cargo_args.push("test".to_owned());
executable_args.push(path.clone());
executable_args.extend(extra_test_binary_args);
}
RunnableKind::Bench { test_id } => {
args.push("bench".to_owned());
extra_args.push(test_id.to_string());
cargo_args.push("bench".to_owned());
executable_args.push(test_id.to_string());
if let TestId::Path(_) = test_id {
extra_args.push("--exact".to_owned());
executable_args.push("--exact".to_owned());
}
extra_args.push("--nocapture".to_owned());
executable_args.extend(extra_test_binary_args);
}
RunnableKind::DocTest { test_id } => {
args.push("test".to_owned());
args.push("--doc".to_owned());
extra_args.push(test_id.to_string());
extra_args.push("--nocapture".to_owned());
cargo_args.push("test".to_owned());
cargo_args.push("--doc".to_owned());
executable_args.push(test_id.to_string());
executable_args.extend(extra_test_binary_args);
}
RunnableKind::Bin => {
let subcommand = match spec {
Some(CargoTargetSpec { target_kind: TargetKind::Test, .. }) => "test",
_ => "run",
};
args.push(subcommand.to_owned());
cargo_args.push(subcommand.to_owned());
}
}

let (allowed_features, target_required_features) = if let Some(mut spec) = spec {
let allowed_features = mem::take(&mut spec.features);
let required_features = mem::take(&mut spec.required_features);
spec.push_to(&mut args, kind);
spec.push_to(&mut cargo_args, kind);
(allowed_features, required_features)
} else {
(Default::default(), Default::default())
Expand All @@ -89,10 +91,10 @@ impl CargoTargetSpec {

match &cargo_config.features {
CargoFeatures::All => {
args.push("--all-features".to_owned());
cargo_args.push("--all-features".to_owned());
for feature in target_required_features {
args.push("--features".to_owned());
args.push(feature);
cargo_args.push("--features".to_owned());
cargo_args.push(feature);
}
}
CargoFeatures::Selected { features, no_default_features } => {
Expand All @@ -108,16 +110,16 @@ impl CargoTargetSpec {

feats.dedup();
for feature in feats {
args.push("--features".to_owned());
args.push(feature);
cargo_args.push("--features".to_owned());
cargo_args.push(feature);
}

if *no_default_features {
args.push("--no-default-features".to_owned());
cargo_args.push("--no-default-features".to_owned());
}
}
}
(args, extra_args)
(cargo_args, executable_args)
}

pub(crate) fn for_file(
Expand Down
11 changes: 11 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ config_data! {
/// Additional arguments to be passed to cargo for runnables such as
/// tests or binaries. For example, it may be `--release`.
runnables_extraArgs: Vec<String> = vec![],
/// Additional arguments to be passed through Cargo to launched tests, benchmarks, or
/// doc-tests.
///
/// Unless the launched target uses a
/// [custom test harness](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-harness-field),
/// they will end up being interpreted as options to
/// [`rustc`’s built-in test harness (“libtest”)](https://doc.rust-lang.org/rustc/tests/index.html#cli-arguments).
runnables_extraTestBinaryArgs: Vec<String> = vec!["--show-output".to_owned()],

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

/// Configuration for workspace symbol search requests.
Expand Down Expand Up @@ -1749,6 +1759,7 @@ impl Config {
RunnablesConfig {
override_cargo: self.runnables_command().clone(),
cargo_extra_args: self.runnables_extraArgs().clone(),
extra_test_binary_args: self.runnables_extraTestBinaryArgs().clone(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rust-analyzer/tests/slow-tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn main() {}
{
"args": {
"cargoArgs": ["test", "--package", "foo", "--test", "spam"],
"executableArgs": ["test_eggs", "--exact", "--nocapture"],
"executableArgs": ["test_eggs", "--exact", "--show-output"],
"cargoExtraArgs": [],
"overrideCargo": null,
"workspaceRoot": server.path().join("foo")
Expand Down Expand Up @@ -190,7 +190,7 @@ fn main() {}
"cargoExtraArgs": [],
"executableArgs": [
"",
"--nocapture"
"--show-output"
]
},
"kind": "cargo",
Expand Down
18 changes: 18 additions & 0 deletions docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,24 @@ Command to be executed instead of 'cargo' for runnables.
--
Additional arguments to be passed to cargo for runnables such as
tests or binaries. For example, it may be `--release`.
--
[[rust-analyzer.runnables.extraTestBinaryArgs]]rust-analyzer.runnables.extraTestBinaryArgs::
+
--
Default:
----
[
"--show-output"
]
----
Additional arguments to be passed through Cargo to launched tests, benchmarks, or
doc-tests.

Unless the launched target uses a
[custom test harness](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-harness-field),
they will end up being interpreted as options to
[`rustc`’s built-in test harness (“libtest”)](https://doc.rust-lang.org/rustc/tests/index.html#cli-arguments).

--
[[rust-analyzer.rustc.source]]rust-analyzer.rustc.source (default: `null`)::
+
Expand Down
10 changes: 10 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,16 @@
"type": "string"
}
},
"rust-analyzer.runnables.extraTestBinaryArgs": {
"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).",
"default": [
"--show-output"
],
"type": "array",
"items": {
"type": "string"
}
},
"rust-analyzer.rustc.source": {
"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.",
"default": null,
Expand Down