Skip to content

Commit a96b826

Browse files
authored
fix(forge): add logs/decoded logs in json test results (#9074)
1 parent 641132f commit a96b826

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,18 @@ impl TestArgs {
501501

502502
// Run tests in a non-streaming fashion and collect results for serialization.
503503
if self.json {
504-
let results = runner.test_collect(filter);
504+
let mut results = runner.test_collect(filter);
505+
results.values_mut().for_each(|suite_result| {
506+
for test_result in suite_result.test_results.values_mut() {
507+
if verbosity >= 2 {
508+
// Decode logs at level 2 and above.
509+
test_result.decoded_logs = decode_console_logs(&test_result.logs);
510+
} else {
511+
// Empty logs for non verbose runs.
512+
test_result.logs = vec![];
513+
}
514+
}
515+
});
505516
println!("{}", serde_json::to_string(&results)?);
506517
return Ok(TestOutcome::new(results, self.allow_failure));
507518
}

crates/forge/src/result.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ pub struct TestResult {
389389
/// be printed to the user.
390390
pub logs: Vec<Log>,
391391

392+
/// The decoded DSTest logging events and Hardhat's `console.log` from [logs](Self::logs).
393+
/// Used for json output.
394+
pub decoded_logs: Vec<String>,
395+
392396
/// What kind of test this was
393397
pub kind: TestKind,
394398

crates/forge/tests/cli/test_cmd.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)
275275
});
276276

277277
const SIMPLE_CONTRACT: &str = r#"
278+
pragma solidity 0.8.18;
279+
278280
import "./test.sol";
281+
import "./console.sol";
279282
280283
contract SimpleContract {
281284
uint256 public num;
@@ -289,12 +292,14 @@ contract SimpleContractTest is DSTest {
289292
function test() public {
290293
SimpleContract c = new SimpleContract();
291294
c.setValues(100);
295+
console.log("Value set: ", 100);
292296
}
293297
}
294298
"#;
295299

296300
forgetest!(can_run_test_with_json_output_verbose, |prj, cmd| {
297301
prj.insert_ds_test();
302+
prj.insert_console();
298303

299304
prj.add_source("Simple.t.sol", SIMPLE_CONTRACT).unwrap();
300305

@@ -306,6 +311,7 @@ forgetest!(can_run_test_with_json_output_verbose, |prj, cmd| {
306311

307312
forgetest!(can_run_test_with_json_output_non_verbose, |prj, cmd| {
308313
prj.insert_ds_test();
314+
prj.insert_console();
309315

310316
prj.add_source("Simple.t.sol", SIMPLE_CONTRACT).unwrap();
311317

crates/forge/tests/fixtures/SimpleContractTestNonVerbose.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"reason": null,
88
"counterexample": null,
99
"logs": [],
10+
"decoded_logs": [],
1011
"kind": {
1112
"Unit": {
1213
"gas": "{...}"

crates/forge/tests/fixtures/SimpleContractTestVerbose.json

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
"status": "Success",
77
"reason": null,
88
"counterexample": null,
9-
"logs": [],
9+
"logs": [
10+
{
11+
"address": "0x000000000000000000636F6e736F6c652e6c6f67",
12+
"topics": [
13+
"0x41304facd9323d75b11bcdd609cb38effffdb05710f7caf0e9b16c6d9d709f50"
14+
],
15+
"data": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f56616c7565207365743a20203130300000000000000000000000000000000000"
16+
}
17+
],
18+
"decoded_logs": [
19+
"Value set: 100"
20+
],
1021
"kind": {
1122
"Unit": {
1223
"gas": "{...}"
@@ -58,7 +69,8 @@
5869
"parent": null,
5970
"children": [
6071
1,
61-
2
72+
2,
73+
3
6274
],
6375
"idx": 0,
6476
"trace": {
@@ -91,6 +103,9 @@
91103
},
92104
{
93105
"Call": 1
106+
},
107+
{
108+
"Call": 2
94109
}
95110
]
96111
},
@@ -153,6 +168,36 @@
153168
},
154169
"logs": [],
155170
"ordering": []
171+
},
172+
{
173+
"parent": 0,
174+
"children": [],
175+
"idx": 3,
176+
"trace": {
177+
"depth": 1,
178+
"success": true,
179+
"caller": "0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496",
180+
"address": "0x000000000000000000636F6e736F6c652e6c6f67",
181+
"maybe_precompile": null,
182+
"selfdestruct_address": null,
183+
"selfdestruct_refund_target": null,
184+
"selfdestruct_transferred_value": null,
185+
"kind": "STATICCALL",
186+
"value": "0x0",
187+
"data": "{...}",
188+
"output": "0x",
189+
"gas_used": "{...}",
190+
"gas_limit": "{...}",
191+
"status": "Stop",
192+
"steps": [],
193+
"decoded": {
194+
"label": null,
195+
"return_data": null,
196+
"call_data": null
197+
}
198+
},
199+
"logs": [],
200+
"ordering": []
156201
}
157202
]
158203
}

0 commit comments

Comments
 (0)