Skip to content

Commit

Permalink
[x] add bench command
Browse files Browse the repository at this point in the history
Closes: #1593
Approved by: metajack
  • Loading branch information
bmwill authored and bors-libra committed Nov 1, 2019
1 parent d74c33e commit 0148fa5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
72 changes: 72 additions & 0 deletions x/src/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::{
cargo::{CargoArgs, CargoCommand},
config::Config,
utils, Result,
};
use std::ffi::OsString;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct Args {
#[structopt(long, short, number_of_values = 1)]
/// Run test on the provided packages
package: Vec<String>,
#[structopt(name = "BENCHNAME", parse(from_os_str))]
benchname: Option<OsString>,
#[structopt(name = "ARGS", parse(from_os_str), last = true)]
args: Vec<OsString>,
}

pub fn run(mut args: Args, config: Config) -> Result<()> {
args.args.extend(args.benchname.clone());

let cmd = CargoCommand::Bench(&args.args);
let base_args = CargoArgs {
all_features: true,
..CargoArgs::default()
};

if !args.package.is_empty() {
let run_together = args.package.iter().filter(|p| !config.is_exception(p));
let run_separate = args.package.iter().filter_map(|p| {
config.package_exceptions().get(p).map(|e| {
(
p,
CargoArgs {
all_features: e.all_features,
..base_args
},
)
})
});
cmd.run_on_packages_together(run_together, &base_args)?;
cmd.run_on_packages_separate(run_separate)?;
} else if utils::project_is_root()? {
cmd.run_with_exclusions(
config.package_exceptions().iter().map(|(p, _)| p),
&base_args,
)?;
cmd.run_on_packages_separate(config.package_exceptions().iter().map(|(name, pkg)| {
(
name,
CargoArgs {
all_features: pkg.all_features,
..base_args
},
)
}))?;
} else {
let package = utils::get_local_package()?;
let all_features = config
.package_exceptions()
.get(&package)
.map(|pkg| pkg.all_features)
.unwrap_or(true);

cmd.run_on_local_package(&CargoArgs {
all_features,
..base_args
})?;
}
Ok(())
}
3 changes: 3 additions & 0 deletions x/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub struct CargoArgs {
}

pub enum CargoCommand<'a> {
Bench(&'a [OsString]),
Check,
Clippy(&'a [OsString]),
Test(&'a [OsString]),
Expand Down Expand Up @@ -170,6 +171,7 @@ impl<'a> CargoCommand<'a> {

pub fn as_str(&self) -> &'static str {
match self {
CargoCommand::Bench(_) => "bench",
CargoCommand::Check => "check",
CargoCommand::Clippy(_) => "clippy",
CargoCommand::Test(_) => "test",
Expand All @@ -178,6 +180,7 @@ impl<'a> CargoCommand<'a> {

fn pass_through_args(&self) -> &[OsString] {
match self {
CargoCommand::Bench(args) => args,
CargoCommand::Check => &[],
CargoCommand::Clippy(args) => args,
CargoCommand::Test(args) => args,
Expand Down
5 changes: 5 additions & 0 deletions x/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use structopt::StructOpt;

mod bench;
mod cargo;
mod check;
mod clippy;
Expand All @@ -17,6 +18,9 @@ struct Args {

#[derive(Debug, StructOpt)]
enum Command {
#[structopt(name = "bench")]
/// Run `cargo bench`
Bench(bench::Args),
#[structopt(name = "check")]
/// Run `cargo check`
Check(check::Args),
Expand All @@ -36,5 +40,6 @@ fn main() -> Result<()> {
Command::Test(args) => test::run(args, config),
Command::Check(args) => check::run(args, config),
Command::Clippy(args) => clippy::run(args, config),
Command::Bench(args) => bench::run(args, config),
}
}

0 comments on commit 0148fa5

Please sign in to comment.