Add a cached fs abstraction

This commit is contained in:
Omar Abdulla
2025-08-14 13:12:23 +03:00
parent f2045db0e9
commit 7f4fadf7b1
7 changed files with 83 additions and 41 deletions
+6 -6
View File
@@ -39,9 +39,9 @@ impl Corpus {
}
/// Scan the corpus base directory and return all tests found.
pub fn enumerate_tests(&self) -> Vec<MetadataFile> {
pub async fn enumerate_tests(&self) -> Vec<MetadataFile> {
let mut tests = Vec::new();
collect_metadata(&self.path, &mut tests);
collect_metadata(&self.path, &mut tests).await;
tests
}
}
@@ -52,7 +52,7 @@ impl Corpus {
/// Found tests are inserted into `tests`.
///
/// `path` is expected to be a directory.
pub fn collect_metadata(path: &Path, tests: &mut Vec<MetadataFile>) {
pub async fn collect_metadata(path: &Path, tests: &mut Vec<MetadataFile>) {
if path.is_dir() {
let dir_entry = match std::fs::read_dir(path) {
Ok(dir_entry) => dir_entry,
@@ -73,12 +73,12 @@ pub fn collect_metadata(path: &Path, tests: &mut Vec<MetadataFile>) {
let path = entry.path();
if path.is_dir() {
collect_metadata(&path, tests);
Box::pin(collect_metadata(&path, tests)).await;
continue;
}
if path.is_file() {
if let Some(metadata) = MetadataFile::try_from_file(&path) {
if let Some(metadata) = MetadataFile::try_from_file(&path).await {
tests.push(metadata)
}
}
@@ -89,7 +89,7 @@ pub fn collect_metadata(path: &Path, tests: &mut Vec<MetadataFile>) {
return;
};
if extension.eq_ignore_ascii_case("sol") || extension.eq_ignore_ascii_case("json") {
if let Some(metadata) = MetadataFile::try_from_file(path) {
if let Some(metadata) = MetadataFile::try_from_file(path).await {
tests.push(metadata)
}
} else {
+15 -12
View File
@@ -2,7 +2,6 @@ use std::{
cmp::Ordering,
collections::BTreeMap,
fmt::Display,
fs::{File, read_to_string},
ops::Deref,
path::{Path, PathBuf},
str::FromStr,
@@ -11,7 +10,9 @@ use std::{
use serde::{Deserialize, Serialize};
use revive_common::EVMVersion;
use revive_dt_common::{iterators::FilesWithExtensionIterator, macros::define_wrapper_type};
use revive_dt_common::{
fs::CachedFileSystem, iterators::FilesWithExtensionIterator, macros::define_wrapper_type,
};
use crate::{
case::Case,
@@ -29,8 +30,8 @@ pub struct MetadataFile {
}
impl MetadataFile {
pub fn try_from_file(path: &Path) -> Option<Self> {
Metadata::try_from_file(path).map(|metadata| Self {
pub async fn try_from_file(path: &Path) -> Option<Self> {
Metadata::try_from_file(path).await.map(|metadata| Self {
path: path.to_owned(),
content: metadata,
})
@@ -151,7 +152,7 @@ impl Metadata {
///
/// # Panics
/// Expects the supplied `path` to be a file.
pub fn try_from_file(path: &Path) -> Option<Self> {
pub async fn try_from_file(path: &Path) -> Option<Self> {
assert!(path.is_file(), "not a file: {}", path.display());
let Some(file_extension) = path.extension() else {
@@ -160,19 +161,20 @@ impl Metadata {
};
if file_extension == METADATA_FILE_EXTENSION {
return Self::try_from_json(path);
return Self::try_from_json(path).await;
}
if file_extension == SOLIDITY_CASE_FILE_EXTENSION {
return Self::try_from_solidity(path);
return Self::try_from_solidity(path).await;
}
tracing::debug!("ignoring invalid corpus file: {}", path.display());
None
}
fn try_from_json(path: &Path) -> Option<Self> {
let file = File::open(path)
async fn try_from_json(path: &Path) -> Option<Self> {
let content = CachedFileSystem::read(path)
.await
.inspect_err(|error| {
tracing::error!(
"opening JSON test metadata file '{}' error: {error}",
@@ -181,7 +183,7 @@ impl Metadata {
})
.ok()?;
match serde_json::from_reader::<_, Metadata>(file) {
match serde_json::from_slice::<Metadata>(content.as_slice()) {
Ok(mut metadata) => {
metadata.file_path = Some(path.to_path_buf());
Some(metadata)
@@ -196,8 +198,9 @@ impl Metadata {
}
}
fn try_from_solidity(path: &Path) -> Option<Self> {
let spec = read_to_string(path)
async fn try_from_solidity(path: &Path) -> Option<Self> {
let spec = CachedFileSystem::read_to_string(path)
.await
.inspect_err(|error| {
tracing::error!(
"opening JSON test metadata file '{}' error: {error}",