Skip to content

Commit

Permalink
fix: opt-in to keep stdout (#1985)
Browse files Browse the repository at this point in the history
* fix: opt-in to keep stdout

* docs
  • Loading branch information
mattsse authored Feb 1, 2025
1 parent fb99a1d commit 1675da8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
14 changes: 13 additions & 1 deletion crates/node-bindings/src/nodes/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub struct Anvil {
fork_block_number: Option<u64>,
args: Vec<OsString>,
timeout: Option<u64>,
keep_stdout: bool,
}

impl Anvil {
Expand Down Expand Up @@ -261,6 +262,14 @@ impl Anvil {
self
}

/// Keep the handle to anvil's stdout in order to read from it.
///
/// Caution: if the stdout handle isn't used, this can end up blocking.
pub const fn keep_stdout(mut self) -> Self {
self.keep_stdout = true;
self
}

/// Consumes the builder and spawns `anvil`.
///
/// # Panics
Expand Down Expand Up @@ -360,7 +369,10 @@ impl Anvil {
}
}

child.stdout = Some(reader.into_inner());
if self.keep_stdout {
// re-attach the stdout handle if requested
child.stdout = Some(reader.into_inner());
}

Ok(AnvilInstance {
child,
Expand Down
15 changes: 14 additions & 1 deletion crates/node-bindings/src/nodes/reth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ pub struct Reth {
chain_or_path: Option<String>,
genesis: Option<Genesis>,
args: Vec<OsString>,
keep_stdout: bool,
}

impl Reth {
Expand All @@ -187,6 +188,7 @@ impl Reth {
chain_or_path: None,
genesis: None,
args: Vec::new(),
keep_stdout: false,
}
}

Expand Down Expand Up @@ -309,6 +311,14 @@ impl Reth {
self
}

/// Keep the handle to reth's stdout in order to read from it.
///
/// Caution: if the stdout handle isn't used, this can end up blocking.
pub const fn keep_stdout(mut self) -> Self {
self.keep_stdout = true;
self
}

/// Adds an argument to pass to `reth`.
///
/// Pass any arg that is not supported by the builder.
Expand Down Expand Up @@ -510,7 +520,10 @@ impl Reth {
}
}

child.stdout = Some(reader.into_inner());
if self.keep_stdout {
// re-attach the stdout handle if requested
child.stdout = Some(reader.into_inner());
}

Ok(RethInstance {
pid: child,
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1956,7 +1956,7 @@ mod tests {

#[tokio::test]
async fn capture_anvil_logs() {
let mut anvil = Anvil::new().spawn();
let mut anvil = Anvil::new().keep_stdout().spawn();

let provider = ProviderBuilder::new().on_http(anvil.endpoint_url());

Expand Down

0 comments on commit 1675da8

Please sign in to comment.