From 74b80c46fcc6ba0a3519a02655ec735bce1ad965 Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Thu, 21 Nov 2024 10:15:31 +0200 Subject: [PATCH] chore(infra): share get_absolute_path infra util with snapi test utils (#2075) --- Cargo.lock | 5 +++++ crates/infra_utils/src/path.rs | 16 ++++++++++++---- crates/mempool_test_utils/Cargo.toml | 1 + .../src/starknet_api_test_utils.rs | 6 ++++-- crates/papyrus_config/Cargo.toml | 1 + crates/papyrus_config/src/config_test.rs | 4 ++-- crates/papyrus_node/Cargo.toml | 1 + crates/papyrus_node/src/config/config_test.rs | 14 +++++++------- crates/starknet_api/Cargo.toml | 1 + crates/starknet_api/src/test_utils.rs | 13 ++----------- crates/starknet_sequencer_node/Cargo.toml | 1 + .../src/config/config_test.rs | 6 +++--- crates/starknet_sierra_compile/Cargo.toml | 1 + .../starknet_sierra_compile/src/compile_test.rs | 5 +++-- 14 files changed, 44 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fea80ce556..f1b0a8c900 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6340,6 +6340,7 @@ version = "0.0.0" dependencies = [ "assert_matches", "blockifier", + "infra_utils", "pretty_assertions", "serde_json", "starknet-types-core", @@ -7221,6 +7222,7 @@ version = "0.0.0" dependencies = [ "assert_matches", "clap", + "infra_utils", "itertools 0.12.1", "lazy_static", "papyrus_test_utils", @@ -7411,6 +7413,7 @@ dependencies = [ "const_format", "futures", "futures-util", + "infra_utils", "insta", "itertools 0.12.1", "lazy_static", @@ -10083,6 +10086,7 @@ dependencies = [ "derive_more 0.99.18", "hex", "indexmap 2.6.0", + "infra_utils", "itertools 0.12.1", "num-bigint 0.4.6", "pretty_assertions", @@ -10520,6 +10524,7 @@ dependencies = [ "cairo-lang-starknet-classes", "cairo-lang-utils", "cairo-native", + "infra_utils", "mempool_test_utils", "papyrus_config", "rstest", diff --git a/crates/infra_utils/src/path.rs b/crates/infra_utils/src/path.rs index b34b1878e1..423585c4ae 100644 --- a/crates/infra_utils/src/path.rs +++ b/crates/infra_utils/src/path.rs @@ -1,5 +1,13 @@ use std::env; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; +use std::sync::LazyLock; + +static PATH_TO_CARGO_MANIFEST_DIR: LazyLock> = + LazyLock::new(|| env::var("CARGO_MANIFEST_DIR").ok().map(|dir| Path::new(&dir).into())); + +pub fn cargo_manifest_dir() -> Option { + PATH_TO_CARGO_MANIFEST_DIR.clone() +} // TODO(Tsabary/ Arni): consolidate with other get_absolute_path functions. /// Resolves a relative path from the project root directory and returns its absolute path. @@ -14,10 +22,10 @@ pub fn resolve_project_relative_path(relative_path: &str) -> PathBuf { } fn path_of_project_root() -> PathBuf { - env::var("CARGO_MANIFEST_DIR") + cargo_manifest_dir() // Attempt to get the `CARGO_MANIFEST_DIR` environment variable and convert it to `PathBuf`. // Ascend two directories ("../..") to get to the project root. - .map(|dir| PathBuf::from(dir).join("../..")) + .map(|dir| dir.join("../..")) // If `CARGO_MANIFEST_DIR` isn't set, fall back to the current working directory - .unwrap_or_else(|_| env::current_dir().expect("Failed to get current directory")) + .unwrap_or(env::current_dir().expect("Failed to get current directory")) } diff --git a/crates/mempool_test_utils/Cargo.toml b/crates/mempool_test_utils/Cargo.toml index 8be25658c3..ef49716460 100644 --- a/crates/mempool_test_utils/Cargo.toml +++ b/crates/mempool_test_utils/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] assert_matches.workspace = true blockifier = { workspace = true, features = ["testing"] } +infra_utils.workspace = true pretty_assertions.workspace = true serde_json.workspace = true starknet-types-core.workspace = true diff --git a/crates/mempool_test_utils/src/starknet_api_test_utils.rs b/crates/mempool_test_utils/src/starknet_api_test_utils.rs index e79f7467a3..b0bb48bd02 100644 --- a/crates/mempool_test_utils/src/starknet_api_test_utils.rs +++ b/crates/mempool_test_utils/src/starknet_api_test_utils.rs @@ -8,6 +8,7 @@ use std::sync::LazyLock; use assert_matches::assert_matches; use blockifier::test_utils::contracts::FeatureContract; use blockifier::test_utils::{create_trivial_calldata, CairoVersion}; +use infra_utils::path::resolve_project_relative_path; use pretty_assertions::assert_ne; use serde_json::to_string_pretty; use starknet_api::block::GasPrice; @@ -18,7 +19,7 @@ use starknet_api::rpc_transaction::{ContractClass, RpcDeployAccountTransactionV3 use starknet_api::test_utils::declare::rpc_declare_tx; use starknet_api::test_utils::deploy_account::DeployAccountTxArgs; use starknet_api::test_utils::invoke::{rpc_invoke_tx, InvokeTxArgs}; -use starknet_api::test_utils::{get_absolute_path, NonceManager}; +use starknet_api::test_utils::NonceManager; use starknet_api::transaction::fields::{ AllResourceBounds, ContractAddressSalt, @@ -66,7 +67,8 @@ pub fn test_valid_resource_bounds() -> ValidResourceBounds { /// Get the contract class used for testing. pub fn contract_class() -> ContractClass { - env::set_current_dir(get_absolute_path(TEST_FILES_FOLDER)).expect("Couldn't set working dir."); + env::set_current_dir(resolve_project_relative_path(TEST_FILES_FOLDER)) + .expect("Couldn't set working dir."); let json_file_path = Path::new(CONTRACT_CLASS_FILE); serde_json::from_reader(File::open(json_file_path).unwrap()).unwrap() } diff --git a/crates/papyrus_config/Cargo.toml b/crates/papyrus_config/Cargo.toml index 7e539043f7..82829b655b 100644 --- a/crates/papyrus_config/Cargo.toml +++ b/crates/papyrus_config/Cargo.toml @@ -20,6 +20,7 @@ validator = { workspace = true, features = ["derive"] } [dev-dependencies] assert_matches.workspace = true +infra_utils.workspace = true itertools.workspace = true lazy_static.workspace = true papyrus_test_utils.workspace = true diff --git a/crates/papyrus_config/src/config_test.rs b/crates/papyrus_config/src/config_test.rs index 39f499091d..66cbd31bfa 100644 --- a/crates/papyrus_config/src/config_test.rs +++ b/crates/papyrus_config/src/config_test.rs @@ -6,11 +6,11 @@ use std::time::Duration; use assert_matches::assert_matches; use clap::Command; +use infra_utils::path::resolve_project_relative_path; use itertools::chain; use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; use serde_json::json; -use starknet_api::test_utils::get_absolute_path; use tempfile::TempDir; use validator::Validate; @@ -52,7 +52,7 @@ use crate::{ lazy_static! { static ref CUSTOM_CONFIG_PATH: PathBuf = - get_absolute_path("crates/papyrus_config/resources/custom_config_example.json"); + resolve_project_relative_path("crates/papyrus_config/resources/custom_config_example.json"); } #[derive(Clone, Copy, Default, Serialize, Deserialize, Debug, PartialEq, Validate)] diff --git a/crates/papyrus_node/Cargo.toml b/crates/papyrus_node/Cargo.toml index 0304ce2280..8f25b05355 100644 --- a/crates/papyrus_node/Cargo.toml +++ b/crates/papyrus_node/Cargo.toml @@ -61,6 +61,7 @@ tokio-stream = { workspace = true, optional = true } [dev-dependencies] assert-json-diff.workspace = true colored.workspace = true +infra_utils.workspace = true insta = { workspace = true, features = ["json"] } metrics-exporter-prometheus.workspace = true papyrus_test_utils.workspace = true diff --git a/crates/papyrus_node/src/config/config_test.rs b/crates/papyrus_node/src/config/config_test.rs index dda43dd8d4..a10df5f641 100644 --- a/crates/papyrus_node/src/config/config_test.rs +++ b/crates/papyrus_node/src/config/config_test.rs @@ -9,6 +9,7 @@ use std::str::FromStr; use assert_json_diff::assert_json_eq; use colored::Colorize; +use infra_utils::path::resolve_project_relative_path; use itertools::Itertools; use papyrus_base_layer::ethereum_base_layer_contract::EthereumBaseLayerConfig; use papyrus_config::dumping::SerializeConfig; @@ -18,7 +19,6 @@ use papyrus_monitoring_gateway::MonitoringGatewayConfig; use pretty_assertions::assert_eq; use serde_json::{json, Map, Value}; use starknet_api::core::ChainId; -use starknet_api::test_utils::get_absolute_path; use tempfile::NamedTempFile; use validator::Validate; @@ -73,14 +73,14 @@ fn get_args(additional_args: Vec<&str>) -> Vec { #[test] fn load_default_config() { - env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir."); + env::set_current_dir(resolve_project_relative_path("")).expect("Couldn't set working dir."); NodeConfig::load_and_process(get_args(vec![])).expect("Failed to load the config."); } #[test] fn load_http_headers() { let args = get_args(vec!["--central.http_headers", "NAME_1:VALUE_1 NAME_2:VALUE_2"]); - env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir."); + env::set_current_dir(resolve_project_relative_path("")).expect("Couldn't set working dir."); let config = NodeConfig::load_and_process(args).unwrap(); let target_http_headers = HashMap::from([ ("NAME_1".to_string(), "VALUE_1".to_string()), @@ -96,7 +96,7 @@ fn load_http_headers() { // Regression test which checks that the default config dumping hasn't changed. fn test_dump_default_config() { let mut default_config = NodeConfig::default(); - env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir."); + env::set_current_dir(resolve_project_relative_path("")).expect("Couldn't set working dir."); let dumped_default_config = default_config.dump(); insta::assert_json_snapshot!(dumped_default_config); @@ -108,7 +108,7 @@ fn test_dump_default_config() { #[test] fn test_default_config_process() { - env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir."); + env::set_current_dir(resolve_project_relative_path("")).expect("Couldn't set working dir."); assert_eq!(NodeConfig::load_and_process(get_args(vec![])).unwrap(), NodeConfig::default()); } @@ -120,7 +120,7 @@ fn test_update_dumped_config_by_command() { "--storage.db_config.path_prefix", "/abc", ]); - env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir."); + env::set_current_dir(resolve_project_relative_path("")).expect("Couldn't set working dir."); let config = NodeConfig::load_and_process(args).unwrap(); assert_eq!(config.central.retry_config.retry_max_delay_millis, 1234); @@ -130,7 +130,7 @@ fn test_update_dumped_config_by_command() { #[cfg(feature = "rpc")] #[test] fn default_config_file_is_up_to_date() { - env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir."); + env::set_current_dir(resolve_project_relative_path("")).expect("Couldn't set working dir."); let from_default_config_file: serde_json::Value = serde_json::from_reader(File::open(DEFAULT_CONFIG_PATH).unwrap()).unwrap(); diff --git a/crates/starknet_api/Cargo.toml b/crates/starknet_api/Cargo.toml index 73879008b9..5fac49f236 100644 --- a/crates/starknet_api/Cargo.toml +++ b/crates/starknet_api/Cargo.toml @@ -16,6 +16,7 @@ cairo-lang-runner.workspace = true derive_more.workspace = true hex.workspace = true indexmap = { workspace = true, features = ["serde"] } +infra_utils.workspace = true itertools.workspace = true num-bigint.workspace = true pretty_assertions.workspace = true diff --git a/crates/starknet_api/src/test_utils.rs b/crates/starknet_api/src/test_utils.rs index 2cb3c19bd8..d88a44f87a 100644 --- a/crates/starknet_api/src/test_utils.rs +++ b/crates/starknet_api/src/test_utils.rs @@ -1,9 +1,8 @@ use std::collections::HashMap; -use std::env; use std::fs::read_to_string; use std::path::{Path, PathBuf}; -use std::sync::LazyLock; +use infra_utils::path::cargo_manifest_dir; use starknet_types_core::felt::Felt; use crate::core::{ContractAddress, Nonce}; @@ -13,19 +12,11 @@ pub mod deploy_account; pub mod invoke; pub mod l1_handler; -static PATH_TO_CARGO_MANIFEST_DIR: LazyLock = - LazyLock::new(|| Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).into()); - -/// Returns the absolute path from the project root. -pub fn get_absolute_path>(relative_path: P) -> PathBuf { - PATH_TO_CARGO_MANIFEST_DIR.join("../..").join(relative_path) -} - /// Returns the path to a file in the resources directory. This assumes the current working /// directory has a `resources` folder. The value for file_path should be the path to the required /// file in the folder "resources". pub fn path_in_resources>(file_path: P) -> PathBuf { - PATH_TO_CARGO_MANIFEST_DIR.join("resources").join(file_path) + cargo_manifest_dir().unwrap().join("resources").join(file_path) } /// Reads from the directory containing the manifest at run time, same as current working directory. diff --git a/crates/starknet_sequencer_node/Cargo.toml b/crates/starknet_sequencer_node/Cargo.toml index 5a91961ff4..dfbd5cc9f4 100644 --- a/crates/starknet_sequencer_node/Cargo.toml +++ b/crates/starknet_sequencer_node/Cargo.toml @@ -44,6 +44,7 @@ validator.workspace = true assert-json-diff.workspace = true assert_matches.workspace = true colored.workspace = true +infra_utils.workspace = true mempool_test_utils.workspace = true pretty_assertions.workspace = true serde_json.workspace = true diff --git a/crates/starknet_sequencer_node/src/config/config_test.rs b/crates/starknet_sequencer_node/src/config/config_test.rs index cea34a49ad..e503d4e877 100644 --- a/crates/starknet_sequencer_node/src/config/config_test.rs +++ b/crates/starknet_sequencer_node/src/config/config_test.rs @@ -5,11 +5,11 @@ use std::fs::File; use assert_json_diff::assert_json_eq; use assert_matches::assert_matches; use colored::Colorize; +use infra_utils::path::resolve_project_relative_path; use papyrus_config::dumping::SerializeConfig; use papyrus_config::validators::config_validate; use papyrus_config::SerializedParam; use rstest::rstest; -use starknet_api::test_utils::get_absolute_path; use starknet_sequencer_infra::component_definitions::{ LocalServerConfig, RemoteClientConfig, @@ -63,7 +63,7 @@ fn test_valid_component_execution_config( /// cargo run --bin sequencer_dump_config -q #[test] fn test_default_config_file_is_up_to_date() { - env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir."); + env::set_current_dir(resolve_project_relative_path("")).expect("Couldn't set working dir."); let from_default_config_file: serde_json::Value = serde_json::from_reader(File::open(DEFAULT_CONFIG_PATH).unwrap()).unwrap(); @@ -115,7 +115,7 @@ fn test_config_parsing() { #[test] fn test_required_params_setting() { // Load the default config file. - let file = std::fs::File::open(get_absolute_path(DEFAULT_CONFIG_PATH)).unwrap(); + let file = std::fs::File::open(resolve_project_relative_path(DEFAULT_CONFIG_PATH)).unwrap(); let mut deserialized = serde_json::from_reader::<_, serde_json::Value>(file).unwrap(); let expected_required_params = deserialized.as_object_mut().unwrap(); expected_required_params.retain(|_, value| { diff --git a/crates/starknet_sierra_compile/Cargo.toml b/crates/starknet_sierra_compile/Cargo.toml index a3c230aae8..8e40f025b3 100644 --- a/crates/starknet_sierra_compile/Cargo.toml +++ b/crates/starknet_sierra_compile/Cargo.toml @@ -27,5 +27,6 @@ validator.workspace = true [dev-dependencies] assert_matches.workspace = true +infra_utils.workspace = true mempool_test_utils.workspace = true rstest.workspace = true diff --git a/crates/starknet_sierra_compile/src/compile_test.rs b/crates/starknet_sierra_compile/src/compile_test.rs index 5ba5ba4792..5b685a2fb8 100644 --- a/crates/starknet_sierra_compile/src/compile_test.rs +++ b/crates/starknet_sierra_compile/src/compile_test.rs @@ -3,9 +3,9 @@ use std::path::Path; use assert_matches::assert_matches; use cairo_lang_starknet_classes::contract_class::ContractClass; +use infra_utils::path::resolve_project_relative_path; use mempool_test_utils::{FAULTY_ACCOUNT_CLASS_FILE, TEST_FILES_FOLDER}; use rstest::rstest; -use starknet_api::test_utils::get_absolute_path; use crate::cairo_lang_compiler::CairoLangSierraToCasmCompiler; use crate::command_line_compiler::CommandLineCompiler; @@ -26,7 +26,8 @@ fn command_line_compiler() -> CommandLineCompiler { CommandLineCompiler::new(SIERRA_TO_CASM_COMPILATION_CONFIG) } fn get_test_contract() -> ContractClass { - env::set_current_dir(get_absolute_path(TEST_FILES_FOLDER)).expect("Failed to set current dir."); + env::set_current_dir(resolve_project_relative_path(TEST_FILES_FOLDER)) + .expect("Failed to set current dir."); let sierra_path = Path::new(FAULTY_ACCOUNT_CLASS_FILE); contract_class_from_file(sierra_path) }