Skip to content

Commit e676cb4

Browse files
committed
Make dependencies to be aggregated into
1 parent e54f5d8 commit e676cb4

4 files changed

Lines changed: 113 additions & 36 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/test-utils/macros/src/lib.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct SnapTestBuilder {
2727
fixture_dir: PathBuf,
2828
snapshot_dir: PathBuf,
2929
target_fn: syn::Path,
30-
insta_assert_macro: syn::Path,
30+
debug_snap: bool,
3131
}
3232

3333
impl SnapTestBuilder {
@@ -55,7 +55,7 @@ impl SnapTestBuilder {
5555
fixture_dir,
5656
snapshot_dir,
5757
target_fn: args.target_fn,
58-
insta_assert_macro: args.insta_assert_macro,
58+
debug_snap: args.debug_snap.value(),
5959
})
6060
}
6161

@@ -85,24 +85,33 @@ impl SnapTestBuilder {
8585

8686
let snapshot_dir = self.snapshot_dir.to_str().unwrap();
8787
let target_fn = &self.target_fn;
88-
let insta_assert_macro = &self.insta_assert_macro;
88+
let snapshot = syn::Ident::new("snapshot", proc_macro2::Span::call_site());
89+
let get_snapshot = if self.debug_snap {
90+
quote! {
91+
let #snapshot = format!("{:#?}", #target_fn(&input));
92+
}
93+
} else {
94+
quote! {
95+
let #snapshot = #target_fn(&input);
96+
}
97+
};
8998

9099
quote! {
91100
#[test]
92101
fn #test_fn_ident() {
93-
let input = std::fs::read_to_string(#fixture_file).unwrap();
94-
let snapshot = #target_fn(&input);
95-
fe_compiler_test_utils::_insta::with_settings! (
96-
{
97-
snapshot_path => #snapshot_dir,
98-
input_file => Some(#file_name.into()),
99-
prepend_module_to_snapshot => false,
100-
},
101-
{
102-
#insta_assert_macro!(snapshot);
103-
})
102+
let input = ::std::fs::read_to_string(#fixture_file).unwrap();
103+
#get_snapshot
104+
let mut settings = ::fe_compiler_test_utils::_macro_support::_insta::Settings::new();
105+
settings.set_snapshot_path(#snapshot_dir);
106+
settings.set_input_file(#file_name);
107+
settings.set_prepend_module_to_snapshot(false);
108+
109+
110+
::fe_compiler_test_utils::_insta_assert_snapshot!{
111+
#snapshot,
112+
settings
113+
}
104114
}
105-
106115
}
107116
}
108117
}
@@ -123,7 +132,7 @@ struct Args {
123132
fixture_dir: syn::LitStr,
124133
snapshot_dir: syn::LitStr,
125134
target_fn: syn::Path,
126-
insta_assert_macro: syn::Path,
135+
debug_snap: syn::LitBool,
127136
}
128137

129138
impl syn::parse::Parse for Args {
@@ -132,7 +141,7 @@ impl syn::parse::Parse for Args {
132141
fixture_dir: ..,
133142
snapshot_dir: ..,
134143
target_fn: ..,
135-
insta_assert_macro: ..,
144+
debug_snap: ..
136145
}`";
137146

138147
let ident = input.parse::<syn::Ident>()?;
@@ -160,17 +169,17 @@ impl syn::parse::Parse for Args {
160169
input.parse::<syn::Token![,]>()?;
161170

162171
let ident = input.parse::<syn::Ident>()?;
163-
if ident != "insta_assert_macro" {
172+
if ident != "debug_snap" {
164173
return Err(Error::new_spanned(ident, error_msg));
165174
}
166175
input.parse::<syn::Token![:]>()?;
167-
let insta_assert_macro = input.parse::<syn::Path>()?;
176+
let debug_snap = input.parse::<syn::LitBool>()?;
168177

169178
Ok(Self {
170179
fixture_dir,
171180
snapshot_dir,
172181
target_fn,
173-
insta_assert_macro,
182+
debug_snap,
174183
})
175184
}
176185
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#[doc(hidden)]
2+
pub use insta as _insta;
3+
4+
// NOTE: Borrowed from `insta` implementation from
5+
// [here](https://docs.rs/insta/1.26/src/insta/macros.rs.html#2-16)
6+
/// Utility macro to return the name of the current function.
7+
#[doc(hidden)]
8+
#[macro_export]
9+
macro_rules! _function_name {
10+
() => {{
11+
fn f() {}
12+
fn type_name_of_val<T>(_: T) -> &'static str {
13+
std::any::type_name::<T>()
14+
}
15+
let mut name = type_name_of_val(f).strip_suffix("::f").unwrap_or("");
16+
while let Some(rest) = name.strip_suffix("::{{closure}}") {
17+
name = rest;
18+
}
19+
name
20+
}};
21+
}
22+
23+
#[doc(hidden)]
24+
#[macro_export]
25+
macro_rules! _insta_assert_snapshot {
26+
($value: expr, $setting: expr) => {
27+
$setting.bind(|| {
28+
$crate::_macro_support::_insta::_macro_support::assert_snapshot(
29+
$crate::_macro_support::_insta::_macro_support::AutoName.into(),
30+
&$value,
31+
env!("CARGO_MANIFEST_DIR"),
32+
$crate::_function_name!(),
33+
module_path!(),
34+
file!(),
35+
line!(),
36+
stringify!($value),
37+
)
38+
.unwrap()
39+
})
40+
};
41+
}
42+
43+
/// Build a set of snapshot tests from a directory of fixtures.
44+
/// `fixture_dir` and `snapshot_dir` should be relative to the workspace root.
45+
/// `target_fn` should take `&str` and return `<T: Display>`.
46+
#[macro_export]
47+
macro_rules! build_snap_tests {
48+
($fixture_dir: literal, $snapshot_dir: literal, $target_fn: path) => {
49+
fe_compiler_test_utils::_build_snap_tests! {
50+
fixture_dir: $fixture_dir,
51+
snapshot_dir: $snapshot_dir,
52+
target_fn: $target_fn,
53+
debug_snap: false
54+
}
55+
};
56+
}
57+
58+
/// Build a set of snapshot tests from a directory of fixtures.
59+
/// `fixture_dir` and `snapshot_dir` should be relative to the workspace root.
60+
/// `target_fn` should take `&str` and return `<T: Debug>`.
61+
#[macro_export]
62+
macro_rules! build_debug_snap_tests {
63+
($fixture_dir: literal, $snapshot_dir: literal, $target_fn: path) => {
64+
fe_compiler_test_utils::_build_snap_tests! {
65+
fixture_dir: $fixture_dir,
66+
snapshot_dir: $snapshot_dir,
67+
target_fn: $target_fn,
68+
debug_snap: true
69+
}
70+
};
71+
}

crates/test-utils/src/lib.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#[doc(hidden)]
2-
pub use fe_test_utils_macros::build_snap_tests as _build_snap_tests;
2+
pub mod _macro_support;
3+
34
#[doc(hidden)]
4-
pub use insta as _insta;
5+
pub use fe_test_utils_macros::build_snap_tests as _build_snap_tests;
56

67
use evm_runtime::{ExitReason, Handler};
78
use fe_common::diagnostics::print_diagnostics;
@@ -29,20 +30,6 @@ macro_rules! assert_harness_gas_report {
2930
}
3031
}
3132

32-
/// Build a set of snapshot tests from a directory of fixtures.
33-
/// `fixture_dir` and `snapshot_dir` should be relative to the workspace root.
34-
#[macro_export]
35-
macro_rules! build_snap_tests {
36-
($fixture_dir: literal, $snapshot_dir: literal, $target_fn: path, $insta_assert_macro: path) => {
37-
fe_compiler_test_utils::_build_snap_tests! {
38-
fixture_dir: $fixture_dir,
39-
snapshot_dir: $snapshot_dir,
40-
target_fn: $target_fn,
41-
insta_assert_macro: $insta_assert_macro
42-
}
43-
};
44-
}
45-
4633
#[derive(Default, Debug)]
4734
pub struct GasReporter {
4835
records: RefCell<Vec<GasRecord>>,

0 commit comments

Comments
 (0)