Skip to content

Commit d421020

Browse files
authored
feat(stageleft_tool)!: use features to gate test-mode staged code (#37)
Previously, we would generate either the `__staged` module with `#[cfg(test)]` APIs included or not based on a boolean flag. This makes it harder to share the generated staged code across separate deployments, since some may be in test mode or not. With this PR, we instead take in a string which is the feature under which test APIs should be included. This allows us to generate the `__staged` module once and share it across several targets.
1 parent 05e2fed commit d421020

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

stageleft_tool/src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl VisitMut for InlineTopLevelMod {
122122
struct GenFinalPubVistor {
123123
current_mod: Option<syn::Path>,
124124
all_macros: Vec<syn::Ident>,
125-
test_mode: bool,
125+
test_mode_feature: Option<String>,
126126
}
127127

128128
fn get_cfg_attrs(attrs: &[syn::Attribute]) -> impl Iterator<Item = &syn::Attribute> + '_ {
@@ -228,8 +228,10 @@ impl VisitMut for GenFinalPubVistor {
228228
i.attrs
229229
.retain(|a| a.to_token_stream().to_string() != "# [cfg (test)]");
230230

231-
if !self.test_mode {
232-
// if test mode is not true, there are no quoted snippets behind #[cfg(test)],
231+
if let Some(feature) = &self.test_mode_feature {
232+
i.attrs.insert(0, parse_quote!(#[cfg(feature = #feature)]));
233+
} else {
234+
// if test mode is not enabled, there are no quoted snippets behind #[cfg(test)],
233235
// so no #[cfg(test)] modules will ever be reachable
234236
i.attrs.insert(
235237
0,
@@ -468,15 +470,19 @@ fn gen_deps_module(stageleft_name: syn::Ident, manifest_path: &Path) -> syn::Ite
468470

469471
/// Generates the contents of `mod __staged`, which contains a copy of the crate's code but with
470472
/// all APIs made public so they can be resolved when quoted code is spliced.
471-
fn gen_staged_mod(lib_path: &Path, orig_crate_ident: syn::Path, test_mode: bool) -> syn::File {
473+
fn gen_staged_mod(
474+
lib_path: &Path,
475+
orig_crate_ident: syn::Path,
476+
test_mode_feature: Option<String>,
477+
) -> syn::File {
472478
let mut orig_flow_lib = syn_inline_mod::parse_and_inline_modules(lib_path);
473479
InlineTopLevelMod {}.visit_file_mut(&mut orig_flow_lib);
474480

475481
let mut flow_lib_pub = syn_inline_mod::parse_and_inline_modules(lib_path);
476482

477483
let mut final_pub_visitor = GenFinalPubVistor {
478484
current_mod: Some(parse_quote!(#orig_crate_ident)),
479-
test_mode,
485+
test_mode_feature,
480486
all_macros: vec![],
481487
};
482488
final_pub_visitor.visit_file_mut(&mut flow_lib_pub);
@@ -500,10 +506,10 @@ pub fn gen_staged_trybuild(
500506
lib_path: &Path,
501507
manifest_path: &Path,
502508
orig_crate_name: String,
503-
test_mode: bool,
509+
test_mode_feature: Option<String>,
504510
) -> syn::File {
505511
let crate_name = syn::Ident::new(&orig_crate_name, Span::call_site());
506-
let flow_lib_pub = gen_staged_mod(lib_path, parse_quote!(#crate_name), test_mode);
512+
let flow_lib_pub = gen_staged_mod(lib_path, parse_quote!(#crate_name), test_mode_feature);
507513

508514
let deps_mod = gen_deps_module(parse_quote!(stageleft), manifest_path);
509515

@@ -517,7 +523,7 @@ pub fn gen_staged_trybuild(
517523
pub fn gen_staged_pub() {
518524
let out_dir = env::var_os("OUT_DIR").unwrap();
519525

520-
let flow_lib_pub = gen_staged_mod(Path::new("src/lib.rs"), parse_quote!(crate), false);
526+
let flow_lib_pub = gen_staged_mod(Path::new("src/lib.rs"), parse_quote!(crate), None);
521527

522528
fs::write(
523529
Path::new(&out_dir).join("lib_pub.rs"),

0 commit comments

Comments
 (0)