Skip to content

Commit

Permalink
[wip] chore: refactor tests layout
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Mar 16, 2024
1 parent 42da942 commit fdcae85
Show file tree
Hide file tree
Showing 187 changed files with 395 additions and 340 deletions.
8 changes: 4 additions & 4 deletions crates/forge/tests/it/cheats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use crate::{
config::*,
test_helpers::{PROJECT, RE_PATH_SEPARATOR},
test_helpers::{RE_PATH_SEPARATOR, TEST_DATA_DEFAULT},
};
use foundry_config::{fs_permissions::PathPermission, Config, FsPermissions};
use foundry_config::{fs_permissions::PathPermission, FsPermissions};
use foundry_test_utils::Filter;

/// Executes all cheat code tests but not fork cheat codes
Expand All @@ -18,9 +18,9 @@ async fn test_cheats_local() {
filter = filter.exclude_tests("(Ffi|File|Line|Root)");
}

let mut config = Config::with_root(PROJECT.root());
let mut config = TEST_DATA_DEFAULT.config.clone();
config.fs_permissions = FsPermissions::new(vec![PathPermission::read_write("./")]);
let runner = runner_with_config(config);
let runner = runner_with_config(&TEST_DATA_DEFAULT, config);

TestConfig::with_filter(runner, filter).run().await;
}
45 changes: 21 additions & 24 deletions crates/forge/tests/it/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Test config.

