Skip to content

Commit

Permalink
[x] add check command
Browse files Browse the repository at this point in the history
Closes: aptos-labs#1568
Approved by: metajack
  • Loading branch information
bmwill authored and bors-libra committed Oct 30, 2019
1 parent 85df5c7 commit e818d9c
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
1 change: 1 addition & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[alias]
x = "run --package x --bin x --"
xcheck = "run --package x --bin x -- check"
xtest = "run --package x --bin x -- test"
12 changes: 12 additions & 0 deletions x/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ impl Cargo {
self
}

pub fn all_targets(&mut self) -> &mut Self {
self.inner.arg("--all-targets");
self
}

pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self {
self.inner.current_dir(dir);
self
Expand Down Expand Up @@ -96,9 +101,11 @@ impl Cargo {
#[derive(Debug, Default)]
pub struct CargoArgs {
pub all_features: bool,
pub all_targets: bool,
}

pub enum CargoCommand<'a> {
Check,
Test(&'a [OsString]),
}

Expand Down Expand Up @@ -157,12 +164,14 @@ impl<'a> CargoCommand<'a> {

pub fn as_str(&self) -> &'static str {
match self {
CargoCommand::Check => "check",
CargoCommand::Test(_) => "test",
}
}

fn pass_through_args(&self) -> &[OsString] {
match self {
CargoCommand::Check => &[],
CargoCommand::Test(args) => args,
}
}
Expand All @@ -171,5 +180,8 @@ impl<'a> CargoCommand<'a> {
if args.all_features {
cargo.all_features();
}
if args.all_targets {
cargo.all_targets();
}
}
}
86 changes: 86 additions & 0 deletions x/src/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use crate::{
cargo::{CargoArgs, CargoCommand},
config::Config,
utils, Result,
};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct Args {
#[structopt(long, short, number_of_values = 1)]
/// Run check on the provided packages
package: Vec<String>,
#[structopt(long)]
/// Run check on all packages in the workspace
workspace: bool,
#[structopt(long)]
/// Run check on all targets of a package (lib, bin, test, example)
all_targets: bool,
}

pub fn run(args: Args, config: Config) -> Result<()> {
let cmd = CargoCommand::Check;
// If we've been asked to build all targets then we need to enable all_features so that
// building the testing targets works
let base_args = CargoArgs {
all_features: args.all_targets,
all_targets: args.all_targets,
};

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: if args.all_targets {
e.all_features
} else {
false
},
..base_args
},
)
})
});
cmd.run_on_packages_together(run_together, &base_args)?;
cmd.run_on_packages_separate(run_separate)?;
} else if utils::project_is_root()? || args.workspace {
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: if args.all_targets {
pkg.all_features
} else {
false
},
..base_args
},
)
}))?;
} else {
let package = utils::get_local_package()?;
let cargo_args = if args.all_targets {
let all_features = config
.package_exceptions()
.get(&package)
.map(|pkg| pkg.all_features)
.unwrap_or(true);
CargoArgs {
all_features,
..base_args
}
} else {
base_args
};

cmd.run_on_local_package(&cargo_args)?;
}
Ok(())
}
5 changes: 5 additions & 0 deletions x/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use structopt::StructOpt;

mod cargo;
mod check;
mod config;
mod test;
mod utils;
Expand All @@ -15,6 +16,9 @@ struct Args {

#[derive(Debug, StructOpt)]
enum Command {
#[structopt(name = "check")]
/// Run `cargo check`
Check(check::Args),
#[structopt(name = "test")]
/// Run tests
Test(test::Args),
Expand All @@ -26,5 +30,6 @@ fn main() -> Result<()> {

match args.cmd {
Command::Test(args) => test::run(args, config),
Command::Check(args) => check::run(args, config),
}
}
20 changes: 16 additions & 4 deletions x/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ pub fn run(mut args: Args, config: Config) -> Result<()> {
args.args.extend(args.testname.clone());

let cmd = CargoCommand::Test(&args.args);
// When testing, by deafult we want to turn on all features to ensure that the fuzzing features
// are flipped on
let base_args = CargoArgs {
all_features: true,
..CargoArgs::default()
};

if args.unit {
cmd.run_with_exclusions(
config.package_exceptions().iter().map(|(p, _)| p),
&CargoArgs { all_features: true },
&base_args,
)?;
cmd.run_on_packages_separate(
config
Expand All @@ -40,6 +46,7 @@ pub fn run(mut args: Args, config: Config) -> Result<()> {
name,
CargoArgs {
all_features: pkg.all_features,
..base_args
},
)
}),
Expand All @@ -53,25 +60,27 @@ pub fn run(mut args: Args, config: Config) -> Result<()> {
p,
CargoArgs {
all_features: e.all_features,
..base_args
},
)
})
});
cmd.run_on_packages_together(run_together, &CargoArgs { all_features: true })?;
cmd.run_on_packages_together(run_together, &base_args)?;
cmd.run_on_packages_separate(run_separate)?;
Ok(())
} else if utils::project_is_root()? {
// TODO Maybe only run a subest of tests if we're not inside
// a package but not at the project root (e.g. language)
cmd.run_with_exclusions(
config.package_exceptions().iter().map(|(p, _)| p),
&CargoArgs { all_features: true },
&base_args,
)?;
cmd.run_on_packages_separate(config.package_exceptions().iter().map(|(name, pkg)| {
(
name,
CargoArgs {
all_features: pkg.all_features,
..base_args
},
)
}))?;
Expand All @@ -84,7 +93,10 @@ pub fn run(mut args: Args, config: Config) -> Result<()> {
.map(|pkg| pkg.all_features)
.unwrap_or(true);

cmd.run_on_local_package(&CargoArgs { all_features })?;
cmd.run_on_local_package(&CargoArgs {
all_features,
..base_args
})?;
Ok(())
}
}

0 comments on commit e818d9c

Please sign in to comment.