Repository: intellij-rust/intellij-rust
Issue: cargo test --no-run won't run becasue --no-run is applied twice

<!--
Hello and thank you for the issue!
If you would like to report a bug, we have added some points below that you can fill out.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **Intellij-Rust plugin version:** v0.2.0.2111-183
* **Rust toolchain version:** rustc 1.30.1 (1433507eb 2018-11-07) / cargo 1.30.0 (a1a4ad372 2018-11-02)
* **IDE name and version:** IntelliJ IDEA 2018.3 (Ultimate Edition) Build #IU-183.4284.148, built on November 21, 2018
* **Operating system:** Windows 10

## Problem description
When I try to run a Cargo configuration that includes `test --no-run` I get an error like the one below:
```
C:/Users/Mike/.cargo/bin/cargo.exe test --no-run --no-run
error: The argument '--no-run' was provided more than once, but cannot be used multiple times

USAGE:
    cargo.exe test --message-format <FMT> --no-run

For more information try --help

Process finished with exit code 1
```
This didn't happen until recently when I upgraded the plugin (I don't remember which version I had before, sorry). My run configuration definitely only has `--no-run` once. Things work fine when I run Cargo from the command line.

## Steps to reproduce
- Create a new cargo run configuration
- Enter `test --no-run` as the command
- Run the configuration

**Project with the problem**: [mmlinford/intellij-rust-no-run-issue](https://github.com/mmlinford/intellij-rust-no-run-issue)

<!--
Please include as much of your codebase as needed to reproduce the error.
If the relevant files are large, please provide a link to a public repository or a [Gist](https://gist.github.com/).
-->


Changed files:
debugger/src/main/kotlin/org/rust/debugger/runconfig/RsDebugRunner.kt, src/main/kotlin/org/rust/cargo/runconfig/RsTestRunner.kt

diff --git a/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsDebugRunner.kt b/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsDebugRunner.kt
index 4b28e3c9c..9e67c1c8b 100644
--- a/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsDebugRunner.kt
+++ b/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsDebugRunner.kt
@@ -68,8 +68,9 @@ class RsDebugRunner : AsyncProgramRunner<RunnerSettings>() {
         val (commandArguments, executableArguments) = CargoArgsParser.parseArgs(cmd.command, cmd.additionalArguments)
 
         val isTestRun = cmd.command == "test"
+        val cmdHasNoRun = "--no-run" in cmd.additionalArguments
         val buildCommand = if (isTestRun) {
-            cmd.prependArgument("--no-run")
+            if (cmdHasNoRun) cmd else cmd.prependArgument("--no-run")
         } else {
             cmd.copy(command = "build", additionalArguments = commandArguments)
         }
@@ -88,6 +89,7 @@ class RsDebugRunner : AsyncProgramRunner<RunnerSettings>() {
 
         return buildProjectAndGetBinaryArtifactPath(environment.project, buildCommand, state, isTestRun)
             .then { binary ->
+                if (isTestRun && cmdHasNoRun) return@then null
                 val path = binary?.path ?: return@then null
                 val runCommand = getRunCommand(path)
                 val sysroot = state.computeSysroot()
diff --git a/src/main/kotlin/org/rust/cargo/runconfig/RsTestRunner.kt b/src/main/kotlin/org/rust/cargo/runconfig/RsTestRunner.kt
index af0da4246..a2441ba31 100644
--- a/src/main/kotlin/org/rust/cargo/runconfig/RsTestRunner.kt
+++ b/src/main/kotlin/org/rust/cargo/runconfig/RsTestRunner.kt
@@ -29,19 +29,22 @@ class RsTestRunner : AsyncProgramRunner<RunnerSettings>() {
         return cleaned.cmd.command == "test"
     }
 
-    override fun execute(environment: ExecutionEnvironment, state: RunProfileState): Promise<RunContentDescriptor?> =
-        buildTests(environment.project, state as CargoRunStateBase)
+    override fun execute(environment: ExecutionEnvironment, state: RunProfileState): Promise<RunContentDescriptor?> {
+        state as CargoRunStateBase
+        val onlyBuild = "--no-run" in state.commandLine.additionalArguments
+        return buildTests(environment.project, state, onlyBuild)
             .then { exitCode ->
-                if (exitCode != 0) return@then null
+                if (onlyBuild || exitCode != 0) return@then null
                 showRunContent(state.execute(environment.executor, this), environment)
             }
+    }
 
     override fun getRunnerId(): String = "RsTestRunner"
 }
 
-private fun buildTests(project: Project, state: CargoRunStateBase): Promise<Int?> {
+private fun buildTests(project: Project, state: CargoRunStateBase, cmdHasNoRun: Boolean): Promise<Int?> {
     val buildProcessHandler = run {
-        val buildCmd = state.commandLine.prependArgument("--no-run")
+        val buildCmd = if (cmdHasNoRun) state.commandLine else state.commandLine.prependArgument("--no-run")
         val buildConfig = CargoCommandConfiguration.CleanConfiguration.Ok(buildCmd, state.config.toolchain)
         val buildState = CargoRunState(state.environment, buildConfig)
         buildState.startProcess()