Skip to content

Commit

Permalink
Fail when snapshots are missing in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
dannymcgee committed May 10, 2024
1 parent 3095bb6 commit 41b3bea
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
5 changes: 5 additions & 0 deletions packages/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ exclude = [
"test-files",
]

[features]
never-create-snapshots = [
"snapshot/never-create"
]

[dependencies]
bitflags = { workspace = true }
gramatika = { workspace = true }
Expand Down
5 changes: 5 additions & 0 deletions packages/parser/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"executor": "@wgsl/tools:cargo-snapshot-test",
"options": {
"package": "wgsl-parser"
},
"configurations": {
"ci": {
"features": "never-create-snapshots"
}
}
},
"lint": {
Expand Down
3 changes: 3 additions & 0 deletions tools/snapshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ anyhow = { workspace = true }
pm2 = { package = "proc-macro2", version = "1.0" }
quote = "1.0"
syn = { version = "2.0", features = ["full"] }

[features]
never-create = []
54 changes: 40 additions & 14 deletions tools/snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ fn snapshot_parse(input: SnapshotParse) -> TokenStream {
let module_path_ident = format_ident!("mod_path");
let module_path_expr = make_module_path_expr();

let title_ident = format_ident!("title");
let title_expr = make_title_expr(&module_path_ident, &snapshot_index_ident);

let snapshot_ident = format_ident!("snapshot");
let snapshot_header_expr = make_snapshot_header_expr(&module_path_ident, &snapshot_index_ident);
let snapshot_header_expr = make_snapshot_header_expr(&title_ident);

let check_and_update_snapshot = make_snapshot_check_and_update_stmts(
&title_ident,
&module_path_ident,
&snapshot_index_ident,
&snapshot_ident,
Expand Down Expand Up @@ -107,6 +111,7 @@ fn snapshot_parse(input: SnapshotParse) -> TokenStream {
accum
});

let #title_ident = #title_expr;
let mut #snapshot_ident = #snapshot_header_expr;

::std::write!(&mut #snapshot_ident, "\n{result}").unwrap();
Expand Down Expand Up @@ -149,10 +154,14 @@ fn snapshot_fmt(args: pm2::TokenStream) -> TokenStream {
let module_path_ident = format_ident!("mod_path");
let module_path_expr = make_module_path_expr();

let title_ident = format_ident!("title");
let title_expr = make_title_expr(&module_path_ident, &snapshot_index_ident);

let snapshot_ident = format_ident!("snapshot");
let snapshot_header_expr = make_snapshot_header_expr(&module_path_ident, &snapshot_index_ident);
let snapshot_header_expr = make_snapshot_header_expr(&title_ident);

let check_and_update_snapshot = make_snapshot_check_and_update_stmts(
&title_ident,
&module_path_ident,
&snapshot_index_ident,
&snapshot_ident,
Expand All @@ -165,6 +174,7 @@ fn snapshot_fmt(args: pm2::TokenStream) -> TokenStream {
let #snapshot_index_ident = #snapshot_index;
let #module_path_ident = #module_path_expr;

let #title_ident = #title_expr;
let mut #snapshot_ident = #snapshot_header_expr;

::std::write!(&mut #snapshot_ident, "\n{}", ::std::format!(#args)).unwrap();
Expand Down Expand Up @@ -200,30 +210,47 @@ fn make_module_path_expr() -> pm2::TokenStream {
}
}

fn make_snapshot_header_expr(
module_path: &pm2::Ident,
snapshot_index: &pm2::Ident,
) -> pm2::TokenStream {
quote! {{
let title = ::std::format!(
fn make_title_expr(module_path: &pm2::Ident, snapshot_index: &pm2::Ident) -> pm2::TokenStream {
quote! {
::std::format!(
"{}::{__func_name__} / #{}",
#module_path.iter().join("::"),
#snapshot_index,
);
)
}
}

fn make_snapshot_header_expr(title: &pm2::Ident) -> pm2::TokenStream {
quote! {
::std::format!(
"================================================================================\n\
{title}\n\
================================================================================"
{}\n\
================================================================================",
#title,
)
}}
}
}

fn make_snapshot_check_and_update_stmts(
#[cfg_attr(not(feature = "never-create"), allow(unused))] title: &pm2::Ident,
module_path: &pm2::Ident,
snapshot_index: &pm2::Ident,
snapshot: &pm2::Ident,
) -> pm2::TokenStream {
#[cfg(feature = "never-create")]
let no_existing_snapshot_stmts = quote! {
::core::panic!(
"No snapshot exists for test case \"{}\"",
#title
);
};

#[cfg(not(feature = "never-create"))]
let no_existing_snapshot_stmts = quote! {
::std::fs::create_dir_all(&dirname).unwrap();
::std::fs::write(&full_path, &#snapshot).unwrap();
};

quote! {
let crate_root = ::core::env!("CARGO_MANIFEST_DIR");
let mut dirname = ::std::path::PathBuf::from(crate_root);
Expand Down Expand Up @@ -259,8 +286,7 @@ fn make_snapshot_check_and_update_stmts(
}
}
} else {
::std::fs::create_dir_all(&dirname).unwrap();
::std::fs::write(&full_path, &#snapshot).unwrap();
#no_existing_snapshot_stmts
}
}
}
Expand Down

0 comments on commit 41b3bea

Please sign in to comment.