Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cheatcodes): skip test suite in setup #9532

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions crates/forge/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,10 @@ impl TestResult {
Self { status: TestStatus::Failure, reason: Some(reason), ..Default::default() }
}

/// Creates a failed test setup result.
pub fn setup_fail(setup: TestSetup) -> Self {
/// Creates a test setup result.
pub fn setup_result(setup: TestSetup) -> Self {
Self {
status: TestStatus::Failure,
status: if setup.skipped { TestStatus::Skipped } else { TestStatus::Failure },
reason: setup.reason,
logs: setup.logs,
traces: setup.traces,
Expand Down Expand Up @@ -755,13 +755,19 @@ pub struct TestSetup {

/// The reason the setup failed, if it did.
pub reason: Option<String>,
/// Whether setup and entire test suite is skipped.
pub skipped: bool,
}

impl TestSetup {
pub fn failed(reason: String) -> Self {
Self { reason: Some(reason), ..Default::default() }
}

pub fn skipped(reason: String) -> Self {
Self { reason: Some(reason), skipped: true, ..Default::default() }
}

pub fn extend(&mut self, raw: RawCallResult, trace_kind: TraceKind) {
self.logs.extend(raw.logs);
self.labels.extend(raw.labels);
Expand Down
13 changes: 8 additions & 5 deletions crates/forge/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ impl<'a> ContractRunner<'a> {
/// Deploys the test contract inside the runner from the sending account, and optionally runs
/// the `setUp` function on the test contract.
pub fn setup(&mut self, call_setup: bool) -> TestSetup {
match self._setup(call_setup) {
Ok(setup) => setup,
Err(err) => TestSetup::failed(err.to_string()),
}
self._setup(call_setup).unwrap_or_else(|err| {
if err.to_string().contains("skipped") {
TestSetup::skipped(err.to_string())
} else {
TestSetup::failed(err.to_string())
}
})
}

fn _setup(&mut self, call_setup: bool) -> Result<TestSetup> {
Expand Down Expand Up @@ -333,7 +336,7 @@ impl<'a> ContractRunner<'a> {
// The setup failed, so we return a single test result for `setUp`
return SuiteResult::new(
start.elapsed(),
[("setUp()".to_string(), TestResult::setup_fail(setup))].into(),
[("setUp()".to_string(), TestResult::setup_result(setup))].into(),
warnings,
)
}
Expand Down
42 changes: 42 additions & 0 deletions crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,48 @@ Ran 1 test suite [ELAPSED]: 0 tests passed, 0 failed, 6 skipped (6 total tests)
"#]]);
});

forgetest_init!(skip_setup, |prj, cmd| {
prj.add_test(
"Counter.t.sol",
r#"
import "forge-std/Test.sol";

contract SkipCounterSetup is Test {

function setUp() public {
vm.skip(true, "skip counter test");
}

function test_require1() public pure {
require(1 > 2);
}

function test_require2() public pure {
require(1 > 2);
}

function test_require3() public pure {
require(1 > 2);
}
}
"#,
)
.unwrap();

cmd.args(["test", "--mc", "SkipCounterSetup"]).assert_success().stdout_eq(str![[r#"
[COMPILING_FILES] with [SOLC_VERSION]
[SOLC_VERSION] [ELAPSED]
Compiler run successful!

Ran 1 test for test/Counter.t.sol:SkipCounterSetup
[SKIP: skipped: skip counter test] setUp() ([GAS])
Suite result: ok. 0 passed; 0 failed; 1 skipped; [ELAPSED]

Ran 1 test suite [ELAPSED]: 0 tests passed, 0 failed, 1 skipped (1 total tests)

"#]]);
});

forgetest_init!(should_generate_junit_xml_report, |prj, cmd| {
prj.wipe_contracts();
prj.insert_ds_test();
Expand Down
Loading