Move test to clap
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 8 Mar 2018 13:56:36 +0000 (16:56 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 8 Mar 2018 20:30:47 +0000 (23:30 +0300)
src/bin/cargo.rs
src/bin/cli/mod.rs
src/bin/cli/test.rs [new file with mode: 0644]

index 0e2b54da67ebe6caceab4665e40d6020334d9c3b..46e77ac6c3e9ac8cf552d6779b467dc5fa637132 100644 (file)
@@ -92,7 +92,7 @@ fn main() {
         "build" | "bench" | "check" | "clean" | "doc" | "fetch" | "generate-lockfile" |
         "git-checkout" | "init" | "install" | "locate-project" | "login" | "metadata" |
         "new" | "owner" | "package" | "pkgid" | "publish" | "read-manifest" | "run" |
-        "rustc" | "rustdoc" | "search" => true,
+        "rustc" | "rustdoc" | "search" | "test" => true,
         _ => false
     });
 
@@ -144,7 +144,7 @@ macro_rules! each_subcommand{
 //        $mac!(rustc);
 //        $mac!(rustdoc);
 //        $mac!(search);
-        $mac!(test);
+//        $mac!(test);
         $mac!(uninstall);
         $mac!(update);
         $mac!(verify_project);
index bd2c0f4242e7b4febe7f901596a9a9c31ba4145d..0a8a4bb0cd67cb4ad2e8960120b83a962d340e2d 100644 (file)
@@ -554,6 +554,45 @@ about this warning.";
             ops::search(&query, config, index, limit, registry)?;
             return Ok(());
         }
+        ("test", Some(args)) => {
+            let ws = workspace_from_args(config, args)?;
+
+            let mut compile_opts = compile_options_from_args(config, args, CompileMode::Test)?;
+            let doc = args.is_present("doc");
+            if doc {
+                compile_opts.mode = ops::CompileMode::Doctest;
+                compile_opts.filter = ops::CompileFilter::new(true,
+                                                              &[], false,
+                                                              &[], false,
+                                                              &[], false,
+                                                              &[], false,
+                                                              false);
+            }
+
+            let ops = ops::TestOptions {
+                no_run: args.is_present("no-run"),
+                no_fail_fast: args.is_present("no-fail-fast"),
+                only_doc: doc,
+                compile_opts,
+            };
+
+            // TESTNAME is actually an argument of the test binary, but it's
+            // important so we explicitly mention it and reconfigure
+            let mut test_args = vec![];
+            test_args.extend(args.value_of("TESTNAME").into_iter().map(|s| s.to_string()));
+            test_args.extend(args.values_of("args").unwrap_or_default().map(|s| s.to_string()));
+
+            let err = ops::run_tests(&ws, &ops, &test_args)?;
+            return match err {
+                None => Ok(()),
+                Some(err) => {
+                    Err(match err.exit.as_ref().and_then(|e| e.code()) {
+                        Some(i) => CliError::new(format_err!("{}", err.hint(&ws)), i),
+                        None => CliError::new(err.into(), 101),
+                    })
+                }
+            };
+        }
         _ => return Ok(())
     }
 }
@@ -646,6 +685,7 @@ See 'cargo help <command>' for more information on a specific command.
             rustc::cli(),
             rustdoc::cli(),
             search::cli(),
+            test::cli(),
         ])
     ;
     app
@@ -677,6 +717,7 @@ mod run;
 mod rustc;
 mod rustdoc;
 mod search;
+mod test;
 
 mod utils {
     use clap::{self, SubCommand, AppSettings};
diff --git a/src/bin/cli/test.rs b/src/bin/cli/test.rs
new file mode 100644 (file)
index 0000000..89cc7c9
--- /dev/null
@@ -0,0 +1,84 @@
+use super::utils::*;
+use clap::AppSettings;
+
+pub fn cli() -> App {
+    subcommand("test")
+        .setting(AppSettings::TrailingVarArg)
+        .about("Execute all unit and integration tests of a local package")
+        .arg(
+            Arg::with_name("TESTNAME").help(
+                "If specified, only run tests containing this string in their names"
+            )
+        )
+        .arg(
+            Arg::with_name("args").help(
+                "Arguments for the test binary"
+            ).multiple(true).last(true)
+        )
+        .arg_targets_all(
+            "Test only this package's library",
+            "Test only the specified binary",
+            "Test all binaries",
+            "Check that the specified examples compile",
+            "Check that all examples compile",
+            "Test only the specified test target",
+            "Test all tests",
+            "Test only the specified bench target",
+            "Test all benches",
+            "Test all targets (default)",
+        )
+        .arg(opt("doc", "Test only this library's documentation"))
+        .arg(
+            opt("no-run", "Compile, but don't run tests")
+        )
+        .arg(
+            opt("no-fail-fast", "Run all tests regardless of failure")
+        )
+        .arg_package(
+            "Package to run tests for",
+            "Test all packages in the workspace",
+            "Exclude packages from the test",
+        )
+        .arg_jobs()
+        .arg_release("Build artifacts in release mode, with optimizations")
+        .arg_features()
+        .arg_target_triple("Build for the target triple")
+        .arg_manifest_path()
+        .arg_message_format()
+        .after_help("\
+All of the trailing arguments are passed to the test binaries generated for
+filtering tests and generally providing options configuring how they run. For
+example, this will run all tests with the name `foo` in their name:
+
+    cargo test foo
+
+If the --package argument is given, then SPEC is a package id specification
+which indicates which package should be tested. If it is not given, then the
+current package is tested. For more information on SPEC and its format, see the
+`cargo help pkgid` command.
+
+All packages in the workspace are tested if the `--all` flag is supplied. The
+`--all` flag is automatically assumed for a virtual manifest.
+Note that `--exclude` has to be specified in conjunction with the `--all` flag.
+
+The --jobs argument affects the building of the test executable but does
+not affect how many jobs are used when running the tests. The default value
+for the --jobs argument is the number of CPUs. If you want to control the
+number of simultaneous running test cases, pass the `--test-threads` option
+to the test binaries:
+
+    cargo test -- --test-threads=1
+
+Compilation can be configured via the `test` profile in the manifest.
+
+By default the rust test harness hides output from test execution to
+keep results readable. Test output can be recovered (e.g. for debugging)
+by passing `--nocapture` to the test binaries:
+
+    cargo test -- --nocapture
+
+To get the list of all options available for the test binaries use this:
+
+    cargo test -- --help
+")
+}