From 67b795b512a95c7fa69cb496873fc48904faae03 Mon Sep 17 00:00:00 2001 From: Oleksandr Zarudnyi Date: Mon, 20 Jan 2025 15:06:00 +0800 Subject: [PATCH] improve test directory path handling --- .../src/directories/matter_labs/mod.rs | 19 +++---- compiler_tester/src/lib.rs | 53 ++++++++++--------- .../src/vm/eravm/system_contracts.rs | 36 ++++++------- 3 files changed, 53 insertions(+), 55 deletions(-) diff --git a/compiler_tester/src/directories/matter_labs/mod.rs b/compiler_tester/src/directories/matter_labs/mod.rs index 6d3e5479..0afee5e8 100644 --- a/compiler_tester/src/directories/matter_labs/mod.rs +++ b/compiler_tester/src/directories/matter_labs/mod.rs @@ -36,36 +36,29 @@ impl Collection for MatterLabsDirectory { let entry = entry?; let path = entry.path(); let entry_type = entry.file_type().map_err(|error| { - anyhow::anyhow!( - "Failed to get the type of file `{}`: {}", - path.to_string_lossy(), - error - ) + anyhow::anyhow!("Failed to get the type of file {path:?}: {error}") })?; if entry_type.is_dir() { tests.extend(Self::read_all( _target, - &path, + path.as_path(), extension, summary.clone(), filters, )?); continue; } else if !entry_type.is_file() { - anyhow::bail!("Invalid type of file `{}`", path.to_string_lossy()); + anyhow::bail!("Invalid type of file {path:?}"); } if entry.file_name().to_string_lossy().starts_with('.') { continue; } - let file_extension = path.extension().ok_or_else(|| { - anyhow::anyhow!( - "Failed to get the extension of file `{}`", - path.to_string_lossy() - ) - })?; + let file_extension = path + .extension() + .ok_or_else(|| anyhow::anyhow!("Failed to get the extension of file {path:?}"))?; if file_extension != extension { continue; } diff --git a/compiler_tester/src/lib.rs b/compiler_tester/src/lib.rs index ff5c2a57..caca803a 100644 --- a/compiler_tester/src/lib.rs +++ b/compiler_tester/src/lib.rs @@ -18,7 +18,7 @@ pub(crate) mod utils; pub(crate) mod vm; pub(crate) mod workflow; -use std::path::Path; +use std::path::PathBuf; use std::sync::Arc; use std::sync::Mutex; @@ -281,7 +281,7 @@ impl CompilerTester { tests.extend(self.directory::( target, - Self::SOLIDITY_SIMPLE, + PathBuf::from(Self::SOLIDITY_SIMPLE.replace("/", std::path::MAIN_SEPARATOR_STR)), era_compiler_common::EXTENSION_SOLIDITY, match toolchain { Toolchain::IrLLVM => solidity_compiler.clone(), @@ -291,14 +291,14 @@ impl CompilerTester { if let era_compiler_common::Target::EraVM = target { tests.extend(self.directory::( target, - Self::VYPER_SIMPLE, + PathBuf::from(Self::VYPER_SIMPLE.replace("/", std::path::MAIN_SEPARATOR_STR)), era_compiler_common::EXTENSION_VYPER, vyper_compiler.clone(), )?); } tests.extend(self.directory::( target, - Self::YUL_SIMPLE, + PathBuf::from(Self::YUL_SIMPLE.replace("/", std::path::MAIN_SEPARATOR_STR)), era_compiler_common::EXTENSION_YUL, match toolchain { Toolchain::IrLLVM => yul_compiler.clone(), @@ -307,20 +307,20 @@ impl CompilerTester { )?); tests.extend(self.directory::( target, - Self::LLVM_SIMPLE, + PathBuf::from(Self::LLVM_SIMPLE.replace("/", std::path::MAIN_SEPARATOR_STR)), era_compiler_common::EXTENSION_LLVM_SOURCE, llvm_compiler, )?); tests.extend(self.directory::( target, - Self::ERAVM_SIMPLE, + PathBuf::from(Self::ERAVM_SIMPLE.replace("/", std::path::MAIN_SEPARATOR_STR)), era_compiler_common::EXTENSION_ERAVM_ASSEMBLY, eravm_compiler, )?); tests.extend(self.directory::( target, - Self::SOLIDITY_COMPLEX, + PathBuf::from(Self::SOLIDITY_COMPLEX.replace("/", std::path::MAIN_SEPARATOR_STR)), era_compiler_common::EXTENSION_JSON, match toolchain { Toolchain::IrLLVM => solidity_compiler.clone(), @@ -330,28 +330,33 @@ impl CompilerTester { if let era_compiler_common::Target::EraVM = target { tests.extend(self.directory::( target, - Self::VYPER_COMPLEX, + PathBuf::from(Self::VYPER_COMPLEX.replace("/", std::path::MAIN_SEPARATOR_STR)), era_compiler_common::EXTENSION_JSON, vyper_compiler.clone(), )?); } - tests.extend(self.directory::( - target, - match target { - era_compiler_common::Target::EraVM => Self::SOLIDITY_ETHEREUM, - era_compiler_common::Target::EVM => Self::SOLIDITY_ETHEREUM_UPSTREAM, - }, - era_compiler_common::EXTENSION_SOLIDITY, - match toolchain { - Toolchain::IrLLVM => solidity_compiler.clone(), - Toolchain::Solc | Toolchain::SolcLLVM => solidity_upstream_compiler.clone(), - }, - )?); + tests.extend( + self.directory::( + target, + PathBuf::from( + match target { + era_compiler_common::Target::EraVM => Self::SOLIDITY_ETHEREUM, + era_compiler_common::Target::EVM => Self::SOLIDITY_ETHEREUM_UPSTREAM, + } + .replace("/", std::path::MAIN_SEPARATOR_STR), + ), + era_compiler_common::EXTENSION_SOLIDITY, + match toolchain { + Toolchain::IrLLVM => solidity_compiler.clone(), + Toolchain::Solc | Toolchain::SolcLLVM => solidity_upstream_compiler.clone(), + }, + )?, + ); if let era_compiler_common::Target::EraVM = target { tests.extend(self.directory::( target, - Self::VYPER_ETHEREUM, + PathBuf::from(Self::VYPER_ETHEREUM.replace("/", std::path::MAIN_SEPARATOR_STR)), era_compiler_common::EXTENSION_VYPER, vyper_compiler, )?); @@ -366,7 +371,7 @@ impl CompilerTester { fn directory( &self, target: era_compiler_common::Target, - path: &str, + path: PathBuf, extension: &'static str, compiler: Arc, ) -> anyhow::Result> @@ -375,12 +380,12 @@ impl CompilerTester { { Ok(T::read_all( target, - Path::new(path), + path.as_path(), extension, self.summary.clone(), &self.filters, ) - .map_err(|error| anyhow::anyhow!("Failed to read the tests directory `{path}`: {error}"))? + .map_err(|error| anyhow::anyhow!("Failed to read the tests directory {path:?}: {error}"))? .into_iter() .map(|test| Arc::new(test) as Arc) .cartesian_product(compiler.all_modes()) diff --git a/compiler_tester/src/vm/eravm/system_contracts.rs b/compiler_tester/src/vm/eravm/system_contracts.rs index e8659549..88aa8130 100644 --- a/compiler_tester/src/vm/eravm/system_contracts.rs +++ b/compiler_tester/src/vm/eravm/system_contracts.rs @@ -220,20 +220,20 @@ impl SystemContracts { let solidity_system_contracts = vec![ ( web3::types::Address::zero(), - Self::normalize_path_fs( + Self::normalize_name_fs( Self::PATH_EMPTY_CONTRACT.0, Some(Self::PATH_EMPTY_CONTRACT.1), ), ), ( web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_IDENTITY.into()), - Self::normalize_path_fs(Self::PATH_IDENTITY.0, Some(Self::PATH_IDENTITY.1)), + Self::normalize_name_fs(Self::PATH_IDENTITY.0, Some(Self::PATH_IDENTITY.1)), ), ( web3::types::Address::from_low_u64_be( zkevm_opcode_defs::ADDRESS_ACCOUNT_CODE_STORAGE.into(), ), - Self::normalize_path_fs( + Self::normalize_name_fs( Self::PATH_ACCOUNT_CODE_STORAGE.0, Some(Self::PATH_ACCOUNT_CODE_STORAGE.1), ), @@ -242,13 +242,13 @@ impl SystemContracts { web3::types::Address::from_low_u64_be( zkevm_opcode_defs::ADDRESS_NONCE_HOLDER.into(), ), - Self::normalize_path_fs(Self::PATH_NONCE_HOLDER.0, Some(Self::PATH_NONCE_HOLDER.1)), + Self::normalize_name_fs(Self::PATH_NONCE_HOLDER.0, Some(Self::PATH_NONCE_HOLDER.1)), ), ( web3::types::Address::from_low_u64_be( zkevm_opcode_defs::ADDRESS_KNOWN_CODES_STORAGE.into(), ), - Self::normalize_path_fs( + Self::normalize_name_fs( Self::PATH_KNOWN_CODES_STORAGE.0, Some(Self::PATH_KNOWN_CODES_STORAGE.1), ), @@ -257,7 +257,7 @@ impl SystemContracts { web3::types::Address::from_low_u64_be( zkevm_opcode_defs::ADDRESS_IMMUTABLE_SIMULATOR.into(), ), - Self::normalize_path_fs( + Self::normalize_name_fs( Self::PATH_IMMUTABLE_SIMULATOR.0, Some(Self::PATH_IMMUTABLE_SIMULATOR.1), ), @@ -266,7 +266,7 @@ impl SystemContracts { web3::types::Address::from_low_u64_be( zkevm_opcode_defs::ADDRESS_CONTRACT_DEPLOYER.into(), ), - Self::normalize_path_fs( + Self::normalize_name_fs( Self::PATH_CONTRACT_DEPLOYER.0, Some(Self::PATH_CONTRACT_DEPLOYER.1), ), @@ -275,11 +275,11 @@ impl SystemContracts { web3::types::Address::from_low_u64_be( zkevm_opcode_defs::ADDRESS_L1_MESSENGER.into(), ), - Self::normalize_path_fs(Self::PATH_L1_MESSENGER.0, Some(Self::PATH_L1_MESSENGER.1)), + Self::normalize_name_fs(Self::PATH_L1_MESSENGER.0, Some(Self::PATH_L1_MESSENGER.1)), ), ( web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_MSG_VALUE.into()), - Self::normalize_path_fs( + Self::normalize_name_fs( Self::PATH_MSG_VALUE_SIMULATOR.0, Some(Self::PATH_MSG_VALUE_SIMULATOR.1), ), @@ -288,14 +288,14 @@ impl SystemContracts { web3::types::Address::from_low_u64_be( zkevm_opcode_defs::ADDRESS_SYSTEM_CONTEXT.into(), ), - Self::normalize_path_fs( + Self::normalize_name_fs( Self::PATH_SYSTEM_CONTEXT.0, Some(Self::PATH_SYSTEM_CONTEXT.1), ), ), ( web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_ETH_TOKEN.into()), - Self::normalize_path_fs(Self::PATH_BASE_TOKEN.0, Some(Self::PATH_BASE_TOKEN.1)), + Self::normalize_name_fs(Self::PATH_BASE_TOKEN.0, Some(Self::PATH_BASE_TOKEN.1)), ), ]; @@ -368,14 +368,14 @@ impl SystemContracts { let default_aa = builds .remove( - Self::normalize_path_solc(Self::PATH_DEFAULT_AA.0, Some(Self::PATH_DEFAULT_AA.1)) + Self::normalize_name_solc(Self::PATH_DEFAULT_AA.0, Some(Self::PATH_DEFAULT_AA.1)) .as_str(), ) .ok_or_else(|| { anyhow::anyhow!("The default AA code not found in the compiler build artifacts") })?; let evm_emulator = builds - .remove(Self::normalize_path_solc(Self::PATH_EVM_EMULATOR, None).as_str()) + .remove(Self::normalize_name_solc(Self::PATH_EVM_EMULATOR, None).as_str()) .ok_or_else(|| { anyhow::anyhow!("The EVM emulator code not found in the compiler build artifacts") })?; @@ -388,7 +388,7 @@ impl SystemContracts { let mut deployed_contracts = Vec::with_capacity(system_contracts.len()); for (address, path) in system_contracts.into_iter() { let build = builds - .remove(Self::normalize_path_solc(path.as_str(), None).as_str()) + .remove(Self::normalize_name_solc(path.as_str(), None).as_str()) .unwrap_or_else(|| panic!("System contract `{path}` not found in the builds")); deployed_contracts.push((address, build)); } @@ -444,9 +444,9 @@ impl SystemContracts { } /// - /// Normalizes paths with respect to the file system. + /// Normalizes contract names with respect to the file system. /// - fn normalize_path_fs(path: &str, name: Option<&str>) -> String { + fn normalize_name_fs(path: &str, name: Option<&str>) -> String { let contract_name = era_compiler_common::ContractName::new( path.replace("/", std::path::MAIN_SEPARATOR_STR), name.map(|name| name.to_string()), @@ -455,9 +455,9 @@ impl SystemContracts { } /// - /// Normalizes paths with respect to `solc`. + /// Normalizes contract names with respect to `solc`. /// - fn normalize_path_solc(path: &str, name: Option<&str>) -> String { + fn normalize_name_solc(path: &str, name: Option<&str>) -> String { let contract_name = era_compiler_common::ContractName::new( path.replace(std::path::MAIN_SEPARATOR_STR, "/"), name.map(|name| name.to_string()),