Skip to content

Commit

Permalink
Allow setting env vars to start the process with
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Dec 30, 2024
1 parent 12405b9 commit 390cbde
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 121 deletions.
8 changes: 5 additions & 3 deletions cooldb/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::time::Duration;
use tokio::time::timeout;
use tokio_bin_process::bin_path;
use tokio_bin_process::event::Level;
use tokio_bin_process::event_matcher::EventMatcher;
use tokio_bin_process::BinProcess;
use tokio_bin_process::{bin_path, BinProcessBuilder};

#[tokio::test(flavor = "multi_thread")]
async fn test_cooldb() {
Expand All @@ -25,8 +25,10 @@ async fn test_cooldb() {
}

async fn cooldb() -> BinProcess {
let mut cooldb =
BinProcess::start_binary(bin_path!("cooldb"), "cooldb", &["--log-format", "json"]).await;
let mut cooldb = BinProcessBuilder::from_path(bin_path!("cooldb"))
.with_args(vec!["--log-format".to_owned(), "json".to_owned()])
.start()
.await;

timeout(
Duration::from_secs(30),
Expand Down
18 changes: 11 additions & 7 deletions integration-test/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::time::Duration;
use tokio::time::timeout;
use tokio_bin_process::bin_path;
use tokio_bin_process::event::Level;
use tokio_bin_process::event_matcher::EventMatcher;
use tokio_bin_process::BinProcess;
use tokio_bin_process::{bin_path, BinProcessBuilder};

#[tokio::test(flavor = "multi_thread")]
async fn test_cooldb() {
Expand Down Expand Up @@ -86,12 +86,16 @@ async fn test_error_at_startup() {
}

async fn integration_test(mode: &str) -> BinProcess {
let mut integration_test = BinProcess::start_binary(
bin_path!("integration-test"),
"test",
&["--log-format", "json", "--mode", mode],
)
.await;
let mut integration_test = BinProcessBuilder::from_path(bin_path!("integration-test"))
.with_log_name(Some("test".to_owned()))
.with_args(vec![
"--log-format".to_owned(),
"json".to_owned(),
"--mode".to_owned(),
mode.to_owned(),
])
.start()
.await;

timeout(
Duration::from_secs(30),
Expand Down
39 changes: 24 additions & 15 deletions tokio-bin-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,41 @@
//!
//! ```rust
//! use tokio_bin_process::event::Level;
//! use tokio_bin_process::{BinProcess, bin_path};
//! use tokio_bin_process::{BinProcess, BinProcessBuilder, bin_path};
//! use tokio_bin_process::event_matcher::EventMatcher;
//! use std::time::Duration;
//! use std::path::PathBuf;
//! # // hack to make the doc test compile
//! # macro_rules! bin_path {
//! # ($bin_name:expr) => {
//! # &std::path::Path::new("foo")
//! # std::path::PathBuf::from("foo")
//! # };
//! # }
//!
//! /// you'll want a helper like this as you'll be creating this in every integration test.
//! async fn cooldb_process() -> BinProcess {
//! // start the process
//! let mut process = BinProcess::start_binary(
//! // Locate the path to the cooldb binary from an integration test or benchmark
//! bin_path!("cooldb"),
//! "cooldb", // The name that BinProcess should prepend its forwarded logs with
//! &[
//! let mut process = BinProcessBuilder::from_path(
//! // Locate the path to the cooldb binary from an integration test or benchmark
//! bin_path!("cooldb")
//! )
//! .with_log_name(Some("cooldb1".to_owned())) // The name that BinProcess should prepend its forwarded logs with
//! .with_env_vars(vec![
//! // provide any custom env vars required
//! ("FOO".to_owned(), "BAR".to_owned()),
//! // tokio-bin-process relies on reading tracing json's output,
//! // so configure the application to produce that
//! ("COOLDB_LOG_FORMAT".to_owned(), "JSON".to_owned())
//! ])
//! .with_args(vec![
//! // provide any custom CLI args required
//! "--foo", "bar",
//! "--foo".to_owned(), "bar".to_owned(),
//! // tokio-bin-process relies on reading tracing json's output,
//! // so configure the application to produce that
//! "--log-format", "json"
//! ],
//! )
//! .await;
//! "--log-format".to_owned(), "json".to_owned()
//! ])
//! .start()
//! .await;
//!
//! // block asynchrounously until the application gives an event indicating that its ready
//! tokio::time::timeout(
Expand Down Expand Up @@ -78,14 +86,15 @@
//! ```
//!
//! 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`].
//! We can make use of that for speed and robustness with [`BinProcessBuilder::from_path`].
//!
//! 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`].
//! 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 [`BinProcessBuilder::from_cargo_name`].
pub mod event;
pub mod event_matcher;
mod process;

pub use process::BinProcess;
pub use process::BinProcessBuilder;

/// When called from within an integration test or benchmark, returns the path to the binary with the specified crate name in the current package.
///
Expand All @@ -99,6 +108,6 @@ pub use process::BinProcess;
#[macro_export]
macro_rules! bin_path {
($bin_name:expr) => {
&std::path::Path::new(std::env!(concat!("CARGO_BIN_EXE_", $bin_name)))
std::path::PathBuf::from(std::env!(concat!("CARGO_BIN_EXE_", $bin_name)))
};
}
Loading

0 comments on commit 390cbde

Please sign in to comment.