Skip to content

Commit

Permalink
chore(infra): share get_absolute_path infra util with snapi test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Nov 20, 2024
1 parent eb4e92e commit ca5efcc
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 21 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

17 changes: 12 additions & 5 deletions crates/infra_utils/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use std::env;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;

static PATH_TO_CARGO_MANIFEST_DIR: LazyLock<Option<PathBuf>> =
LazyLock::new(|| env::var("CARGO_MANIFEST_DIR").ok().map(|dir| Path::new(&dir).into()));

pub fn cargo_manifest_dir() -> Option<PathBuf> {
PATH_TO_CARGO_MANIFEST_DIR.clone()
}

// TODO(Tsabary/ Arni): consolidate with other get_absolute_path functions.
/// Returns the absolute path from the project root.
pub fn get_absolute_path(relative_path: &str) -> PathBuf {
let base_dir = env::var("CARGO_MANIFEST_DIR")
let base_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"));
base_dir.join(relative_path)
}
1 change: 1 addition & 0 deletions crates/mempool_test_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::get_absolute_path;
use pretty_assertions::assert_ne;
use serde_json::to_string_pretty;
use starknet_api::block::GasPrice;
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_config/src/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::time::Duration;

use assert_matches::assert_matches;
use clap::Command;
use infra_utils::path::get_absolute_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;

Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_node/src/config/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::str::FromStr;

use assert_json_diff::assert_json_eq;
use colored::Colorize;
use infra_utils::path::get_absolute_path;
use itertools::Itertools;
use papyrus_base_layer::ethereum_base_layer_contract::EthereumBaseLayerConfig;
use papyrus_config::dumping::SerializeConfig;
Expand All @@ -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;

Expand Down
1 change: 1 addition & 0 deletions crates/starknet_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 2 additions & 11 deletions crates/starknet_api/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -13,19 +12,11 @@ pub mod deploy_account;
pub mod invoke;
pub mod l1_handler;

static PATH_TO_CARGO_MANIFEST_DIR: LazyLock<PathBuf> =
LazyLock::new(|| Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).into());

/// Returns the absolute path from the project root.
pub fn get_absolute_path<P: AsRef<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<P: AsRef<Path>>(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.
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_sequencer_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_sequencer_node/src/config/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::get_absolute_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,
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_sierra_compile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion crates/starknet_sierra_compile/src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::get_absolute_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;
Expand Down

0 comments on commit ca5efcc

Please sign in to comment.