This commit is contained in:
vms 2021-04-29 10:29:34 +03:00
parent 17e2dd1cc0
commit 4297da044c
8 changed files with 45 additions and 24 deletions

View File

@ -20,6 +20,5 @@ darling = "0.12.2"
quote = "1.0.9"
proc-macro2 = "1.0.26"
proc-macro-error = { version = "1.0.4", default-features = false }
caller_modpath = "0.1.1"
syn = { version = '1.0.64', features = ['full'] }
thiserror = "1.0.24"

View File

@ -21,6 +21,8 @@ use darling::Error as DarlingError;
use syn::Error as SynError;
use thiserror::Error as ThisError;
use std::path::PathBuf;
#[derive(Debug, ThisError)]
pub enum TestGeneratorError {
#[error("Can't load Wasm modules into FCE: {0}")]
@ -45,6 +47,9 @@ pub enum TestGeneratorError {
#[error("a Wasm file compiled with newer version of sdk that supports multi-value")]
ManyFnOutputsUnsupported,
#[error("{0} is invalid UTF8 path")]
InvalidUTF8Path(PathBuf),
}
#[derive(Debug, ThisError)]

View File

@ -23,7 +23,11 @@ use darling::FromMeta;
use syn::parse::Parser;
use std::path::PathBuf;
pub fn fce_test_impl(attrs: TokenStream, input: TokenStream, full_path: PathBuf) -> TResult<TokenStream> {
pub fn fce_test_impl(
attrs: TokenStream,
input: TokenStream,
full_path: PathBuf,
) -> TResult<TokenStream> {
// from https://github.com/dtolnay/syn/issues/788
let parser = syn::punctuated::Punctuated::<syn::NestedMeta, syn::Token![,]>::parse_terminated;
let attrs = parser.parse2(attrs)?;

View File

@ -117,13 +117,16 @@ pub(super) fn generate_test_glue_code(
attrs: FCETestAttributes,
full_path: PathBuf,
) -> TResult<TokenStream> {
let fce_config = TomlAppServiceConfig::load(&attrs.config_path)?;
let config_path = full_path.join(&attrs.config_path);
let fce_config = TomlAppServiceConfig::load(&config_path)?;
let modules_dir = match config_utils::resolve_modules_dir(&fce_config, attrs.modules_dir) {
Some(modules_dir) => modules_dir,
None => return Err(TestGeneratorError::ModulesDirUnspecified),
};
let app_service_ctor = generate_app_service_ctor(&attrs.config_path, &modules_dir, full_path);
let app_service_ctor = generate_app_service_ctor(&attrs.config_path, &modules_dir)?;
let modules_dir = full_path.join(modules_dir);
let module_interfaces = fce_test::config_utils::collect_modules(&fce_config, modules_dir)?;
let module_definitions =
@ -155,15 +158,12 @@ pub(super) fn generate_test_glue_code(
Ok(glue_code)
}
fn generate_app_service_ctor(config_path: &str, modules_dir: &Path, full_path: PathBuf) -> TokenStream {
let modules_dir = modules_dir.to_string_lossy().to_string();
fn generate_app_service_ctor(config_path: &str, modules_dir: &Path) -> TResult<TokenStream> {
let modules_dir = modules_dir
.to_str()
.ok_or_else(|| TestGeneratorError::InvalidUTF8Path(modules_dir.to_path_buf()))?;
let config_path = full_path.join(config_path);
let config_path = config_path.to_str().unwrap();
let modules_dir = full_path.join(modules_dir);
let modules_dir = modules_dir.to_str().unwrap();
quote! {
let service_ctor = quote! {
let tmp_dir = std::env::temp_dir();
let service_id = fluence_test::internal::Uuid::new_v4().to_string();
@ -171,16 +171,32 @@ fn generate_app_service_ctor(config_path: &str, modules_dir: &Path, full_path: P
let tmp_dir = tmp_dir.to_string_lossy().to_string();
std::fs::create_dir(&tmp_dir).expect("can't create a directory for service in tmp");
let mut __fce_generated_fce_config = fluence_test::internal::TomlAppServiceConfig::load(#config_path.to_string())
.unwrap_or_else(|e| panic!("app service located at `{}` config can't be loaded: {}", #config_path, e));
let backtrace = fluence_test::internal::backtrace::Backtrace::new();
let backtrace_path = backtrace.frames().iter()
.flat_map(fluence_test::internal::backtrace::BacktraceFrame::symbols)
.skip_while(|s| s.filename()
.map(|p|!p.ends_with(file!())).unwrap_or(true))
.nth(1 as usize).expect("can't obtain full path to a binary");
let module_path = backtrace_path.filename().expect("can't obtain filename");
let module_path = module_path.parent().expect("can't get a parent dir");
//let module_path = module_path.parent().expect("can't pop file name");
let config_path = module_path.join(#config_path);
let modules_dir = module_path.join(#modules_dir);
let modules_dir = modules_dir.to_str().expect("modules_dir contains invalid UTF8 string");
let mut __fce_generated_fce_config = fluence_test::internal::TomlAppServiceConfig::load(&config_path)
.unwrap_or_else(|e| panic!("app service config located at `{:?}` can't be loaded: {}", config_path, e));
__fce_generated_fce_config.service_base_dir = Some(tmp_dir);
__fce_generated_fce_config.toml_faas_config.modules_dir = Some(#modules_dir.to_string());
__fce_generated_fce_config.toml_faas_config.modules_dir = Some(modules_dir.to_string());
let fce = fluence_test::internal::AppService::new_with_empty_facade(__fce_generated_fce_config, service_id, std::collections::HashMap::new())
.unwrap_or_else(|e| panic!("app service can't be created: {}", e));
let fce = std::rc::Rc::new(std::cell::RefCell::new(fce));
}
};
Ok(service_ctor)
}
fn generate_module_ctors<'n>(

View File

@ -22,5 +22,4 @@ fluence-sdk-test-macro-impl = { path = "../fce-test-macro-impl", version = "0.1.
quote = "1.0.9"
proc-macro2 = "1.0.24"
proc-macro-error = { version = "1.0.4", default-features = false }
caller_modpath = "0.1.1"
syn = { version = '1.0.64', features = ['full'] }

View File

@ -31,12 +31,8 @@
use fluence_sdk_test_macro_impl::fce_test_impl;
use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;
use caller_modpath::expose_caller_modpath;
use caller_modpath::CallerModpath;
use syn::spanned::Spanned;
use std::path::PathBuf;
/// This macro allows user to write tests for services in the following form:
///```rust
/// #[fce_test(config = "/path/to/Config.toml", modules_dir = "path/to/service/modules")]
@ -45,14 +41,13 @@ use std::path::PathBuf;
/// assert_eq!(&service_result, "Hi, name!");
/// }
///```
#[expose_caller_modpath]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn fce_test(attrs: TokenStream, input: TokenStream) -> TokenStream {
let attrs: proc_macro2::TokenStream = attrs.into();
let attrs_span = attrs.span();
let full_path = proc_macro::Span::caller_modpath();
let full_path = PathBuf::from(full_path);
let mut full_path = proc_macro::Span::call_site().source_file().path();
let _ = full_path.pop();
match fce_test_impl(attrs, input.into(), full_path) {
Ok(stream) => stream.into(),

View File

@ -22,6 +22,7 @@ doctest = false
fluence-sdk-test-macro = { path = "../crates/fce-test-macro", version = "0.1.4" }
fluence-app-service = { version = "0.7.0", features = ["raw-module-api"] }
backtrace = "0.3.58"
serde = { version = "1.0.118", features = ["derive"] }
serde_json = "1.0.64"
uuid = { version = "0.8.2", features = ["v4"] }

View File

@ -34,6 +34,8 @@ pub mod internal {
pub use fluence_app_service::AppService;
pub use fluence_app_service::TomlAppServiceConfig;
pub use backtrace;
pub use serde;
pub use serde_json;