use crate::test_helpers::{COMPILED, EVM_OPTS, PROJECT, TEST_OPTS};
use crate::test_helpers::ForgeTestData;
use forge::{
result::{SuiteResult, TestStatus},
MultiContractRunner, MultiContractRunnerBuilder,
Expand Down Expand Up @@ -31,10 +31,6 @@ impl TestConfig {
Self::with_filter(runner, Filter::matches_all())
}

pub fn filter(filter: Filter) -> Self {
Self::with_filter(runner(), filter)
}

pub fn with_filter(runner: MultiContractRunner, filter: Filter) -> Self {
init_tracing();
Self { runner, should_fail: false, filter }
Expand Down Expand Up @@ -119,58 +115,59 @@ pub fn manifest_root() -> &'static Path {
}

/// Builds a base runner
pub fn base_runner() -> MultiContractRunnerBuilder {
pub fn base_runner(test_data: &ForgeTestData) -> MultiContractRunnerBuilder {
init_tracing();
MultiContractRunnerBuilder::default()
.sender(EVM_OPTS.sender)
.with_test_options(TEST_OPTS.clone())
.sender(test_data.evm_opts.sender)
.with_test_options(test_data.test_opts.clone())
}

/// Builds a non-tracing runner
pub fn runner() -> MultiContractRunner {
let mut config = Config::with_root(PROJECT.root());
pub fn runner(test_data: &ForgeTestData) -> MultiContractRunner {
let mut config = test_data.config.clone();
config.fs_permissions = FsPermissions::new(vec![PathPermission::read_write(manifest_root())]);
runner_with_config(config)
runner_with_config(test_data, config)
}

/// Builds a non-tracing runner
pub fn runner_with_config(mut config: Config) -> MultiContractRunner {
pub fn runner_with_config(test_data: &ForgeTestData, mut config: Config) -> MultiContractRunner {
config.rpc_endpoints = rpc_endpoints();
config.allow_paths.push(manifest_root().to_path_buf());

let root = &PROJECT.paths.root;
let opts = &*EVM_OPTS;
let root = test_data.project.root();
let opts = test_data.evm_opts.clone();
let env = opts.local_evm_env();
let output = COMPILED.clone();
base_runner()
let output = test_data.output.clone();
base_runner(test_data)
.with_cheats_config(CheatsConfig::new(&config, opts.clone(), None))
.sender(config.sender)
.with_test_options(test_data.test_opts.clone())
.build(root, output, env, opts.clone())
.unwrap()
}

/// Builds a tracing runner
pub fn tracing_runner() -> MultiContractRunner {
let mut opts = EVM_OPTS.clone();
pub fn tracing_runner(test_data: &ForgeTestData) -> MultiContractRunner {
let mut opts = test_data.evm_opts.clone();
opts.verbosity = 5;
base_runner()
.build(&PROJECT.paths.root, (*COMPILED).clone(), EVM_OPTS.local_evm_env(), opts)
base_runner(test_data)
.build(test_data.project.root(), test_data.output.clone(), opts.local_evm_env(), opts)
.unwrap()
}

// Builds a runner that runs against forked state
pub async fn forked_runner(rpc: &str) -> MultiContractRunner {
let mut opts = EVM_OPTS.clone();
pub async fn forked_runner(test_data: &ForgeTestData, rpc: &str) -> MultiContractRunner {
let mut opts = test_data.evm_opts.clone();

opts.env.chain_id = None; // clear chain id so the correct one gets fetched from the RPC
opts.fork_url = Some(rpc.to_string());

let env = opts.evm_env().await.expect("Could not instantiate fork environment");
let fork = opts.get_fork(&Default::default(), env.clone());

base_runner()
base_runner(test_data)
.with_fork(fork)
.build(&PROJECT.paths.root, (*COMPILED).clone(), env, opts)
.build(test_data.project.root(), test_data.output.clone(), env, opts)
.unwrap()
}

Expand Down
14 changes: 7 additions & 7 deletions crates/forge/tests/it/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Forge tests for core functionality.

use crate::config::*;
use crate::{config::*, test_helpers::TEST_DATA_DEFAULT};
use forge::result::SuiteResult;
use foundry_evm::traces::TraceKind;
use foundry_test_utils::Filter;
Expand All @@ -9,7 +9,7 @@ use std::{collections::BTreeMap, env};
#[tokio::test(flavor = "multi_thread")]
async fn test_core() {
let filter = Filter::new(".*", ".*", ".*core");
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
let results = runner.test_collect(&filter);

assert_multiple(
Expand Down Expand Up @@ -79,7 +79,7 @@ async fn test_core() {
#[tokio::test(flavor = "multi_thread")]
async fn test_linking() {
let filter = Filter::new(".*", ".*", ".*linking");
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
let results = runner.test_collect(&filter);

assert_multiple(
Expand Down Expand Up @@ -113,7 +113,7 @@ async fn test_linking() {
#[tokio::test(flavor = "multi_thread")]
async fn test_logs() {
let filter = Filter::new(".*", ".*", ".*logs");
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
let results = runner.test_collect(&filter);

assert_multiple(
Expand Down Expand Up @@ -678,7 +678,7 @@ async fn test_env_vars() {
env::remove_var(env_var_key);

let filter = Filter::new("testSetEnv", ".*", ".*");
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
let _ = runner.test_collect(&filter);

assert_eq!(env::var(env_var_key).unwrap(), env_var_val);
Expand All @@ -687,7 +687,7 @@ async fn test_env_vars() {
#[tokio::test(flavor = "multi_thread")]
async fn test_doesnt_run_abstract_contract() {
let filter = Filter::new(".*", ".*", ".*Abstract.t.sol".to_string().as_str());
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
let results = runner.test_collect(&filter);
assert!(!results.contains_key("core/Abstract.t.sol:AbstractTestBase"));
assert!(results.contains_key("core/Abstract.t.sol:AbstractTest"));
Expand All @@ -696,7 +696,7 @@ async fn test_doesnt_run_abstract_contract() {
#[tokio::test(flavor = "multi_thread")]
async fn test_trace() {
let filter = Filter::new(".*", ".*", ".*trace");
let mut runner = tracing_runner();
let mut runner = tracing_runner(&TEST_DATA_DEFAULT);
let suite_result = runner.test_collect(&filter);

// TODO: This trace test is very basic - it is probably a good candidate for snapshot
Expand Down
28 changes: 15 additions & 13 deletions crates/forge/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use crate::{
config::*,
test_helpers::{PROJECT, RE_PATH_SEPARATOR},
test_helpers::{RE_PATH_SEPARATOR, TEST_DATA_DEFAULT},
};
use forge::result::SuiteResult;
use foundry_config::{fs_permissions::PathPermission, Config, FsPermissions};
use foundry_config::{fs_permissions::PathPermission, FsPermissions};
use foundry_test_utils::Filter;

/// Executes reverting fork test
Expand All @@ -16,7 +16,7 @@ async fn test_cheats_fork_revert() {
".*",
&format!(".*cheats{RE_PATH_SEPARATOR}Fork"),
);
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
let suite_result = runner.test_collect(&filter);
assert_eq!(suite_result.len(), 1);

Expand All @@ -33,9 +33,9 @@ async fn test_cheats_fork_revert() {
/// Executes all non-reverting fork cheatcodes
#[tokio::test(flavor = "multi_thread")]
async fn test_cheats_fork() {
let mut config = Config::with_root(PROJECT.root());
let mut config = TEST_DATA_DEFAULT.config.clone();
config.fs_permissions = FsPermissions::new(vec![PathPermission::read("./fixtures")]);
let runner = runner_with_config(config);
let runner = runner_with_config(&TEST_DATA_DEFAULT, config);
let filter = Filter::new(".*", ".*", &format!(".*cheats{RE_PATH_SEPARATOR}Fork"))
.exclude_tests(".*Revert");
TestConfig::with_filter(runner, filter).run().await;
Expand All @@ -44,9 +44,9 @@ async fn test_cheats_fork() {
/// Executes eth_getLogs cheatcode
#[tokio::test(flavor = "multi_thread")]
async fn test_get_logs_fork() {
let mut config = Config::with_root(PROJECT.root());
let mut config = TEST_DATA_DEFAULT.config.clone();
config.fs_permissions = FsPermissions::new(vec![PathPermission::read("./fixtures")]);
let runner = runner_with_config(config);
let runner = runner_with_config(&TEST_DATA_DEFAULT, config);
let filter = Filter::new("testEthGetLogs", ".*", &format!(".*cheats{RE_PATH_SEPARATOR}Fork"))
.exclude_tests(".*Revert");
TestConfig::with_filter(runner, filter).run().await;
Expand All @@ -55,9 +55,9 @@ async fn test_get_logs_fork() {
/// Executes rpc cheatcode
#[tokio::test(flavor = "multi_thread")]
async fn test_rpc_fork() {
let mut config = Config::with_root(PROJECT.root());
let mut config = TEST_DATA_DEFAULT.config.clone();
config.fs_permissions = FsPermissions::new(vec![PathPermission::read("./fixtures")]);
let runner = runner_with_config(config);
let runner = runner_with_config(&TEST_DATA_DEFAULT, config);
let filter = Filter::new("testRpc", ".*", &format!(".*cheats{RE_PATH_SEPARATOR}Fork"))
.exclude_tests(".*Revert");
TestConfig::with_filter(runner, filter).run().await;
Expand All @@ -67,7 +67,7 @@ async fn test_rpc_fork() {
#[tokio::test(flavor = "multi_thread")]
async fn test_launch_fork() {
let rpc_url = foundry_common::rpc::next_http_archive_rpc_endpoint();
let runner = forked_runner(&rpc_url).await;
let runner = forked_runner(&TEST_DATA_DEFAULT, &rpc_url).await;
let filter = Filter::new(".*", ".*", &format!(".*fork{RE_PATH_SEPARATOR}Launch"));
TestConfig::with_filter(runner, filter).run().await;
}
Expand All @@ -76,21 +76,23 @@ async fn test_launch_fork() {
#[tokio::test(flavor = "multi_thread")]
async fn test_launch_fork_ws() {
let rpc_url = foundry_common::rpc::next_ws_archive_rpc_endpoint();
let runner = forked_runner(&rpc_url).await;
let runner = forked_runner(&TEST_DATA_DEFAULT, &rpc_url).await;
let filter = Filter::new(".*", ".*", &format!(".*fork{RE_PATH_SEPARATOR}Launch"));
TestConfig::with_filter(runner, filter).run().await;
}

/// Tests that we can transact transactions in forking mode
#[tokio::test(flavor = "multi_thread")]
async fn test_transact_fork() {
let runner = runner(&TEST_DATA_DEFAULT);
let filter = Filter::new(".*", ".*", &format!(".*fork{RE_PATH_SEPARATOR}Transact"));
TestConfig::filter(filter).run().await;
TestConfig::with_filter(runner, filter).run().await;
}

/// Tests that we can create the same fork (provider,block) concurretnly in different tests
#[tokio::test(flavor = "multi_thread")]
async fn test_create_same_fork() {
let runner = runner(&TEST_DATA_DEFAULT);
let filter = Filter::new(".*", ".*", &format!(".*fork{RE_PATH_SEPARATOR}ForkSame"));
TestConfig::filter(filter).run().await;
TestConfig::with_filter(runner, filter).run().await;
}
12 changes: 6 additions & 6 deletions crates/forge/tests/it/fs.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
//! Filesystem tests.

use crate::{config::*, test_helpers::PROJECT};
use foundry_config::{fs_permissions::PathPermission, Config, FsPermissions};
use crate::{config::*, test_helpers::TEST_DATA_DEFAULT};
use foundry_config::{fs_permissions::PathPermission, FsPermissions};
use foundry_test_utils::Filter;

#[tokio::test(flavor = "multi_thread")]
async fn test_fs_disabled() {
let mut config = Config::with_root(PROJECT.root());
let mut config = TEST_DATA_DEFAULT.config.clone();
config.fs_permissions = FsPermissions::new(vec![PathPermission::none("./")]);
let runner = runner_with_config(config);
let runner = runner_with_config(&TEST_DATA_DEFAULT, config);
let filter = Filter::new(".*", ".*", ".*fs/Disabled");
TestConfig::with_filter(runner, filter).run().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_fs_default() {
let mut config = Config::with_root(PROJECT.root());
let mut config = TEST_DATA_DEFAULT.config.clone();
config.fs_permissions = FsPermissions::new(vec![PathPermission::read("./fixtures")]);
let runner = runner_with_config(config);
let runner = runner_with_config(&TEST_DATA_DEFAULT, config);
let filter = Filter::new(".*", ".*", ".*fs/Default");
TestConfig::with_filter(runner, filter).run().await;
}
21 changes: 10 additions & 11 deletions crates/forge/tests/it/fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
//! Fuzz tests.

use std::collections::BTreeMap;

use crate::{config::*, test_helpers::TEST_DATA_DEFAULT};
use alloy_primitives::{Bytes, U256};
use forge::fuzz::CounterExample;

use forge::result::{SuiteResult, TestStatus};
use forge::{
fuzz::CounterExample,
result::{SuiteResult, TestStatus},
};
use foundry_test_utils::Filter;

use crate::config::*;
use std::collections::BTreeMap;

#[tokio::test(flavor = "multi_thread")]
async fn test_fuzz() {
let filter = Filter::new(".*", ".*", ".*fuzz/")
.exclude_tests(r"invariantCounter|testIncrement\(address\)|testNeedle\(uint256\)|testSuccessChecker\(uint256\)|testSuccessChecker2\(int256\)|testSuccessChecker3\(uint32\)")
.exclude_paths("invariant");
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
let suite_result = runner.test_collect(&filter);

assert!(!suite_result.is_empty());
Expand Down Expand Up @@ -52,7 +51,7 @@ async fn test_successful_fuzz_cases() {
let filter = Filter::new(".*", ".*", ".*fuzz/FuzzPositive")
.exclude_tests(r"invariantCounter|testIncrement\(address\)|testNeedle\(uint256\)")
.exclude_paths("invariant");
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
let suite_result = runner.test_collect(&filter);

assert!(!suite_result.is_empty());
Expand Down Expand Up @@ -82,7 +81,7 @@ async fn test_successful_fuzz_cases() {
#[ignore]
async fn test_fuzz_collection() {
let filter = Filter::new(".*", ".*", ".*fuzz/FuzzCollection.t.sol");
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
runner.test_options.invariant.depth = 100;
runner.test_options.invariant.runs = 1000;
runner.test_options.fuzz.runs = 1000;
Expand Down Expand Up @@ -111,7 +110,7 @@ async fn test_fuzz_collection() {
#[tokio::test(flavor = "multi_thread")]
async fn test_persist_fuzz_failure() {
let filter = Filter::new(".*", ".*", ".*fuzz/FuzzFailurePersist.t.sol");
let mut runner = runner();
let mut runner = runner(&TEST_DATA_DEFAULT);
runner.test_options.fuzz.runs = 1000;

macro_rules! get_failure_result {
Expand Down
Loading

0 comments on commit fdcae85

Please sign in to comment.