Skip to content

Commit

Permalink
Build only required binary (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored Sep 1, 2023
1 parent a7959eb commit 23b579c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
4 changes: 2 additions & 2 deletions tokio-bin-process/single-crate-test/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use tokio_bin_process::bin_path;
use tokio_bin_process::BinProcess;

#[tokio::test(flavor = "multi_thread")]
async fn test_cooldb_crate_name() {
let process = BinProcess::start_crate_name(
async fn test_cooldb_binary_name() {
let process = BinProcess::start_binary_name(
"single-crate-test",
"cooldb",
&["--log-format", "json"],
Expand Down
2 changes: 1 addition & 1 deletion tokio-bin-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
//! When Cargo builds integration tests or benchmarks it provides a path to the binary under test.
//! We can make use of that for speed and robustness with [`BinProcess::start_binary`].
//!
//! But that is not always flexible enough so as a fallback [`BinProcess`] can invoke Cargo again internally to ensure the binary we need is compiled via [`BinProcess::start_crate_name`].
//! But that is not always flexible enough so as a fallback [`BinProcess`] can invoke Cargo again internally to ensure the binary we need is compiled via [`BinProcess::start_binary_name`].
pub mod event;
pub mod event_matcher;
mod process;
Expand Down
46 changes: 27 additions & 19 deletions tokio-bin-process/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use tracing_subscriber::fmt::TestWriter;

struct CargoCache {
metadata: Option<Metadata>,
built_packages: HashSet<BuiltPackage>,
built_binaries: HashSet<BuiltBinary>,
}

#[derive(Hash, PartialEq, Eq)]
struct BuiltPackage {
struct BuiltBinary {
name: String,
profile: String,
}
Expand All @@ -41,7 +41,7 @@ struct BuiltPackage {
static CARGO_CACHE: Lazy<Mutex<CargoCache>> = Lazy::new(|| {
Mutex::new(CargoCache {
metadata: None,
built_packages: HashSet::new(),
built_binaries: HashSet::new(),
})
});

Expand Down Expand Up @@ -76,14 +76,14 @@ static CARGO_CACHE: Lazy<Mutex<CargoCache>> = Lazy::new(|| {
/// ).await;
/// # };
/// ```
/// Using `start_binary` instead of `start_crate_name` here is faster and more robust as `BinProcess` does not need to invoke Cargo.
/// Using `start_binary` instead of `start_binary_name` here is faster and more robust as `BinProcess` does not need to invoke Cargo.
///
/// ### You are writing an integration test or bench and the binary you want to test is in the same workspace but in a different package to the test or bench you are writing.
/// Use [`BinProcess::start_crate_name`] like this:
/// Use [`BinProcess::start_binary_name`] like this:
/// ```rust
/// # use tokio_bin_process::BinProcess;
/// # async {
/// BinProcess::start_crate_name(
/// BinProcess::start_binary_name(
/// "cooldb",
/// "cooldb",
/// &["--log-format", "json"],
Expand All @@ -93,11 +93,11 @@ static CARGO_CACHE: Lazy<Mutex<CargoCache>> = Lazy::new(|| {
/// ```
///
/// ### You are writing an example or other binary within a package
/// Use [`BinProcess::start_crate_name`] like this:
/// Use [`BinProcess::start_binary_name`] like this:
/// ```rust
/// # use tokio_bin_process::BinProcess;
/// # async {
/// BinProcess::start_crate_name(
/// BinProcess::start_binary_name(
/// "cooldb",
/// "cooldb",
/// &["--log-format", "json"],
Expand All @@ -107,11 +107,11 @@ static CARGO_CACHE: Lazy<Mutex<CargoCache>> = Lazy::new(|| {
/// ```
///
/// ### You need to compile the binary with an arbitrary profile
/// Use [`BinProcess::start_crate_name`] like this:
/// Use [`BinProcess::start_binary_name`] like this:
/// ```rust
/// # use tokio_bin_process::BinProcess;
/// # async {
/// BinProcess::start_crate_name(
/// BinProcess::start_binary_name(
/// "cooldb",
/// "cooldb",
/// &["--log-format", "json"],
Expand Down Expand Up @@ -158,7 +158,8 @@ fn setup_tracing_subscriber_for_test_logic() {
impl BinProcess {
/// Prefer [`BinProcess::start_binary`] where possible as it is faster and more robust.
///
/// Start the crate binary named `cargo_crate_name` in a process and returns a `BinProcess` which can be used to interact with the process.
/// Start the binary named `cargo_bin_name` in the current workspace in a new process.
/// A `BinProcess` is returned which can be used to interact with the process.
///
/// `log_name` is prepended to the logs that `BinProcess` forwards to stdout.
/// This helps to differentiate between `tracing` logs generated by the test itself and the process under test.
Expand All @@ -171,8 +172,8 @@ impl BinProcess {
/// * When it is `None` it will use "release" if `tokio-bin-process` was compiled in a release derived profile or "dev" if it was compiled in a dev derived profile.
///
/// The reason `None` will only ever result in a "release" or "dev" profile is due to a limitation on what profile information Cargo exposes to us.
pub async fn start_crate_name(
cargo_crate_name: &str,
pub async fn start_binary_name(
cargo_bin_name: &str,
log_name: &str,
binary_args: &[&str],
cargo_profile: Option<&str>,
Expand All @@ -192,20 +193,27 @@ impl BinProcess {
if cargo_cache.metadata.is_none() {
cargo_cache.metadata = Some(MetadataCommand::new().exec().unwrap());
}
let built_package = BuiltPackage {
name: cargo_crate_name.to_owned(),
let built_package = BuiltBinary {
name: cargo_bin_name.to_owned(),
profile: profile.to_owned(),
};
if !cargo_cache.built_packages.contains(&built_package) {
let all_args = vec!["build", "--all-features", "--profile", profile];
if !cargo_cache.built_binaries.contains(&built_package) {
let all_args = vec![
"build",
"--all-features",
"--profile",
profile,
"--bin",
cargo_bin_name,
];
let metadata = cargo_cache.metadata.as_ref().unwrap();
run_command(
metadata.workspace_root.as_std_path(),
env!("CARGO"),
&all_args,
)
.unwrap();
cargo_cache.built_packages.insert(built_package);
cargo_cache.built_binaries.insert(built_package);
}

cargo_cache
Expand All @@ -224,7 +232,7 @@ impl BinProcess {
"bench" => "release",
profile => profile,
};
let bin_path = target_dir.join(target_profile_name).join(cargo_crate_name);
let bin_path = target_dir.join(target_profile_name).join(cargo_bin_name);
BinProcess::start_binary(
bin_path.into_std_path_buf().as_path(),
log_name,
Expand Down
6 changes: 3 additions & 3 deletions tokio-bin-process/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tokio_bin_process::event_matcher::EventMatcher;
use tokio_bin_process::BinProcess;

#[tokio::test(flavor = "multi_thread")]
async fn test_cooldb_by_crate_name() {
async fn test_cooldb_by_binary_name() {
// Setup cooldb
let mut cooldb = cooldb(None).await;

Expand All @@ -25,7 +25,7 @@ async fn test_cooldb_by_crate_name() {
}

#[tokio::test(flavor = "multi_thread")]
async fn test_cooldb_by_crate_name_bench_profile() {
async fn test_cooldb_by_binary_name_bench_profile() {
// Setup cooldb with custom profile
let mut cooldb = cooldb(Some("bench")).await;

Expand All @@ -45,7 +45,7 @@ async fn test_cooldb_by_crate_name_bench_profile() {
}

async fn cooldb(profile: Option<&'static str>) -> BinProcess {
let mut cooldb = BinProcess::start_crate_name(
let mut cooldb = BinProcess::start_binary_name(
"cooldb",
"cooldb",
&["--log-format", "json", "--mode", "standard"],
Expand Down

0 comments on commit 23b579c

Please sign in to comment.