From b61e00bc2f5a9a95861983c1b25df2ac5e2291c9 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Thu, 22 Jan 2026 12:21:23 -0800 Subject: [PATCH] fix migration tests for trybuild environment and goldenize stderr --- akd/crates/macros/src/lib.rs | 55 +++++++++++-------- .../compile_panics/load_migrations.stderr | 7 +++ .../tests/compile_panics/migration.stderr | 15 +++++ 3 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 akd/crates/macros/tests/compile_panics/load_migrations.stderr create mode 100644 akd/crates/macros/tests/compile_panics/migration.stderr diff --git a/akd/crates/macros/src/lib.rs b/akd/crates/macros/src/lib.rs index 9f104b4cd8..05ad32122a 100644 --- a/akd/crates/macros/src/lib.rs +++ b/akd/crates/macros/src/lib.rs @@ -39,38 +39,47 @@ use std::fs; use std::path::Path; use syn::{parse_macro_input, LitStr}; -/// Resolves a directory path by walking up to find the workspace root (first Cargo.toml). -/// All paths are resolved relative to the workspace root. +/// Resolves a directory path relative to the invoking crate. +/// Handles special case of trybuild tests by mapping back to the source crate. fn resolve_path(path_str: &str) -> std::path::PathBuf { // Try to get the current crate directory from CARGO_MANIFEST_DIR - let start_dir = if let Ok(crate_dir) = std::env::var("CARGO_MANIFEST_DIR") { - std::path::PathBuf::from(crate_dir) + let crate_dir = if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") { + std::path::PathBuf::from(dir) } else { - // CARGO_MANIFEST_DIR not set (e.g., in trybuild tests) - // Start from the current directory and walk up + // CARGO_MANIFEST_DIR not set - start from current directory std::env::current_dir().expect("Could not determine current directory") }; - let mut current = start_dir.as_path(); - - // Walk up to find the first Cargo.toml (workspace root) - loop { - if current.join("Cargo.toml").exists() { - return current.join(path_str); - } - - // Move to parent directory - match current.parent() { - Some(parent) => current = parent, - None => { - // Reached filesystem root without finding Cargo.toml - panic!( - "Could not find Cargo.toml in any parent directory starting from {}", - start_dir.display() - ); + // Check if we're in a trybuild test environment + // trybuild creates test crates in paths like: target/tests/trybuild/{crate-name}/ + let crate_dir_str = crate_dir.to_string_lossy(); + if crate_dir_str.contains("/target/tests/trybuild/") { + // We're in a trybuild test - need to find the source crate + // The trybuild Cargo.toml contains the path to the actual crate in dependencies + let cargo_toml_path = crate_dir.join("Cargo.toml"); + if let Ok(content) = fs::read_to_string(&cargo_toml_path) { + // Look for the macros dependency path like: + // [dependencies.macros] + // path = "/path/to/actual/crate/" + for line in content.lines() { + if line.starts_with("path = ") { + // Extract the path - it's in quotes + if let Some(start) = line.find('"') { + if let Some(end) = line.rfind('"') { + let dep_path = &line[start + 1..end]; + let source_crate = std::path::PathBuf::from(dep_path); + if source_crate.exists() { + return source_crate.join(path_str); + } + } + } + } } } } + + // Not in trybuild or couldn't resolve - use the crate directory directly + crate_dir.join(path_str) } /// Helper function to load a migration from a directory path. diff --git a/akd/crates/macros/tests/compile_panics/load_migrations.stderr b/akd/crates/macros/tests/compile_panics/load_migrations.stderr new file mode 100644 index 0000000000..b178fbadc4 --- /dev/null +++ b/akd/crates/macros/tests/compile_panics/load_migrations.stderr @@ -0,0 +1,7 @@ +error: proc macro panicked + --> tests/compile_panics/load_migrations.rs:4:34 + | +4 | const MIGRATIONS: &[Migration] = load_migrations!("tests/test_panic_migrations"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: message: Migration name '20250930_03_really_long_name_that_exceeds_the_50_character_limit' exceeds 50 characters (length: 64) diff --git a/akd/crates/macros/tests/compile_panics/migration.stderr b/akd/crates/macros/tests/compile_panics/migration.stderr new file mode 100644 index 0000000000..db2e684294 --- /dev/null +++ b/akd/crates/macros/tests/compile_panics/migration.stderr @@ -0,0 +1,15 @@ +error: proc macro panicked + --> tests/compile_panics/migration.rs:4:35 + | +4 | ...: Migration = migration!("tests/test_panic_migrations/20250930_03_really_long_name_that_exceeds_the_50_character_limit"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: message: Migration name '20250930_03_really_long_name_that_exceeds_the_50_character_limit' exceeds 50 characters (length: 64) + +error: proc macro panicked + --> tests/compile_panics/migration.rs:5:36 + | +5 | const NO_UP_MIGRATION: Migration = migration!("tests/test_panic_migrations/20250930_04_no_up"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: message: Required file 'up.sql' not found in migration directory: $DIR/tests/test_panic_migrations/20250930_04_no_up