Skip to content

Commit e88e563

Browse files
authored
feat(forge): add option to suppress successful tests traces (foundry-rs#9865)
1 parent 1bcd17c commit e88e563

File tree

2 files changed

+146
-2
lines changed

2 files changed

+146
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ pub struct TestArgs {
130130
#[arg(long, env = "FORGE_ALLOW_FAILURE")]
131131
allow_failure: bool,
132132

133+
/// Suppress successful test traces and show only traces for failures.
134+
#[arg(long, short, env = "FORGE_SUPPRESS_SUCCESSFUL_TRACES", help_heading = "Display options")]
135+
suppress_successful_traces: bool,
136+
133137
/// Output test results as JUnit XML report.
134138
#[arg(long, conflicts_with_all = ["quiet", "json", "gas_report", "summary", "list", "show_progress"], help_heading = "Display options")]
135139
pub junit: bool,
@@ -572,6 +576,8 @@ impl TestArgs {
572576

573577
// Process individual test results, printing logs and traces when necessary.
574578
for (name, result) in tests {
579+
let show_traces =
580+
!self.suppress_successful_traces || result.status == TestStatus::Failure;
575581
if !silent {
576582
sh_println!("{}", result.short_result(name))?;
577583

@@ -583,7 +589,7 @@ impl TestArgs {
583589
}
584590

585591
// We only display logs at level 2 and above
586-
if verbosity >= 2 {
592+
if verbosity >= 2 && show_traces {
587593
// We only decode logs from Hardhat and DS-style console events
588594
let console_logs = decode_console_logs(&result.logs);
589595
if !console_logs.is_empty() {
@@ -634,7 +640,7 @@ impl TestArgs {
634640
}
635641
}
636642

637-
if !silent && !decoded_traces.is_empty() {
643+
if !silent && show_traces && !decoded_traces.is_empty() {
638644
sh_println!("Traces:")?;
639645
for trace in &decoded_traces {
640646
sh_println!("{trace}")?;

crates/forge/tests/cli/test_cmd.rs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,3 +2998,141 @@ forgetest_init!(colored_traces, |prj, cmd| {
29982998
.assert_success()
29992999
.stdout_eq(file!["../fixtures/colored_traces.svg": TermSvg]);
30003000
});
3001+
3002+
// Tests that traces for successful tests can be suppressed by using `-s` flag.
3003+
// <https://github.com/foundry-rs/foundry/issues/9864>
3004+
forgetest_init!(should_only_show_failed_tests_trace, |prj, cmd| {
3005+
prj.add_test(
3006+
"SuppressTracesTest.t.sol",
3007+
r#"
3008+
import "forge-std/Test.sol";
3009+
import {Counter} from "../src/Counter.sol";
3010+
3011+
contract SuppressTracesTest is Test {
3012+
Counter public counter;
3013+
3014+
function setUp() public {
3015+
counter = new Counter();
3016+
counter.setNumber(0);
3017+
}
3018+
3019+
function test_increment_success() public {
3020+
console.log("test increment success");
3021+
counter.increment();
3022+
assertEq(counter.number(), 1);
3023+
}
3024+
3025+
function test_increment_failure() public {
3026+
console.log("test increment failure");
3027+
counter.increment();
3028+
assertEq(counter.number(), 100);
3029+
}
3030+
}
3031+
"#,
3032+
)
3033+
.unwrap();
3034+
3035+
// Show traces and logs for failed test only.
3036+
cmd.args(["test", "--mc", "SuppressTracesTest", "-vvvv", "-s"]).assert_failure().stdout_eq(
3037+
str![[r#"
3038+
[COMPILING_FILES] with [SOLC_VERSION]
3039+
[SOLC_VERSION] [ELAPSED]
3040+
Compiler run successful!
3041+
3042+
Ran 2 tests for test/SuppressTracesTest.t.sol:SuppressTracesTest
3043+
[FAIL: assertion failed: 1 != 100] test_increment_failure() ([GAS])
3044+
Logs:
3045+
test increment failure
3046+
3047+
Traces:
3048+
[137242] SuppressTracesTest::setUp()
3049+
├─ [96345] → new Counter@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
3050+
│ └─ ← [Return] 481 bytes of code
3051+
├─ [2592] Counter::setNumber(0)
3052+
│ └─ ← [Stop]
3053+
└─ ← [Stop]
3054+
3055+
[35178] SuppressTracesTest::test_increment_failure()
3056+
├─ [0] console::log("test increment failure") [staticcall]
3057+
│ └─ ← [Stop]
3058+
├─ [22418] Counter::increment()
3059+
│ └─ ← [Stop]
3060+
├─ [424] Counter::number() [staticcall]
3061+
│ └─ ← [Return] 1
3062+
├─ [0] VM::assertEq(1, 100) [staticcall]
3063+
│ └─ ← [Revert] assertion failed: 1 != 100
3064+
└─ ← [Revert] assertion failed: 1 != 100
3065+
3066+
[PASS] test_increment_success() ([GAS])
3067+
Suite result: FAILED. 1 passed; 1 failed; 0 skipped; [ELAPSED]
3068+
3069+
Ran 1 test suite [ELAPSED]: 1 tests passed, 1 failed, 0 skipped (2 total tests)
3070+
3071+
Failing tests:
3072+
Encountered 1 failing test in test/SuppressTracesTest.t.sol:SuppressTracesTest
3073+
[FAIL: assertion failed: 1 != 100] test_increment_failure() ([GAS])
3074+
3075+
Encountered a total of 1 failing tests, 1 tests succeeded
3076+
3077+
"#]],
3078+
);
3079+
3080+
// Show traces and logs for all tests.
3081+
cmd.forge_fuse()
3082+
.args(["test", "--mc", "SuppressTracesTest", "-vvvv"])
3083+
.assert_failure()
3084+
.stdout_eq(str![[r#"
3085+
No files changed, compilation skipped
3086+
3087+
Ran 2 tests for test/SuppressTracesTest.t.sol:SuppressTracesTest
3088+
[FAIL: assertion failed: 1 != 100] test_increment_failure() ([GAS])
3089+
Logs:
3090+
test increment failure
3091+
3092+
Traces:
3093+
[137242] SuppressTracesTest::setUp()
3094+
├─ [96345] → new Counter@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
3095+
│ └─ ← [Return] 481 bytes of code
3096+
├─ [2592] Counter::setNumber(0)
3097+
│ └─ ← [Stop]
3098+
└─ ← [Stop]
3099+
3100+
[35178] SuppressTracesTest::test_increment_failure()
3101+
├─ [0] console::log("test increment failure") [staticcall]
3102+
│ └─ ← [Stop]
3103+
├─ [22418] Counter::increment()
3104+
│ └─ ← [Stop]
3105+
├─ [424] Counter::number() [staticcall]
3106+
│ └─ ← [Return] 1
3107+
├─ [0] VM::assertEq(1, 100) [staticcall]
3108+
│ └─ ← [Revert] assertion failed: 1 != 100
3109+
└─ ← [Revert] assertion failed: 1 != 100
3110+
3111+
[PASS] test_increment_success() ([GAS])
3112+
Logs:
3113+
test increment success
3114+
3115+
Traces:
3116+
[35229] SuppressTracesTest::test_increment_success()
3117+
├─ [0] console::log("test increment success") [staticcall]
3118+
│ └─ ← [Stop]
3119+
├─ [22418] Counter::increment()
3120+
│ └─ ← [Stop]
3121+
├─ [424] Counter::number() [staticcall]
3122+
│ └─ ← [Return] 1
3123+
├─ [0] VM::assertEq(1, 1) [staticcall]
3124+
│ └─ ← [Return]
3125+
└─ ← [Stop]
3126+
3127+
Suite result: FAILED. 1 passed; 1 failed; 0 skipped; [ELAPSED]
3128+
3129+
Ran 1 test suite [ELAPSED]: 1 tests passed, 1 failed, 0 skipped (2 total tests)
3130+
3131+
Failing tests:
3132+
Encountered 1 failing test in test/SuppressTracesTest.t.sol:SuppressTracesTest
3133+
[FAIL: assertion failed: 1 != 100] test_increment_failure() ([GAS])
3134+
3135+
Encountered a total of 1 failing tests, 1 tests succeeded
3136+
3137+
"#]]);
3138+
});

0 commit comments

Comments
 (0)