From 4297da044c28cfdd9064a96450e7ba2aa1385590 Mon Sep 17 00:00:00 2001 From: vms Date: Thu, 29 Apr 2021 10:29:34 +0300 Subject: [PATCH] refactor --- crates/fce-test-macro-impl/Cargo.toml | 1 - crates/fce-test-macro-impl/src/errors.rs | 5 +++ .../src/fce_test/fce_test_impl.rs | 6 ++- .../src/fce_test/glue_code_generator.rs | 44 +++++++++++++------ crates/fce-test-macro/Cargo.toml | 1 - crates/fce-test-macro/src/lib.rs | 9 +--- fluence-test/Cargo.toml | 1 + fluence-test/src/lib.rs | 2 + 8 files changed, 45 insertions(+), 24 deletions(-) diff --git a/crates/fce-test-macro-impl/Cargo.toml b/crates/fce-test-macro-impl/Cargo.toml index faa8a94..28541a6 100644 --- a/crates/fce-test-macro-impl/Cargo.toml +++ b/crates/fce-test-macro-impl/Cargo.toml @@ -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" diff --git a/crates/fce-test-macro-impl/src/errors.rs b/crates/fce-test-macro-impl/src/errors.rs index 2497763..05bb4fc 100644 --- a/crates/fce-test-macro-impl/src/errors.rs +++ b/crates/fce-test-macro-impl/src/errors.rs @@ -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)] diff --git a/crates/fce-test-macro-impl/src/fce_test/fce_test_impl.rs b/crates/fce-test-macro-impl/src/fce_test/fce_test_impl.rs index 6b82660..0f5c8c5 100644 --- a/crates/fce-test-macro-impl/src/fce_test/fce_test_impl.rs +++ b/crates/fce-test-macro-impl/src/fce_test/fce_test_impl.rs @@ -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 { +pub fn fce_test_impl( + attrs: TokenStream, + input: TokenStream, + full_path: PathBuf, +) -> TResult { // from https://github.com/dtolnay/syn/issues/788 let parser = syn::punctuated::Punctuated::::parse_terminated; let attrs = parser.parse2(attrs)?; diff --git a/crates/fce-test-macro-impl/src/fce_test/glue_code_generator.rs b/crates/fce-test-macro-impl/src/fce_test/glue_code_generator.rs index 648dd10..0fc1249 100644 --- a/crates/fce-test-macro-impl/src/fce_test/glue_code_generator.rs +++ b/crates/fce-test-macro-impl/src/fce_test/glue_code_generator.rs @@ -117,13 +117,16 @@ pub(super) fn generate_test_glue_code( attrs: FCETestAttributes, full_path: PathBuf, ) -> TResult { - 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 { + 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>( diff --git a/crates/fce-test-macro/Cargo.toml b/crates/fce-test-macro/Cargo.toml index 1a0d4c4..9b2fd6e 100644 --- a/crates/fce-test-macro/Cargo.toml +++ b/crates/fce-test-macro/Cargo.toml @@ -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'] } diff --git a/crates/fce-test-macro/src/lib.rs b/crates/fce-test-macro/src/lib.rs index e45e0d9..f562c17 100644 --- a/crates/fce-test-macro/src/lib.rs +++ b/crates/fce-test-macro/src/lib.rs @@ -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(), diff --git a/fluence-test/Cargo.toml b/fluence-test/Cargo.toml index 26a4384..3914b5e 100644 --- a/fluence-test/Cargo.toml +++ b/fluence-test/Cargo.toml @@ -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"] } diff --git a/fluence-test/src/lib.rs b/fluence-test/src/lib.rs index c5e397e..2106be1 100644 --- a/fluence-test/src/lib.rs +++ b/fluence-test/src/lib.rs @@ -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;