Skip to content
Open
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
25 changes: 5 additions & 20 deletions crates/forge-runner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::coverage_api::run_coverage;
use crate::forge_config::{ExecutionDataToSave, ForgeConfig};
use crate::package_tests::with_config_resolved::TestCaseResolvedConfig;
use crate::package_tests::{TestCase, TestCaseDeprecated};
use crate::package_tests::TestCase;
use crate::running::{run_fuzz_test, run_test};
use crate::test_case_summary::TestCaseSummary;
use anyhow::Result;
Expand All @@ -13,7 +12,6 @@ use foundry_ui::UI;
use foundry_ui::components::warning::WarningMessage;
use futures::StreamExt;
use futures::stream::FuturesUnordered;
use package_tests::with_config_resolved::TestCaseWithResolvedConfig;
use profiler_api::run_profiler;
use rand::SeedableRng;
use rand::prelude::StdRng;
Expand Down Expand Up @@ -59,11 +57,6 @@ const BUILTINS: [&str; 11] = [
];

pub trait TestCaseFilter {
// TODO: Remove in next PRs
fn should_be_run<T>(&self, test_case: &TestCaseDeprecated<T>) -> bool
where
T: TestCaseIsIgnored;

fn should_run(&self, is_test_case_ignored: bool) -> bool;
}

