Skip to content

Commit 1b115d8

Browse files
authored
chore: set seed for flaky tests, use etherscan api key (#12255)
* chore: set seed for flaky tests * env var * fix
1 parent 09edf8f commit 1b115d8

File tree

9 files changed

+31
-71
lines changed

9 files changed

+31
-71
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,5 @@ jobs:
112112
SVM_TARGET_PLATFORM: ${{ matrix.svm_target_platform }}
113113
HTTP_ARCHIVE_URLS: ${{ secrets.HTTP_ARCHIVE_URLS }}
114114
WS_ARCHIVE_URLS: ${{ secrets.WS_ARCHIVE_URLS }}
115+
ETHERSCAN_KEY: ${{ secrets.ETHERSCAN_API_KEY }}
115116
run: cargo nextest run ${{ matrix.flags }}

crates/cast/tests/cli/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,9 +2887,9 @@ contract CounterInExternalLibScript is Script {
28872887
.unwrap()
28882888
.tx_hash();
28892889

2890-
// Cache project selectors.
2890+
// Build and cache project selectors.
2891+
cmd.forge_fuse().args(["build"]).assert_success();
28912892
cmd.forge_fuse().args(["selectors", "cache"]).assert_success();
2892-
28932893
// Assert cast with local artifacts can decode external lib signature.
28942894
cmd.cast_fuse()
28952895
.args(["run", format!("{tx_hash}").as_str(), "--rpc-url", &handle.http_endpoint()])

crates/cast/tests/cli/selectors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ contract ContractWithCustomError {
186186
}
187187
"#,
188188
);
189-
// Store selectors in local cache.
189+
// Build and cache project selectors.
190+
cmd.forge_fuse().args(["build"]).assert_success();
190191
cmd.forge_fuse().args(["selectors", "cache"]).assert_success();
191192

192193
// Assert cast can decode custom error with local cache.

crates/forge/tests/cli/failure_assertions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ forgetest!(expect_emit_tests_should_fail, |prj, cmd| {
199199
prj.add_source("ExpectEmitFailures.sol", expect_emit_failure_tests);
200200

201201
cmd.forge_fuse().arg("build").assert_success();
202+
cmd.forge_fuse().args(["selectors", "cache"]).assert_success();
202203

203204
cmd.forge_fuse().args(["test", "--mc", "ExpectEmitFailureTest"]).assert_failure().stdout_eq(str![[r#"No files changed, compilation skipped
204205
...

crates/forge/tests/cli/test_cmd/fuzz.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)
117117

118118
forgetest_init!(test_fuzz_fail_on_revert, |prj, cmd| {
119119
prj.wipe_contracts();
120-
prj.update_config(|config| config.fuzz.fail_on_revert = false);
120+
prj.update_config(|config| {
121+
config.fuzz.fail_on_revert = false;
122+
config.fuzz.seed = Some(U256::from(100u32));
123+
});
121124
prj.add_source(
122125
"Counter.sol",
123126
r#"
@@ -672,6 +675,9 @@ Ran 1 test suite [ELAPSED]: 3 tests passed, 0 failed, 0 skipped (3 total tests)
672675
// random values (instead edge cases) are generated if no fixtures defined
673676
forgetest_init!(fuzz_uint, |prj, cmd| {
674677
prj.wipe_contracts();
678+
prj.update_config(|config| {
679+
config.fuzz.seed = Some(U256::from(100u32));
680+
});
675681
prj.add_test(
676682
"FuzzUint.t.sol",
677683
r#"

crates/forge/tests/cli/test_cmd/invariant/common.rs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ forgetest!(invariant_calldata_dictionary, |prj, cmd| {
188188
prj.wipe_contracts();
189189
prj.insert_utils();
190190
prj.update_config(|config| {
191+
config.fuzz.seed = Some(U256::from(1));
191192
config.invariant.depth = 10;
192193
});
193194

@@ -293,7 +294,7 @@ Ran 1 test for test/InvariantCalldataDictionary.t.sol:InvariantCalldataDictionar
293294
[SEQUENCE]
294295
invariant_owner_never_changes() ([RUNS])
295296
296-
[STATS]
297+
...
297298
298299
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; [ELAPSED]
299300
@@ -1056,35 +1057,17 @@ contract InvariantRollForkStateTest is Test {
10561057

10571058
assert_invariant(cmd.args(["test", "-j1"])).failure().stdout_eq(str![[r#"
10581059
...
1059-
Ran 1 test for test/InvariantRollFork.t.sol:InvariantRollForkStateTest
1060-
[FAIL: wrong supply]
1061-
[SEQUENCE]
1062-
invariant_fork_handler_state() ([RUNS])
1063-
1064-
[STATS]
1065-
1066-
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; [ELAPSED]
1067-
1068-
Ran 1 test for test/InvariantRollFork.t.sol:InvariantRollForkBlockTest
1069-
[FAIL: too many blocks mined]
1070-
[SEQUENCE]
1071-
invariant_fork_handler_block() ([RUNS])
1072-
1073-
[STATS]
1074-
1075-
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; [ELAPSED]
1076-
10771060
Ran 2 test suites [ELAPSED]: 0 tests passed, 2 failed, 0 skipped (2 total tests)
10781061
10791062
Failing tests:
10801063
Encountered 1 failing test in test/InvariantRollFork.t.sol:InvariantRollForkBlockTest
10811064
[FAIL: too many blocks mined]
1082-
[SEQUENCE]
1065+
...
10831066
invariant_fork_handler_block() ([RUNS])
10841067
10851068
Encountered 1 failing test in test/InvariantRollFork.t.sol:InvariantRollForkStateTest
10861069
[FAIL: wrong supply]
1087-
[SEQUENCE]
1070+
...
10881071
invariant_fork_handler_state() ([RUNS])
10891072
10901073
Encountered a total of 2 failing tests, 0 tests succeeded
@@ -1098,6 +1081,7 @@ forgetest_init!(invariant_scrape_values, |prj, cmd| {
10981081
prj.wipe_contracts();
10991082
prj.update_config(|config| {
11001083
config.invariant.depth = 10;
1084+
config.fuzz.seed = Some(U256::from(100u32));
11011085
});
11021086

11031087
prj.add_test(
@@ -1173,24 +1157,6 @@ contract FindFromLogValueTest is Test {
11731157

11741158
assert_invariant(cmd.args(["test", "-j1"])).failure().stdout_eq(str![[r#"
11751159
...
1176-
Ran 1 test for test/InvariantScrapeValues.t.sol:FindFromReturnValueTest
1177-
[FAIL: value from return found]
1178-
[SEQUENCE]
1179-
invariant_value_not_found() ([RUNS])
1180-
1181-
[STATS]
1182-
1183-
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; [ELAPSED]
1184-
1185-
Ran 1 test for test/InvariantScrapeValues.t.sol:FindFromLogValueTest
1186-
[FAIL: value from logs found]
1187-
[SEQUENCE]
1188-
invariant_value_not_found() ([RUNS])
1189-
1190-
[STATS]
1191-
1192-
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; [ELAPSED]
1193-
11941160
Ran 2 test suites [ELAPSED]: 0 tests passed, 2 failed, 0 skipped (2 total tests)
11951161
11961162
Failing tests:

crates/forge/tests/cli/test_cmd/invariant/target.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -842,33 +842,12 @@ contract DynamicTargetContract is Test {
842842

843843
assert_invariant(cmd.args(["test", "-j1"])).failure().stdout_eq(str![[r#"
844844
...
845-
Ran 1 test for test/FuzzedTargetContracts.t.sol:ExplicitTargetContract
846845
[PASS] invariant_explicit_target() ([RUNS])
847-
848-
[STATS]
849-
850-
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
851-
852-
Ran 1 test for test/FuzzedTargetContracts.t.sol:DynamicTargetContract
853-
[FAIL: wrong target selector called]
854-
[SEQUENCE]
855-
invariant_dynamic_targets() ([RUNS])
856-
857-
[STATS]
858-
859-
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; [ELAPSED]
860-
861-
Ran 2 test suites [ELAPSED]: 1 tests passed, 1 failed, 0 skipped (2 total tests)
862-
863-
Failing tests:
864-
Encountered 1 failing test in test/FuzzedTargetContracts.t.sol:DynamicTargetContract
846+
...
865847
[FAIL: wrong target selector called]
866848
[SEQUENCE]
867849
invariant_dynamic_targets() ([RUNS])
868-
869-
Encountered a total of 1 failing tests, 1 tests succeeded
870-
871-
Tip: Run `forge test --rerun` to retry only the 1 failed test
850+
...
872851
873852
"#]]);
874853
});

crates/forge/tests/cli/test_cmd/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4055,7 +4055,7 @@ contract ContractWithCustomError {
40554055
"#,
40564056
);
40574057
// Build and cache project selectors.
4058-
cmd.forge_fuse().args(["build"]).assert_success();
4058+
cmd.forge_fuse().args(["build", "--force"]).assert_success();
40594059

40604060
// Assert cast can decode custom error with local cache.
40614061
cmd.cast_fuse()

crates/test-utils/src/rpc.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ use foundry_config::{
77
RpcEndpointUrl, RpcEndpoints,
88
};
99
use rand::seq::SliceRandom;
10-
use std::sync::{
11-
LazyLock,
12-
atomic::{AtomicUsize, Ordering},
10+
use std::{
11+
env,
12+
sync::{
13+
LazyLock,
14+
atomic::{AtomicUsize, Ordering},
15+
},
1316
};
1417

1518
macro_rules! shuffled_list {
@@ -153,7 +156,10 @@ fn next_archive_url(is_ws: bool) -> String {
153156

154157
/// Returns the next etherscan api key.
155158
pub fn next_etherscan_api_key() -> String {
156-
let key = ETHERSCAN_KEYS.next().to_string();
159+
let mut key = env::var("ETHERSCAN_KEY").unwrap_or_default();
160+
if key.is_empty() {
161+
key = ETHERSCAN_KEYS.next().to_string();
162+
}
157163
test_debug!("next_etherscan_api_key() = {}...", &key[..6]);
158164
key
159165
}

0 commit comments

Comments
 (0)