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

@@ -7,10 +7,10 @@ mod tycho_rpc;
mod tycho_runner;
mod utils;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use clap::Parser;
use miette::miette;
use miette::{miette, IntoDiagnostic, WrapErr};
use tracing_subscriber::EnvFilter;
use crate::test_runner::TestRunner;
@@ -18,17 +18,14 @@ use crate::test_runner::TestRunner;
#[derive(Parser, Debug)]
#[command(version, about = "Run indexer within a specified range of blocks")]
struct Args {
/// Name of the package to test
#[arg(long, required_unless_present = "package_path", conflicts_with = "package_path")]
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`
/// Path to the root directory containing all packages. If not provided, it will look for
/// packages in the current working directory.
#[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
#[arg(long)]
@@ -48,28 +45,30 @@ struct Args {
}
impl Args {
fn package_path(&self) -> miette::Result<PathBuf> {
match (self.package_path.as_ref(), self.package.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() {
fn root_path(&self) -> miette::Result<PathBuf> {
match self.root_path.as_ref() {
Some(path) => Ok(path.clone()),
None => {
let current_dir = std::env::current_dir()
.map_err(|e| miette!("Failed to get current directory: {}", e))?;
let mut evm_dir = current_dir.join("../evm");
if !evm_dir.exists() {
evm_dir = current_dir.join("evm");
if !evm_dir.exists() {
return Err(miette!("evm directory not found"));
}
.into_diagnostic()
.wrap_err("Failed to get current directory")?;
let expected_child_dirs = ["evm", "proto", "substreams"];
if expected_child_dirs
.iter()
.all(|dir| current_dir.join(dir).exists())
{
return Ok(current_dir);
}
Ok(evm_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());
}
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 test_runner = TestRunner::new(
args.package_path()?,
args.evm_path()?,
args.root_path()?,
args.package,
args.match_test,
args.tycho_logs,
args.db_url,