Skip to content

Commit 5dcf01b

Browse files
authored
test: extract get_compiled to utils (#11779)
1 parent 2855a45 commit 5dcf01b

File tree

9 files changed

+133
-116
lines changed

9 files changed

+133
-116
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ foundry-compilers = { version = "0.19.1", default-features = false, features = [
215215
foundry-fork-db = "0.18"
216216
solang-parser = { version = "=0.3.9", package = "foundry-solang-parser" }
217217
solar = { package = "solar-compiler", version = "=0.1.7", default-features = false }
218+
svm = { package = "svm-rs", version = "0.5", default-features = false, features = [
219+
"rustls",
220+
] }
218221

219222
## alloy
220223
alloy-consensus = { version = "1.0.23", default-features = false }

crates/forge/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ mockall = "0.13"
111111
globset = "0.4"
112112
paste = "1.0"
113113
similar-asserts.workspace = true
114-
svm = { package = "svm-rs", version = "0.5", default-features = false, features = [
115-
"rustls",
116-
] }
114+
svm.workspace = true
117115
tempfile.workspace = true
118116

119117
alloy-signer-local.workspace = true

crates/forge/tests/cli/verify.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,12 @@ contract Verify is Unique {
7070
);
7171
}
7272

73-
#[expect(clippy::disallowed_macros)]
7473
fn parse_verification_result(cmd: &mut TestCommand, retries: u32) -> eyre::Result<()> {
7574
// Give Etherscan some time to verify the contract.
7675
Retry::new(retries, Duration::from_secs(30)).run(|| -> eyre::Result<()> {
7776
let output = cmd.execute();
7877
let out = String::from_utf8_lossy(&output.stdout);
79-
println!("{out}");
78+
test_debug!("{out}");
8079
if out.contains("Contract successfully verified") {
8180
return Ok(());
8281
}
@@ -155,11 +154,10 @@ fn deploy_contract(
155154
.unwrap_or_else(|| panic!("Failed to parse deployer {output}"))
156155
}
157156

158-
#[expect(clippy::disallowed_macros)]
159157
fn verify_on_chain(info: Option<EnvExternalities>, prj: TestProject, mut cmd: TestCommand) {
160158
// only execute if keys present
161159
if let Some(info) = info {
162-
println!("verifying on {}", info.chain);
160+
test_debug!("verifying on {}", info.chain);
163161

164162
let contract_path = "src/Verify.sol:Verify";
165163
let address = deploy_contract(&info, contract_path, prj, &mut cmd);
@@ -186,11 +184,10 @@ fn verify_on_chain(info: Option<EnvExternalities>, prj: TestProject, mut cmd: Te
186184
}
187185
}
188186

189-
#[expect(clippy::disallowed_macros)]
190187
fn guess_constructor_args(info: Option<EnvExternalities>, prj: TestProject, mut cmd: TestCommand) {
191188
// only execute if keys present
192189
if let Some(info) = info {
193-
println!("verifying on {}", info.chain);
190+
test_debug!("verifying on {}", info.chain);
194191
add_unique(&prj);
195192
add_verify_target_with_constructor(&prj);
196193

@@ -227,12 +224,11 @@ fn guess_constructor_args(info: Option<EnvExternalities>, prj: TestProject, mut
227224
}
228225
}
229226

230-
#[expect(clippy::disallowed_macros)]
231227
/// Executes create --verify on the given chain
232228
fn create_verify_on_chain(info: Option<EnvExternalities>, prj: TestProject, mut cmd: TestCommand) {
233229
// only execute if keys present
234230
if let Some(info) = info {
235-
println!("verifying on {}", info.chain);
231+
test_debug!("verifying on {}", info.chain);
236232
add_single_verify_target_file(&prj);
237233

238234
let contract_path = "src/Verify.sol:Verify";

crates/forge/tests/it/cheats.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use crate::{
33
config::*,
44
test_helpers::{
55
ForgeTestData, ForgeTestProfile, RE_PATH_SEPARATOR, TEST_DATA_DEFAULT,
6-
TEST_DATA_MULTI_VERSION, TEST_DATA_PARIS, get_compiled,
6+
TEST_DATA_MULTI_VERSION, TEST_DATA_PARIS,
77
},
88
};
99
use alloy_primitives::U256;
1010
use foundry_cli::utils::install_crypto_provider;
1111
use foundry_compilers::artifacts::output_selection::ContractOutputSelection;
1212
use foundry_config::{FsPermissions, fs_permissions::PathPermission};
13-
use foundry_test_utils::{Filter, init_tracing};
13+
use foundry_test_utils::{Filter, init_tracing, util::get_compiled};
1414

1515
/// Executes all cheat code tests but not fork cheat codes or tests that require isolation mode or
1616
/// specific seed.

crates/forge/tests/it/test_helpers.rs

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,29 @@ use alloy_primitives::U256;
55
use forge::{MultiContractRunner, MultiContractRunnerBuilder};
66
use foundry_cli::utils::install_crypto_provider;
77
use foundry_compilers::{
8-
Project, ProjectCompileOutput, SolcConfig, Vyper,
8+
Project, ProjectCompileOutput, SolcConfig,
99
artifacts::{EvmVersion, Libraries, Settings},
1010
compilers::multi::MultiCompiler,
11-
utils::RuntimeOrHandle,
1211
};
1312
use foundry_config::{
1413
Config, FsPermissions, FuzzConfig, FuzzCorpusConfig, FuzzDictionaryConfig, InvariantConfig,
1514
RpcEndpointUrl, RpcEndpoints, fs_permissions::PathPermission,
1615
};
1716
use foundry_evm::{constants::CALLER, opts::EvmOpts};
1817
use foundry_test_utils::{
19-
fd_lock, init_tracing,
18+
init_tracing,
2019
rpc::{next_http_archive_rpc_url, next_rpc_endpoint},
21-
test_debug,
20+
util::get_compiled,
2221
};
2322
use revm::primitives::hardfork::SpecId;
2423
use std::{
2524
env, fmt,
26-
io::Write,
2725
path::{Path, PathBuf},
2826
sync::{Arc, LazyLock},
2927
};
3028

3129
pub const RE_PATH_SEPARATOR: &str = "/";
3230
const TESTDATA: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../testdata");
33-
static VYPER: LazyLock<PathBuf> = LazyLock::new(|| std::env::temp_dir().join("vyper"));
3431

3532
/// Profile for the tests group. Used to configure separate configurations for test runs.
3633
pub enum ForgeTestProfile {
@@ -256,84 +253,6 @@ impl ForgeTestData {
256253
}
257254
}
258255

259-
/// Installs Vyper if it's not already present.
260-
pub fn get_vyper() -> Vyper {
261-
if let Ok(vyper) = Vyper::new("vyper") {
262-
return vyper;
263-
}
264-
if let Ok(vyper) = Vyper::new(&*VYPER) {
265-
return vyper;
266-
}
267-
RuntimeOrHandle::new().block_on(async {
268-
#[cfg(target_family = "unix")]
269-
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
270-
271-
let suffix = match svm::platform() {
272-
svm::Platform::MacOsAarch64 => "darwin",
273-
svm::Platform::LinuxAmd64 => "linux",
274-
svm::Platform::WindowsAmd64 => "windows.exe",
275-
platform => panic!(
276-
"unsupported platform {platform:?} for installing vyper, \
277-
install it manually and add it to $PATH"
278-
),
279-
};
280-
let url = format!("https://github.com/vyperlang/vyper/releases/download/v0.4.3/vyper.0.4.3+commit.bff19ea2.{suffix}");
281-
282-
test_debug!("downloading vyper from {url}");
283-
let res = reqwest::Client::builder().build().unwrap().get(url).send().await.unwrap();
284-
285-
assert!(res.status().is_success());
286-
287-
let bytes = res.bytes().await.unwrap();
288-
289-
std::fs::write(&*VYPER, bytes).unwrap();
290-
291-
#[cfg(target_family = "unix")]
292-
std::fs::set_permissions(&*VYPER, Permissions::from_mode(0o755)).unwrap();
293-
294-
Vyper::new(&*VYPER).unwrap()
295-
})
296-
}
297-
298-
#[tracing::instrument]
299-
pub fn get_compiled(project: &mut Project) -> ProjectCompileOutput {
300-
let lock_file_path = project.sources_path().join(".lock");
301-
// Compile only once per test run.
302-
// We need to use a file lock because `cargo-nextest` runs tests in different processes.
303-
// This is similar to [`foundry_test_utils::util::initialize`], see its comments for more
304-
// details.
305-
let mut lock = fd_lock::new_lock(&lock_file_path);
306-
let read = lock.read().unwrap();
307-
let out;
308-
309-
let mut write = None;
310-
if !project.cache_path().exists() || std::fs::read(&lock_file_path).unwrap() != b"1" {
311-
drop(read);
312-
write = Some(lock.write().unwrap());
313-
test_debug!("cache miss for {}", lock_file_path.display());
314-
} else {
315-
test_debug!("cache hit for {}", lock_file_path.display());
316-
}
317-
318-
if project.compiler.vyper.is_none() {
319-
project.compiler.vyper = Some(get_vyper());
320-
}
321-
322-
test_debug!("compiling {}", lock_file_path.display());
323-
out = project.compile().unwrap();
324-
test_debug!("compiled {}", lock_file_path.display());
325-
326-
if out.has_compiler_errors() {
327-
panic!("Compiled with errors:\n{out}");
328-
}
329-
330-
if let Some(ref mut write) = write {
331-
write.write_all(b"1").unwrap();
332-
}
333-
334-
out
335-
}
336-
337256
/// Default data for the tests group.
338257
pub static TEST_DATA_DEFAULT: LazyLock<ForgeTestData> =
339258
LazyLock::new(|| ForgeTestData::new(ForgeTestProfile::Default));

crates/test-utils/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ rand.workspace = true
3333
snapbox = { version = "0.6", features = ["json", "regex", "term-svg"] }
3434
tempfile.workspace = true
3535
ui_test = "0.30.2"
36+
reqwest.workspace = true
37+
svm.workspace = true
3638

3739
# Pinned dependencies. See /Cargo.toml.
3840
[target.'cfg(any())'.dependencies]

crates/test-utils/src/fd_lock.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ pub fn new_lock(lock_path: impl AsRef<Path>) -> RwLock<File> {
1919
}
2020
new_lock(lock_path.as_ref())
2121
}
22+
23+
pub(crate) const LOCK_TOKEN: &[u8] = b"1";
24+
25+
pub(crate) fn lock_exists(lock_path: &Path) -> bool {
26+
std::fs::read(lock_path).is_ok_and(|b| b == LOCK_TOKEN)
27+
}

0 commit comments

Comments
 (0)