Skip to content

Commit

Permalink
Removed shared databases from tests
Browse files Browse the repository at this point in the history
commit-id:d4abac73
  • Loading branch information
integraledelebesgue committed Dec 6, 2024
1 parent ced6504 commit 991564e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 97 deletions.
20 changes: 8 additions & 12 deletions crates/cairo-lang-executable/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::{LazyLock, Mutex};

use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
use cairo_lang_plugins::test_utils::expand_module_text;
Expand All @@ -13,14 +11,6 @@ use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use crate::compile;
use crate::plugin::executable_plugin_suite;

/// Salsa database configured to find the corelib, when reused by different tests should be able to
/// use the cached queries that rely on the corelib's code, which vastly reduces the tests runtime.
pub static SHARED_DB: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
let mut db = RootDatabase::builder().skip_auto_withdraw_gas().detect_corelib().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite() + executable_plugin_suite());
Mutex::new(db)
});

#[derive(Default)]
struct ExpandExecutableTestRunner {}

Expand All @@ -30,7 +20,10 @@ impl TestFileRunner for ExpandExecutableTestRunner {
inputs: &OrderedHashMap<String, String>,
args: &OrderedHashMap<String, String>,
) -> TestRunnerResult {
let db = SHARED_DB.lock().unwrap().snapshot();
let mut db =
RootDatabase::builder().skip_auto_withdraw_gas().detect_corelib().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite() + executable_plugin_suite());

let (_, cairo_code) = get_direct_or_file_content(&inputs["cairo_code"]);
let (test_module, semantic_diagnostics) = TestModule::builder(&db, &cairo_code, None)
.build_and_check_for_diagnostics(&db)
Expand Down Expand Up @@ -66,7 +59,10 @@ impl TestFileRunner for CompileExecutableTestRunner {
inputs: &OrderedHashMap<String, String>,
args: &OrderedHashMap<String, String>,
) -> TestRunnerResult {
let db = SHARED_DB.lock().unwrap().snapshot();
let mut db =
RootDatabase::builder().skip_auto_withdraw_gas().detect_corelib().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite() + executable_plugin_suite());

let (_, cairo_code) = get_direct_or_file_content(&inputs["cairo_code"]);
let (test_module, semantic_diagnostics) = TestModule::builder(&db, &cairo_code, None)
.build_and_check_for_diagnostics(&db)
Expand Down
10 changes: 5 additions & 5 deletions crates/cairo-lang-lowering/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::{LazyLock, Mutex};

