Skip to content

Commit

Permalink
chore: share the test util path_in_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Nov 14, 2024
1 parent 88a733d commit e011876
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/papyrus_load_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_load_test/src/create_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions crates/papyrus_load_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<jsonVal, Box<TransactionError>>;

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion crates/papyrus_load_test/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 2 additions & 6 deletions crates/papyrus_rpc/src/v0_8/execution_test.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}

Expand Down
19 changes: 8 additions & 11 deletions crates/papyrus_storage/src/serialization/serializers_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -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 =
Expand All @@ -219,8 +215,9 @@ fn fix_casm_regression_files() {
let mut serialized: Vec<u8> = 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();
}
}
12 changes: 8 additions & 4 deletions crates/starknet_api/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ pub fn get_absolute_path<P: AsRef<Path>>(relative_path: P) -> PathBuf {
Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).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<P: AsRef<Path>>(file_path: P) -> PathBuf {
Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).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<P: AsRef<Path>>(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()
}

#[derive(Debug, Default)]
pub struct NonceManager {
next_nonce: HashMap<ContractAddress, Felt>,
Expand Down
8 changes: 3 additions & 5 deletions crates/starknet_client/src/test_utils/read_resource.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit e011876

Please sign in to comment.