]> git.proxmox.com Git - cargo.git/commitdiff
Allow more advanced filtering of what to build
authorAlex Crichton <alex@alexcrichton.com>
Wed, 29 Apr 2015 16:42:29 +0000 (09:42 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 29 Apr 2015 17:59:02 +0000 (10:59 -0700)
This commit fills out the functionality of `--lib`, `--test`, `--bin`,
`--bench`, and `--example` for the `cargo {test,build,bench}` commands all at
once. The support for all of this was introduced long ago, and the flags just
weren't exposed at the time.

.travis.yml
src/bin/bench.rs
src/bin/build.rs
src/bin/test.rs
src/cargo/ops/cargo_compile.rs
tests/test_cargo_compile.rs

index 3aea8943f39a6b17d8f06b8560f4d1ba6dc1c8e9..7e4cf095fac0f91a6cd09293279ab01006d8be1c 100644 (file)
@@ -16,7 +16,8 @@ after_success: |
   git push -f https://${TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages
 env:
   global:
-    secure: scGpeetUfba5RWyuS4yt10bPoFAI9wpHEReIFqEx7eH5vr2Anajk6+70jW6GdrWVdUvdINiArlQ3An2DeB9vEUWcBjw8WvuPtOH0tDMoSsuVloPlFD8yn1Ac0Bx9getAO5ofxqtoNg+OV4MDVuGabEesqAOWqURNrBC7XK+ntC8=
+    - secure: scGpeetUfba5RWyuS4yt10bPoFAI9wpHEReIFqEx7eH5vr2Anajk6+70jW6GdrWVdUvdINiArlQ3An2DeB9vEUWcBjw8WvuPtOH0tDMoSsuVloPlFD8yn1Ac0Bx9getAO5ofxqtoNg+OV4MDVuGabEesqAOWqURNrBC7XK+ntC8=
+    - RUST_TEST_THREADS=1
 os:
   - linux
   - osx
index 735b2e0d4e6a826924ed15dc62371ed7b38259a5..ec5e92722998233ad8145c000db6c070de7ed10c 100644 (file)
@@ -8,11 +8,15 @@ struct Options {
     flag_package: Option<String>,
     flag_jobs: Option<u32>,
     flag_features: Vec<String>,
-    flag_bench: Option<String>,
     flag_no_default_features: bool,
     flag_target: Option<String>,
     flag_manifest_path: Option<String>,
     flag_verbose: bool,
+    flag_lib: bool,
+    flag_bin: Vec<String>,
+    flag_example: Vec<String>,
+    flag_test: Vec<String>,
+    flag_bench: Vec<String>,
     arg_args: Vec<String>,
 }
 
@@ -24,7 +28,11 @@ Usage:
 
 Options:
     -h, --help               Print this message
-    --bench NAME             Name of the bench to run
+    --lib                    Benchmark only this package's library
+    --bin NAME               Benchmark only the specified binary
+    --example NAME           Benchmark only the specified example
+    --test NAME              Benchmark only the specified test
+    --bench NAME             Benchmark only the specified bench
     --no-run                 Compile, but don't run benchmarks
     -p SPEC, --package SPEC  Package to run benchmarks for
     -j N, --jobs N           The number of jobs to run in parallel
@@ -50,11 +58,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
     let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path));
     config.shell().set_verbose(options.flag_verbose);
 
-    let mut benches = Vec::new();
-    if let Some(s) = options.flag_bench {
-        benches.push(s);
-    }
-
     let ops = ops::TestOptions {
         no_run: options.flag_no_run,
         compile_opts: ops::CompileOptions {
@@ -67,14 +70,11 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
             exec_engine: None,
             release: true,
             mode: ops::CompileMode::Bench,
-            filter: if benches.is_empty() {
-                ops::CompileFilter::Everything
-            } else {
-                ops::CompileFilter::Only {
-                    lib: false, bins: &[], examples: &[], tests: &[],
-                    benches: &benches,
-                }
-            },
+            filter: ops::CompileFilter::new(options.flag_lib,
+                                            &options.flag_bin,
+                                            &options.flag_test,
+                                            &options.flag_example,
+                                            &options.flag_bench),
         },
     };
 
index 4ba7de2921a22d6b709a00bda78f437ea28af5aa..cb925510abb34472b767130df0a652658fb08463 100644 (file)
@@ -15,7 +15,11 @@ struct Options {
     flag_manifest_path: Option<String>,
     flag_verbose: bool,
     flag_release: bool,
-    flag_lib: bool
+    flag_lib: bool,
+    flag_bin: Vec<String>,
+    flag_example: Vec<String>,
+    flag_test: Vec<String>,
+    flag_bench: Vec<String>,
 }
 
 pub const USAGE: &'static str = "
@@ -28,7 +32,11 @@ Options:
     -h, --help               Print this message
     -p SPEC, --package SPEC  Package to build
     -j N, --jobs N           The number of jobs to run in parallel
-    --lib                    Build only lib (if present in package)
+    --lib                    Build only this package's library
+    --bin NAME               Build only the specified binary
+    --example NAME           Build only the specified example
+    --test NAME              Build only the specified test
+    --bench NAME             Build only the specified benchmark
     --release                Build artifacts in release mode, with optimizations
     --features FEATURES      Space-separated list of features to also build
     --no-default-features    Do not build the `default` feature
