Skip to content

Commit

Permalink
Refactor cargo_test into an ops module
Browse files Browse the repository at this point in the history
The logic for doc tests will get a little complex, so this is moved to aseparate
module instead of inside the executable.
  • Loading branch information
alexcrichton committed Aug 5, 2014
1 parent 23bb49c commit 505593a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
29 changes: 11 additions & 18 deletions src/bin/cargo-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ extern crate docopt;
use std::io::process::ExitStatus;

use cargo::ops;
use cargo::{execute_main_without_stdin};
use cargo::core::{MultiShell};
use cargo::util;
use cargo::execute_main_without_stdin;
use cargo::core::MultiShell;
use cargo::util::{CliResult, CliError, CargoError};
use cargo::util::important_paths::{find_root_manifest_for_cwd};

Expand Down Expand Up @@ -48,24 +47,18 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
target: None,
};

let test_executables = try!(ops::compile(&root,
&mut compile_opts).map_err(|err| {
let err = try!(ops::run_tests(&root, &mut compile_opts,
options.arg_args.as_slice()).map_err(|err| {
CliError::from_boxed(err, 101)
}));

let test_dir = root.dir_path().join("target").join("test");

for file in test_executables.iter() {
try!(util::process(test_dir.join(file.as_slice()))
.args(options.arg_args.as_slice())
.exec().map_err(|e| {
let exit_status = match e.exit {
match err {
None => Ok(None),
Some(err) => {
let status = match err.exit {
Some(ExitStatus(i)) => i as uint,
_ => 1,
_ => 101,
};
CliError::from_boxed(e.mark_human(), exit_status)
}));
Err(CliError::from_boxed(err.mark_human(), status))
}
}

Ok(None)
}
15 changes: 2 additions & 13 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct CompileOptions<'a> {
}

pub fn compile(manifest_path: &Path,
options: &mut CompileOptions) -> CargoResult<Vec<String>> {
options: &mut CompileOptions) -> CargoResult<()> {
let CompileOptions { update, env, ref mut shell, jobs, target } = *options;
let target = target.map(|s| s.to_string());

Expand Down Expand Up @@ -127,18 +127,7 @@ pub fn compile(manifest_path: &Path,

try!(ops::write_resolve(&package, &resolve));

let test_executables: Vec<String> = targets.iter()
.filter_map(|target| {
if target.get_profile().is_test() {
debug!("Run Target: {}", target.get_name());
Some(target.file_stem())
} else {
debug!("Skip Target: {}", target.get_name());
None
}
}).collect();

Ok(test_executables)
Ok(())
}

fn source_ids_from_config(configs: &HashMap<String, config::ConfigValue>,
Expand Down
33 changes: 33 additions & 0 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use core::Source;
use sources::PathSource;
use ops;
use util::{process, CargoResult, ProcessError};

pub fn run_tests(manifest_path: &Path,
options: &mut ops::CompileOptions,
args: &[String]) -> CargoResult<Option<ProcessError>> {
let mut source = PathSource::for_path(&manifest_path.dir_path());
try!(source.update());
let package = try!(source.get_root_package());

try!(ops::compile(manifest_path, options));

let mut exes = package.get_targets().iter().filter_map(|target| {
if !target.get_profile().is_test() { return None }
let root = package.get_root().join("target");
let root = match target.get_profile().get_dest() {
Some(dest) => root.join(dest),
None => root,
};
Some(root.join(target.file_stem()))
});

for exe in exes {
match process(exe).args(args).exec() {
Ok(()) => {}
Err(e) => return Ok(Some(e))
}
}

Ok(None)
}
2 changes: 2 additions & 0 deletions src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use self::cargo_new::{new, NewOptions};
pub use self::cargo_doc::{doc, DocOptions};
pub use self::cargo_generate_lockfile::{generate_lockfile, write_resolve};
pub use self::cargo_generate_lockfile::{update_lockfile, load_lockfile};
pub use self::cargo_test::run_tests;

mod cargo_clean;
mod cargo_compile;
Expand All @@ -16,3 +17,4 @@ mod cargo_run;
mod cargo_new;
mod cargo_doc;
mod cargo_generate_lockfile;
mod cargo_test;

0 comments on commit 505593a

Please sign in to comment.