From 7c08a90e64f2eeb0e635be8cb838d9ac74d5515d Mon Sep 17 00:00:00 2001 From: Chen Gong Date: Wed, 19 Aug 2020 00:06:32 +0800 Subject: [PATCH] fix(rime_test): set data directories to working directory using rime::SetupDeployer API --- src/rime/setup.cc | 28 ++++++++++++++++++++++++++++ src/rime/setup.h | 2 ++ src/rime_api.cc | 31 ++++--------------------------- test/rime_test_main.cc | 14 +++++++++++--- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/rime/setup.cc b/src/rime/setup.cc index 7767ed795..708bef571 100644 --- a/src/rime/setup.cc +++ b/src/rime/setup.cc @@ -10,7 +10,9 @@ #endif // RIME_ENABLE_LOGGING #include +#include #include +#include #include namespace rime { @@ -33,6 +35,32 @@ RIME_API void LoadModules(const char* module_names[]) { } } +// assume member is a non-null pointer in struct *p. +#define PROVIDED(p, member) ((p) && RIME_STRUCT_HAS_MEMBER(*(p), (p)->member) && (p)->member) + +RIME_API void SetupDeployer(RimeTraits *traits) { + if (!traits) return; + Deployer &deployer(Service::instance().deployer()); + if (PROVIDED(traits, shared_data_dir)) + deployer.shared_data_dir = traits->shared_data_dir; + if (PROVIDED(traits, user_data_dir)) + deployer.user_data_dir = traits->user_data_dir; + if (PROVIDED(traits, distribution_name)) + deployer.distribution_name = traits->distribution_name; + if (PROVIDED(traits, distribution_code_name)) + deployer.distribution_code_name = traits->distribution_code_name; + if (PROVIDED(traits, distribution_version)) + deployer.distribution_version = traits->distribution_version; + if (PROVIDED(traits, prebuilt_data_dir)) + deployer.prebuilt_data_dir = traits->prebuilt_data_dir; + else + deployer.prebuilt_data_dir = deployer.shared_data_dir / "build"; + if (PROVIDED(traits, staging_dir)) + deployer.staging_dir = traits->staging_dir; + else + deployer.staging_dir = deployer.user_data_dir / "build"; +} + RIME_API void SetupLogging(const char* app_name, int min_log_level, const char* log_dir) { #ifdef RIME_ENABLE_LOGGING FLAGS_minloglevel = min_log_level; diff --git a/src/rime/setup.h b/src/rime/setup.h index 6382692f3..31a9fffc1 100644 --- a/src/rime/setup.h +++ b/src/rime/setup.h @@ -17,6 +17,8 @@ extern const char* kLegacyModules[]; RIME_API void LoadModules(const char* module_names[]); +RIME_API void SetupDeployer(RimeTraits *traits); + RIME_API void SetupLogging(const char* app_name, int min_log_level, const char* log_dir); RIME_API void SetupLogging(const char* app_name); diff --git a/src/rime_api.cc b/src/rime_api.cc index c604893e9..f3151a546 100644 --- a/src/rime_api.cc +++ b/src/rime_api.cc @@ -25,32 +25,9 @@ using namespace rime; using namespace std::placeholders; -// assuming member is a pointer in struct *p +// assume member is a non-null pointer in struct *p. #define PROVIDED(p, member) ((p) && RIME_STRUCT_HAS_MEMBER(*(p), (p)->member) && (p)->member) -static void setup_deployer(RimeTraits *traits) { - if (!traits) return; - Deployer &deployer(Service::instance().deployer()); - if (PROVIDED(traits, shared_data_dir)) - deployer.shared_data_dir = traits->shared_data_dir; - if (PROVIDED(traits, user_data_dir)) - deployer.user_data_dir = traits->user_data_dir; - if (PROVIDED(traits, distribution_name)) - deployer.distribution_name = traits->distribution_name; - if (PROVIDED(traits, distribution_code_name)) - deployer.distribution_code_name = traits->distribution_code_name; - if (PROVIDED(traits, distribution_version)) - deployer.distribution_version = traits->distribution_version; - if (PROVIDED(traits, prebuilt_data_dir)) - deployer.prebuilt_data_dir = traits->prebuilt_data_dir; - else - deployer.prebuilt_data_dir = deployer.shared_data_dir / "build"; - if (PROVIDED(traits, staging_dir)) - deployer.staging_dir = traits->staging_dir; - else - deployer.staging_dir = deployer.user_data_dir / "build"; -} - RIME_API void RimeSetupLogging(const char* app_name) { SetupLogging(app_name); } @@ -74,7 +51,7 @@ static void rime_declare_module_dependencies() { RIME_API void RimeSetup(RimeTraits *traits) { rime_declare_module_dependencies(); - setup_deployer(traits); + SetupDeployer(traits); if (PROVIDED(traits, app_name)) { if (RIME_STRUCT_HAS_MEMBER(*traits, traits->min_log_level) && RIME_STRUCT_HAS_MEMBER(*traits, traits->log_dir)) { @@ -97,7 +74,7 @@ RIME_API void RimeSetNotificationHandler(RimeNotificationHandler handler, } RIME_API void RimeInitialize(RimeTraits *traits) { - setup_deployer(traits); + SetupDeployer(traits); LoadModules(PROVIDED(traits, modules) ? traits->modules : kDefaultModules); Service::instance().StartService(); } @@ -152,7 +129,7 @@ RIME_API void RimeJoinMaintenanceThread() { // deployment RIME_API void RimeDeployerInitialize(RimeTraits *traits) { - setup_deployer(traits); + SetupDeployer(traits); LoadModules(PROVIDED(traits, modules) ? traits->modules : kDeployerModules); } diff --git a/test/rime_test_main.cc b/test/rime_test_main.cc index c612c16d1..79f51c2c1 100644 --- a/test/rime_test_main.cc +++ b/test/rime_test_main.cc @@ -1,11 +1,19 @@ #include +#include #include -#include -int main(int argc, char** argv) { +int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); + + RIME_STRUCT(RimeTraits, traits); + // put all files in the working directory ($build/test). + traits.shared_data_dir = + traits.user_data_dir = + traits.prebuilt_data_dir = + traits.staging_dir = "."; + rime::SetupDeployer(&traits); rime::SetupLogging("rime.test"); - rime::Service::instance().deployer().staging_dir = "."; rime::LoadModules(rime::kDefaultModules); + return RUN_ALL_TESTS(); }