Skip to content

Commit 4c0bfd3

Browse files
authored
Implement test debug feature (#21)
1 parent 687271d commit 4c0bfd3

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
## [Unreleased]
66

7+
## [1.0.5] - 2025-01-27
8+
- Use foundry's debugger for `--debug` flag.
9+
710
## [1.0.4] - 2025-01-27
811
- Rewrite the Huff test module to use `anvil` and `forge` features from `foundry` to fork the mainnet.
912
- This is experimental, and there will be some breaking changes.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license = "MIT OR Apache-2.0"
1111
readme = "README.md"
1212
repository = "https://github.com/cakevm/huff-neo"
1313
rust-version = "1.84"
14-
version = "1.0.4"
14+
version = "1.0.5"
1515

1616
[workspace.dependencies]
1717
huff-neo-codegen = { path = "crates/codegen" }
@@ -30,6 +30,7 @@ comfy-table = "7.1.3"
3030
foundry-cli = { package = "foundry-cli", git = "https://github.com/foundry-rs/foundry", rev = "0821048" }
3131
foundry-common = { package = "foundry-common", git = "https://github.com/foundry-rs/foundry", rev = "0821048" }
3232
foundry-config = { package = "foundry-config", git = "https://github.com/foundry-rs/foundry", rev = "0821048" }
33+
foundry-debugger = { package = "foundry-debugger", git = "https://github.com/foundry-rs/foundry", rev = "0821048" }
3334
foundry-evm = { package = "foundry-evm", git = "https://github.com/foundry-rs/foundry", rev = "0821048" }
3435
foundry-evm-traces = { package = "foundry-evm-traces", git = "https://github.com/foundry-rs/foundry", rev = "0821048" }
3536
hex = "0.4.3"

crates/test-runner/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ alloy-primitives.workspace = true
1919
anvil.workspace = true
2020
comfy-table.workspace = true
2121
foundry-common.workspace = true
22+
foundry-debugger.workspace = true
2223
foundry-evm.workspace = true
2324
phf.workspace = true
2425
revm.workspace = true

crates/test-runner/src/lib.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ pub mod errors;
2020

2121
/// Re-export the Inspector from anvil crate
2222
pub use anvil::eth::backend::mem::inspector::Inspector;
23+
use foundry_debugger::Debugger;
2324
use foundry_evm::backend::Backend;
2425
use foundry_evm::fork::CreateFork;
25-
use foundry_evm::traces::InternalTraceMode;
26+
use foundry_evm::traces::{InternalTraceMode, SparsedTraceArena, TraceKind};
2627
use revm::db::CacheDB;
2728
use revm::primitives::{Env, SpecId};
2829

@@ -157,13 +158,44 @@ impl<'t> HuffTester<'t> {
157158
}
158159

159160
// Execute our tests and return a vector of the results
160-
self.macros
161+
let results = self
162+
.macros
161163
.into_iter()
162164
.map(|macro_def| {
163165
let db = Backend::spawn(self.config.fork.take());
164166
let mut cache_db = CacheDB::new(db);
165167
self.runner.run_test(&mut cache_db, macro_def, self.ast)
166168
})
167-
.collect::<Result<Vec<TestResult>, RunnerError>>()
169+
.collect::<Result<Vec<TestResult>, RunnerError>>();
170+
171+
if !self.config.debug {
172+
return results;
173+
}
174+
175+
//
176+
// Run the debugger
177+
//
178+
let Ok(ref results_vec) = results else {
179+
return results;
180+
};
181+
if results_vec.len() != 1 {
182+
return Err(RunnerError::GenericError("Debugging is only supported for a single test".to_string()));
183+
}
184+
let Some(test_result) = results_vec.first() else {
185+
return Err(RunnerError::GenericError("No test result found".to_string()));
186+
};
187+
188+
let Some(trace_area) = test_result.clone().inspector.tracer else {
189+
return Err(RunnerError::GenericError("No trace found".to_string()));
190+
};
191+
192+
let traces = [(TraceKind::Execution, SparsedTraceArena { arena: trace_area.into_traces(), ignored: Default::default() })];
193+
194+
let builder = Debugger::builder().traces(traces.iter().filter(|(t, _)| t.is_execution()).cloned().collect());
195+
196+
let mut debugger = builder.build();
197+
debugger.try_run_tui().map_err(|e| RunnerError::GenericError(format!("{:?}", e)))?;
198+
199+
results
168200
}
169201
}

0 commit comments

Comments
 (0)