refactor: add a root-path argument to build the necessary paths ouf of it, instead of passing them separately

This commit is contained in:
adrian
2025-09-11 15:00:52 +02:00
committed by Adrian Benavides
parent aedab8e8b2
commit fc56311a81
4 changed files with 68 additions and 45 deletions

View File

@@ -16,8 +16,8 @@ for test in "${args[@]}"; do
protocol="${test%%=*}" protocol="${test%%=*}"
filter="${test#*=}" filter="${test#*=}"
if [[ "$test" == *"="* ]]; then if [[ "$test" == *"="* ]]; then
tycho-protocol-sdk --package-path "/app/substreams/$protocol" --db-url "$DATABASE_URL" --evm-path "/app/evm" --match-test "$filter" tycho-protocol-sdk --package "$protocol" --db-url "$DATABASE_URL" --match-test "$filter"
else else
tycho-protocol-sdk --package-path "/app/substreams/$protocol" --db-url "$DATABASE_URL" --evm-path "/app/evm" tycho-protocol-sdk --package "$protocol" --db-url "$DATABASE_URL"
fi fi
done done

View File

@@ -7,10 +7,10 @@ mod tycho_rpc;
mod tycho_runner; mod tycho_runner;
mod utils; mod utils;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use clap::Parser; use clap::Parser;
use miette::miette; use miette::{miette, IntoDiagnostic, WrapErr};
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
use crate::test_runner::TestRunner; use crate::test_runner::TestRunner;
@@ -18,17 +18,14 @@ use crate::test_runner::TestRunner;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about = "Run indexer within a specified range of blocks")] #[command(version, about = "Run indexer within a specified range of blocks")]
struct Args { struct Args {
/// Name of the package to test /// Path to the root directory containing all packages. If not provided, it will look for
#[arg(long, required_unless_present = "package_path", conflicts_with = "package_path")] /// packages in the current working directory.
package: Option<String>,
/// Path to the package to test
#[arg(long, required_unless_present = "package", conflicts_with = "package")]
package_path: Option<String>,
/// Path to the evm directory. If not provided, it will look for it in `../evm`
#[arg(long)] #[arg(long)]
evm_path: Option<PathBuf>, root_path: Option<PathBuf>,
/// Name of the package to test
#[arg(long)]
package: String,
/// If provided, only run the tests with a matching name /// If provided, only run the tests with a matching name
#[arg(long)] #[arg(long)]
@@ -48,28 +45,30 @@ struct Args {
} }
impl Args { impl Args {
fn package_path(&self) -> miette::Result<PathBuf> { fn root_path(&self) -> miette::Result<PathBuf> {
match (self.package_path.as_ref(), self.package.as_ref()) { match self.root_path.as_ref() {
(Some(path), _) => Ok(Path::new(path).to_path_buf()),
(_, Some(package)) => Ok(Path::new("../substreams").join(package)),
_ => Err(miette!("Either package or package_path must be provided")),
}
}
fn evm_path(&self) -> miette::Result<PathBuf> {
match self.evm_path.as_ref() {
Some(path) => Ok(path.clone()), Some(path) => Ok(path.clone()),
None => { None => {
let current_dir = std::env::current_dir() let current_dir = std::env::current_dir()
.map_err(|e| miette!("Failed to get current directory: {}", e))?; .into_diagnostic()
let mut evm_dir = current_dir.join("../evm"); .wrap_err("Failed to get current directory")?;
if !evm_dir.exists() { let expected_child_dirs = ["evm", "proto", "substreams"];
evm_dir = current_dir.join("evm"); if expected_child_dirs
if !evm_dir.exists() { .iter()
return Err(miette!("evm directory not found")); .all(|dir| current_dir.join(dir).exists())
{
return Ok(current_dir);
} }
let parent_dir = current_dir
.parent()
.ok_or_else(|| miette!("Current directory has no parent directory"))?;
if expected_child_dirs
.iter()
.all(|dir| parent_dir.join(dir).exists())
{
return Ok(parent_dir.to_path_buf());
} }
Ok(evm_dir) Err(miette!("Couldn't find a valid path from {}", current_dir.display()))
} }
} }
} }
@@ -84,8 +83,8 @@ fn main() -> miette::Result<()> {
let args = Args::parse(); let args = Args::parse();
let test_runner = TestRunner::new( let test_runner = TestRunner::new(
args.package_path()?, args.root_path()?,
args.evm_path()?, args.package,
args.match_test, args.match_test,
args.tycho_logs, args.tycho_logs,
args.db_url, args.db_url,

View File

@@ -11,7 +11,7 @@ use num_bigint::BigUint;
use num_traits::Zero; use num_traits::Zero;
use postgres::{Client, Error, NoTls}; use postgres::{Client, Error, NoTls};
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use tracing::{debug, error, info}; use tracing::{debug, error, info, warn};
use tycho_client::feed::BlockHeader; use tycho_client::feed::BlockHeader;
use tycho_common::{ use tycho_common::{
dto::{Chain, ProtocolComponent, ResponseAccount, ResponseProtocolState}, dto::{Chain, ProtocolComponent, ResponseAccount, ResponseProtocolState},
@@ -52,13 +52,17 @@ pub struct TestRunner {
impl TestRunner { impl TestRunner {
pub fn new( pub fn new(
substreams_path: PathBuf, root_path: PathBuf,
evm_path: PathBuf, protocol: String,
match_test: Option<String>, match_test: Option<String>,
tycho_logs: bool, tycho_logs: bool,
db_url: String, db_url: String,
vm_traces: bool, vm_traces: bool,
) -> Self { ) -> Self {
let substreams_path = root_path
.join("substreams")
.join(protocol);
let evm_path = root_path.join("evm");
let adapter_contract_builder = let adapter_contract_builder =
AdapterContractBuilder::new(evm_path.to_string_lossy().to_string()); AdapterContractBuilder::new(evm_path.to_string_lossy().to_string());
Self { Self {
@@ -81,6 +85,22 @@ impl TestRunner {
.substreams_path .substreams_path
.join("integration_test.tycho.yaml"); .join("integration_test.tycho.yaml");
// Skip if test files don't exist
if !config_yaml_path.exists() {
warn!(
"integration_test.tycho.yaml file not found at {}",
self.substreams_path.display()
);
return Ok(());
}
let substreams_yaml_path = self
.substreams_path
.join("substreams.yaml");
if !substreams_yaml_path.exists() {
warn!("substreams.yaml file not found at {}", self.substreams_path.display());
return Ok(());
}
let config = Self::parse_config(&config_yaml_path)?; let config = Self::parse_config(&config_yaml_path)?;
let tests = match &self.match_test { let tests = match &self.match_test {
@@ -128,7 +148,7 @@ impl TestRunner {
} }
fn parse_config(config_yaml_path: &PathBuf) -> miette::Result<IntegrationTestsConfig> { fn parse_config(config_yaml_path: &PathBuf) -> miette::Result<IntegrationTestsConfig> {
info!("Config YAML: {}", config_yaml_path.display()); info!("Parsing config YAML at {}", config_yaml_path.display());
let yaml = Yaml::file(config_yaml_path); let yaml = Yaml::file(config_yaml_path);
let figment = Figment::new().merge(yaml); let figment = Figment::new().merge(yaml);
let config = figment let config = figment

View File

@@ -9,11 +9,12 @@ use figment::{
value::Value, value::Value,
Figment, Figment,
}; };
use miette::IntoDiagnostic; use miette::{miette, IntoDiagnostic};
use tracing::error; use tracing::{error, info};
/// Build a Substreams package with modifications to the YAML file. /// Build a Substreams package with modifications to the YAML file.
pub fn build_spkg(yaml_file_path: &PathBuf, initial_block: u64) -> miette::Result<String> { pub fn build_spkg(yaml_file_path: &PathBuf, initial_block: u64) -> miette::Result<String> {
info!("Building spkg from {:?}", yaml_file_path);
// Create a backup file of the unmodified Substreams protocol YAML config file. // Create a backup file of the unmodified Substreams protocol YAML config file.
let backup_file_path = yaml_file_path.with_extension("backup"); let backup_file_path = yaml_file_path.with_extension("backup");
fs::copy(yaml_file_path, &backup_file_path).into_diagnostic()?; fs::copy(yaml_file_path, &backup_file_path).into_diagnostic()?;
@@ -55,7 +56,13 @@ pub fn build_spkg(yaml_file_path: &PathBuf, initial_block: u64) -> miette::Resul
fs::write(yaml_file_path, yaml_string).into_diagnostic()?; fs::write(yaml_file_path, yaml_string).into_diagnostic()?;
// Run the substreams pack command to create the spkg // Run the substreams pack command to create the spkg
// WARNING: Ensure substreams is in the PATH if Command::new("substreams")
.arg("--version")
.output()
.is_err()
{
return Err(miette!("Substreams CLI is not installed or not found in PATH"));
}
match Command::new("substreams") match Command::new("substreams")
.arg("pack") .arg("pack")
.arg(yaml_file_path) .arg(yaml_file_path)
@@ -71,10 +78,7 @@ pub fn build_spkg(yaml_file_path: &PathBuf, initial_block: u64) -> miette::Resul
} }
Err(e) => { Err(e) => {
error!( error!(
"Error running substreams pack command: {}. \ "Error running substreams pack command. Ensure that the wasm target was built. {e}",
Ensure that the wasm target was built and that substreams CLI\
is installed and exported on PATH",
e
); );
} }
} }