mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 09:08:11 +00:00
Keep library trials available without a UI suite and preserve the generator API
This commit is contained in:
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -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<Ident> {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -99,6 +99,12 @@ fn module_file(dir: &Path, path_dir: &Path, module: &ItemMod) -> Option<PathBuf>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Registrations {
|
||||
trials: Vec<TokenStream>,
|
||||
suite: Vec<TokenStream>,
|
||||
}
|
||||
|
||||
fn walk(
|
||||
items: &[Item],
|
||||
file: &Path,
|
||||
@@ -106,7 +112,7 @@ fn walk(
|
||||
path_dir: &Path,
|
||||
path: &[Ident],
|
||||
inherited: &[Attribute],
|
||||
calls: &mut Vec<TokenStream>,
|
||||
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<TokenStream> {
|
||||
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"));
|
||||
|
||||
@@ -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::<String>();
|
||||
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(""));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user