Skip to content

Commit eab385e

Browse files
committed
Apply cargo flags in test explorer
1 parent 5b08b17 commit eab385e

File tree

4 files changed

+102
-80
lines changed

4 files changed

+102
-80
lines changed

crates/flycheck/src/lib.rs

+42-42
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,50 @@ pub enum InvocationLocation {
4141
Workspace,
4242
}
4343

44+
#[derive(Clone, Debug, PartialEq, Eq)]
45+
pub struct CargoOptions {
46+
pub target_triples: Vec<String>,
47+
pub all_targets: bool,
48+
pub no_default_features: bool,
49+
pub all_features: bool,
50+
pub features: Vec<String>,
51+
pub extra_args: Vec<String>,
52+
pub extra_env: FxHashMap<String, String>,
53+
pub target_dir: Option<Utf8PathBuf>,
54+
}
55+
56+
impl CargoOptions {
57+
fn apply_on_command(&self, cmd: &mut Command) {
58+
for target in &self.target_triples {
59+
cmd.args(["--target", target.as_str()]);
60+
}
61+
if self.all_targets {
62+
cmd.arg("--all-targets");
63+
}
64+
if self.all_features {
65+
cmd.arg("--all-features");
66+
} else {
67+
if self.no_default_features {
68+
cmd.arg("--no-default-features");
69+
}
70+
if !self.features.is_empty() {
71+
cmd.arg("--features");
72+
cmd.arg(self.features.join(" "));
73+
}
74+
}
75+
if let Some(target_dir) = &self.target_dir {
76+
cmd.arg("--target-dir").arg(target_dir);
77+
}
78+
cmd.envs(&self.extra_env);
79+
}
80+
}
81+
4482
#[derive(Clone, Debug, PartialEq, Eq)]
4583
pub enum FlycheckConfig {
4684
CargoCommand {
4785
command: String,
48-
target_triples: Vec<String>,
49-
all_targets: bool,
50-
no_default_features: bool,
51-
all_features: bool,
52-
features: Vec<String>,
53-
extra_args: Vec<String>,
54-
extra_env: FxHashMap<String, String>,
86+
options: CargoOptions,
5587
ansi_color_output: bool,
56-
target_dir: Option<Utf8PathBuf>,
5788
},
5889
CustomCommand {
5990
command: String,
@@ -332,18 +363,7 @@ impl FlycheckActor {
332363
saved_file: Option<&AbsPath>,
333364
) -> Option<Command> {
334365
let (mut cmd, args) = match &self.config {
335-
FlycheckConfig::CargoCommand {
336-
command,
337-
target_triples,
338-
no_default_features,
339-
all_targets,
340-
all_features,
341-
extra_args,
342-
features,
343-
extra_env,
344-
ansi_color_output,
345-
target_dir,
346-
} => {
366+
FlycheckConfig::CargoCommand { command, options, ansi_color_output } => {
347367
let mut cmd = Command::new(Tool::Cargo.path());
348368
if let Some(sysroot_root) = &self.sysroot_root {
349369
cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(sysroot_root));
@@ -365,28 +385,8 @@ impl FlycheckActor {
365385
cmd.arg("--manifest-path");
366386
cmd.arg(self.root.join("Cargo.toml"));
367387

368-
for target in target_triples {
369-
cmd.args(["--target", target.as_str()]);
370-
}
371-
if *all_targets {
372-
cmd.arg("--all-targets");
373-
}
374-
if *all_features {
375-
cmd.arg("--all-features");
376-
} else {
377-
if *no_default_features {
378-
cmd.arg("--no-default-features");
379-
}
380-
if !features.is_empty() {
381-
cmd.arg("--features");
382-
cmd.arg(features.join(" "));
383-
}
384-
}
385-
if let Some(target_dir) = target_dir {
386-
cmd.arg("--target-dir").arg(target_dir);
387-
}
388-
cmd.envs(extra_env);
389-
(cmd, extra_args.clone())
388+
options.apply_on_command(&mut cmd);
389+
(cmd, options.extra_args.clone())
390390
}
391391
FlycheckConfig::CustomCommand {
392392
command,

crates/flycheck/src/test_runner.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use crossbeam_channel::Receiver;
77
use serde::Deserialize;
88
use toolchain::Tool;
99

10-
use crate::command::{CommandHandle, ParseFromLine};
10+
use crate::{
11+
command::{CommandHandle, ParseFromLine},
12+
CargoOptions,
13+
};
1114

1215
#[derive(Debug, Deserialize)]
1316
#[serde(tag = "event", rename_all = "camelCase")]
@@ -58,13 +61,14 @@ pub struct CargoTestHandle {
5861
// cargo test --workspace --no-fail-fast -- module::func -Z unstable-options --format=json
5962

6063
impl CargoTestHandle {
61-
pub fn new(path: Option<&str>) -> std::io::Result<Self> {
64+
pub fn new(path: Option<&str>, options: CargoOptions) -> std::io::Result<Self> {
6265
let mut cmd = Command::new(Tool::Cargo.path());
6366
cmd.env("RUSTC_BOOTSTRAP", "1");
6467
cmd.arg("test");
6568
cmd.arg("--workspace");
6669
// --no-fail-fast is needed to ensure that all requested tests will run
6770
cmd.arg("--no-fail-fast");
71+
options.apply_on_command(&mut cmd);
6872
cmd.arg("--");
6973
if let Some(path) = path {
7074
cmd.arg(path);

crates/rust-analyzer/src/config.rs

+51-33
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use std::{fmt, iter, ops::Not};
1111

1212
use cfg::{CfgAtom, CfgDiff};
13-
use flycheck::FlycheckConfig;
13+
use flycheck::{CargoOptions, FlycheckConfig};
1414
use ide::{
1515
AssistConfig, CallableSnippets, CompletionConfig, DiagnosticsConfig, ExprFillDefaultMode,
1616
HighlightConfig, HighlightRelatedConfig, HoverConfig, HoverDocFormat, InlayFieldsToResolve,
@@ -1364,6 +1364,22 @@ impl Config {
13641364
self.data.check_workspace
13651365
}
13661366

1367+
pub fn cargo_test_options(&self) -> CargoOptions {
1368+
CargoOptions {
1369+
target_triples: self.data.cargo_target.clone().into_iter().collect(),
1370+
all_targets: false,
1371+
no_default_features: self.data.cargo_noDefaultFeatures,
1372+
all_features: matches!(self.data.cargo_features, CargoFeaturesDef::All),
1373+
features: match self.data.cargo_features.clone() {
1374+
CargoFeaturesDef::All => vec![],
1375+
CargoFeaturesDef::Selected(it) => it,
1376+
},
1377+
extra_args: self.extra_args().clone(),
1378+
extra_env: self.extra_env().clone(),
1379+
target_dir: self.target_dir_from_config(),
1380+
}
1381+
}
1382+
13671383
pub fn flycheck(&self) -> FlycheckConfig {
13681384
match &self.data.check_overrideCommand {
13691385
Some(args) if !args.is_empty() => {
@@ -1389,37 +1405,39 @@ impl Config {
13891405
}
13901406
Some(_) | None => FlycheckConfig::CargoCommand {
13911407
command: self.data.check_command.clone(),
1392-
target_triples: self
1393-
.data
1394-
.check_targets
1395-
.clone()
1396-
.and_then(|targets| match &targets.0[..] {
1397-
[] => None,
1398-
targets => Some(targets.into()),
1399-
})
1400-
.unwrap_or_else(|| self.data.cargo_target.clone().into_iter().collect()),
1401-
all_targets: self.data.check_allTargets.unwrap_or(self.data.cargo_allTargets),
1402-
no_default_features: self
1403-
.data
1404-
.check_noDefaultFeatures
1405-
.unwrap_or(self.data.cargo_noDefaultFeatures),
1406-
all_features: matches!(
1407-
self.data.check_features.as_ref().unwrap_or(&self.data.cargo_features),
1408-
CargoFeaturesDef::All
1409-
),
1410-
features: match self
1411-
.data
1412-
.check_features
1413-
.clone()
1414-
.unwrap_or_else(|| self.data.cargo_features.clone())
1415-
{
1416-
CargoFeaturesDef::All => vec![],
1417-
CargoFeaturesDef::Selected(it) => it,
1408+
options: CargoOptions {
1409+
target_triples: self
1410+
.data
1411+
.check_targets
1412+
.clone()
1413+
.and_then(|targets| match &targets.0[..] {
1414+
[] => None,
1415+
targets => Some(targets.into()),
1416+
})
1417+
.unwrap_or_else(|| self.data.cargo_target.clone().into_iter().collect()),
1418+
all_targets: self.data.check_allTargets.unwrap_or(self.data.cargo_allTargets),
1419+
no_default_features: self
1420+
.data
1421+
.check_noDefaultFeatures
1422+
.unwrap_or(self.data.cargo_noDefaultFeatures),
1423+
all_features: matches!(
1424+
self.data.check_features.as_ref().unwrap_or(&self.data.cargo_features),
1425+
CargoFeaturesDef::All
1426+
),
1427+
features: match self
1428+
.data
1429+
.check_features
1430+
.clone()
1431+
.unwrap_or_else(|| self.data.cargo_features.clone())
1432+
{
1433+
CargoFeaturesDef::All => vec![],
1434+
CargoFeaturesDef::Selected(it) => it,
1435+
},
1436+
extra_args: self.check_extra_args(),
1437+
extra_env: self.check_extra_env(),
1438+
target_dir: self.target_dir_from_config(),
14181439
},
1419-
extra_args: self.check_extra_args(),
1420-
extra_env: self.check_extra_env(),
14211440
ansi_color_output: self.color_diagnostic_output(),
1422-
target_dir: self.target_dir_from_config(),
14231441
},
14241442
}
14251443
}
@@ -2772,7 +2790,7 @@ mod tests {
27722790
.unwrap();
27732791
assert_eq!(config.data.cargo_targetDir, None);
27742792
assert!(
2775-
matches!(config.flycheck(), FlycheckConfig::CargoCommand { target_dir, .. } if target_dir.is_none())
2793+
matches!(config.flycheck(), FlycheckConfig::CargoCommand { options, .. } if options.target_dir.is_none())
27762794
);
27772795
}
27782796

@@ -2791,7 +2809,7 @@ mod tests {
27912809
.unwrap();
27922810
assert_eq!(config.data.cargo_targetDir, Some(TargetDirectory::UseSubdirectory(true)));
27932811
assert!(
2794-
matches!(config.flycheck(), FlycheckConfig::CargoCommand { target_dir, .. } if target_dir == Some(Utf8PathBuf::from("target/rust-analyzer")))
2812+
matches!(config.flycheck(), FlycheckConfig::CargoCommand { options, .. } if options.target_dir == Some(Utf8PathBuf::from("target/rust-analyzer")))
27952813
);
27962814
}
27972815

@@ -2813,7 +2831,7 @@ mod tests {
28132831
Some(TargetDirectory::Directory(Utf8PathBuf::from("other_folder")))
28142832
);
28152833
assert!(
2816-
matches!(config.flycheck(), FlycheckConfig::CargoCommand { target_dir, .. } if target_dir == Some(Utf8PathBuf::from("other_folder")))
2834+
matches!(config.flycheck(), FlycheckConfig::CargoCommand { options, .. } if options.target_dir == Some(Utf8PathBuf::from("other_folder")))
28172835
);
28182836
}
28192837
}

crates/rust-analyzer/src/handlers/request.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ pub(crate) fn handle_run_test(
220220
None => "".to_owned(),
221221
};
222222
let handle = if lca.is_empty() {
223-
flycheck::CargoTestHandle::new(None)
223+
flycheck::CargoTestHandle::new(None, state.config.cargo_test_options())
224224
} else if let Some((_, path)) = lca.split_once("::") {
225-
flycheck::CargoTestHandle::new(Some(path))
225+
flycheck::CargoTestHandle::new(Some(path), state.config.cargo_test_options())
226226
} else {
227-
flycheck::CargoTestHandle::new(None)
227+
flycheck::CargoTestHandle::new(None, state.config.cargo_test_options())
228228
};
229229
state.test_run_session = Some(handle?);
230230
Ok(())

0 commit comments

Comments
 (0)