From 5ef0f44499321ff2e8de8daf35c3ea2015c89375 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 26 Apr 2020 10:40:13 +0200 Subject: Add `cargo test` to the list of Run commands --- crates/rust-analyzer/src/cargo_target_spec.rs | 1 + crates/rust-analyzer/src/main_loop/handlers.rs | 41 ++++++++----- crates/rust-analyzer/tests/heavy_tests/main.rs | 83 ++++++++++++-------------- 3 files changed, 63 insertions(+), 62 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/cargo_target_spec.rs b/crates/rust-analyzer/src/cargo_target_spec.rs index c2ece49f4..10c25666a 100644 --- a/crates/rust-analyzer/src/cargo_target_spec.rs +++ b/crates/rust-analyzer/src/cargo_target_spec.rs @@ -9,6 +9,7 @@ use crate::{world::WorldSnapshot, Result}; /// /// We use it to cook up the set of cli args we need to pass to Cargo to /// build/test/run the target. +#[derive(Clone)] pub(crate) struct CargoTargetSpec { pub(crate) package: String, pub(crate) target: String, diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index e87e8db5d..6caaf5f88 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -393,28 +393,37 @@ pub fn handle_runnables( } res.push(to_lsp_runnable(&world, file_id, runnable)?); } - let mut check_args = vec!["check".to_string()]; - let label; + // Add `cargo check` and `cargo test` for the whole package match CargoTargetSpec::for_file(&world, file_id)? { Some(spec) => { - label = format!("cargo check -p {}", spec.package); - spec.push_to(&mut check_args); + for &cmd in ["check", "test"].iter() { + res.push(req::Runnable { + range: Default::default(), + label: format!("cargo {} -p {}", cmd, spec.package), + bin: "cargo".to_string(), + args: { + let mut args = vec![cmd.to_string()]; + spec.clone().push_to(&mut args); + args + }, + extra_args: Vec::new(), + env: FxHashMap::default(), + cwd: workspace_root.map(|root| root.to_owned()), + }) + } } None => { - label = "cargo check --all".to_string(); - check_args.push("--all".to_string()) + res.push(req::Runnable { + range: Default::default(), + label: "cargo check --workspace".to_string(), + bin: "cargo".to_string(), + args: vec!["check".to_string(), "--workspace".to_string()], + extra_args: Vec::new(), + env: FxHashMap::default(), + cwd: workspace_root.map(|root| root.to_owned()), + }); } } - // Always add `cargo check`. - res.push(req::Runnable { - range: Default::default(), - label, - bin: "cargo".to_string(), - args: check_args, - extra_args: Vec::new(), - env: FxHashMap::default(), - cwd: workspace_root.map(|root| root.to_owned()), - }); Ok(res) } diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index b31533e5e..f6245ddd4 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs @@ -87,24 +87,15 @@ fn foo() { } }, { - "args": [ - "check", - "--all" - ], + "args": ["check", "--workspace"], "extraArgs": [], "bin": "cargo", "env": {}, "cwd": null, - "label": "cargo check --all", + "label": "cargo check --workspace", "range": { - "end": { - "character": 0, - "line": 0 - }, - "start": { - "character": 0, - "line": 0 - } + "end": { "character": 0, "line": 0 }, + "start": { "character": 0, "line": 0 } } } ]), @@ -145,42 +136,42 @@ fn main() {} server.request::( RunnablesParams { text_document: server.doc_id("foo/tests/spam.rs"), position: None }, json!([ - { - "args": [ "test", "--package", "foo", "--test", "spam" ], - "extraArgs": [ "test_eggs", "--exact", "--nocapture" ], - "bin": "cargo", - "env": { "RUST_BACKTRACE": "short" }, - "label": "test test_eggs", - "range": { - "end": { "character": 17, "line": 1 }, - "start": { "character": 0, "line": 0 } + { + "args": [ "test", "--package", "foo", "--test", "spam" ], + "extraArgs": [ "test_eggs", "--exact", "--nocapture" ], + "bin": "cargo", + "env": { "RUST_BACKTRACE": "short" }, + "label": "test test_eggs", + "range": { + "end": { "character": 17, "line": 1 }, + "start": { "character": 0, "line": 0 } + }, + "cwd": server.path().join("foo") }, - "cwd": server.path().join("foo") - }, - { - "args": [ - "check", - "--package", - "foo", - "--test", - "spam" - ], - "extraArgs": [], - "bin": "cargo", - "env": {}, - "cwd": server.path().join("foo"), - "label": "cargo check -p foo", - "range": { - "end": { - "character": 0, - "line": 0 + { + "args": [ "check", "--package", "foo", "--test", "spam" ], + "extraArgs": [], + "bin": "cargo", + "env": {}, + "label": "cargo check -p foo", + "range": { + "end": { "character": 0, "line": 0 }, + "start": { "character": 0, "line": 0 } }, - "start": { - "character": 0, - "line": 0 - } + "cwd": server.path().join("foo") + }, + { + "args": [ "test", "--package", "foo", "--test", "spam" ], + "extraArgs": [], + "bin": "cargo", + "env": {}, + "label": "cargo test -p foo", + "range": { + "end": { "character": 0, "line": 0 }, + "start": { "character": 0, "line": 0 } + }, + "cwd": server.path().join("foo") } - } ]), ); } -- cgit v1.2.3