From 978a227421bc8eb78dc76c4cda70a41ace389957 Mon Sep 17 00:00:00 2001 From: iPLAY888 Date: Mon, 5 Jan 2026 15:06:05 +0300 Subject: [PATCH] refactor(revme): consolidate find_all_json_tests into dir_utils Eliminate code duplication by consolidating three separate implementations of find_all_json_tests into a single shared implementation in the dir_utils module. This reduces maintenance burden and ensures consistent behavior across blockchaintest and statetest commands. --- bins/revme/src/cmd/blockchaintest.rs | 18 +----------------- bins/revme/src/cmd/statetest.rs | 3 ++- bins/revme/src/cmd/statetest/runner.rs | 17 ----------------- bins/revme/src/dir_utils.rs | 24 +++++++++++++----------- 4 files changed, 16 insertions(+), 46 deletions(-) diff --git a/bins/revme/src/cmd/blockchaintest.rs b/bins/revme/src/cmd/blockchaintest.rs index 10461e0779..359de0d1d3 100644 --- a/bins/revme/src/cmd/blockchaintest.rs +++ b/bins/revme/src/cmd/blockchaintest.rs @@ -1,6 +1,7 @@ pub mod post_block; pub mod pre_block; +use crate::dir_utils::find_all_json_tests; use clap::Parser; use revm::statetest_types::blockchain::{ @@ -26,7 +27,6 @@ use std::{ time::Instant, }; use thiserror::Error; -use walkdir::{DirEntry, WalkDir}; /// Panics if the value cannot be serialized to JSON. fn print_json(value: &T) { @@ -86,22 +86,6 @@ impl Cmd { } } -/// Find all JSON test files in the given path -/// If path is a file, returns it in a vector -/// If path is a directory, recursively finds all .json files -pub fn find_all_json_tests(path: &Path) -> Vec { - if path.is_file() { - vec![path.to_path_buf()] - } else { - WalkDir::new(path) - .into_iter() - .filter_map(Result::ok) - .filter(|e| e.path().extension() == Some("json".as_ref())) - .map(DirEntry::into_path) - .collect() - } -} - /// Run all blockchain tests from the given files fn run_tests( test_files: Vec, diff --git a/bins/revme/src/cmd/statetest.rs b/bins/revme/src/cmd/statetest.rs index 3094316d53..5c583c0d18 100644 --- a/bins/revme/src/cmd/statetest.rs +++ b/bins/revme/src/cmd/statetest.rs @@ -4,8 +4,9 @@ pub mod utils; pub use runner::{TestError as Error, TestErrorKind}; +use crate::dir_utils::find_all_json_tests; use clap::Parser; -use runner::{find_all_json_tests, run, TestError}; +use runner::{run, TestError}; use std::path::PathBuf; /// `statetest` subcommand diff --git a/bins/revme/src/cmd/statetest/runner.rs b/bins/revme/src/cmd/statetest/runner.rs index 183f599c70..945d94882a 100644 --- a/bins/revme/src/cmd/statetest/runner.rs +++ b/bins/revme/src/cmd/statetest/runner.rs @@ -26,7 +26,6 @@ use std::{ time::{Duration, Instant}, }; use thiserror::Error; -use walkdir::{DirEntry, WalkDir}; /// Error that occurs during test execution #[derive(Debug, Error)] @@ -66,22 +65,6 @@ pub enum TestErrorKind { NoJsonFiles, } -/// Find all JSON test files in the given path -/// If path is a file, returns it in a vector -/// If path is a directory, recursively finds all .json files -pub fn find_all_json_tests(path: &Path) -> Vec { - if path.is_file() { - vec![path.to_path_buf()] - } else { - WalkDir::new(path) - .into_iter() - .filter_map(Result::ok) - .filter(|e| e.path().extension() == Some("json".as_ref())) - .map(DirEntry::into_path) - .collect() - } -} - /// Check if a test should be skipped based on its filename /// Some tests are known to be problematic or take too long fn skip_test(path: &Path) -> bool { diff --git a/bins/revme/src/dir_utils.rs b/bins/revme/src/dir_utils.rs index 09406cdd93..5cc2c40a49 100644 --- a/bins/revme/src/dir_utils.rs +++ b/bins/revme/src/dir_utils.rs @@ -1,16 +1,18 @@ use std::path::{Path, PathBuf}; use walkdir::{DirEntry, WalkDir}; +/// Find all JSON test files in the given path. +/// If path is a file, returns it in a vector. +/// If path is a directory, recursively finds all .json files. pub fn find_all_json_tests(path: &Path) -> Vec { - WalkDir::new(path) - .into_iter() - .filter_map(|e| e.ok()) - .filter(|e| { - e.path() - .extension() - .map(|ext| ext == "json") - .unwrap_or(false) - }) - .map(DirEntry::into_path) - .collect::>() + if path.is_file() { + vec![path.to_path_buf()] + } else { + WalkDir::new(path) + .into_iter() + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some("json".as_ref())) + .map(DirEntry::into_path) + .collect() + } }