-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add macro for generating runtime tests automatically
Signed-off-by: Ryan Levick <[email protected]>
- Loading branch information
Showing
6 changed files
with
74 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "test-codegen-macro" | ||
version = "0.0.0" | ||
edition.workspace = true | ||
publish = false | ||
|
||
[lib] | ||
proc-macro = true | ||
doctest = false | ||
test = false | ||
|
||
[dependencies] | ||
heck = "0.4.0" | ||
quote = "1.0.32" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Test Codegen Macro | ||
|
||
A macro for automatically producing `#[test]` annotated functions based on file directory structure. This is used by the runtime tests so that when adding a runtime test, you're not required to also add a test function corresponding to that runtime test. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use heck::*; | ||
use proc_macro::TokenStream; | ||
use std::{env, path::PathBuf}; | ||
|
||
/// This macro generates the `#[test]` functions for the runtime tests. | ||
#[proc_macro] | ||
pub fn codegen_tests(_input: TokenStream) -> TokenStream { | ||
let mut tests = Vec::new(); | ||
let tests_path = | ||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/runtime-tests/tests"); | ||
for entry in std::fs::read_dir(tests_path).expect("failed to read tests directory") { | ||
let entry = entry.expect("error reading test directory entry"); | ||
let test = entry.path(); | ||
|
||
if entry.file_type().unwrap().is_dir() { | ||
let requires_services = entry.path().join("services").exists(); | ||
|
||
let name = test.file_stem().unwrap().to_str().unwrap(); | ||
let ident = quote::format_ident!("{}", name.to_snake_case()); | ||
let feature_attribute = if requires_services { | ||
quote::quote!(#[cfg(feature = "e2e-tests")]) | ||
} else { | ||
quote::quote!() | ||
}; | ||
// Generate the following code: | ||
// ```rust | ||
// #[test] | ||
// fn outbound_mysql() { | ||
// run("outbound-mysql") | ||
// } | ||
// ``` | ||
tests.push(quote::quote! { | ||
#[test] | ||
#feature_attribute | ||
fn #ident() { | ||
run(#name) | ||
} | ||
}); | ||
} | ||
} | ||
(quote::quote!(#(#tests)*)).into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters