Fix wrong profile selection for `cargo build` and `cargo rustc`.
authorEric Huss <eric@huss.org>
Sun, 8 Oct 2017 22:23:21 +0000 (15:23 -0700)
committerEric Huss <eric@huss.org>
Sun, 15 Oct 2017 01:53:22 +0000 (18:53 -0700)
src/cargo/ops/cargo_compile.rs
tests/build.rs
tests/check.rs
tests/rustc.rs

index d6678ac6b723200b641b5c4213fcf6a2520bc64a..b646afeb026e121e669ea228685388e8ffab72d2 100644 (file)
@@ -611,6 +611,16 @@ fn generate_targets<'a>(pkg: &'a Package,
 
     let test_profile = if profile.check {
         &profiles.check_test
+    } else if mode == CompileMode::Build {
+        test
+    } else {
+        profile
+    };
+
+    let bench_profile = if profile.check {
+        &profiles.check_test
+    } else if mode == CompileMode::Build {
+        &profiles.bench
     } else {
         profile
     };
@@ -645,7 +655,7 @@ fn generate_targets<'a>(pkg: &'a Package,
             targets.append(&mut propose_indicated_targets(
                 pkg, tests, "test", Target::is_test, test_profile)?);
             targets.append(&mut propose_indicated_targets(
-                pkg, benches, "bench", Target::is_bench, test_profile)?);
+                pkg, benches, "bench", Target::is_bench, bench_profile)?);
             targets
         }
     };
index 5cb3476fee6afda7f47a3269dc21f324ea284473..09411650eba444a5709a1bce320cc42399e073bb 100644 (file)
@@ -3902,3 +3902,59 @@ fn uplift_dsym_of_bin_on_mac() {
     assert_that(&p.bin("c.dSYM"), is_not(existing_dir()));
     assert_that(&p.bin("d.dSYM"), is_not(existing_dir()));
 }
+
+// Make sure that `cargo build` chooses the correct profile for building
+// targets based on filters (assuming --profile is not specified).
+#[test]
+fn build_filter_infer_profile() {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.1.0"
+            authors = []
+        "#)
+        .file("src/lib.rs", "")
+        .file("src/main.rs", "fn main() {}")
+        .file("tests/t1.rs", "")
+        .file("benches/b1.rs", "")
+        .file("examples/ex1.rs", "fn main() {}")
+        .build();
+
+    assert_that(p.cargo("build").arg("-v"),
+        execs().with_status(0)
+        .with_stderr_contains("\
+            [RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib \
+            --emit=dep-info,link[..]")
+        .with_stderr_contains("\
+            [RUNNING] `rustc --crate-name foo src[/]main.rs --crate-type bin \
+            --emit=dep-info,link[..]")
+        );
+
+    p.root().join("target").rm_rf();
+    assert_that(p.cargo("build").arg("-v").arg("--test=t1"),
+        execs().with_status(0)
+        .with_stderr_contains("\
+            [RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib \
+            --emit=dep-info,link[..]")
+        .with_stderr_contains("\
+            [RUNNING] `rustc --crate-name t1 tests[/]t1.rs --emit=dep-info,link[..]")
+        .with_stderr_contains("\
+            [RUNNING] `rustc --crate-name foo src[/]main.rs --crate-type bin \
+            --emit=dep-info,link[..]")
+        );
+
+    p.root().join("target").rm_rf();
+    assert_that(p.cargo("build").arg("-v").arg("--bench=b1"),
+        execs().with_status(0)
+        .with_stderr_contains("\
+            [RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib \
+            --emit=dep-info,link[..]")
+        .with_stderr_contains("\
+            [RUNNING] `rustc --crate-name b1 benches[/]b1.rs --emit=dep-info,link \
+            -C opt-level=3[..]")
+        .with_stderr_contains("\
+            [RUNNING] `rustc --crate-name foo src[/]main.rs --crate-type bin \
+            --emit=dep-info,link[..]")
+        );
+}
index 2e547bd2149a51ae26afe0eff66ee5fbd5aef43e..f58348487c1f0dd02220f1329f4b65192fc81cf4 100644 (file)
@@ -478,9 +478,9 @@ fn check_unit_test_profile() {
                     badtext
                 }
             }
-        "#);
+        "#)
+        .build();
 
-    foo.build();
     assert_that(foo.cargo("check"),
                 execs().with_status(0));
     assert_that(foo.cargo("check").arg("--profile").arg("test"),
@@ -529,9 +529,9 @@ fn check_unit_test_all_tests() {
             mod tests {
                 fn unused_unit_b1() {}
             }
-        "#);
+        "#)
+        .build();
 
-    p.build();
     assert_that(p.cargo("check"),
                 execs().with_status(0)
                 .with_stderr_contains("[..]unused_normal_lib[..]")
@@ -575,8 +575,8 @@ fn check_artifacts()
         .file("src/main.rs", "fn main() {}")
         .file("tests/t1.rs", "")
         .file("examples/ex1.rs", "fn main() {}")
-        .file("benches/b1.rs", "");
-    p.build();
+        .file("benches/b1.rs", "")
+        .build();
     assert_that(p.cargo("check"), execs().with_status(0));
     assert_that(&p.root().join("target/debug/libfoo.rmeta"),
         existing_file());
index 36bd14e601acacc9138e77f9844fdd5af147d5a3..da6aced75983e851bc69de9650228b3140bf1864 100644 (file)
@@ -217,7 +217,7 @@ fn build_with_args_to_one_of_multiple_tests() {
 [RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib --emit=dep-info,link \
         -C debuginfo=2 -C metadata=[..] \
         --out-dir [..]`
-[RUNNING] `rustc --crate-name bar tests[/]bar.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 \
+[RUNNING] `rustc --crate-name bar tests[/]bar.rs --emit=dep-info,link -C debuginfo=2 \
         -C debug-assertions [..]--test[..]`
 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
 ", url = p.url())));