Repository: dtolnay/cargo-expand (language: rust)

Issue:
Expanding tests
Related to #20, but I can't seem to use the solution given there:
```
$ cargo expand -- --test
error: Invalid value for '<ITEM>': `--test` is not an identifier
$ cargo expand --version
cargo-expand 0.4.4
```
How should I pass arguments to rustc with the addition of the `cargo-expand` arguments to expand specific items?


Discussion / hints:
That issue is from 0.3. It should be `cargo expand --test name_of_test_target`.
Okay then I'm confused as to what a test target is. My project is set up with several crates in a workspace, and I'm trying to expand the tests in `lib.rs` of one of the sub-crates. What is the value for `name_of_test_target`?
```
$ cargo expand --manifest-path ./units/Cargo.toml --test units
error: no test target named `units`
No such file or directory (os error 2)
$ cargo expand --manifest-path ./units/Cargo.toml --test ./units
error: no test target named `./units`
No such file or directory (os error 2)
$ cargo expand --manifest-path ./units/Cargo.toml --test lib
error: no test target named `lib`
No such file or directory (os error 2)
$ cargo expand --manifest-path ./units/Cargo.toml --test ./units/lib.rs
error: no test target named `./units/lib.rs`
No such file or directory (os error 2)
$ cargo expand --manifest-path ./units/Cargo.toml --test ./units/lib
error: no test target named `./units/lib`
No such file or directory (os error 2)
```
Okay after some searching I think I understand what test targets are, but that's not what I'm looking for. Is there a way to tell `expand` to compile the `lib` in "test mode"? I think in `rustc` the option is called `--test`.
The test target is by default `test_target` for a test located in `tests/test_target.rs`, or `test_target` for a test that is named explicitly in Cargo.toml:

```toml
[[test]]
name = "test_target"
path = "path/to/test.rs"
```

Tests in lib.rs are wacky and I don't think Cargo has a way to refer to them by target, but they are expanded as part of lib.rs whenever `cfg(test)` is set so this should work:

```sh
RUSTFLAGS='--cfg test' cargo expand
```

Fix patch:
diff --git a/src/main.rs b/src/main.rs
index 9cf3977..18ec8fb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -231,6 +231,15 @@ fn apply_args(cmd: &mut Command, args: &Args, outfile: &Path) {
         cmd.arg(test);
     }
 
+    if args.tests {
+        let mut rustflags = OsString::from("--cfg test");
+        if let Some(env_rustflags) = env::var_os("RUSTFLAGS") {
+            rustflags.push(" ");
+            rustflags.push(env_rustflags);
+        }
+        cmd.env("RUSTFLAGS", rustflags);
+    }
+
     if let Some(bench) = &args.bench {
         cmd.arg("--bench");
         cmd.arg(bench);
diff --git a/src/opts.rs b/src/opts.rs
index 8170351..91161f5 100644
--- a/src/opts.rs
+++ b/src/opts.rs
@@ -51,6 +51,10 @@ pub struct Args {
     #[structopt(long, value_name = "NAME")]
     pub test: Option<String>,
 
+    /// Include tests when expanding the lib or bin
+    #[structopt(long)]
+    pub tests: bool,
+
     /// Build only the specified bench target
     #[structopt(long, value_name = "NAME")]
     pub bench: Option<String>,


Test patch: