Skip to content

Commit

Permalink
fix(rime_test): set data directories to working directory using rime:…
Browse files Browse the repository at this point in the history
…:SetupDeployer API
  • Loading branch information
lotem committed Aug 18, 2020
1 parent 594d7de commit 7c08a90
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
28 changes: 28 additions & 0 deletions src/rime/setup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#endif // RIME_ENABLE_LOGGING

#include <rime_api.h>
#include <rime/deployer.h>
#include <rime/module.h>
#include <rime/service.h>
#include <rime/setup.h>

namespace rime {
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/rime/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
31 changes: 4 additions & 27 deletions src/rime_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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)) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 11 additions & 3 deletions test/rime_test_main.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#include <gtest/gtest.h>
#include <rime_api.h>
#include <rime/setup.h>
#include <rime/service.h>

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();
}

0 comments on commit 7c08a90

Please sign in to comment.