Repository: rust-analyzer/rust-analyzer
Pull request #4154 (2020-04-26 08:41:06)

Add `cargo test` to the list of Run commands


bors r+
🤖


Patch:
diff --git a/crates/rust-analyzer/src/cargo_target_spec.rs b/crates/rust-analyzer/src/cargo_target_spec.rs
index c2ece49f42..10c25666a0 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 e87e8db5d3..6caaf5f88d 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)
 }
 


Test patch:
diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs
index b31533e5ef..f6245ddd47 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::<Runnables>(
         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")
             }
-          }
         ]),
     );
 }