diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rs index 339015183..2e66c6d6f 100644 --- a/crates/automation/src/lib.rs +++ b/crates/automation/src/lib.rs @@ -70,5 +70,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/browse/src/lib.rs b/crates/browse/src/lib.rs index 2ca6cba0a..029c8c2bc 100644 --- a/crates/browse/src/lib.rs +++ b/crates/browse/src/lib.rs @@ -24,5 +24,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::ui::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/color-ui/src/lib.rs b/crates/color-ui/src/lib.rs index fba4f89c7..49a93c7ea 100644 --- a/crates/color-ui/src/lib.rs +++ b/crates/color-ui/src/lib.rs @@ -31,5 +31,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::ui::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/dialogs/src/lib.rs b/crates/dialogs/src/lib.rs index 763876054..be0cde168 100644 --- a/crates/dialogs/src/lib.rs +++ b/crates/dialogs/src/lib.rs @@ -400,5 +400,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/draw/src/lib.rs b/crates/draw/src/lib.rs index 2e7e1ea2a..9c3772468 100644 --- a/crates/draw/src/lib.rs +++ b/crates/draw/src/lib.rs @@ -22,5 +22,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/imaging/src/lib.rs b/crates/imaging/src/lib.rs index 77b7dac13..631f47adf 100644 --- a/crates/imaging/src/lib.rs +++ b/crates/imaging/src/lib.rs @@ -29,5 +29,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/pages/src/lib.rs b/crates/pages/src/lib.rs index 3a58fdb6d..d6abe6938 100644 --- a/crates/pages/src/lib.rs +++ b/crates/pages/src/lib.rs @@ -55,5 +55,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/print/src/lib.rs b/crates/print/src/lib.rs index 86922604e..ea3b45778 100644 --- a/crates/print/src/lib.rs +++ b/crates/print/src/lib.rs @@ -1090,5 +1090,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/review/src/lib.rs b/crates/review/src/lib.rs index 5beb64ebb..931b7f571 100644 --- a/crates/review/src/lib.rs +++ b/crates/review/src/lib.rs @@ -46,5 +46,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/text/src/hyphen/mod.rs b/crates/text/src/hyphen/mod.rs index d664811ec..9eed829f2 100644 --- a/crates/text/src/hyphen/mod.rs +++ b/crates/text/src/hyphen/mod.rs @@ -14,7 +14,7 @@ //! Then the paragraph's limits ([`Limits`]) are applied. #[cfg(feature = "generator")] -pub(crate) mod dict; +pub mod dict; #[cfg(not(feature = "generator"))] pub(crate) mod dict; #[cfg(feature = "generator")] diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs index 4c724fbe7..b13506f45 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -71,7 +71,8 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } /// Paragraph-wide line composition with stateful line measurement. diff --git a/crates/theme-editor/src/lib.rs b/crates/theme-editor/src/lib.rs index 2c33c17c9..2730241e4 100644 --- a/crates/theme-editor/src/lib.rs +++ b/crates/theme-editor/src/lib.rs @@ -33,5 +33,6 @@ pub mod testing { #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/theme-store/src/lib.rs b/crates/theme-store/src/lib.rs index 33f491219..b20d4708c 100644 --- a/crates/theme-store/src/lib.rs +++ b/crates/theme-store/src/lib.rs @@ -74,5 +74,6 @@ pub(crate) mod two_processes; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::ui::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/timeline/src/lib.rs b/crates/timeline/src/lib.rs index 679c94ea0..1c6b85a19 100644 --- a/crates/timeline/src/lib.rs +++ b/crates/timeline/src/lib.rs @@ -99,5 +99,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); } diff --git a/crates/ui-macros/src/component.rs b/crates/ui-macros/src/component.rs index ebf5d48a6..65c236eed 100644 --- a/crates/ui-macros/src/component.rs +++ b/crates/ui-macros/src/component.rs @@ -179,25 +179,28 @@ pub(crate) fn purpose_of(attrs: &[Attribute]) -> String { sentence.trim_end_matches('.').to_owned() } -/// The attributes on a registration block, from the crate being compiled. See `gate_for`. -pub(crate) fn registration_gate() -> TokenStream { +/// Whether the crate being compiled declares its own suite feature. +pub(crate) fn declares_suite() -> bool { let manifest = std::env::var_os("CARGO_MANIFEST_DIR").and_then(|dir| std::fs::read_to_string(std::path::Path::new(&dir).join("Cargo.toml")).ok()); - gate_for(manifest.as_deref().unwrap_or_default()) + declares_suite_in(manifest.as_deref().unwrap_or_default()) } /// The attributes on a registration block. The registration's types can exist only under the defining package's /// `suite` feature, which may be off while craft-ui's `testing` is on, so the block is gated on it (the cfg /// is evaluated in the defining crate) when the package's `manifest` declares that feature, and is not gated otherwise. -pub(crate) fn gate_for(manifest: &str) -> TokenStream { +pub(crate) fn declares_suite_in(manifest: &str) -> bool { let mut in_features = false; - let declares = manifest.lines().map(str::trim).any(|line| { + manifest.lines().map(str::trim).any(|line| { if line.starts_with('[') { in_features = line.trim_end_matches(|c: char| c.is_whitespace()) == "[features]"; return false; } in_features && line.strip_prefix("suite").is_some_and(|rest| rest.trim_start().starts_with('=')) - }); - if declares { quote!(#[cfg(feature = "suite")]) } else { quote!() } + }) +} + +pub(crate) fn registration_gate() -> TokenStream { + if declares_suite() { quote!(#[cfg(feature = "suite")]) } else { quote!() } } fn registry_helper(types: &[Type]) -> syn::Result { diff --git a/crates/ui-macros/src/lib.rs b/crates/ui-macros/src/lib.rs index 6d666df3a..31633ee43 100644 --- a/crates/ui-macros/src/lib.rs +++ b/crates/ui-macros/src/lib.rs @@ -135,7 +135,7 @@ pub fn trial(attr: TokenStream, item: TokenStream) -> TokenStream { } let ident = &sig.ident; let name = ident.to_string(); - let gate = component::registration_gate(); + let gate = component::declares_suite().then(|| quote!(#[cfg(any(test, feature = "suite"))])); let helper = quote::format_ident!("__registry_trial_{}", ident); quote! { #gate diff --git a/crates/ui-macros/src/registry.rs b/crates/ui-macros/src/registry.rs index 15c068c46..53bf34e0c 100644 --- a/crates/ui-macros/src/registry.rs +++ b/crates/ui-macros/src/registry.rs @@ -99,6 +99,12 @@ fn module_file(dir: &Path, path_dir: &Path, module: &ItemMod) -> Option } } +#[derive(Default)] +struct Registrations { + trials: Vec, + suite: Vec, +} + fn walk( items: &[Item], file: &Path, @@ -106,7 +112,7 @@ fn walk( path_dir: &Path, path: &[Ident], inherited: &[Attribute], - calls: &mut Vec, + calls: &mut Registrations, ) -> syn::Result<()> { for item in items { match item { @@ -137,7 +143,7 @@ fn walk( let helper = format_ident!("__registry_component_{}", crate::command_set::snake(&item.ident.to_string())); let mut cfg = inherited.to_vec(); cfg.extend(attrs_cfg(&item.attrs)); - calls.push(quote! { #(#cfg)* crate::#(#path::)*#helper(registry); }); + calls.suite.push(quote! { #(#cfg)* crate::#(#path::)*#helper(registry); }); } Item::Fn(item) if marker(&item.attrs, "story") || marker(&item.attrs, "trial") => { let (prefix, method) = @@ -145,13 +151,14 @@ fn walk( let helper = format_ident!("__registry_{}_{}", prefix, item.sig.ident); let mut cfg = inherited.to_vec(); cfg.extend(attrs_cfg(&item.attrs)); - calls.push(quote! { #(#cfg)* registry.#method(crate::#(#path::)*#helper()); }); + let entries = if marker(&item.attrs, "trial") { &mut calls.trials } else { &mut calls.suite }; + entries.push(quote! { #(#cfg)* registry.#method(crate::#(#path::)*#helper()); }); } Item::Enum(item) if derives(&item.attrs, "CommandSet") && opt(&item.attrs, "command", "story") => { let helper = format_ident!("__registry_command_{}", crate::command_set::snake(&item.ident.to_string())); let mut cfg = inherited.to_vec(); cfg.extend(attrs_cfg(&item.attrs)); - calls.push(quote! { #(#cfg)* registry.add_story_set(crate::#(#path::)*#helper()); }); + calls.suite.push(quote! { #(#cfg)* registry.add_story_set(crate::#(#path::)*#helper()); }); } Item::Macro(item) if item.mac.path.is_ident("include") => { let expression: syn::Expr = syn::parse2(item.mac.tokens.clone())?; @@ -174,7 +181,7 @@ fn walk( let helper = format_ident!("__registry_component_{}", crate::command_set::snake(&name.ident.to_string())); let mut cfg = inherited.to_vec(); cfg.extend(attrs_cfg(&item.attrs)); - calls.push(quote! { #(#cfg)* crate::#(#path::)*#helper(registry); }); + calls.suite.push(quote! { #(#cfg)* crate::#(#path::)*#helper(registry); }); } } _ => {} @@ -193,13 +200,17 @@ pub fn expand() -> syn::Result { let source = if invocation.starts_with(&src) { src.join("lib.rs") } else { invocation }; let text = std::fs::read_to_string(&source).map_err(|e| syn::Error::new(Span::call_site(), format!("cannot read {}: {e}", source.display())))?; let parsed = syn::parse_file(&text)?; - let mut calls = Vec::new(); + let mut calls = Registrations::default(); walk(&parsed.items, &source, source.parent().unwrap_or(&manifest), source.parent().unwrap_or(&manifest), &[], &[], &mut calls)?; let k = crate::util::ui_crate(); + let Registrations { trials, suite } = calls; + let gate = crate::component::registration_gate(); Ok(quote! { /// Adds this crate's suite items to the caller's registry. pub fn register(registry: &mut #k::testing::Registry) { - #(#calls)* + #(#trials)* + #gate + { #(#suite)* } } }) } @@ -214,7 +225,7 @@ mod tests { let parsed = syn::parse_file("include!(\"included.rs\");"); assert!(parsed.is_ok()); if let Ok(parsed) = parsed { - let mut calls = Vec::new(); + let mut calls = Registrations::default(); let dir = root.parent().unwrap_or(Path::new("")); assert!(walk(&parsed.items, &root, dir, dir, &[], &[], &mut calls).is_err()); } @@ -227,11 +238,12 @@ mod tests { let parsed = syn::parse_file(&source); assert!(parsed.is_ok()); if let Ok(parsed) = parsed { - let mut calls = Vec::new(); + let mut calls = Registrations::default(); let result = walk(&parsed.items, &root, root.parent().unwrap_or(Path::new("")), root.parent().unwrap_or(Path::new("")), &[], &[], &mut calls); assert!(result.is_ok()); - let generated = quote! { #(#calls)* }.to_string(); + let Registrations { trials, suite } = calls; + let generated = quote! { #(#trials)* #(#suite)* }.to_string(); assert!(generated.contains("feature = \"sample\"")); assert!(generated.contains("__registry_story_view")); assert!(generated.contains("__registry_trial_changes")); diff --git a/crates/ui-macros/src/tests.rs b/crates/ui-macros/src/tests.rs index b6ae5d5d6..ed0ed2c36 100644 --- a/crates/ui-macros/src/tests.rs +++ b/crates/ui-macros/src/tests.rs @@ -1,13 +1,10 @@ //! The registration gate the component derive reads from the package manifest. -use proc_macro2::TokenStream; - #[test] fn registration_is_gated_only_when_the_package_declares_suite() { - let compact = |tokens: TokenStream| tokens.to_string().split_whitespace().collect::(); let declared = "[package]\nname = \"app\"\n\n[features]\nsuite = [\"craft-ui/testing\"]\n"; - assert_eq!(compact(crate::component::gate_for(declared)), "#[cfg(feature=\"suite\")]"); + assert!(crate::component::declares_suite_in(declared)); let undeclared = "[package]\nname = \"gallery\"\nsuite = 1\n\n[dependencies]\nsuite = \"1\"\n"; - assert_eq!(compact(crate::component::gate_for(undeclared)), ""); - assert_eq!(compact(crate::component::gate_for("")), ""); + assert!(!crate::component::declares_suite_in(undeclared)); + assert!(!crate::component::declares_suite_in("")); } diff --git a/crates/update/src/lib.rs b/crates/update/src/lib.rs index 7ae50b3a7..06897fd1d 100644 --- a/crates/update/src/lib.rs +++ b/crates/update/src/lib.rs @@ -97,5 +97,6 @@ pub(crate) mod integration; #[cfg(test)] fn main() { - craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[crate::testing::register])); + craft_ui::registry!(); + craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register])); }