From b7e9f18f1b9ab648af6914516366ae358e4a6371 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 9 Aug 2024 10:50:05 +0200 Subject: [PATCH] pass test name to test_config to construct db name --- .../turbo-tasks-backend/tests/test_config.trs | 9 +++--- .../turbo-tasks-fetch/tests/test_config.trs | 2 +- .../tests/test_config.trs | 2 +- .../turbo-tasks-memory/tests/test_config.trs | 2 +- .../crates/turbo-tasks-testing/src/run.rs | 29 +++++++++++++------ 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/turbopack/crates/turbo-tasks-backend/tests/test_config.trs b/turbopack/crates/turbo-tasks-backend/tests/test_config.trs index 82930411b1c7ec..2f4c2421a61e7d 100644 --- a/turbopack/crates/turbo-tasks-backend/tests/test_config.trs +++ b/turbopack/crates/turbo-tasks-backend/tests/test_config.trs @@ -1,9 +1,8 @@ -|initial | { - let path = std::path::PathBuf::from(concat!( +|name, initial| { + let path = std::path::PathBuf::from(format!(concat!( env!("OUT_DIR"), - "/.cache/", - module_path!(), - )); + "/.cache/{}", + ), name)); if initial { let _ = std::fs::remove_dir_all(&path); } diff --git a/turbopack/crates/turbo-tasks-fetch/tests/test_config.trs b/turbopack/crates/turbo-tasks-fetch/tests/test_config.trs index c0bc5cb7eef064..037d1ff9fe8cb3 100644 --- a/turbopack/crates/turbo-tasks-fetch/tests/test_config.trs +++ b/turbopack/crates/turbo-tasks-fetch/tests/test_config.trs @@ -1,3 +1,3 @@ -|_initial | { +|_name, _initial | { turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX)) } diff --git a/turbopack/crates/turbo-tasks-macros-tests/tests/test_config.trs b/turbopack/crates/turbo-tasks-macros-tests/tests/test_config.trs index c0bc5cb7eef064..037d1ff9fe8cb3 100644 --- a/turbopack/crates/turbo-tasks-macros-tests/tests/test_config.trs +++ b/turbopack/crates/turbo-tasks-macros-tests/tests/test_config.trs @@ -1,3 +1,3 @@ -|_initial | { +|_name, _initial | { turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX)) } diff --git a/turbopack/crates/turbo-tasks-memory/tests/test_config.trs b/turbopack/crates/turbo-tasks-memory/tests/test_config.trs index c0bc5cb7eef064..037d1ff9fe8cb3 100644 --- a/turbopack/crates/turbo-tasks-memory/tests/test_config.trs +++ b/turbopack/crates/turbo-tasks-memory/tests/test_config.trs @@ -1,3 +1,3 @@ -|_initial | { +|_name, _initial | { turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX)) } diff --git a/turbopack/crates/turbo-tasks-testing/src/run.rs b/turbopack/crates/turbo-tasks-testing/src/run.rs index 90906a8a4dd5b0..c28066896b00ea 100644 --- a/turbopack/crates/turbo-tasks-testing/src/run.rs +++ b/turbopack/crates/turbo-tasks-testing/src/run.rs @@ -10,12 +10,15 @@ use turbo_tasks::{run_once, trace::TraceRawVcs, TurboTasksApi}; pub struct Registration { execution_lock: OnceLock<()>, func: fn(), - create_turbo_tasks: fn(bool) -> Arc, + create_turbo_tasks: fn(&str, bool) -> Arc, } impl Registration { #[doc(hidden)] - pub const fn new(create_turbo_tasks: fn(bool) -> Arc, func: fn()) -> Self { + pub const fn new( + create_turbo_tasks: fn(&str, bool) -> Arc, + func: fn(), + ) -> Self { Registration { execution_lock: OnceLock::new(), func, @@ -30,8 +33,8 @@ impl Registration { self.execution_lock.get_or_init(self.func); } - pub fn create_turbo_tasks(&self, initial: bool) -> Arc { - (self.create_turbo_tasks)(initial) + pub fn create_turbo_tasks(&self, name: &str, initial: bool) -> Arc { + (self.create_turbo_tasks)(name, initial) } } @@ -40,12 +43,12 @@ macro_rules! register { ($($other_register_fns:expr),* $(,)?) => {{ use turbo_tasks::TurboTasksApi; use std::sync::Arc; - fn create_turbo_tasks(initial: bool) -> Arc { + fn create_turbo_tasks(name: &str, initial: bool) -> Arc { let inner = include!(concat!( env!("CARGO_MANIFEST_DIR"), "/tests/test_config.trs" )); - (inner)(initial) + (inner)(name, initial) } fn register_impl() { $($other_register_fns();)* @@ -69,10 +72,16 @@ where T: TraceRawVcs + Send + 'static, { registration.ensure_registered(); - let tt = registration.create_turbo_tasks(true); + let name = closure_to_name(&fut); + let tt = registration.create_turbo_tasks(&name, true); run_once(tt, async move { Ok(fut.await) }).await.unwrap() } +fn closure_to_name(value: &T) -> String { + let name = std::any::type_name_of_val(value); + name.replace("::{{closure}}", "").replace("::", "_") +} + pub async fn run( registration: &Registration, fut: impl Fn() -> F + Send + 'static, @@ -82,14 +91,16 @@ where T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static, { registration.ensure_registered(); - let tt = registration.create_turbo_tasks(true); + + let name = closure_to_name(&fut); + let tt = registration.create_turbo_tasks(&name, true); println!("Run #1 (without cache)"); let first = run_once(tt.clone(), fut()).await?; println!("Run #2 (with memory cache, same TurboTasks instance)"); let second = run_once(tt.clone(), fut()).await?; tt.stop_and_wait().await; assert_eq!(first, second); - let tt = registration.create_turbo_tasks(false); + let tt = registration.create_turbo_tasks(&name, false); println!("Run #3 (with persistent cache if available, new TurboTasks instance)"); let third = run_once(tt.clone(), fut()).await?; tt.stop_and_wait().await;