Expand Down Expand Up @@ -117,24 +110,16 @@ pub fn maybe_generate_coverage(
#[must_use]
#[tracing::instrument(skip_all, level = "debug")]
pub fn run_for_test_case(
case: &Arc<TestCase>,
case: Arc<TestCase>,
casm_program: Arc<RawCasmProgram>,
forge_config: Arc<ForgeConfig>,
versioned_program_path: Arc<Utf8PathBuf>,
send: Sender<()>,
) -> JoinHandle<Result<AnyTestCaseSummary>> {
// TODO: Change all tests running functions to use `TestCase` in next PRs
let deprecated_test_case: TestCaseDeprecated<TestCaseResolvedConfig> = TestCaseDeprecated {
name: case.name.clone(),
test_details: case.test_details.clone(),
config: case.config.clone(),
};
let deprecated_test_case = Arc::new(deprecated_test_case);

if case.config.fuzzer_config.is_none() {
tokio::task::spawn(async move {
let res = run_test(
deprecated_test_case,
case,
casm_program,
forge_config,
versioned_program_path,
Expand All @@ -146,7 +131,7 @@ pub fn run_for_test_case(
} else {
tokio::task::spawn(async move {
let res = run_with_fuzzing(
deprecated_test_case,
case,
casm_program,
forge_config.clone(),
versioned_program_path,
Expand All @@ -160,7 +145,7 @@ pub fn run_for_test_case(

#[tracing::instrument(skip_all, level = "debug")]
fn run_with_fuzzing(
case: Arc<TestCaseWithResolvedConfig>,
case: Arc<TestCase>,
casm_program: Arc<RawCasmProgram>,
forge_config: Arc<ForgeConfig>,
versioned_program_path: Arc<Utf8PathBuf>,
Expand Down
91 changes: 0 additions & 91 deletions crates/forge-runner/src/package_tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::forge_config::ForgeTrackedResource;
use crate::package_tests::raw::TestTargetRaw;
use crate::package_tests::with_config::{TestCaseWithConfig, TestTargetWithConfig};
use crate::package_tests::with_config_resolved::{TestCaseResolvedConfig, sanitize_test_case_name};
use crate::running::config_run::run_config_pass;
use crate::running::hints_to_params;
use anyhow::{Result, anyhow};
use cairo_lang_sierra::extensions::NamedType;
Expand All @@ -29,7 +26,6 @@ use serde::Serialize;
use starknet_types_core::felt::Felt;
use std::collections::HashMap;
use std::sync::Arc;
use universal_sierra_compiler_api::compile_raw_sierra_at_path;
use universal_sierra_compiler_api::representation::RawCasmProgram;

pub mod raw;
Expand Down Expand Up @@ -129,93 +125,6 @@ impl TestDetails {
}
}

// TODO: Remove in next PRs
#[derive(Debug, Clone)]
pub struct TestTargetDeprecated<C> {
pub tests_location: TestTargetLocation,
pub sierra_program: ProgramArtifact,
pub sierra_program_path: Arc<Utf8PathBuf>,
pub casm_program: Arc<RawCasmProgram>,
pub test_cases: Vec<TestCaseDeprecated<C>>,
}

// TODO: Remove in next PRs
impl TestTargetDeprecated<TestCaseWithConfig> {
#[tracing::instrument(skip_all, level = "debug")]
pub fn from_raw_deprecated(
test_target_raw: TestTargetRaw,
tracked_resource: &ForgeTrackedResource,
) -> Result<TestTargetWithConfig> {
macro_rules! by_id {
($field:ident) => {{
let temp: HashMap<_, _> = test_target_raw
.sierra_program
.program
.$field
.iter()
.map(|f| (f.id.id, f))
.collect();

temp
}};
}
let funcs = by_id!(funcs);
let type_declarations = by_id!(type_declarations);

let casm_program = Arc::new(compile_raw_sierra_at_path(
test_target_raw.sierra_program_path.as_std_path(),
)?);

let sierra_program_registry =
ProgramRegistry::<CoreType, CoreLibfunc>::new(&test_target_raw.sierra_program.program)?;
let type_size_map = get_type_size_map(
&test_target_raw.sierra_program.program,
&sierra_program_registry,
)
.ok_or_else(|| anyhow!("can not get type size map"))?;

let default_executables = vec![];
let debug_info = test_target_raw.sierra_program.debug_info.clone();
let executables = debug_info
.as_ref()
.and_then(|info| info.executables.get("snforge_internal_test_executable"))
.unwrap_or(&default_executables);

let test_cases = executables
.par_iter()
.map(|case| -> Result<TestCaseWithConfig> {
let func = funcs[&case.id];

let test_details = TestDetails::build(func, &type_declarations, &type_size_map);

let raw_config = run_config_pass(&test_details, &casm_program, tracked_resource)?;

Ok(TestCaseWithConfig {
config: raw_config.into(),
name: case.debug_name.clone().unwrap().into(),
test_details,
})
})
.collect::<Result<_>>()?;

Ok(TestTargetWithConfig {
tests_location: test_target_raw.tests_location,
test_cases,
sierra_program: test_target_raw.sierra_program,
sierra_program_path: test_target_raw.sierra_program_path.into(),
casm_program,
})
}
}

// TODO: Remove in next PRs
#[derive(Debug, Clone, PartialEq)]
pub struct TestCaseDeprecated<C> {
pub test_details: TestDetails,
pub name: String,
pub config: C,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TestCase {
pub test_details: TestDetails,
Expand Down
5 changes: 0 additions & 5 deletions crates/forge-runner/src/package_tests/with_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::{TestCaseDeprecated, TestTargetDeprecated};
use crate::{
TestCaseIsIgnored,
expected_result::{ExpectedPanicValue, ExpectedTestResult},
Expand All @@ -9,10 +8,6 @@ use cheatnet::runtime_extensions::forge_config_extension::config::{
};
use conversions::serde::serialize::SerializeToFeltVec;

pub type TestTargetWithConfig = TestTargetDeprecated<TestCaseConfig>;

pub type TestCaseWithConfig = TestCaseDeprecated<TestCaseConfig>;

/// Test case with config that has not yet been resolved
/// see [`super::with_config_resolved::TestCaseResolvedConfig`] for more info
#[derive(Debug, Clone)]
Expand Down
25 changes: 1 addition & 24 deletions crates/forge-runner/src/package_tests/with_config_resolved.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
use super::{TestCaseDeprecated, TestTargetDeprecated};
use crate::{TestCaseIsIgnored, expected_result::ExpectedTestResult, package_tests::TestDetails};
use anyhow::Result;
use cairo_vm::types::program::Program;
use crate::{TestCaseIsIgnored, expected_result::ExpectedTestResult};
use cheatnet::runtime_extensions::forge_config_extension::config::{
RawAvailableResourceBoundsConfig, RawFuzzerConfig,
};
use starknet_api::block::BlockNumber;
use universal_sierra_compiler_api::representation::RawCasmProgram;
use url::Url;

pub type TestTargetWithResolvedConfig = TestTargetDeprecated<TestCaseResolvedConfig>;

pub type TestCaseWithResolvedConfig = TestCaseDeprecated<TestCaseResolvedConfig>;

#[must_use]
pub fn sanitize_test_case_name(name: &str) -> String {
// Test names generated by `#[test]` and `#[fuzzer]` macros contain internal suffixes
name.replace("__snforge_internal_test_generated", "")
.replace("__snforge_internal_fuzzer_generated", "")
}

impl TestCaseWithResolvedConfig {
#[must_use]
pub fn new(name: &str, test_details: TestDetails, config: TestCaseResolvedConfig) -> Self {
Self {
name: sanitize_test_case_name(name),
test_details,
config,
}
}

pub fn try_into_program(&self, casm_program: &RawCasmProgram) -> Result<Program> {
self.test_details.try_into_program(casm_program)
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct ResolvedForkConfig {
pub url: Url,
Expand Down
11 changes: 6 additions & 5 deletions crates/forge-runner/src/running.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::backtrace::add_backtrace_footer;
use crate::forge_config::{ForgeConfig, RuntimeConfig};
use crate::gas::calculate_used_gas;
use crate::package_tests::with_config_resolved::{ResolvedForkConfig, TestCaseWithResolvedConfig};
use crate::package_tests::TestCase;
use crate::package_tests::with_config_resolved::ResolvedForkConfig;
use crate::test_case_summary::{Single, TestCaseSummary};
use anyhow::{Result, bail};
use blockifier::execution::call_info::CallInfo;
Expand Down Expand Up @@ -62,7 +63,7 @@ pub use syscall_handler::syscall_handler_offset;
#[must_use]
#[tracing::instrument(skip_all, level = "debug")]
pub fn run_test(
case: Arc<TestCaseWithResolvedConfig>,
case: Arc<TestCase>,
casm_program: Arc<RawCasmProgram>,
forge_config: Arc<ForgeConfig>,
versioned_program_path: Arc<Utf8PathBuf>,
Expand Down Expand Up @@ -98,7 +99,7 @@ pub fn run_test(
#[tracing::instrument(skip_all, level = "debug")]
#[allow(clippy::too_many_arguments)]
pub(crate) fn run_fuzz_test(
case: Arc<TestCaseWithResolvedConfig>,
case: Arc<TestCase>,
program: Program,
casm_program: Arc<RawCasmProgram>,
forge_config: Arc<ForgeConfig>,
Expand Down Expand Up @@ -165,7 +166,7 @@ pub enum RunResult {
#[expect(clippy::too_many_lines)]
#[tracing::instrument(skip_all, level = "debug")]
pub fn run_test_case(
case: &TestCaseWithResolvedConfig,
case: &TestCase,
program: &Program,
casm_program: &RawCasmProgram,
runtime_config: &RuntimeConfig,
Expand Down Expand Up @@ -395,7 +396,7 @@ pub fn run_test_case(

fn extract_test_case_summary(
run_result: Result<RunResult>,
case: &TestCaseWithResolvedConfig,
case: &TestCase,
forge_config: &ForgeConfig,
versioned_program_path: &Utf8Path,
) -> TestCaseSummary<Single> {
Expand Down
4 changes: 2 additions & 2 deletions crates/forge-runner/src/test_case_summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::expected_result::{ExpectedPanicValue, ExpectedTestResult};
use crate::gas::check_available_gas;
use crate::gas::report::SingleTestGasInfo;
use crate::gas::stats::GasStats;
use crate::package_tests::with_config_resolved::TestCaseWithResolvedConfig;
use crate::package_tests::TestCase;
use crate::running::{RunCompleted, RunStatus};
use cairo_annotations::trace_data::VersionedCallTrace as VersionedProfilerCallTrace;
use camino::Utf8Path;
Expand Down Expand Up @@ -286,7 +286,7 @@ impl TestCaseSummary<Single> {
fuzzer_args,
fork_data,
}: RunCompleted,
test_case: &TestCaseWithResolvedConfig,
test_case: &TestCase,
contracts_data: &ContractsData,
versioned_program_path: &Utf8Path,
trace_args: &TraceArgs,
Expand Down
Loading
Loading