Skip to content

Commit

Permalink
[x] integrate nextest, a new test runner
Browse files Browse the repository at this point in the history
See diem/diem#7994 for the details.
  • Loading branch information
sunshowers authored and bors-libra committed Mar 26, 2021
1 parent 7b1cc87 commit 4aa4472
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 15 deletions.
1 change: 1 addition & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ xfix = "run --package x --bin x -- fix"
xtest = "run --package x --bin x -- test"
xlint = "run --package x --bin x -- lint"
xbuild = "run --package x --bin x -- build"
nextest = "run --package x --bin x -- nextest"
73 changes: 69 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions devtools/x-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ impl XCoreContext {
Ok(self.package_graph_plus()?.head())
}

/// For a given list of workspace packages, returns a tuple of (known, unknown) packages.
///
/// Initializes the package graph if it isn't already done so, and returns an error if the
pub fn partition_workspace_names<'a, B>(
&self,
names: impl IntoIterator<Item = &'a str>,
) -> Result<(B, B)>
where
B: Default + Extend<&'a str>,
{
let workspace = self.package_graph()?.workspace();
let (known, unknown) = names
.into_iter()
.partition(|name| workspace.contains_name(name));
Ok((known, unknown))
}

/// Returns information about the subsets for this workspace.
pub fn subsets(&self) -> Result<&WorkspaceSubsets> {
Ok(self.package_graph_plus()?.suffix())
Expand Down
2 changes: 2 additions & 0 deletions devtools/x/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "Apache-2.0"

[dependencies]
camino = "1.0.3"
cargo_metadata = "0.13.1"
determinator = "0.4.0"
serde = { version = "1.0.124", features = ["derive"] }
serde_json = "1.0.64"
Expand All @@ -25,6 +26,7 @@ chrono = "0.4.19"
globset = "0.4.6"
regex = "1.4.3"
rayon = "1.5.0"
testrunner = { git = "https://github.com/diem/diem-devtools", branch = "main" }
indexmap = "1.6.2"
x-core = { path = "../x-core" }
x-lint = { path = "../x-lint" }
3 changes: 0 additions & 3 deletions devtools/x/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ use structopt::StructOpt;
pub struct Args {
#[structopt(flatten)]
package_args: SelectedPackageArgs,
#[structopt(long, number_of_values = 1)]
/// Package to exclude (see `cargo help pkgid`)
exclude: Vec<String>,
#[structopt(flatten)]
build_args: BuildArgs,
#[structopt(long, parse(from_os_str))]
Expand Down
39 changes: 32 additions & 7 deletions devtools/x/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use crate::{
utils::{apply_sccache_if_possible, project_root},
Result,
};
use anyhow::anyhow;
use anyhow::{anyhow, Context};
use cargo_metadata::Message;
use indexmap::map::IndexMap;
use log::{info, warn};
use std::{
env,
ffi::{OsStr, OsString},
io::Cursor,
path::Path,
process::{Command, Output, Stdio},
time::Instant,
Expand Down Expand Up @@ -207,12 +209,10 @@ impl Cargo {
}

/// Runs this command, capturing the standard output into a `Vec<u8>`.
/// No logging/timing will be displayed as the result of this call from x.
#[allow(dead_code)]
/// Standard error is forwarded.
pub fn run_with_output(&mut self) -> Result<Vec<u8>> {
self.inner.stderr(Stdio::inherit());
// Since system out hijacked don't log for this command
self.do_run(false).map(|o| o.stdout)
self.do_run(true).map(|o| o.stdout)
}

/// Internal run command, where the magic happens.
Expand Down Expand Up @@ -332,14 +332,39 @@ impl<'a> CargoCommand<'a> {
return Ok(());
}

let mut cargo = self.prepare_cargo(packages);
cargo.run()
}

/// Runs this command on the selected packages, returning the standard output as a bytestring.
pub fn run_capture_messages(
&self,
packages: &SelectedPackages<'_>,
) -> Result<impl Iterator<Item = Result<Message>>> {
// Early return if we have no packages to run.
let output = if !packages.should_invoke() {
info!("no packages to {}: exiting early", self.as_str());
vec![]
} else {
let mut cargo = self.prepare_cargo(packages);
cargo.args(&["--message-format", "json"]);
cargo.run_with_output()?
};

Ok(Message::parse_stream(Cursor::new(output))
.map(|message| message.context("error while parsing message from Cargo")))
}

fn prepare_cargo(&self, packages: &SelectedPackages<'_>) -> Cargo {
let mut cargo = Cargo::new(self.cargo_config(), self.as_str(), true);
cargo
.current_dir(project_root())
.args(self.direct_args())
.packages(packages)
.pass_through(self.pass_through_args())
.envs(self.get_extra_env().to_owned())
.run()
.envs(self.get_extra_env().to_owned());

cargo
}

pub fn as_str(&self) -> &'static str {
Expand Down
25 changes: 24 additions & 1 deletion devtools/x/src/cargo/selected_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::{changed_since::changed_since_impl, context::XContext, Result};
use anyhow::anyhow;
use guppy::graph::DependencyDirection;
use log::warn;
use std::collections::BTreeSet;
use structopt::StructOpt;
use x_core::WorkspaceStatus;
Expand All @@ -17,6 +18,9 @@ pub struct SelectedPackageArgs {
#[structopt(long, short, number_of_values = 1)]
/// Run on the specified members (package subsets)
pub(crate) members: Vec<String>,
#[structopt(long, number_of_values = 1)]
/// Exclude packages
pub(crate) exclude: Vec<String>,
#[structopt(long, short)]
/// Run on packages changed since the merge base of this commit
changed_since: Option<String>,
Expand Down Expand Up @@ -70,7 +74,26 @@ impl SelectedPackageArgs {
);
}

Ok(SelectedPackages::new(includes))
let mut ret = SelectedPackages::new(includes);

if !self.exclude.is_empty() {
let workspace = xctx.core().package_graph()?.workspace();
// Check that all the excluded package names are valid.
let (known, unknown): (Vec<_>, Vec<_>) = xctx
.core()
.partition_workspace_names(self.exclude.iter().map(|package| package.as_str()))?;
if !unknown.is_empty() {
warn!(
"excluded package(s) `{}` not found in workspace `{}`",
unknown.join(", "),
workspace.root()
)
}

ret.add_excludes(known);
}

Ok(ret)
}
}

Expand Down
5 changes: 5 additions & 0 deletions devtools/x/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod generate_summaries;
mod generate_workspace_hack;
mod installer;
mod lint;
mod nextest;
mod playground;
mod test;
mod tools;
Expand Down Expand Up @@ -68,6 +69,9 @@ enum Command {
#[structopt(name = "test")]
/// Run tests
Test(test::Args),
#[structopt(name = "nextest")]
/// Run tests with new test runner
Nextest(nextest::Args),
#[structopt(name = "tools")]
/// Run tests
Tools(tools::Args),
Expand Down Expand Up @@ -115,6 +119,7 @@ fn main() -> Result<()> {
match args.cmd {
Command::Tools(args) => tools::run(args, xctx),
Command::Test(args) => test::run(args, xctx),
Command::Nextest(args) => nextest::run(args, xctx),
Command::Build(args) => build::run(args, xctx),
Command::ChangedSince(args) => changed_since::run(args, xctx),
Command::Check(args) => check::run(args, xctx),
Expand Down
Loading

0 comments on commit 4aa4472

Please sign in to comment.