Skip to content

Commit 5ef0f44

Browse files
committed
Add cargo test to the list of Run commands
1 parent fe99a29 commit 5ef0f44

File tree

3 files changed

+63
-62
lines changed

3 files changed

+63
-62
lines changed

crates/rust-analyzer/src/cargo_target_spec.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{world::WorldSnapshot, Result};
99
///
1010
/// We use it to cook up the set of cli args we need to pass to Cargo to
1111
/// build/test/run the target.
12+
#[derive(Clone)]
1213
pub(crate) struct CargoTargetSpec {
1314
pub(crate) package: String,
1415
pub(crate) target: String,

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -393,28 +393,37 @@ pub fn handle_runnables(
393393
}
394394
res.push(to_lsp_runnable(&world, file_id, runnable)?);
395395
}
396-
let mut check_args = vec!["check".to_string()];
397-
let label;
396+
// Add `cargo check` and `cargo test` for the whole package
398397
match CargoTargetSpec::for_file(&world, file_id)? {
399398
Some(spec) => {
400-
label = format!("cargo check -p {}", spec.package);
401-
spec.push_to(&mut check_args);
399+
for &cmd in ["check", "test"].iter() {
400+
res.push(req::Runnable {
401+
range: Default::default(),
402+
label: format!("cargo {} -p {}", cmd, spec.package),
403+
bin: "cargo".to_string(),
404+
args: {
405+
let mut args = vec![cmd.to_string()];
406+
spec.clone().push_to(&mut args);
407+
args
408+
},
409+
extra_args: Vec::new(),
410+
env: FxHashMap::default(),
411+
cwd: workspace_root.map(|root| root.to_owned()),
412+
})
413+
}
402414
}
403415
None => {
404-
label = "cargo check --all".to_string();
405-
check_args.push("--all".to_string())
416+
res.push(req::Runnable {
417+
range: Default::default(),
418+
label: "cargo check --workspace".to_string(),
419+
bin: "cargo".to_string(),
420+
args: vec!["check".to_string(), "--workspace".to_string()],
421+
extra_args: Vec::new(),
422+
env: FxHashMap::default(),
423+
cwd: workspace_root.map(|root| root.to_owned()),
424+
});
406425
}
407426
}
408-
// Always add `cargo check`.
409-
res.push(req::Runnable {
410-
range: Default::default(),
411-
label,
412-
bin: "cargo".to_string(),
413-
args: check_args,
414-
extra_args: Vec::new(),
415-
env: FxHashMap::default(),
416-
cwd: workspace_root.map(|root| root.to_owned()),
417-
});
418427
Ok(res)
419428
}
420429

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

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,15 @@ fn foo() {
8787
}
8888
},
8989
{
90-
"args": [
91-
"check",
92-
"--all"
93-
],
90+
"args": ["check", "--workspace"],
9491
"extraArgs": [],
9592
"bin": "cargo",
9693
"env": {},
9794
"cwd": null,
98-
"label": "cargo check --all",
95+
"label": "cargo check --workspace",
9996
"range": {
100-
"end": {
101-
"character": 0,
102-
"line": 0
103-
},
104-
"start": {
105-
"character": 0,
106-
"line": 0
107-
}
97+
"end": { "character": 0, "line": 0 },
98+
"start": { "character": 0, "line": 0 }
10899
}
109100
}
110101
]),
@@ -145,42 +136,42 @@ fn main() {}
145136
server.request::<Runnables>(
146137
RunnablesParams { text_document: server.doc_id("foo/tests/spam.rs"), position: None },
147138
json!([
148-
{
149-
"args": [ "test", "--package", "foo", "--test", "spam" ],
150-
"extraArgs": [ "test_eggs", "--exact", "--nocapture" ],
151-
"bin": "cargo",
152-
"env": { "RUST_BACKTRACE": "short" },
153-
"label": "test test_eggs",
154-
"range": {
155-
"end": { "character": 17, "line": 1 },
156-
"start": { "character": 0, "line": 0 }
139+
{
140+
"args": [ "test", "--package", "foo", "--test", "spam" ],
141+
"extraArgs": [ "test_eggs", "--exact", "--nocapture" ],
142+
"bin": "cargo",
143+
"env": { "RUST_BACKTRACE": "short" },
144+
"label": "test test_eggs",
145+
"range": {
146+
"end": { "character": 17, "line": 1 },
147+
"start": { "character": 0, "line": 0 }
148+
},
149+
"cwd": server.path().join("foo")
157150
},
158-
"cwd": server.path().join("foo")
159-
},
160-
{
161-
"args": [
162-
"check",
163-
"--package",
164-
"foo",
165-
"--test",
166-
"spam"
167-
],
168-
"extraArgs": [],
169-
"bin": "cargo",
170-
"env": {},
171-
"cwd": server.path().join("foo"),
172-
"label": "cargo check -p foo",
173-
"range": {
174-
"end": {
175-
"character": 0,
176-
"line": 0
151+
{
152+
"args": [ "check", "--package", "foo", "--test", "spam" ],
153+
"extraArgs": [],
154+
"bin": "cargo",
155+
"env": {},
156+
"label": "cargo check -p foo",
157+
"range": {
158+
"end": { "character": 0, "line": 0 },
159+
"start": { "character": 0, "line": 0 }
177160
},
178-
"start": {
179-
"character": 0,
180-
"line": 0
181-
}
161+
"cwd": server.path().join("foo")
162+
},
163+
{
164+
"args": [ "test", "--package", "foo", "--test", "spam" ],
165+
"extraArgs": [],
166+
"bin": "cargo",
167+
"env": {},
168+
"label": "cargo test -p foo",
169+
"range": {
170+
"end": { "character": 0, "line": 0 },
171+
"start": { "character": 0, "line": 0 }
172+
},
173+
"cwd": server.path().join("foo")
182174
}
183-
}
184175
]),
185176
);
186177
}

0 commit comments

Comments
 (0)