Skip to content

Commit 844f3f5

Browse files
authored
perf: borrow output when building test runner (#8294)
* chore: borrow output when building test runner * chore: strip always * chore: lenient stripping * fix * fix: don't actually strip always
1 parent ed79650 commit 844f3f5

File tree

6 files changed

+32
-30
lines changed

6 files changed

+32
-30
lines changed

crates/common/src/compile.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,7 @@ impl ContractSources {
292292
libraries: Option<&Libraries>,
293293
) -> Result<Self> {
294294
let mut sources = Self::default();
295-
296295
sources.insert(output, root, libraries)?;
297-
298296
Ok(sources)
299297
}
300298

crates/forge/bin/cmd/clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ pub fn find_main_contract<'a>(
571571
rv = Some((PathBuf::from(f), a));
572572
}
573573
}
574-
rv.ok_or(eyre::eyre!("contract not found"))
574+
rv.ok_or_else(|| eyre::eyre!("contract not found"))
575575
}
576576

577577
#[cfg(test)]

crates/forge/bin/cmd/coverage.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl CoverageArgs {
9090
let report = self.prepare(&project, &output)?;
9191

9292
p_println!(!self.test.build_args().silent => "Running tests...");
93-
self.collect(project, output, report, Arc::new(config), evm_opts).await
93+
self.collect(project, &output, report, Arc::new(config), evm_opts).await
9494
}
9595

9696
/// Builds the project.
@@ -222,7 +222,7 @@ impl CoverageArgs {
222222
async fn collect(
223223
self,
224224
project: Project,
225-
output: ProjectCompileOutput,
225+
output: &ProjectCompileOutput,
226226
mut report: CoverageReport,
227227
config: Arc<Config>,
228228
evm_opts: EvmOpts,

crates/forge/bin/cmd/test/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,7 @@ impl TestArgs {
294294

295295
// Prepare the test builder
296296
let should_debug = self.debug.is_some();
297-
298-
// Clone the output only if we actually need it later for the debugger.
299-
let output_clone = should_debug.then(|| output.clone());
300-
301297
let config = Arc::new(config);
302-
303298
let runner = MultiContractRunnerBuilder::new(config.clone())
304299
.set_debug(should_debug)
305300
.initial_balance(evm_opts.initial_balance)
@@ -308,7 +303,7 @@ impl TestArgs {
308303
.with_fork(evm_opts.get_fork(&config, env.clone()))
309304
.with_test_options(test_options)
310305
.enable_isolation(evm_opts.isolate)
311-
.build(project_root, output, env, evm_opts)?;
306+
.build(project_root, &output, env, evm_opts)?;
312307

313308
if let Some(debug_test_pattern) = &self.debug {
314309
let test_pattern = &mut filter.args_mut().test_pattern;
@@ -335,11 +330,8 @@ impl TestArgs {
335330
return Err(eyre::eyre!("no tests were executed"));
336331
};
337332

338-
let sources = ContractSources::from_project_output(
339-
output_clone.as_ref().unwrap(),
340-
project.root(),
341-
Some(&libraries),
342-
)?;
333+
let sources =
334+
ContractSources::from_project_output(&output, project.root(), Some(&libraries))?;
343335

344336
// Run the debugger.
345337
let mut builder = Debugger::builder()

crates/forge/src/multi_runner.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,21 @@ impl MultiContractRunnerBuilder {
365365
pub fn build<C: Compiler>(
366366
self,
367367
root: &Path,
368-
output: ProjectCompileOutput<C>,
368+
output: &ProjectCompileOutput<C>,
369369
env: revm::primitives::Env,
370370
evm_opts: EvmOpts,
371371
) -> Result<MultiContractRunner> {
372-
let output = output.with_stripped_file_prefixes(root);
373-
let linker = Linker::new(root, output.artifact_ids().collect());
372+
let contracts = output
373+
.artifact_ids()
374+
.map(|(mut id, v)| {
375+
// TODO: Use ArtifactId::with_stripped_file_prefixes
376+
if let Ok(stripped) = id.source.strip_prefix(root) {
377+
id.source = stripped.to_path_buf();
378+
}
379+
(id, v)
380+
})
381+
.collect();
382+
let linker = Linker::new(root, contracts);
374383

375384
// Build revert decoder from ABIs of all artifacts.
376385
let abis = linker

crates/forge/tests/it/test_helpers.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ impl ForgeTestData {
175175
///
176176
/// Uses [get_compiled] to lazily compile the project.
177177
pub fn new(profile: ForgeTestProfile) -> Self {
178+
init_tracing();
179+
178180
let mut project = profile.project();
179181
let output = get_compiled(&mut project);
180182
let test_opts = profile.test_opts(&output);
@@ -220,9 +222,6 @@ impl ForgeTestData {
220222
opts.isolate = true;
221223
}
222224

223-
let env = opts.local_evm_env();
224-
let output = self.output.clone();
225-
226225
let sender = config.sender;
227226

228227
let mut builder = self.base_runner();
@@ -231,7 +230,7 @@ impl ForgeTestData {
231230
.enable_isolation(opts.isolate)
232231
.sender(sender)
233232
.with_test_options(self.test_opts.clone())
234-
.build(root, output, env, opts)
233+
.build(root, &self.output, opts.local_evm_env(), opts)
235234
.unwrap()
236235
}
237236

@@ -240,7 +239,7 @@ impl ForgeTestData {
240239
let mut opts = self.evm_opts.clone();
241240
opts.verbosity = 5;
242241
self.base_runner()
243-
.build(self.project.root(), self.output.clone(), opts.local_evm_env(), opts)
242+
.build(self.project.root(), &self.output, opts.local_evm_env(), opts)
244243
.unwrap()
245244
}
246245

@@ -256,7 +255,7 @@ impl ForgeTestData {
256255

257256
self.base_runner()
258257
.with_fork(fork)
259-
.build(self.project.root(), self.output.clone(), env, opts)
258+
.build(self.project.root(), &self.output, env, opts)
260259
.unwrap()
261260
}
262261
}
@@ -273,12 +272,16 @@ pub fn get_vyper() -> Vyper {
273272
#[cfg(target_family = "unix")]
274273
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
275274

276-
let url = match svm::platform() {
277-
svm::Platform::MacOsAarch64 => "https://github.com/vyperlang/vyper/releases/download/v0.4.0rc6/vyper.0.4.0rc6+commit.33719560.darwin",
278-
svm::Platform::LinuxAmd64 => "https://github.com/vyperlang/vyper/releases/download/v0.4.0rc6/vyper.0.4.0rc6+commit.33719560.linux",
279-
svm::Platform::WindowsAmd64 => "https://github.com/vyperlang/vyper/releases/download/v0.4.0rc6/vyper.0.4.0rc6+commit.33719560.windows.exe",
280-
_ => panic!("unsupported")
275+
let suffix = match svm::platform() {
276+
svm::Platform::MacOsAarch64 => "darwin",
277+
svm::Platform::LinuxAmd64 => "linux",
278+
svm::Platform::WindowsAmd64 => "windows.exe",
279+
platform => panic!(
280+
"unsupported platform {platform:?} for installing vyper, \
281+
install it manually and add it to $PATH"
282+
),
281283
};
284+
let url = format!("https://github.com/vyperlang/vyper/releases/download/v0.4.0/vyper.0.4.0+commit.e9db8d9f.{suffix}");
282285

283286
let res = reqwest::Client::builder().build().unwrap().get(url).send().await.unwrap();
284287

0 commit comments

Comments
 (0)