|
16 | 16 |
|
17 | 17 | #![allow(dead_code)] |
18 | 18 |
|
| 19 | +use std::collections::HashMap; |
19 | 20 | use std::path::PathBuf; |
20 | 21 | use std::process::Child; |
21 | 22 | use std::process::Command; |
22 | 23 | use std::process::Output; |
23 | 24 | use std::process::Stdio; |
| 25 | +use std::sync::LazyLock; |
24 | 26 | use std::thread; |
25 | 27 | use std::time::Duration; |
26 | 28 | use std::time::SystemTime; |
27 | 29 | use std::time::UNIX_EPOCH; |
28 | 30 |
|
| 31 | +/// All example targets, compiled once and cached for the lifetime of the test |
| 32 | +/// process. This avoids spawning concurrent `cargo build` subprocesses (which |
| 33 | +/// deadlock on Cargo's file lock) when tests run in parallel. |
| 34 | +static EXAMPLES: LazyLock<HashMap<String, PathBuf>> = LazyLock::new(|| { |
| 35 | + snapbox::cmd::compile_examples([]) |
| 36 | + .expect("failed to compile examples") |
| 37 | + .map(|(name, path)| (name, path.expect("failed to compile example"))) |
| 38 | + .collect() |
| 39 | +}); |
| 40 | + |
29 | 41 | // Find an executable produced by the Cargo build. |
30 | 42 | // |
31 | | -// For example targets (prefixed with "examples/"), uses snapbox to locate the |
32 | | -// compiled example reliably regardless of build directory layout. |
| 43 | +// For example targets (prefixed with "examples/"), looks up the pre-compiled |
| 44 | +// example from the EXAMPLES cache. |
33 | 45 | // |
34 | 46 | // For bin targets, uses CARGO_BIN_EXE_<name> (Cargo >=1.94) with a fallback |
35 | 47 | // to locating the binary relative to the test executable. |
36 | 48 | pub fn find_exec(name: &str) -> PathBuf { |
37 | 49 | if let Some(example_name) = name.strip_prefix("examples/") { |
38 | | - return snapbox::cmd::compile_example(example_name, []).expect("failed to compile example"); |
| 50 | + return EXAMPLES |
| 51 | + .get(example_name) |
| 52 | + .unwrap_or_else(|| panic!("example {example_name:?} not found in compiled examples")) |
| 53 | + .clone(); |
39 | 54 | } |
40 | 55 |
|
41 | 56 | let env_var = format!("CARGO_BIN_EXE_{name}"); |
|
0 commit comments