use cairo_lang_defs::db::{DefsDatabase, DefsGroup, try_ext_as_virtual_impl};
use cairo_lang_filesystem::db::{
AsFilesGroupMut, ExternalFiles, FilesDatabase, FilesGroup, init_dev_corelib, init_files_group,
Expand All @@ -26,17 +24,20 @@ use crate::utils::InliningStrategy;
pub struct LoweringDatabaseForTesting {
storage: salsa::Storage<LoweringDatabaseForTesting>,
}

impl salsa::Database for LoweringDatabaseForTesting {}
impl ExternalFiles for LoweringDatabaseForTesting {
fn try_ext_as_virtual(&self, external_id: salsa::InternId) -> Option<VirtualFile> {
try_ext_as_virtual_impl(self.upcast(), external_id)
}
}

impl salsa::ParallelDatabase for LoweringDatabaseForTesting {
fn snapshot(&self) -> salsa::Snapshot<LoweringDatabaseForTesting> {
salsa::Snapshot::new(LoweringDatabaseForTesting { storage: self.storage.snapshot() })
}
}

impl LoweringDatabaseForTesting {
pub fn new() -> Self {
let mut res = LoweringDatabaseForTesting { storage: Default::default() };
Expand All @@ -58,13 +59,12 @@ impl LoweringDatabaseForTesting {
}
}

pub static SHARED_DB: LazyLock<Mutex<LoweringDatabaseForTesting>> =
LazyLock::new(|| Mutex::new(LoweringDatabaseForTesting::new()));
impl Default for LoweringDatabaseForTesting {
fn default() -> Self {
SHARED_DB.lock().unwrap().snapshot()
LoweringDatabaseForTesting::new()
}
}

impl AsFilesGroupMut for LoweringDatabaseForTesting {
fn as_files_group_mut(&mut self) -> &mut (dyn FilesGroup + 'static) {
self
Expand Down
12 changes: 7 additions & 5 deletions crates/cairo-lang-semantic/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::{LazyLock, Mutex};

use cairo_lang_defs::db::{DefsDatabase, DefsGroup, try_ext_as_virtual_impl};
use cairo_lang_defs::ids::{FunctionWithBodyId, ModuleId, SubmoduleId, SubmoduleLongId};
use cairo_lang_diagnostics::{Diagnostics, DiagnosticsBuilder};
Expand All @@ -26,17 +24,21 @@ use crate::{ConcreteFunctionWithBodyId, SemanticDiagnostic, semantic};
pub struct SemanticDatabaseForTesting {
storage: salsa::Storage<SemanticDatabaseForTesting>,
}

impl salsa::Database for SemanticDatabaseForTesting {}

impl ExternalFiles for SemanticDatabaseForTesting {
fn try_ext_as_virtual(&self, external_id: salsa::InternId) -> Option<VirtualFile> {
try_ext_as_virtual_impl(self.upcast(), external_id)
}
}

impl salsa::ParallelDatabase for SemanticDatabaseForTesting {
fn snapshot(&self) -> salsa::Snapshot<SemanticDatabaseForTesting> {
salsa::Snapshot::new(SemanticDatabaseForTesting { storage: self.storage.snapshot() })
}
}

impl SemanticDatabaseForTesting {
pub fn new_empty() -> Self {
let mut res = SemanticDatabaseForTesting { storage: Default::default() };
Expand All @@ -54,13 +56,13 @@ impl SemanticDatabaseForTesting {
SemanticDatabaseForTesting { storage: self.storage.snapshot() }
}
}
pub static SHARED_DB: LazyLock<Mutex<SemanticDatabaseForTesting>> =
LazyLock::new(|| Mutex::new(SemanticDatabaseForTesting::new_empty()));

impl Default for SemanticDatabaseForTesting {
fn default() -> Self {
SHARED_DB.lock().unwrap().snapshot()
SemanticDatabaseForTesting::new_empty()
}
}

impl AsFilesGroupMut for SemanticDatabaseForTesting {
fn as_files_group_mut(&mut self) -> &mut (dyn FilesGroup + 'static) {
self
Expand Down
26 changes: 14 additions & 12 deletions crates/cairo-lang-sierra-generator/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::{Arc, LazyLock, Mutex};
use std::sync::Arc;

use cairo_lang_defs::db::{DefsDatabase, DefsGroup, try_ext_as_virtual_impl};
use cairo_lang_defs::ids::ModuleId;
Expand Down Expand Up @@ -41,26 +41,21 @@ use crate::utils::{jump_statement, return_statement, simple_statement};
pub struct SierraGenDatabaseForTesting {
storage: salsa::Storage<SierraGenDatabaseForTesting>,
}

impl salsa::Database for SierraGenDatabaseForTesting {}

impl ExternalFiles for SierraGenDatabaseForTesting {
fn try_ext_as_virtual(&self, external_id: salsa::InternId) -> Option<VirtualFile> {
try_ext_as_virtual_impl(self.upcast(), external_id)
}
}

impl salsa::ParallelDatabase for SierraGenDatabaseForTesting {
fn snapshot(&self) -> salsa::Snapshot<SierraGenDatabaseForTesting> {
salsa::Snapshot::new(SierraGenDatabaseForTesting { storage: self.storage.snapshot() })
}
}
pub static SHARED_DB: LazyLock<Mutex<SierraGenDatabaseForTesting>> =
LazyLock::new(|| Mutex::new(SierraGenDatabaseForTesting::new_empty()));
pub static SHARED_DB_WITHOUT_AD_WITHDRAW_GAS: LazyLock<Mutex<SierraGenDatabaseForTesting>> =
LazyLock::new(|| {
let mut db = SierraGenDatabaseForTesting::new_empty();
let add_withdraw_gas_flag_id = FlagId::new(db.upcast_mut(), "add_withdraw_gas");
db.set_flag(add_withdraw_gas_flag_id, Some(Arc::new(Flag::AddWithdrawGas(false))));
Mutex::new(db)
});

impl SierraGenDatabaseForTesting {
pub fn new_empty() -> Self {
let mut res = SierraGenDatabaseForTesting { storage: Default::default() };
Expand All @@ -78,19 +73,26 @@ impl SierraGenDatabaseForTesting {
init_dev_corelib(&mut res, corelib_path);
res
}

pub fn without_add_withdraw_gas() -> Self {
SHARED_DB_WITHOUT_AD_WITHDRAW_GAS.lock().unwrap().snapshot()
let mut db = SierraGenDatabaseForTesting::new_empty();
let add_withdraw_gas_flag_id = FlagId::new(db.upcast_mut(), "add_withdraw_gas");
db.set_flag(add_withdraw_gas_flag_id, Some(Arc::new(Flag::AddWithdrawGas(false))));
db
}

/// Snapshots the db for read only.
pub fn snapshot(&self) -> SierraGenDatabaseForTesting {
SierraGenDatabaseForTesting { storage: self.storage.snapshot() }
}
}

impl Default for SierraGenDatabaseForTesting {
fn default() -> Self {
SHARED_DB.lock().unwrap().snapshot()
SierraGenDatabaseForTesting::new_empty()
}
}

impl AsFilesGroupMut for SierraGenDatabaseForTesting {
fn as_files_group_mut(&mut self) -> &mut (dyn FilesGroup + 'static) {
self
Expand Down
23 changes: 19 additions & 4 deletions crates/cairo-lang-starknet/src/plugin/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::get_diagnostics_as_string;
use cairo_lang_compiler::project::ProjectConfig;
use cairo_lang_debug::debug::DebugWithDb;
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::ModuleId;
Expand All @@ -9,13 +11,16 @@ use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_filesystem::ids::FileLongId;
use cairo_lang_filesystem::span::{TextOffset, TextSpan, TextWidth};
use cairo_lang_plugins::test_utils::expand_module_text;
use cairo_lang_semantic::db::PluginSuiteInput;
use cairo_lang_semantic::inline_macros::get_default_plugin_suite;
use cairo_lang_semantic::test_utils::TestModule;
use cairo_lang_test_utils::parse_test_file::{TestFileRunner, TestRunnerResult};
use cairo_lang_test_utils::{get_direct_or_file_content, verify_diagnostics_expectation};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::{Intern, Upcast};

use crate::test_utils::{SHARED_DB, SHARED_DB_WITH_CONTRACTS};
use crate::starknet_plugin_suite;
use crate::test_utils::CONTRACTS_CRATE_DIR;

#[derive(Default)]
struct ExpandContractTestRunner {}
Expand All @@ -27,7 +32,9 @@ impl TestFileRunner for ExpandContractTestRunner {
inputs: &OrderedHashMap<String, String>,
args: &OrderedHashMap<String, String>,
) -> TestRunnerResult {
let db = SHARED_DB.lock().unwrap().snapshot();
let mut db = RootDatabase::builder().detect_corelib().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite() + starknet_plugin_suite());

let (_, cairo_code) = get_direct_or_file_content(&inputs["cairo_code"]);
let (test_module, _semantic_diagnostics) = TestModule::builder(&db, &cairo_code, None)
.build_and_check_for_diagnostics(&db)
Expand Down Expand Up @@ -121,7 +128,15 @@ impl TestFileRunner for ExpandContractFromCrateTestRunner {
inputs: &OrderedHashMap<String, String>,
_args: &OrderedHashMap<String, String>,
) -> TestRunnerResult {
let db = SHARED_DB_WITH_CONTRACTS.lock().unwrap().snapshot();
let mut db = RootDatabase::builder()
.detect_corelib()
.with_project_config(
ProjectConfig::from_directory(Path::new(CONTRACTS_CRATE_DIR)).unwrap(),
)
.build()
.unwrap();

db.set_plugins_from_suite(get_default_plugin_suite() + starknet_plugin_suite());
let contract_file_id =
FileLongId::OnDisk(PathBuf::from(inputs["contract_file_name"].clone())).intern(&db);
let contract_module_ids = db.file_modules(contract_file_id).unwrap();
Expand Down
25 changes: 3 additions & 22 deletions crates/cairo-lang-starknet/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::{Path, PathBuf};
use std::sync::{LazyLock, Mutex};

use cairo_lang_compiler::CompilerConfig;
use cairo_lang_compiler::db::RootDatabase;
Expand All @@ -12,7 +11,6 @@ use cairo_lang_semantic::db::PluginSuiteInput;
use cairo_lang_semantic::inline_macros::get_default_plugin_suite;
use cairo_lang_starknet_classes::allowed_libfuncs::BUILTIN_ALL_LIBFUNCS_LIST;
use cairo_lang_starknet_classes::contract_class::ContractClass;
use cairo_lang_test_utils::test_lock;
use itertools::Itertools;

use crate::compile::compile_contract_in_prepared_db;
Expand All @@ -25,35 +23,18 @@ pub fn get_example_file_path(file_name: &str) -> PathBuf {
path
}

/// Salsa database configured to find the corelib, when reused by different tests should be able to
/// use the cached queries that rely on the corelib's code, which vastly reduces the tests runtime.
pub static SHARED_DB: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
let mut db = RootDatabase::builder().detect_corelib().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite() + starknet_plugin_suite());
Mutex::new(db)
});

const CONTRACTS_CRATE_DIR: &str = "cairo_level_tests";
pub const CONTRACTS_CRATE_DIR: &str = "cairo_level_tests";

/// Salsa database configured to find the corelib, and the contracts crate. When reused by different
/// tests should be able to use the cached queries that rely on the corelib's or the contracts
/// crates code, which vastly reduces the tests runtime.
pub static SHARED_DB_WITH_CONTRACTS: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
/// Returns the compiled test contract from the contracts crate, with replaced ids.
pub fn get_test_contract(example_file_name: &str) -> ContractClass {
let mut db = RootDatabase::builder()
.detect_corelib()
.with_project_config(ProjectConfig::from_directory(Path::new(CONTRACTS_CRATE_DIR)).unwrap())
.build()
.unwrap();

db.set_plugins_from_suite(get_default_plugin_suite() + starknet_plugin_suite());
Mutex::new(db)
});

/// Returns the compiled test contract from the contracts crate, with replaced ids.
pub fn get_test_contract(example_file_name: &str) -> ContractClass {
let locked_db = test_lock(&SHARED_DB_WITH_CONTRACTS);
let db = locked_db.snapshot();
drop(locked_db);
let crate_configs = db.crate_configs();
let contracts_crate = crate_configs
.iter()
Expand Down
68 changes: 31 additions & 37 deletions tests/e2e_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::ops::DerefMut;
use std::sync::{Arc, LazyLock, Mutex};
use std::sync::Arc;

use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
Expand All @@ -17,35 +16,35 @@ use cairo_lang_sierra_generator::replace_ids::replace_sierra_ids_in_program;
use cairo_lang_sierra_to_casm::compiler;
use cairo_lang_sierra_to_casm::metadata::{MetadataComputationConfig, calc_metadata};
use cairo_lang_test_utils::parse_test_file::{TestFileRunner, TestRunnerResult};
use cairo_lang_test_utils::test_lock;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use itertools::Itertools;

/// Salsa databases configured to find the corelib, when reused by different tests should be able to
/// use the cached queries that rely on the corelib's code, which vastly reduces the tests runtime.
static SHARED_DB_WITH_GAS_NO_OPTS: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
let mut db = RootDatabase::builder().detect_corelib().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite());
db.set_optimization_config(Arc::new(
OptimizationConfig::default().with_skip_const_folding(true),
));
Mutex::new(db)
});
static SHARED_DB_NO_GAS_NO_OPTS: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
let mut db = RootDatabase::builder().detect_corelib().skip_auto_withdraw_gas().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite());
db.set_optimization_config(Arc::new(
OptimizationConfig::default().with_skip_const_folding(true),
));
Mutex::new(db)
});
static SHARED_DB_WITH_OPTS: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
let mut db = RootDatabase::builder().detect_corelib().skip_auto_withdraw_gas().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite());
db.set_optimization_config(Arc::new(OptimizationConfig::default()));
Mutex::new(db)
});
/// Builds a [`RootDatabase`] according to the [`E2eTestParams`] provided.
fn build_db(params: &E2eTestParams) -> RootDatabase {
if !params.skip_optimization_passes {
let mut db =
RootDatabase::builder().detect_corelib().skip_auto_withdraw_gas().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite());
db.set_optimization_config(Arc::new(OptimizationConfig::default()));
db
} else if params.add_withdraw_gas {
let mut db = RootDatabase::builder().detect_corelib().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite());
db.set_optimization_config(Arc::new(
OptimizationConfig::default().with_skip_const_folding(true),
));
db
} else {
let mut db =
RootDatabase::builder().detect_corelib().skip_auto_withdraw_gas().build().unwrap();
db.set_plugins_from_suite(get_default_plugin_suite());
db.set_optimization_config(Arc::new(
OptimizationConfig::default().with_skip_const_folding(true),
));
db
}
}

cairo_lang_test_utils::test_file_test_with_runner!(
general_e2e,
Expand Down Expand Up @@ -219,18 +218,13 @@ fn run_e2e_test(
inputs: &OrderedHashMap<String, String>,
params: E2eTestParams,
) -> TestRunnerResult {
let mut locked_db = test_lock(if !params.skip_optimization_passes {
&SHARED_DB_WITH_OPTS
} else if params.add_withdraw_gas {
&SHARED_DB_WITH_GAS_NO_OPTS
} else {
&SHARED_DB_NO_GAS_NO_OPTS
});
let db = build_db(&params);

// Parse code and create semantic model.
let test_module = TestModule::builder(locked_db.deref_mut(), inputs["cairo"].as_str(), None)
.build_and_check_for_diagnostics(&*locked_db)
let test_module = TestModule::builder(&db, inputs["cairo"].as_str(), None)
.build_and_check_for_diagnostics(&db)
.unwrap();
let db = locked_db.snapshot();

DiagnosticsReporter::stderr().with_crates(&[test_module.crate_id]).ensure(&db).unwrap();

// Compile to Sierra.
Expand Down

0 comments on commit 991564e

Please sign in to comment.