Skip to content
Open
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
12 changes: 6 additions & 6 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,18 +909,18 @@ config_data! {
/// Override the command used for bench runnables.
/// The first element of the array should be the program to execute (for example, `cargo`).
///
/// Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${test_name}` to dynamically
/// Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${executable_args}` to dynamically
/// replace the package name, target option (such as `--bin` or `--example`), the target name and
/// the test name (name of test function or test mod path).
/// the arguments passed to test binary args (includes `rust-analyzer.runnables.extraTestBinaryArgs`).
runnables_bench_overrideCommand: Option<Vec<String>> = None,
/// Command to be executed instead of 'cargo' for runnables.
runnables_command: Option<String> = None,
/// Override the command used for bench runnables.
/// The first element of the array should be the program to execute (for example, `cargo`).
///
/// Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${test_name}` to dynamically
/// Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${executable_args}` to dynamically
/// replace the package name, target option (such as `--bin` or `--example`), the target name and
/// the test name (name of test function or test mod path).
/// the arguments passed to test binary args (includes `rust-analyzer.runnables.extraTestBinaryArgs`).
runnables_doctest_overrideCommand: Option<Vec<String>> = None,
/// Additional arguments to be passed to cargo for runnables such as
/// tests or binaries. For example, it may be `--release`.
Expand All @@ -938,9 +938,9 @@ config_data! {
/// Override the command used for test runnables.
/// The first element of the array should be the program to execute (for example, `cargo`).
///
/// Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${test_name}` to dynamically
/// Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${executable_args}` to dynamically
/// replace the package name, target option (such as `--bin` or `--example`), the target name and
/// the test name (name of test function or test mod path).
/// the arguments passed to test binary args (includes `rust-analyzer.runnables.extraTestBinaryArgs`).
runnables_test_overrideCommand: Option<Vec<String>> = None,

/// Path to the Cargo.toml of the rust compiler workspace, for usage in rustc_private
Expand Down
85 changes: 61 additions & 24 deletions crates/rust-analyzer/src/target_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,38 +119,21 @@ impl CargoTargetSpec {
let extra_test_binary_args = config.extra_test_binary_args;

let mut cargo_args = Vec::new();
let mut executable_args = Vec::new();
let executable_args = Self::executable_args_for(kind, extra_test_binary_args);

match kind {
RunnableKind::Test { test_id, attr } => {
RunnableKind::Test { .. } => {
cargo_args.push(config.test_command);
executable_args.push(test_id.to_string());
if let TestId::Path(_) = test_id {
executable_args.push("--exact".to_owned());
}
executable_args.extend(extra_test_binary_args);
if attr.ignore {
executable_args.push("--ignored".to_owned());
}
}
RunnableKind::TestMod { path } => {
RunnableKind::TestMod { .. } => {
cargo_args.push(config.test_command);
executable_args.push(path.clone());
executable_args.extend(extra_test_binary_args);
}
RunnableKind::Bench { test_id } => {
RunnableKind::Bench { .. } => {
cargo_args.push(config.bench_command);
executable_args.push(test_id.to_string());
if let TestId::Path(_) = test_id {
executable_args.push("--exact".to_owned());
}
executable_args.extend(extra_test_binary_args);
}
RunnableKind::DocTest { test_id } => {
RunnableKind::DocTest { .. } => {
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 {
Expand Down Expand Up @@ -243,16 +226,70 @@ impl CargoTargetSpec {
TargetKind::BuildScript | TargetKind::Other => "",
};

let target = |kind, target| match kind {
TargetKind::Bin | TargetKind::Test | TargetKind::Bench | TargetKind::Example => target,
_ => "",
};

let replace_placeholders = |arg: String| match &spec {
Some(spec) => arg
.replace("${package}", &spec.package)
.replace("${target_arg}", target_arg(spec.target_kind))
.replace("${target}", &spec.target)
.replace("${target}", target(spec.target_kind, &spec.target))
.replace("${test_name}", &test_name),
_ => arg,
};

args.map(|args| args.into_iter().map(replace_placeholders).collect())
let extra_test_binary_args = config.extra_test_binary_args;
let executable_args = Self::executable_args_for(kind, extra_test_binary_args);

args.map(|mut args| {
let exec_args_idx = args.iter().position(|a| a == "${executable_args}");

if let Some(idx) = exec_args_idx {
args.splice(idx..idx + 1, executable_args);
}

args.into_iter().map(replace_placeholders).filter(|a| !a.trim().is_empty()).collect()
})
}

fn executable_args_for(
kind: &RunnableKind,
extra_test_binary_args: impl IntoIterator<Item = String>,
) -> Vec<String> {
let mut executable_args = Vec::new();

match kind {
RunnableKind::Test { test_id, attr } => {
executable_args.push(test_id.to_string());
if let TestId::Path(_) = test_id {
executable_args.push("--exact".to_owned());
}
executable_args.extend(extra_test_binary_args);
if attr.ignore {
executable_args.push("--ignored".to_owned());
}
}
RunnableKind::TestMod { path } => {
executable_args.push(path.clone());
executable_args.extend(extra_test_binary_args);
}
RunnableKind::Bench { test_id } => {
executable_args.push(test_id.to_string());
if let TestId::Path(_) = test_id {
executable_args.push("--exact".to_owned());
}
executable_args.extend(extra_test_binary_args);
}
RunnableKind::DocTest { test_id } => {
executable_args.push(test_id.to_string());
executable_args.extend(extra_test_binary_args);
}
RunnableKind::Bin => {}
}

executable_args
}

pub(crate) fn push_to(self, buf: &mut Vec<String>, kind: &RunnableKind) {
Expand Down
12 changes: 6 additions & 6 deletions docs/book/src/configuration_generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -1362,9 +1362,9 @@ Default: `null`
Override the command used for bench runnables.
The first element of the array should be the program to execute (for example, `cargo`).

Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${test_name}` to dynamically
Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${executable_args}` to dynamically
replace the package name, target option (such as `--bin` or `--example`), the target name and
the test name (name of test function or test mod path).
the arguments passed to test binary args (includes `rust-analyzer.runnables.extraTestBinaryArgs`).


## rust-analyzer.runnables.command {#runnables.command}
Expand All @@ -1381,9 +1381,9 @@ Default: `null`
Override the command used for bench runnables.
The first element of the array should be the program to execute (for example, `cargo`).

Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${test_name}` to dynamically
Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${executable_args}` to dynamically
replace the package name, target option (such as `--bin` or `--example`), the target name and
the test name (name of test function or test mod path).
the arguments passed to test binary args (includes `rust-analyzer.runnables.extraTestBinaryArgs`).


## rust-analyzer.runnables.extraArgs {#runnables.extraArgs}
Expand Down Expand Up @@ -1426,9 +1426,9 @@ Default: `null`
Override the command used for test runnables.
The first element of the array should be the program to execute (for example, `cargo`).

Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${test_name}` to dynamically
Use the placeholders `${package}`, `${target_arg}`, `${target}`, `${executable_args}` to dynamically
replace the package name, target option (such as `--bin` or `--example`), the target name and
the test name (name of test function or test mod path).
the arguments passed to test binary args (includes `rust-analyzer.runnables.extraTestBinaryArgs`).


## rust-analyzer.rustc.source {#rustc.source}
Expand Down
6 changes: 3 additions & 3 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2840,7 +2840,7 @@
"title": "Runnables",
"properties": {
"rust-analyzer.runnables.bench.overrideCommand": {
"markdownDescription": "Override the command used for bench runnables.\nThe first element of the array should be the program to execute (for example, `cargo`).\n\nUse the placeholders `${package}`, `${target_arg}`, `${target}`, `${test_name}` to dynamically\nreplace the package name, target option (such as `--bin` or `--example`), the target name and\nthe test name (name of test function or test mod path).",
"markdownDescription": "Override the command used for bench runnables.\nThe first element of the array should be the program to execute (for example, `cargo`).\n\nUse the placeholders `${package}`, `${target_arg}`, `${target}`, `${executable_args}` to dynamically\nreplace the package name, target option (such as `--bin` or `--example`), the target name and\nthe arguments passed to test binary args (includes `rust-analyzer.runnables.extraTestBinaryArgs`).",
"default": null,
"type": [
"null",
Expand Down Expand Up @@ -2869,7 +2869,7 @@
"title": "Runnables",
"properties": {
"rust-analyzer.runnables.doctest.overrideCommand": {
"markdownDescription": "Override the command used for bench runnables.\nThe first element of the array should be the program to execute (for example, `cargo`).\n\nUse the placeholders `${package}`, `${target_arg}`, `${target}`, `${test_name}` to dynamically\nreplace the package name, target option (such as `--bin` or `--example`), the target name and\nthe test name (name of test function or test mod path).",
"markdownDescription": "Override the command used for bench runnables.\nThe first element of the array should be the program to execute (for example, `cargo`).\n\nUse the placeholders `${package}`, `${target_arg}`, `${target}`, `${executable_args}` to dynamically\nreplace the package name, target option (such as `--bin` or `--example`), the target name and\nthe arguments passed to test binary args (includes `rust-analyzer.runnables.extraTestBinaryArgs`).",
"default": null,
"type": [
"null",
Expand Down Expand Up @@ -2923,7 +2923,7 @@
"title": "Runnables",
"properties": {
"rust-analyzer.runnables.test.overrideCommand": {
"markdownDescription": "Override the command used for test runnables.\nThe first element of the array should be the program to execute (for example, `cargo`).\n\nUse the placeholders `${package}`, `${target_arg}`, `${target}`, `${test_name}` to dynamically\nreplace the package name, target option (such as `--bin` or `--example`), the target name and\nthe test name (name of test function or test mod path).",
"markdownDescription": "Override the command used for test runnables.\nThe first element of the array should be the program to execute (for example, `cargo`).\n\nUse the placeholders `${package}`, `${target_arg}`, `${target}`, `${executable_args}` to dynamically\nreplace the package name, target option (such as `--bin` or `--example`), the target name and\nthe arguments passed to test binary args (includes `rust-analyzer.runnables.extraTestBinaryArgs`).",
"default": null,
"type": [
"null",
Expand Down