]> git.lizzy.rs Git - rust.git/commitdiff
rustbuild: Add support for crate tests + doctests
authorAlex Crichton <alex@alexcrichton.com>
Fri, 29 Apr 2016 21:23:15 +0000 (14:23 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 12 May 2016 15:52:20 +0000 (08:52 -0700)
This commit adds support to rustbuild to run crate unit tests (those defined by
`#[test]`) as well as documentation tests. All tests are powered by `cargo test`
under the hood.

Each step requires the `libtest` library is built for that corresponding stage.
Ideally the `test` crate would be a dev-dependency, but for now it's just easier
to ensure that we sequence everything in the right order.

Currently no filtering is implemented, so there's not actually a method of
testing *only* libstd or *only* libcore, but rather entire swaths of crates are
tested all at once.

A few points of note here are:

* The `coretest` and `collectionstest` crates are just listed as `[[test]]`
  entires for `cargo test` to naturally pick up. This mean that `cargo test -p
  core` actually runs all the tests for libcore.
* Libraries that aren't tested all mention `test = false` in their `Cargo.toml`
* Crates aren't currently allowed to have dev-dependencies due to
  rust-lang/cargo#860, but we can likely alleviate this restriction once
  workspaces are implemented.

cc #31590

24 files changed:
src/bootstrap/build/check.rs
src/bootstrap/build/mod.rs
src/bootstrap/build/step.rs
src/bootstrap/rustc.rs
src/bootstrap/rustdoc.rs
src/liballoc/Cargo.toml
src/libcollections/Cargo.toml
src/libcore/Cargo.toml
src/libflate/lib.rs
src/libpanic_abort/Cargo.toml
src/libpanic_unwind/Cargo.toml
src/librand/Cargo.toml
src/librustc_bitflags/Cargo.toml
src/librustc_borrowck/Cargo.toml
src/librustc_lint/Cargo.toml
src/librustc_resolve/Cargo.toml
src/librustc_trans/Cargo.toml
src/librustc_typeck/Cargo.toml
src/libstd/Cargo.toml
src/libunwind/Cargo.toml
src/rustc/Cargo.toml
src/rustc/libc_shim/Cargo.toml
src/rustc/std_shim/Cargo.toml
src/rustc/test_shim/Cargo.toml

index 0cd96881b58743ad7bbaee5fa8c6408633f4b595..f12b0dadeacc1ce9389a0de629436809211ef57d 100644 (file)
 //! This file implements the various regression test suites that we execute on
 //! our CI.
 
-use std::fs;
+use std::env;
+use std::fs::{self, File};
+use std::io::prelude::*;
 use std::path::{PathBuf, Path};
 use std::process::Command;
 
 use build_helper::output;
+use bootstrap::{dylib_path, dylib_path_var};
 
-use build::{Build, Compiler};
+use build::{Build, Compiler, Mode};
 
 /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler.
 ///
@@ -222,3 +225,75 @@ fn markdown_test(build: &Build, compiler: &Compiler, markdown: &Path) {
     cmd.arg("--test-args").arg(build.flags.args.join(" "));
     build.run(&mut cmd);
 }
+
+/// Run all unit tests plus documentation tests for an entire crate DAG defined
+/// by a `Cargo.toml`
+///
+/// This is what runs tests for crates like the standard library, compiler, etc.
+/// It essentially is the driver for running `cargo test`.
+///
+/// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
+/// arguments, and those arguments are discovered from `Cargo.lock`.
+pub fn krate(build: &Build,
+             compiler: &Compiler,
+             target: &str,
+             mode: Mode) {
+    let (name, path, features) = match mode {
+        Mode::Libstd => ("libstd", "src/rustc/std_shim", build.std_features()),
+        Mode::Libtest => ("libtest", "src/rustc/test_shim", String::new()),
+        Mode::Librustc => ("librustc", "src/rustc", build.rustc_features()),
+        _ => panic!("can only test libraries"),
+    };
+    println!("Testing {} stage{} ({} -> {})", name, compiler.stage,
+             compiler.host, target);
+
+    // Build up the base `cargo test` command.
+    let mut cargo = build.cargo(compiler, mode, target, "test");
+    cargo.arg("--manifest-path")
+         .arg(build.src.join(path).join("Cargo.toml"))
+         .arg("--features").arg(features);
+
+    // Generate a list of `-p` arguments to pass to the `cargo test` invocation
+    // by crawling the corresponding Cargo.lock file.
+    let lockfile = build.src.join(path).join("Cargo.lock");
+    let mut contents = String::new();
+    t!(t!(File::open(&lockfile)).read_to_string(&mut contents));
+    let mut lines = contents.lines();
+    while let Some(line) = lines.next() {
+        let prefix = "name = \"";
+        if !line.starts_with(prefix) {
+            continue
+        }
+        lines.next(); // skip `version = ...`
+
+        // skip crates.io or otherwise non-path crates
+        if let Some(line) = lines.next() {
+            if line.starts_with("source") {
+                continue
+            }
+        }
+
+        let crate_name = &line[prefix.len()..line.len() - 1];
+
+        // Right now jemalloc is our only target-specific crate in the sense
+        // that it's not present on all platforms. Custom skip it here for now,
+        // but if we add more this probably wants to get more generalized.
+        if crate_name.contains("jemalloc") {
+            continue
+        }
+
+        cargo.arg("-p").arg(crate_name);
+    }
+
+    // The tests are going to run with the *target* libraries, so we need to
+    // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
+    //
+    // Note that to run the compiler we need to run with the *host* libraries,
+    // but our wrapper scripts arrange for that to be the case anyway.
+    let mut dylib_path = dylib_path();
+    dylib_path.insert(0, build.sysroot_libdir(compiler, target));
+    cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
+    cargo.args(&build.flags.args);
+
+    build.run(&mut cargo);
+}
index b38532fb3dfa7192ad328159acaa115a76341c2e..44f161fb487f43a5ea8fecac0f5e610f6cc370da 100644 (file)
@@ -380,6 +380,15 @@ pub fn build(&mut self) {
                     check::compiletest(self, &compiler, target.target,
                                        "run-make", "run-make")
                 }
+                CheckCrateStd { compiler } => {
+                    check::krate(self, &compiler, target.target, Mode::Libstd)
+                }
+                CheckCrateTest { compiler } => {
+                    check::krate(self, &compiler, target.target, Mode::Libtest)
+                }
+                CheckCrateRustc { compiler } => {
+                    check::krate(self, &compiler, target.target, Mode::Librustc)
+                }
 
                 DistDocs { stage } => dist::docs(self, stage, target.target),
                 DistMingw { _dummy } => dist::mingw(self, target.target),
@@ -485,6 +494,7 @@ fn cargo(&self,
                   self.config.rust_debug_assertions.to_string())
              .env("RUSTC_SNAPSHOT", &self.rustc)
              .env("RUSTC_SYSROOT", self.sysroot(compiler))
+             .env("RUSTC_LIBDIR", self.rustc_libdir(compiler))
              .env("RUSTC_SNAPSHOT_LIBDIR", self.rustc_snapshot_libdir())
              .env("RUSTC_RPATH", self.config.rust_rpath.to_string())
              .env("RUSTDOC", self.out.join("bootstrap/debug/rustdoc"))
@@ -520,7 +530,6 @@ fn cargo(&self,
         if self.config.rust_optimize {
             cargo.arg("--release");
         }
-        self.add_rustc_lib_path(compiler, &mut cargo);
         return cargo
     }
 
index 4a336589b44c53e0ebec066d79877f6e80909efa..c494d965a19c3f0d5aad082cba4adc3dbdb1c6b6 100644 (file)
@@ -120,6 +120,9 @@ macro_rules! targets {
             (check_docs, CheckDocs { compiler: Compiler<'a> }),
             (check_error_index, CheckErrorIndex { compiler: Compiler<'a> }),
             (check_rmake, CheckRMake { compiler: Compiler<'a> }),
+            (check_crate_std, CheckCrateStd { compiler: Compiler<'a> }),
+            (check_crate_test, CheckCrateTest { compiler: Compiler<'a> }),
+            (check_crate_rustc, CheckCrateRustc { compiler: Compiler<'a> }),
 
             // Distribution targets, creating tarballs
             (dist, Dist { stage: u32 }),
@@ -376,6 +379,9 @@ pub fn deps(&self, build: &'a Build) -> Vec<Step<'a>> {
                     self.check_cfail(compiler),
                     self.check_rfail(compiler),
                     self.check_pfail(compiler),
+                    self.check_crate_std(compiler),
+                    self.check_crate_test(compiler),
+                    self.check_crate_rustc(compiler),
                     self.check_codegen(compiler),
                     self.check_codegen_units(compiler),
                     self.check_debuginfo(compiler),
@@ -437,6 +443,15 @@ pub fn deps(&self, build: &'a Build) -> Vec<Step<'a>> {
             Source::CheckErrorIndex { compiler } => {
                 vec![self.libstd(compiler), self.tool_error_index(compiler.stage)]
             }
+            Source::CheckCrateStd { compiler } => {
+                vec![self.libtest(compiler)]
+            }
+            Source::CheckCrateTest { compiler } => {
+                vec![self.libtest(compiler)]
+            }
+            Source::CheckCrateRustc { compiler } => {
+                vec![self.libtest(compiler)]
+            }
 
             Source::ToolLinkchecker { stage } |
             Source::ToolTidy { stage } => {
index 046bc34438c4293e7345c9fc59996c39d9d488b4..97decedf91dce6c20f560a7b94d40a95954abbb5 100644 (file)
@@ -29,6 +29,7 @@
 
 use std::env;
 use std::ffi::OsString;
+use std::path::PathBuf;
 use std::process::Command;
 
 fn main() {
@@ -43,16 +44,22 @@ fn main() {
     // have the standard library built yet and may not be able to produce an
     // executable. Otherwise we just use the standard compiler we're
     // bootstrapping with.
-    let rustc = if target.is_none() {
-        env::var_os("RUSTC_SNAPSHOT").unwrap()
+    let (rustc, libdir) = if target.is_none() {
+        ("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
     } else {
-        env::var_os("RUSTC_REAL").unwrap()
+        ("RUSTC_REAL", "RUSTC_LIBDIR")
     };
     let stage = env::var("RUSTC_STAGE").unwrap();
 
+    let rustc = env::var_os(rustc).unwrap();
+    let libdir = env::var_os(libdir).unwrap();
+    let mut dylib_path = bootstrap::dylib_path();
+    dylib_path.insert(0, PathBuf::from(libdir));
+
     let mut cmd = Command::new(rustc);
     cmd.args(&args)
-       .arg("--cfg").arg(format!("stage{}", stage));
+       .arg("--cfg").arg(format!("stage{}", stage))
+       .env(bootstrap::dylib_path_var(), env::join_paths(&dylib_path).unwrap());
 
     if let Some(target) = target {
         // The stage0 compiler has a special sysroot distinct from what we
index 8c618196113baf7cc255e098f4c257ff191aaf77..88ac26d32f6c3d1f1e55acfe98b6490168cbfb1b 100644 (file)
 //!
 //! See comments in `src/bootstrap/rustc.rs` for more information.
 
+extern crate bootstrap;
+
 use std::env;
 use std::process::Command;
+use std::path::PathBuf;
 
 fn main() {
     let args = env::args_os().skip(1).collect::<Vec<_>>();
     let rustdoc = env::var_os("RUSTDOC_REAL").unwrap();
+    let libdir = env::var_os("RUSTC_LIBDIR").unwrap();
+
+    let mut dylib_path = bootstrap::dylib_path();
+    dylib_path.insert(0, PathBuf::from(libdir));
 
     let mut cmd = Command::new(rustdoc);
     cmd.args(&args)
        .arg("--cfg").arg(format!("stage{}", env::var("RUSTC_STAGE").unwrap()))
-       .arg("--cfg").arg("dox");
+       .arg("--cfg").arg("dox")
+       .env(bootstrap::dylib_path_var(), env::join_paths(&dylib_path).unwrap());
     std::process::exit(match cmd.status() {
         Ok(s) => s.code().unwrap_or(1),
         Err(e) => panic!("\n\nfailed to run {:?}: {}\n\n", cmd, e),
index 5da0f1a10b9775836c6d4aaea2ccacdbcf912f2d..0889ca9fc84d4e72361d24933ce2de1081ec7c3d 100644 (file)
@@ -6,7 +6,6 @@ version = "0.0.0"
 [lib]
 name = "alloc"
 path = "lib.rs"
-test = false
 
 [dependencies]
 core = { path = "../libcore" }
index 18e322ff74f6cb9405e4f5be1d16173b704a8cb5..65d456e750f6f6c5a3b5bed06787b30b373403d2 100644 (file)
@@ -6,9 +6,12 @@ version = "0.0.0"
 [lib]
 name = "collections"
 path = "lib.rs"
-test = false
 
 [dependencies]
 alloc = { path = "../liballoc" }
 core = { path = "../libcore" }
 rustc_unicode = { path = "../librustc_unicode" }
+
+[[test]]
+name = "collectionstest"
+path = "../libcollectionstest/lib.rs"
index 98f941f0057a36ce0e67eacf3361b3530b1c2fad..02fe574b81edd07ddfa66ed5c4474c02570e920b 100644 (file)
@@ -8,3 +8,7 @@ build = "build.rs"
 name = "core"
 path = "lib.rs"
 test = false
+
+[[test]]
+name = "coretest"
+path = "../libcoretest/lib.rs"
index 1cc008c5ee9cebd69c5120d7f2bd6bab5e8b13dd..b578b064d67caece89816bdecee93c709041b2a7 100644 (file)
 #![feature(libc)]
 #![feature(staged_api)]
 #![feature(unique)]
-#![cfg_attr(test, feature(rustc_private, rand))]
-
-#[cfg(test)]
-#[macro_use]
-extern crate log;
+#![cfg_attr(test, feature(rand))]
 
 extern crate libc;
 
@@ -175,14 +171,8 @@ fn test_flate_round_trip() {
             for _ in 0..2000 {
                 input.extend_from_slice(r.choose(&words).unwrap());
             }
-            debug!("de/inflate of {} bytes of random word-sequences",
-                   input.len());
             let cmp = deflate_bytes(&input);
             let out = inflate_bytes(&cmp).unwrap();
-            debug!("{} bytes deflated to {} ({:.1}% size)",
-                   input.len(),
-                   cmp.len(),
-                   100.0 * ((cmp.len() as f64) / (input.len() as f64)));
             assert_eq!(&*input, &*out);
         }
     }
index a7905703f596f3f6e2e332c2d4bafb0ec15f73b4..9d62be64fc4ecf836c364046d582fede41c8edff 100644 (file)
@@ -5,6 +5,7 @@ version = "0.0.0"
 
 [lib]
 path = "lib.rs"
+test = false
 
 [dependencies]
 core = { path = "../libcore" }
index 27edecd6f9668e83ded991ad2be037a63a0bbff6..18f37a8bb174ee8a2982439e8b88673a1eabe652 100644 (file)
@@ -5,6 +5,7 @@ version = "0.0.0"
 
 [lib]
 path = "lib.rs"
+test = false
 
 [dependencies]
 alloc = { path = "../liballoc" }
index 784654c0859900dd7c3e9edab786d8707bcfbd6b..86b061db05413f3704975c8e15d92753fae4ea8b 100644 (file)
@@ -6,7 +6,6 @@ version = "0.0.0"
 [lib]
 name = "rand"
 path = "lib.rs"
-test = false
 
 [dependencies]
 core = { path = "../libcore" }
index 926ed5960d68a0ee885914af9149e90a97d82935..d1e66dcf9351b282356f72f430aab33395eb3b8b 100644 (file)
@@ -7,3 +7,4 @@ version = "0.0.0"
 name = "rustc_bitflags"
 path = "lib.rs"
 test = false
+doctest = false
index 6da87f97fb79fdcded43f2cd4c79dfd35450ecf3..fbc267aaa6a06c54f2e5dd9469eac49996bc4215 100644 (file)
@@ -7,6 +7,7 @@ version = "0.0.0"
 name = "rustc_borrowck"
 path = "lib.rs"
 crate-type = ["dylib"]
+test = false
 
 [dependencies]
 log = { path = "../liblog" }
index 3f0cd397e769c5858e91c257949e85edcbcb501c..7674cc529bef81e85b1348b47634fdb6a9ccc6d8 100644 (file)
@@ -7,6 +7,7 @@ version = "0.0.0"
 name = "rustc_lint"
 path = "lib.rs"
 crate-type = ["dylib"]
+test = false
 
 [dependencies]
 log = { path = "../liblog" }
index 217445715a81b8d31e1d16256b3586a05a0ee059..a63460d912d7d3033821ff16363b6e69b1631a75 100644 (file)
@@ -7,6 +7,7 @@ version = "0.0.0"
 name = "rustc_resolve"
 path = "lib.rs"
 crate-type = ["dylib"]
+test = false
 
 [dependencies]
 log = { path = "../liblog" }
index ccb430fbb782fe3d8e777319e6645177ba610f8b..9a0580472b4017f779a097efe1a1f1a08e821525 100644 (file)
@@ -7,6 +7,7 @@ version = "0.0.0"
 name = "rustc_trans"
 path = "lib.rs"
 crate-type = ["dylib"]
+test = false
 
 [dependencies]
 arena = { path = "../libarena" }
index e9dabf16eaece7455128e1efbe6d60627ab38ac5..a0c4c7534fab27b78c7c69ac3c67c2fa9b1a487e 100644 (file)
@@ -7,6 +7,7 @@ version = "0.0.0"
 name = "rustc_typeck"
 path = "lib.rs"
 crate-type = ["dylib"]
+test = false
 
 [dependencies]
 log = { path = "../liblog" }
index 6d33fb311cff05d16cd87abb30b3630725cfafbb..eded6e24f3ef5db2f6f0d2f06179c43b73f9ea8c 100644 (file)
@@ -8,7 +8,6 @@ build = "build.rs"
 name = "std"
 path = "lib.rs"
 crate-type = ["dylib", "rlib"]
-test = false
 
 [dependencies]
 alloc = { path = "../liballoc" }
index dca222c49d0a11107c43e4779bd850d100154e73..b537c6b1b71c1a242de8a49dcf79fd6b6188fee4 100644 (file)
@@ -7,6 +7,7 @@ build = "build.rs"
 [lib]
 name = "unwind"
 path = "lib.rs"
+test = false
 
 [dependencies]
 core = { path = "../libcore" }
index 7431c35efba01752e01dc51b9e9f6d22f9caf62c..24499cb8f08c2efdc02607bc1b785f0cb41c113d 100644 (file)
@@ -13,6 +13,8 @@ path = "rustdoc.rs"
 
 [profile.release]
 opt-level = 2
+[profile.bench]
+opt-level = 2
 
 # These options are controlled from our rustc wrapper script, so turn them off
 # here and have them controlled elsewhere.
index a7860b50e08ff50aa19ebf1f256c5f4daba474d9..8fc713e0f1bb3a6467d1d13b832e49629af543e6 100644 (file)
@@ -15,6 +15,7 @@ build = "build.rs"
 [lib]
 name = "libc"
 path = "../../liblibc/src/lib.rs"
+test = false
 
 [dependencies]
 core = { path = "../../libcore" }
index 1ce3937157da04de1a804f56441b5cc9e9ee8dfa..5602ef866b83ae800e44f877f026f16ef054d566 100644 (file)
@@ -30,6 +30,8 @@ path = "lib.rs"
 
 [profile.release]
 opt-level = 2
+[profile.bench]
+opt-level = 2
 
 # These options are controlled from our rustc wrapper script, so turn them off
 # here and have them controlled elsewhere.
index bf5766504867cf19f7d18d213aacd90519ac080b..87f2ccd51e8850ee933034d1b898107b5fde327a 100644 (file)
@@ -14,6 +14,8 @@ path = "lib.rs"
 
 [profile.release]
 opt-level = 2
+[profile.bench]
+opt-level = 2
 
 # These options are controlled from our rustc wrapper script, so turn them off
 # here and have them controlled elsewhere.