Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[x] add bench command #1593

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
}
}