@@ -63,13 +71,11 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
         exec_engine: None,
         mode: ops::CompileMode::Build,
         release: options.flag_release,
-        filter: if options.flag_lib {
-            ops::CompileFilter::Only {
-                lib: true, bins: &[], examples: &[], benches: &[], tests: &[]
-            }
-        } else {
-            ops::CompileFilter::Everything
-        },
+        filter: ops::CompileFilter::new(options.flag_lib,
+                                        &options.flag_bin,
+                                        &options.flag_test,
+                                        &options.flag_example,
+                                        &options.flag_bench),
     };
 
     ops::compile(&root, &opts).map(|_| None).map_err(|err| {
index 1b09e6d2ce0d644845b9957d9882493dbe06d265..d46b67d44aa926c56ec39c585eb0556e20695d6c 100644 (file)
@@ -8,12 +8,15 @@ struct Options {
     flag_features: Vec<String>,
     flag_jobs: Option<u32>,
     flag_manifest_path: Option<String>,
-    flag_test: Option<String>,
-    flag_bin: Option<String>,
     flag_no_default_features: bool,
     flag_no_run: bool,
     flag_package: Option<String>,
     flag_target: Option<String>,
+    flag_lib: bool,
+    flag_bin: Vec<String>,
+    flag_example: Vec<String>,
+    flag_test: Vec<String>,
+    flag_bench: Vec<String>,
     flag_verbose: bool,
 }
 
@@ -25,8 +28,11 @@ Usage:
 
 Options:
     -h, --help               Print this message
-    --test NAME              Name of the integration test to run
-    --bin NAME               Name of the binary to run tests for
+    --lib                    Test only this package's library
+    --bin NAME               Test only the specified binary
+    --example NAME           Test only the specified example
+    --test NAME              Test only the specified integration test
+    --bench NAME             Test only the specified benchmark
     --no-run                 Compile, but don't run tests
     -p SPEC, --package SPEC  Package to run tests for
     -j N, --jobs N           The number of jobs to run in parallel
@@ -54,14 +60,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
     let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path));
     config.shell().set_verbose(options.flag_verbose);
 
-    let (mut tests, mut bins) = (Vec::new(), Vec::new());
-    if let Some(s) = options.flag_test {
-        tests.push(s);
-    }
-    if let Some(s) = options.flag_bin {
-        bins.push(s);
-    }
-
     let ops = ops::TestOptions {
         no_run: options.flag_no_run,
         compile_opts: ops::CompileOptions {
@@ -74,14 +72,11 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
             exec_engine: None,
             release: false,
             mode: ops::CompileMode::Test,
-            filter: if tests.is_empty() && bins.is_empty() {
-                ops::CompileFilter::Everything
-            } else {
-                ops::CompileFilter::Only {
-                    lib: false, examples: &[], benches: &[],
-                    tests: &tests, bins: &bins,
-                }
-            }
+            filter: ops::CompileFilter::new(options.flag_lib,
+                                            &options.flag_bin,
+                                            &options.flag_test,
+                                            &options.flag_example,
+                                            &options.flag_bench),
         },
     };
 
index 0ef49cdde258cecd64fac0282ba93ffe74019060..9c6ba139831e4dc7b95b8f4aec09bf814362443b 100644 (file)
@@ -184,6 +184,22 @@ pub fn compile_pkg(package: &Package, options: &CompileOptions)
 }
 
 impl<'a> CompileFilter<'a> {
+    pub fn new(lib_only: bool,
+               bins: &'a [String],
+               tests: &'a [String],
+               examples: &'a [String],
+               benches: &'a [String]) -> CompileFilter<'a> {
+        if lib_only || !bins.is_empty() || !tests.is_empty() ||
+           !examples.is_empty() || !benches.is_empty() {
+            CompileFilter::Only {
+                lib: lib_only, bins: bins, examples: examples, benches: benches,
+                tests: tests,
+            }
+        } else {
+            CompileFilter::Everything
+        }
+    }
+
     pub fn matches(&self, target: &Target) -> bool {
         match *self {
             CompileFilter::Everything => true,
index 3faea78b519c896e99ae43b1c7d7e4dada8b4f69..0bf34c841b7be6a9432cba0e0b68ccad71c6d28e 100644 (file)
@@ -1654,3 +1654,30 @@ test!(dashes_in_crate_name_bad {
     assert_that(p.cargo_process("build").arg("-v"),
                 execs().with_status(101));
 });
+
+test!(filtering {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/lib.rs", "")
+        .file("src/bin/a.rs", "fn main() {}")
+        .file("src/bin/b.rs", "fn main() {}")
+        .file("examples/a.rs", "fn main() {}")
+        .file("examples/b.rs", "fn main() {}");
+    p.build();
+
+    assert_that(p.cargo("build").arg("--lib"),
+                execs().with_status(0));
+    assert_that(&p.bin("a"), is_not(existing_file()));
+
+    assert_that(p.cargo("build").arg("--bin=a").arg("--example=a"),
+                execs().with_status(0));
+    assert_that(&p.bin("a"), existing_file());
+    assert_that(&p.bin("b"), is_not(existing_file()));
+    assert_that(&p.bin("examples/a"), existing_file());
+    assert_that(&p.bin("examples/b"), is_not(existing_file()));
+});