Skip to content

Commit bcbfa1c

Browse files
committed
Add depth trace tag when running forge test
1 parent 3a38e02 commit bcbfa1c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

crates/evm/traces/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ pub fn render_trace_arena(arena: &SparsedTraceArena) -> String {
187187
render_trace_arena_inner(arena, false, false)
188188
}
189189

190+
/// Prunes trace depth if depth is provided as an argument
191+
pub fn prune_trace_depth(arena: &mut CallTraceArena, depth: usize) {
192+
for node in arena.nodes_mut() {
193+
if node.trace.depth >= depth {
194+
Vec::clear(&mut node.ordering);
195+
}
196+
}
197+
}
198+
190199
/// Render a collection of call traces to a string optionally including contract creation bytecodes
191200
/// and in JSON format.
192201
pub fn render_trace_arena_inner(

crates/forge/src/cmd/test/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use foundry_config::{
4141
use foundry_debugger::Debugger;
4242
use foundry_evm::{
4343
opts::EvmOpts,
44-
traces::{backtrace::BacktraceBuilder, identifier::TraceIdentifiers},
44+
traces::{backtrace::BacktraceBuilder, identifier::TraceIdentifiers, prune_trace_depth},
4545
};
4646
use regex::Regex;
4747
use std::{
@@ -136,6 +136,10 @@ pub struct TestArgs {
136136
#[arg(long, short, env = "FORGE_SUPPRESS_SUCCESSFUL_TRACES", help_heading = "Display options")]
137137
suppress_successful_traces: bool,
138138

139+
/// Defines the depth of a trace
140+
#[arg(long, short)]
141+
depth: Option<usize>,
142+
139143
/// Output test results as JUnit XML report.
140144
#[arg(long, conflicts_with_all = ["quiet", "json", "gas_report", "summary", "list", "show_progress"], help_heading = "Display options")]
141145
pub junit: bool,
@@ -652,6 +656,11 @@ impl TestArgs {
652656

653657
if should_include {
654658
decode_trace_arena(arena, &decoder).await;
659+
660+
if let Some(depth) = self.depth {
661+
prune_trace_depth(arena, depth);
662+
}
663+
655664
decoded_traces.push(render_trace_arena_inner(arena, false, verbosity > 4));
656665
}
657666
}
@@ -1037,6 +1046,12 @@ mod tests {
10371046
assert!(args.fuzz_seed.is_some());
10381047
}
10391048

1049+
#[test]
1050+
fn depth_trace() {
1051+
let args: TestArgs = TestArgs::parse_from(["foundry-cli", "--depth", "2"]);
1052+
assert!(args.depth.is_some());
1053+
}
1054+
10401055
// <https://github.com/foundry-rs/foundry/issues/5913>
10411056
#[test]
10421057
fn fuzz_seed_exists() {

0 commit comments

Comments
 (0)