Skip to content

Commit

Permalink
Extract integration helpers to separate package (#912)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #911 

## Introduced changes

<!-- A brief description of the changes -->

- Moved helper code from integration tests to `utils` crate within
`forge` so it can be reused for benchmarks.

## Breaking changes

<!-- List of all breaking changes, if applicable -->

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
cptartur authored Oct 30, 2023
1 parent 230f916 commit da085bd
Show file tree
Hide file tree
Showing 30 changed files with 139 additions and 78 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ name = "forge"
[[bin]]
name = "snforge"
path = "src/main.rs"

[dev-dependencies]
test_utils = { path = "test_utils" }
27 changes: 27 additions & 0 deletions crates/forge/test_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "test_utils"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow.workspace = true
assert_fs.workspace = true
camino.workspace = true
indoc.workspace = true
scarb-metadata = { git = "https://github.com/software-mansion/scarb.git", rev = "58cc88e" }
cairo-lang-runner.workspace = true
cairo-lang-casm.workspace = true
cairo-lang-sierra-to-casm.workspace = true
cairo-lang-sierra.workspace = true
cairo-lang-utils.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-filesystem.workspace = true
serde_json.workspace = true
serde.workspace = true
tempfile.workspace = true
tokio.workspace = true
forge = { path = "../../forge" }
test-collector = { path = "../../test-collector" }
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use indoc::indoc;
use scarb_metadata::MetadataCommand;

#[allow(clippy::module_name_repetitions)]
#[must_use]
pub fn corelib_path() -> Utf8PathBuf {
// create an empty scarb project to extract corelib location from metadata
let dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -34,6 +35,7 @@ pub fn corelib_path() -> Utf8PathBuf {
corelib_for_package(&scarb_metadata, package).unwrap()
}

#[must_use]
pub fn predeployed_contracts() -> TempDir {
let predeployed_contracts = TempDir::new().unwrap();
predeployed_contracts
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Contract {
}

impl Contract {
#[must_use]
pub fn new(name: &str, code: &str) -> Self {
Self {
name: name.to_string(),
Expand Down Expand Up @@ -109,6 +110,7 @@ impl<'a> TestCase {
self.enviroment_variables.insert(key.into(), value.into());
}

#[must_use]
pub fn env(&self) -> &HashMap<String, String> {
&self.enviroment_variables
}
Expand All @@ -118,6 +120,7 @@ impl<'a> TestCase {
.map_err(|_| anyhow!("Failed to convert TestCase path to Utf8PathBuf"))
}

#[must_use]
pub fn linked_libraries(&self) -> Vec<LinkedLibrary> {
let snforge_std_path = PathBuf::from_str("../../snforge_std")
.unwrap()
Expand Down Expand Up @@ -151,6 +154,7 @@ impl<'a> TestCase {
.collect()
}

#[must_use]
pub fn find_test_result(results: &[TestCrateSummary]) -> &TestCrateSummary {
results
.iter()
Expand All @@ -162,11 +166,11 @@ impl<'a> TestCase {
#[macro_export]
macro_rules! test_case {
( $test_code:expr ) => ({
use $crate::integration::common::runner::TestCase;
use $crate::runner::TestCase;
TestCase::from($test_code, vec![]).unwrap()
});
( $test_code:expr, $( $contract:expr ),*) => ({
use $crate::integration::common::runner::TestCase;
use $crate::runner::TestCase;

let contracts = vec![$($contract,)*];
TestCase::from($test_code, contracts).unwrap()
Expand All @@ -177,7 +181,7 @@ macro_rules! test_case {
macro_rules! assert_passed {
($result:expr) => {{
use forge::test_case_summary::TestCaseSummary;
use $crate::integration::common::runner::TestCase;
use $crate::runner::TestCase;

let result = TestCase::find_test_result(&$result);
assert!(
Expand All @@ -199,7 +203,7 @@ macro_rules! assert_failed {
($result:expr) => {{
use forge::test_case_summary::TestCaseSummary;

use $crate::integration::common::runner::TestCase;
use $crate::runner::TestCase;

let result = TestCase::find_test_result(&$result);
assert!(
Expand All @@ -221,7 +225,7 @@ macro_rules! assert_case_output_contains {
($result:expr, $test_case_name:expr, $asserted_msg:expr) => {{
use forge::test_case_summary::TestCaseSummary;

use $crate::integration::common::runner::TestCase;
use $crate::runner::TestCase;

let test_case_name = $test_case_name;
let test_name_suffix = format!("::{test_case_name}");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use crate::integration::common::corelib::{corelib_path, predeployed_contracts};
use crate::integration::common::runner::TestCase;
use crate::corelib::{corelib_path, predeployed_contracts};
use crate::runner::TestCase;
use camino::Utf8PathBuf;

use forge::{run, CancellationTokens, RunnerConfig, RunnerParams, TestCrateSummary};
Expand All @@ -10,6 +10,7 @@ use std::path::PathBuf;
use tempfile::tempdir;
use tokio::runtime::Runtime;

#[must_use]
pub fn run_test_case(test: &TestCase) -> Vec<TestCrateSummary> {
let rt = Runtime::new().expect("Could not instantiate Runtime");

Expand Down
4 changes: 2 additions & 2 deletions crates/forge/tests/integration/cheat_fork.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::formatdoc;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

static CHEATNET_RPC_URL: &str = "http://188.34.188.184:9545/rpc/v0.4";

Expand Down
6 changes: 3 additions & 3 deletions crates/forge/tests/integration/declare.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn simple_declare() {
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/tests/integration/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn error_handling() {
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/tests/integration/deploy_at.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_case_output_contains, assert_failed, assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_case_output_contains, assert_failed, assert_passed, test_case};

#[test]
fn deploy_at_correct_address() {
Expand Down
7 changes: 3 additions & 4 deletions crates/forge/tests/integration/dispatchers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_case_output_contains, assert_failed, assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use std::string::ToString;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_case_output_contains, assert_failed, assert_passed, test_case};

#[test]
fn simple_call_and_invoke() {
Expand Down
4 changes: 2 additions & 2 deletions crates/forge/tests/integration/env.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_case_output_contains, assert_failed, assert_passed, test_case};
use cairo_felt::Felt252;
use indoc::indoc;
use num_bigint::BigUint;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_case_output_contains, assert_failed, assert_passed, test_case};

#[test]
fn read_short_string() {
Expand Down
4 changes: 2 additions & 2 deletions crates/forge/tests/integration/fuzzing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn fuzzed_argument() {
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/tests/integration/get_class_hash.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn get_class_hash() {
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/tests/integration/l1_handler_executor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn l1_handler_executor() {
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/tests/integration/mock_call.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn mock_call_simple() {
Expand Down
1 change: 0 additions & 1 deletion crates/forge/tests/integration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod cheat_fork;
pub(crate) mod common;
mod declare;
mod deploy;
mod deploy_at;
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/tests/integration/prank.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn prank() {
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/tests/integration/precalculate_address.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn precalculate_address() {
Expand Down
4 changes: 2 additions & 2 deletions crates/forge/tests/integration/pure_cairo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_failed, assert_passed, test_case};
use indoc::indoc;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_failed, assert_passed, test_case};

#[test]
fn simple() {
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/tests/integration/roll.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::integration::common::runner::Contract;
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use std::path::Path;
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn roll() {
Expand Down
21 changes: 11 additions & 10 deletions crates/forge/tests/integration/setup_fork.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_case_output_contains, assert_failed, assert_passed, test_case};
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;

use crate::integration::common::corelib::{corelib_path, predeployed_contracts};
use crate::integration::common::runner::Contract;
use camino::Utf8PathBuf;
use forge::scarb::config::{ForgeConfig, ForkTarget};
use forge::{run, CancellationTokens, RunnerConfig, RunnerParams};
use indoc::formatdoc;
use starknet::core::types::BlockId;
use starknet::core::types::BlockTag::Latest;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use tempfile::tempdir;
use test_collector::RawForkParams;
use tokio::runtime::Runtime;

use forge::scarb::config::{ForgeConfig, ForkTarget};
use forge::{run, CancellationTokens, RunnerConfig, RunnerParams};
use test_collector::RawForkParams;
use test_utils::corelib::{corelib_path, predeployed_contracts};
use test_utils::runner::Contract;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_case_output_contains, assert_failed, assert_passed, test_case};

static CHEATNET_RPC_URL: &str = "http://188.34.188.184:9545/rpc/v0.4";

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/forge/tests/integration/should_panic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn should_panic() {
Expand Down
4 changes: 2 additions & 2 deletions crates/forge/tests/integration/signing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::integration::common::running_tests::run_test_case;
use crate::{assert_passed, test_case};
use indoc::indoc;
use test_utils::running_tests::run_test_case;
use test_utils::{assert_passed, test_case};

#[test]
fn simple_signing_flow() {
Expand Down
Loading

0 comments on commit da085bd

Please sign in to comment.