Skip to content

Commit

Permalink
Add cargo test --doc
Browse files Browse the repository at this point in the history
Supports testing only the documentation (like `--lib`, `--bin`, etc).
  • Loading branch information
alexcrichton committed Apr 15, 2016
1 parent 88e3081 commit a63c642
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/bin/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
let ops = ops::TestOptions {
no_run: options.flag_no_run,
no_fail_fast: false,
only_doc: false,
compile_opts: ops::CompileOptions {
config: config,
jobs: options.flag_jobs,
Expand Down
25 changes: 19 additions & 6 deletions src/bin/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Options {
flag_package: Vec<String>,
flag_target: Option<String>,
flag_lib: bool,
flag_doc: bool,
flag_bin: Vec<String>,
flag_example: Vec<String>,
flag_test: Vec<String>,
Expand All @@ -33,6 +34,7 @@ Usage:
Options:
-h, --help Print this message
--lib Test only this package's library
--doc Test only this library's documentation
--bin NAME Test only the specified binary
--example NAME Test only the specified example
--test NAME Test only the specified integration test target
Expand Down Expand Up @@ -79,9 +81,24 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
&options.flag_color));
let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));

let empty = Vec::new();
let (mode, filter);
if options.flag_doc {
mode = ops::CompileMode::Build;
filter = ops::CompileFilter::new(true, &empty, &empty, &empty, &empty);
} else {
mode = ops::CompileMode::Test;
filter = ops::CompileFilter::new(options.flag_lib,
&options.flag_bin,
&options.flag_test,
&options.flag_example,
&options.flag_bench);
}

let ops = ops::TestOptions {
no_run: options.flag_no_run,
no_fail_fast: options.flag_no_fail_fast,
only_doc: options.flag_doc,
compile_opts: ops::CompileOptions {
config: config,
jobs: options.flag_jobs,
Expand All @@ -91,12 +108,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
spec: &options.flag_package,
exec_engine: None,
release: options.flag_release,
mode: ops::CompileMode::Test,
filter: ops::CompileFilter::new(options.flag_lib,
&options.flag_bin,
&options.flag_test,
&options.flag_example,
&options.flag_bench),
mode: mode,
filter: filter,
target_rustdoc_args: None,
target_rustc_args: None,
},
Expand Down
9 changes: 6 additions & 3 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ pub struct TestOptions<'a> {
pub compile_opts: ops::CompileOptions<'a>,
pub no_run: bool,
pub no_fail_fast: bool,
pub only_doc: bool,
}



pub fn run_tests(manifest_path: &Path,
options: &TestOptions,
test_args: &[String]) -> CargoResult<Option<CargoTestError>> {
Expand All @@ -20,7 +19,11 @@ pub fn run_tests(manifest_path: &Path,
if options.no_run {
return Ok(None)
}
let mut errors = try!(run_unit_tests(options, test_args, &compilation));
let mut errors = if options.only_doc {
Vec::new()
} else {
try!(run_unit_tests(options, test_args, &compilation))
};

// If we have an error and want to fail fast, return
if !errors.is_empty() && !options.no_fail_fast {
Expand Down
27 changes: 27 additions & 0 deletions tests/test_cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2082,3 +2082,30 @@ test!(selective_test_optional_dep {
{running} `rustc a[..]src[..]lib.rs [..]`
", compiling = COMPILING, running = RUNNING)));
});

test!(only_test_docs {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/lib.rs", r#"
#[test]
fn foo() {
let a: u32 = "hello";
}
/// ```
/// println!("ok");
/// ```
pub fn bar() {
}
"#)
.file("tests/foo.rs", "this is not rust");
p.build();

assert_that(p.cargo("test").arg("--doc"),
execs().with_status(0));
});

0 comments on commit a63c642

Please sign in to comment.