From e889dc4541e984331b4af87186c2f421cad43b1f Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Wed, 13 Nov 2024 17:58:26 +0200 Subject: [PATCH] chore: share the test util path_in_resources --- Cargo.lock | 1 + crates/papyrus_load_test/Cargo.toml | 1 + crates/papyrus_load_test/src/create_files.rs | 2 +- crates/papyrus_load_test/src/lib.rs | 8 ++------ crates/papyrus_load_test/src/transactions.rs | 3 ++- crates/papyrus_rpc/src/v0_8/execution_test.rs | 8 ++------ .../src/serialization/serializers_test.rs | 19 ++++++++----------- crates/starknet_api/src/test_utils.rs | 17 +++++++++++++---- .../src/test_utils/read_resource.rs | 8 +++----- 9 files changed, 33 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 847f3a4c48..a4dfe87f74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7325,6 +7325,7 @@ dependencies = [ "reqwest 0.11.27", "serde", "serde_json", + "starknet_api", "tokio", ] diff --git a/crates/papyrus_load_test/Cargo.toml b/crates/papyrus_load_test/Cargo.toml index b1054d6a2e..b321a4481c 100644 --- a/crates/papyrus_load_test/Cargo.toml +++ b/crates/papyrus_load_test/Cargo.toml @@ -17,6 +17,7 @@ rand.workspace = true reqwest.workspace = true serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true, features = ["arbitrary_precision"] } +starknet_api.workspace = true tokio.workspace = true [dev-dependencies] diff --git a/crates/papyrus_load_test/src/create_files.rs b/crates/papyrus_load_test/src/create_files.rs index 4880a60092..7ca7f56b1b 100644 --- a/crates/papyrus_load_test/src/create_files.rs +++ b/crates/papyrus_load_test/src/create_files.rs @@ -6,11 +6,11 @@ use once_cell::sync::OnceCell; use rand::Rng; use reqwest::{Client, Url}; use serde_json::Value as jsonVal; +use starknet_api::test_utils::path_in_resources; use crate::{ get_last_block_number, get_random_block_number, - path_in_resources, GET_BLOCK_TRANSACTION_COUNT_BY_HASH_WEIGHT, GET_BLOCK_TRANSACTION_COUNT_BY_NUMBER_WEIGHT, GET_BLOCK_WITH_FULL_TRANSACTIONS_BY_HASH_WEIGHT, diff --git a/crates/papyrus_load_test/src/lib.rs b/crates/papyrus_load_test/src/lib.rs index e8e259cef9..9b97a0a615 100644 --- a/crates/papyrus_load_test/src/lib.rs +++ b/crates/papyrus_load_test/src/lib.rs @@ -13,14 +13,15 @@ mod precision_test; pub mod scenarios; pub mod transactions; +use std::fs; use std::sync::LazyLock; -use std::{env, fs}; use goose::goose::{GooseUser, TransactionError}; use once_cell::sync::OnceCell; use rand::Rng; use serde::Deserialize; use serde_json::{json, Value as jsonVal}; +use starknet_api::test_utils::path_in_resources; type PostResult = Result>; @@ -71,11 +72,6 @@ pub fn get_random_block_number() -> u64 { rng.gen_range(0..=last_block) } -// Returns the path to the file_name inside the resources folder in payprus_loadtest module. -pub fn path_in_resources(file_name: &str) -> String { - env::var("CARGO_MANIFEST_DIR").unwrap() + "/resources/" + file_name -} - // TODO(dvir): update those number with real statics after the node will be in production. // Weight for each request to the node. const BLOCK_HASH_AND_NUMBER_WEIGHT: usize = 10; diff --git a/crates/papyrus_load_test/src/transactions.rs b/crates/papyrus_load_test/src/transactions.rs index f98d2d4094..43b506e498 100644 --- a/crates/papyrus_load_test/src/transactions.rs +++ b/crates/papyrus_load_test/src/transactions.rs @@ -6,8 +6,9 @@ use std::sync::Arc; use goose::goose::{Transaction, TransactionFunction}; use rand::Rng; use serde_json::{json, Value as jsonVal}; +use starknet_api::test_utils::path_in_resources; -use crate::{create_request, jsonrpc_request, path_in_resources, post_jsonrpc_request}; +use crate::{create_request, jsonrpc_request, post_jsonrpc_request}; create_get_transaction_function_with_requests_from_file! { get_block_with_transaction_hashes_by_number, "block_number.txt"; diff --git a/crates/papyrus_rpc/src/v0_8/execution_test.rs b/crates/papyrus_rpc/src/v0_8/execution_test.rs index 0313f85656..0d6ab94691 100644 --- a/crates/papyrus_rpc/src/v0_8/execution_test.rs +++ b/crates/papyrus_rpc/src/v0_8/execution_test.rs @@ -1,6 +1,4 @@ -use std::env; use std::fs::read_to_string; -use std::path::Path; use std::sync::Arc; use assert_matches::assert_matches; @@ -65,7 +63,7 @@ use starknet_api::data_availability::L1DataAvailabilityMode; use starknet_api::deprecated_contract_class::ContractClass as SN_API_DeprecatedContractClass; use starknet_api::hash::StarkHash; use starknet_api::state::{StorageKey, ThinStateDiff as StarknetApiStateDiff}; -use starknet_api::test_utils::read_json_file; +use starknet_api::test_utils::{path_in_resources, read_json_file}; use starknet_api::transaction::fields::{Calldata, Fee}; use starknet_api::transaction::{ L1HandlerTransaction, @@ -1344,9 +1342,7 @@ fn get_decompressed_program() { } fn get_test_compressed_program() -> String { - let path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()) - .join("resources") - .join("base64_compressed_program.txt"); + let path = path_in_resources("base64_compressed_program.txt"); read_to_string(path).expect("Couldn't read compressed program") } diff --git a/crates/papyrus_storage/src/serialization/serializers_test.rs b/crates/papyrus_storage/src/serialization/serializers_test.rs index 1e9b5bb93a..f39ba4414e 100644 --- a/crates/papyrus_storage/src/serialization/serializers_test.rs +++ b/crates/papyrus_storage/src/serialization/serializers_test.rs @@ -12,7 +12,7 @@ use starknet_api::block::BlockNumber; use starknet_api::core::ContractAddress; use starknet_api::hash::StarkHash; use starknet_api::state::StorageKey; -use starknet_api::test_utils::read_json_file; +use starknet_api::test_utils::{path_in_resources, read_json_file}; use starknet_api::transaction::TransactionOffsetInBlock; use crate::db::serialization::StorageSerde; @@ -162,10 +162,7 @@ fn casm_serialization_regression() { json_casm .serialize_into(&mut serialized) .expect("Failed to serialize casm file: {json_file_name}"); - let bin_path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()) - .join("resources") - .join("casm") - .join(bin_file_name); + let bin_path = path_in_resources(Path::new("casm").join(bin_file_name)); let mut bin_file = File::open(bin_path) .expect("Failed to open bin file: {bin_file_name}\n{FIX_SUGGESTION}"); let mut regression_casm_bytes = Vec::new(); @@ -187,10 +184,10 @@ fn casm_deserialization_regression() { fix_casm_regression_files() } - let resources_path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("resources"); for (json_file_name, bin_file_name) in CASM_SERIALIZATION_REGRESSION_FILES { - let mut regression_casm_file = File::open(resources_path.join("casm").join(bin_file_name)) - .expect("Failed to open bin file: {bin_file_name}\n{FIX_SUGGESTION}"); + let mut regression_casm_file = + File::open(path_in_resources(Path::new("casm").join(bin_file_name))) + .expect("Failed to open bin file: {bin_file_name}\n{FIX_SUGGESTION}"); let mut regression_casm_bytes = Vec::new(); regression_casm_file .read_to_end(&mut regression_casm_bytes) @@ -210,7 +207,6 @@ result.\n{FIX_SUGGESTION}" } fn fix_casm_regression_files() { - let resources_path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("resources"); for (json_file_name, bin_file_name) in CASM_SERIALIZATION_REGRESSION_FILES { let json_path = format!("casm/{}", json_file_name); let json_casm: CasmContractClass = @@ -219,8 +215,9 @@ fn fix_casm_regression_files() { let mut serialized: Vec = Vec::new(); json_casm.serialize_into(&mut serialized).unwrap(); let casm_bytes = serialized.into_boxed_slice(); - let mut hardcoded_file = File::create(resources_path.join("casm").join(bin_file_name)) - .expect("Failed to create bin file {bin_file_name}\n"); + let mut hardcoded_file = + File::create(path_in_resources(Path::new("casm").join(bin_file_name))) + .expect("Failed to create bin file {bin_file_name}\n"); hardcoded_file.write_all(&casm_bytes).unwrap(); } } diff --git a/crates/starknet_api/src/test_utils.rs b/crates/starknet_api/src/test_utils.rs index 8562ef53ed..2cb3c19bd8 100644 --- a/crates/starknet_api/src/test_utils.rs +++ b/crates/starknet_api/src/test_utils.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::env; use std::fs::read_to_string; use std::path::{Path, PathBuf}; +use std::sync::LazyLock; use starknet_types_core::felt::Felt; @@ -12,16 +13,24 @@ 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::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("../..").join(relative_path) + 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) } /// Reads from the directory containing the manifest at run time, same as current working directory. pub fn read_json_file>(path_in_resource_dir: P) -> serde_json::Value { - let path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()) - .join("resources") - .join(path_in_resource_dir); + let path = path_in_resources(path_in_resource_dir); let json_str = read_to_string(path.to_str().unwrap()) .unwrap_or_else(|_| panic!("Failed to read file at path: {}", path.display())); serde_json::from_str(&json_str).unwrap() diff --git a/crates/starknet_client/src/test_utils/read_resource.rs b/crates/starknet_client/src/test_utils/read_resource.rs index 6627703e29..e9ce891b5e 100644 --- a/crates/starknet_client/src/test_utils/read_resource.rs +++ b/crates/starknet_client/src/test_utils/read_resource.rs @@ -1,11 +1,9 @@ -use std::env; use std::fs::read_to_string; -use std::path::Path; use std::string::String; +use starknet_api::test_utils::path_in_resources; + pub fn read_resource_file(path_in_resource_dir: &str) -> String { - let path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()) - .join("resources") - .join(path_in_resource_dir); + let path = path_in_resources(path_in_resource_dir); return read_to_string(path.to_str().unwrap()).unwrap(); }