]> git.proxmox.com Git - cargo.git/commitdiff
Add tests for feature freshness
authorNipunn Koorapati <nipunn@dropbox.com>
Tue, 4 Oct 2016 22:21:16 +0000 (15:21 -0700)
committerNipunn Koorapati <nipunn1313@gmail.com>
Mon, 7 Nov 2016 06:56:12 +0000 (17:56 +1100)
tests/freshness.rs

index 03baf550ee543c045926ac6247ecf2a43bdc4300..49ab26a8c7cf410627dd0eaece6418ab33267747 100644 (file)
@@ -145,7 +145,7 @@ fn rebuild_sub_package_then_while_package() {
 }
 
 #[test]
-fn changing_features_is_ok() {
+fn changing_lib_features_caches_targets() {
     let p = project("foo")
         .file("Cargo.toml", r#"
             [package]
@@ -170,18 +170,203 @@ fn changing_features_is_ok() {
                        .with_stderr("\
 [..]Compiling foo v0.0.1 ([..])
 [FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+"));
+
+    /* Targets should be cached from the first build */
+
+    assert_that(p.cargo("build"),
+                execs().with_status(0)
+                       .with_stderr("\
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
 "));
 
     assert_that(p.cargo("build"),
+                execs().with_status(0)
+                       .with_stdout(""));
+
+    assert_that(p.cargo("build").arg("--features").arg("foo"),
+                execs().with_status(0)
+                       .with_stderr("\
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+"));
+}
+
+#[test]
+fn changing_bin_paths_common_target_features_caches_targets() {
+    /// Make sure dep_cache crate is built once per feature
+    let p = project("foo")
+        .file(".cargo/config", r#"
+            [build]
+            target-dir = "./target"
+        "#)
+        .file("dep_crate/Cargo.toml", r#"
+            [package]
+            name    = "dep_crate"
+            version = "0.0.1"
+            authors = []
+
+            [features]
+            ftest  = []
+        "#)
+        .file("dep_crate/src/lib.rs", r#"
+            #[cfg(feature = "ftest")]
+            pub fn yo() {
+                println!("ftest on")
+            }
+            #[cfg(not(feature = "ftest"))]
+            pub fn yo() {
+                println!("ftest off")
+            }
+        "#)
+        .file("a/Cargo.toml", r#"
+            [package]
+            name    = "a"
+            version = "0.0.1"
+            authors = []
+
+            [dependencies]
+            dep_crate = {path = "../dep_crate", features = []}
+        "#)
+        .file("a/src/lib.rs", "")
+        .file("a/src/main.rs", r#"
+            extern crate dep_crate;
+            use dep_crate::yo;
+            fn main() {
+                yo();
+            }
+        "#)
+        .file("b/Cargo.toml", r#"
+            [package]
+            name    = "b"
+            version = "0.0.1"
+            authors = []
+
+            [dependencies]
+            dep_crate = {path = "../dep_crate", features = ["ftest"]}
+        "#)
+        .file("b/src/lib.rs", "")
+        .file("b/src/main.rs", r#"
+            extern crate dep_crate;
+            use dep_crate::yo;
+            fn main() {
+                yo();
+            }
+        "#);
+
+    /* Build and rebuild a/. Ensure dep_crate only builds once */
+    assert_that(p.cargo_process("run").cwd(p.root().join("a")),
+                execs().with_status(0)
+                       .with_stdout("ftest off")
+                       .with_stderr("\
+[..]Compiling dep_crate v0.0.1 ([..])
+[..]Compiling a v0.0.1 ([..])
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+[RUNNING] `[..]target/debug/a`
+"));
+    assert_that(p.cargo("clean").arg("-p").arg("a").cwd(p.root().join("a")),
+                execs().with_status(0));
+    assert_that(p.cargo("run").cwd(p.root().join("a")),
+                execs().with_status(0)
+                       .with_stdout("ftest off")
+                       .with_stderr("\
+[..]Compiling a v0.0.1 ([..])
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+[RUNNING] `[..]target/debug/a`
+"));
+
+    /* Build and rebuild b/. Ensure dep_crate only builds once */
+    assert_that(p.cargo("run").cwd(p.root().join("b")),
+                execs().with_status(0)
+                       .with_stdout("ftest on")
+                       .with_stderr("\
+[..]Compiling dep_crate v0.0.1 ([..])
+[..]Compiling b v0.0.1 ([..])
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+[RUNNING] `[..]target/debug/b`
+"));
+    assert_that(p.cargo("clean").arg("-p").arg("b").cwd(p.root().join("b")),
+                execs().with_status(0));
+    assert_that(p.cargo("run").cwd(p.root().join("b")),
+                execs().with_status(0)
+                       .with_stdout("ftest on")
+                       .with_stderr("\
+[..]Compiling b v0.0.1 ([..])
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+[RUNNING] `[..]target/debug/b`
+"));
+
+    /* Build a/ package again. If we cache different feature dep builds correctly,
+     * this should not cause a rebuild of dep_crate */
+    assert_that(p.cargo("clean").arg("-p").arg("a").cwd(p.root().join("a")),
+                execs().with_status(0));
+    assert_that(p.cargo("run").cwd(p.root().join("a")),
+                execs().with_status(0)
+                       .with_stdout("ftest off")
+                       .with_stderr("\
+[..]Compiling a v0.0.1 ([..])
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+[RUNNING] `[..]target/debug/a`
+"));
+
+    /* Build b/ package again. If we cache different feature dep builds correctly,
+     * this should not cause a rebuild */
+    assert_that(p.cargo("clean").arg("-p").arg("b").cwd(p.root().join("b")),
+                execs().with_status(0));
+    assert_that(p.cargo("run").cwd(p.root().join("b")),
+                execs().with_status(0)
+                       .with_stdout("ftest on")
+                       .with_stderr("\
+[..]Compiling b v0.0.1 ([..])
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+[RUNNING] `[..]target/debug/b`
+"));
+}
+
+#[test]
+fn changing_bin_features_caches_targets() {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            authors = []
+            version = "0.0.1"
+
+            [features]
+            foo = []
+        "#)
+        .file("src/main.rs", "fn main() {}");
+
+    assert_that(p.cargo_process("build"),
+                execs().with_status(0)
+                       .with_stderr("\
+[..]Compiling foo v0.0.1 ([..])
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+"));
+
+    assert_that(p.cargo("build").arg("--features").arg("foo"),
                 execs().with_status(0)
                        .with_stderr("\
 [..]Compiling foo v0.0.1 ([..])
 [FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+"));
+
+    /* Targets should be cached from the first build */
+
+    assert_that(p.cargo("build"),
+                execs().with_status(0)
+                       .with_stderr("\
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
 "));
 
     assert_that(p.cargo("build"),
                 execs().with_status(0)
                        .with_stdout(""));
+
+    assert_that(p.cargo("build").arg("--features").arg("foo"),
+                execs().with_status(0)
+                       .with_stderr("\
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+"));
 }
 
 #[test]