diff options
author | saturneric <[email protected]> | 2023-12-13 10:01:06 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-13 10:01:06 +0000 |
commit | 42264ed0d7a3c91fbe9f307984964ffc9e5fe65c (patch) | |
tree | a3ddecbd6ad723e42d68cc2c5aed7a88c4e242a3 /src | |
parent | feat: move test to src and add submodule googletest (diff) | |
download | GpgFrontend-42264ed0d7a3c91fbe9f307984964ffc9e5fe65c.tar.gz GpgFrontend-42264ed0d7a3c91fbe9f307984964ffc9e5fe65c.zip |
refactor: improve the structure of main,core and test module
Diffstat (limited to '')
-rw-r--r-- | src/GpgFrontendContext.cpp (renamed from src/before_exit.cpp) | 39 | ||||
-rw-r--r-- | src/GpgFrontendContext.h (renamed from src/type.h) | 48 | ||||
-rw-r--r-- | src/app.cpp | 66 | ||||
-rw-r--r-- | src/app.h | 8 | ||||
-rw-r--r-- | src/cmd.cpp | 22 | ||||
-rw-r--r-- | src/cmd.h | 10 | ||||
-rw-r--r-- | src/core/function/SecureMemoryAllocator.cpp | 18 | ||||
-rw-r--r-- | src/core/function/gpg/GpgContext.h | 2 | ||||
-rw-r--r-- | src/core/model/GpgData.h | 3 | ||||
-rw-r--r-- | src/core/utils/MemoryUtils.h | 9 | ||||
-rw-r--r-- | src/init.cpp | 92 | ||||
-rw-r--r-- | src/init.h | 23 | ||||
-rw-r--r-- | src/main.cpp | 48 | ||||
-rw-r--r-- | src/module/GpgFrontendModuleInit.cpp | 8 | ||||
-rw-r--r-- | src/module/GpgFrontendModuleInit.h | 2 | ||||
-rw-r--r-- | src/test/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/test/GpgFrontendTest.cpp | 25 | ||||
-rw-r--r-- | src/test/GpgFrontendTest.h | 21 | ||||
-rw-r--r-- | src/test/core/GpgCoreTest.cpp | 4 | ||||
-rw-r--r-- | src/test/core/GpgCoreTest.h | 4 | ||||
-rw-r--r-- | src/ui/GpgFrontendApplication.cpp | 19 | ||||
-rw-r--r-- | src/ui/GpgFrontendApplication.h | 9 |
22 files changed, 339 insertions, 145 deletions
diff --git a/src/before_exit.cpp b/src/GpgFrontendContext.cpp index e5e4ed63..72d2b0c1 100644 --- a/src/before_exit.cpp +++ b/src/GpgFrontendContext.cpp @@ -26,10 +26,37 @@ * */ -#include "core/function/GlobalSettingStation.h" +#include "GpgFrontendContext.h" -/** - * @brief Actions performed before exiting the application - * - */ -void BeforeExit() {} +#include <qcoreapplication.h> + +#include <memory> + +#include "ui/GpgFrontendApplication.h" + +namespace GpgFrontend { + +std::shared_ptr<GpgFrontendContext> GpgFrontendContext::global_context = + nullptr; + +auto GpgFrontendContext::CreateInstance(int argc, char** argv) + -> std::weak_ptr<GpgFrontendContext> { + global_context = std::make_shared<GpgFrontendContext>(); + global_context->argc = argc; + global_context->argv = argv; + return global_context; +} + +auto GpgFrontendContext::GetInstance() -> std::weak_ptr<GpgFrontendContext> { + return global_context; +} + +void GpgFrontendContext::InitCoreApplication() { + app = std::make_unique<QCoreApplication>(argc, argv); +} + +void GpgFrontendContext::InitGUIApplication() { + app = std::make_unique<UI::GpgFrontendApplication>(argc, argv); +} + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/type.h b/src/GpgFrontendContext.h index 1b98b60f..54d2f0b8 100644 --- a/src/type.h +++ b/src/GpgFrontendContext.h @@ -28,8 +28,50 @@ #pragma once -struct InitArgs { +namespace GpgFrontend { + +struct GpgFrontendContext { int argc; - char **argv; + char** argv; spdlog::level::level_enum log_level; -};
\ No newline at end of file + + bool load_ui_env; + std::unique_ptr<QCoreApplication> app; + + /** + * @brief Create a Instance object + * + * @param argc + * @param argv + * @return std::weak_ptr<GpgFrontendContext> + */ + static auto CreateInstance(int argc, char** argv) + -> std::weak_ptr<GpgFrontendContext>; + + /** + * @brief Get the Instance object + * + * @return std::weak_ptr<GpgFrontendContext> + */ + static auto GetInstance() -> std::weak_ptr<GpgFrontendContext>; + + /** + * @brief + * + */ + void InitGUIApplication(); + + /** + * @brief + * + */ + void InitCoreApplication(); + + private: + static std::shared_ptr<GpgFrontendContext> global_context; +}; + +using GFCxtWPtr = std::weak_ptr<GpgFrontendContext>; +using GFCxtSPtr = std::shared_ptr<GpgFrontendContext>; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/app.cpp b/src/app.cpp index d12761fd..5ebf6813 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -28,15 +28,15 @@ #include <csetjmp> +#include "GpgFrontendContext.h" #include "core/GpgConstants.h" #include "core/GpgCoreInit.h" #include "module/GpgFrontendModuleInit.h" -#include "ui/GpgFrontendApplication.h" +#include "spdlog/spdlog.h" #include "ui/GpgFrontendUIInit.h" // main #include "init.h" -#include "type.h" /** * \brief Store the jump buff and make it possible to recover from a crash. @@ -47,6 +47,8 @@ sigjmp_buf recover_env; jmp_buf recover_env; #endif +namespace GpgFrontend { + constexpr int kCrashCode = ~0; ///< /** @@ -56,31 +58,25 @@ constexpr int kCrashCode = ~0; ///< * @param argv * @return int */ -auto StartApplication(InitArgs args) -> int { - SPDLOG_INFO("start running application"); - -#ifdef RELEASE - // re - signal(SIGSEGV, HandleSignal); - signal(SIGFPE, HandleSignal); - signal(SIGILL, HandleSignal); -#endif - - // clean something before exit - atexit(BeforeExit); - - // initialize qt resources - Q_INIT_RESOURCE(gpgfrontend); - - // create qt application - auto* app = GpgFrontend::UI::GpgFrontendApplication::GetInstance( - args.argc, args.argv, true); - - // init the logging system for main - InitLoggingSystem(args); - - // change path to search for related - InitGlobalPathEnv(); +auto StartApplication(const GFCxtWPtr& p_ctx) -> int { + GFCxtSPtr ctx = p_ctx.lock(); + if (ctx == nullptr) { + SPDLOG_ERROR("cannot get gpgfrontend context."); + return -1; + } + + if (!ctx->load_ui_env) { + SPDLOG_ERROR("the loading of gui application is forbidden."); + return -1; + } + + auto* app = qobject_cast<QApplication*>(ctx->app.get()); + if (app == nullptr) { + SPDLOG_ERROR("cannot get qapplication from gpgfrontend context."); + return -1; + } + + SPDLOG_INFO("start running gui application"); /** * internationalisation. loop to restart main window @@ -96,17 +92,6 @@ auto StartApplication(InitArgs args) -> int { int r = setjmp(recover_env); #endif if (!r) { - // should load module system first - GpgFrontend::Module::ModuleInitArgs module_init_args; - module_init_args.log_level = args.log_level; - GpgFrontend::Module::LoadGpgFrontendModules(module_init_args); - - // then preload ui - GpgFrontend::UI::PreInitGpgFrontendUI(); - - // then load core - GpgFrontend::InitGpgFrontendCore(); - // after that load ui totally GpgFrontend::UI::InitGpgFrontendUI(app); @@ -143,9 +128,6 @@ auto StartApplication(InitArgs args) -> int { } while (return_from_event_loop_code == GpgFrontend::kRestartCode && restart_count < 3); - // close logging system - ShutdownLoggingSystem(); - // log for debug SPDLOG_INFO("GpgFrontend is about to exit."); @@ -161,3 +143,5 @@ auto StartApplication(InitArgs args) -> int { // exit the program return return_from_event_loop_code; } + +} // namespace GpgFrontend @@ -28,6 +28,10 @@ #pragma once -#include "init.h" +#include "GpgFrontendContext.h" -auto StartApplication(InitArgs args) -> int; +namespace GpgFrontend { + +auto StartApplication(const GFCxtWPtr& p_ctx) -> int; + +} diff --git a/src/cmd.cpp b/src/cmd.cpp index f0d748e0..5c554f32 100644 --- a/src/cmd.cpp +++ b/src/cmd.cpp @@ -36,10 +36,13 @@ // GpgFrontend #include "GpgFrontendBuildInfo.h" -#include "type.h" +#include "GpgFrontendContext.h" +#include "test/GpgFrontendTest.h" namespace po = boost::program_options; +namespace GpgFrontend { + auto PrintVersion() -> int { std::cout << PROJECT_NAME << " " << "v" << VERSION_MAJOR << "." << VERSION_MINOR << "." @@ -79,4 +82,19 @@ auto ParseLogLevel(const po::variables_map& vm) -> spdlog::level::level_enum { return spdlog::level::info; } -auto RunTest(int argc, char** argv) {}
\ No newline at end of file +auto RunTest(const GFCxtWPtr& p_ctx) -> int { + GpgFrontend::GFCxtSPtr ctx = p_ctx.lock(); + if (ctx == nullptr) { + SPDLOG_ERROR("cannot get gpgfrontend context for test running"); + return -1; + } + + GpgFrontend::Test::GpgFrontendContext test_init_args; + test_init_args.argc = ctx->argc; + test_init_args.argv = ctx->argv; + test_init_args.log_level = ctx->log_level; + + return GpgFrontend::Test::ExecuteAllTestCase(test_init_args); +} + +} // namespace GpgFrontend
\ No newline at end of file @@ -31,9 +31,17 @@ // boost #include <boost/program_options.hpp> +#include "GpgFrontendContext.h" + +namespace GpgFrontend { + // functions auto PrintVersion() -> int; auto ParseLogLevel(const boost::program_options::variables_map&) - -> spdlog::level::level_enum;
\ No newline at end of file + -> spdlog::level::level_enum; + +auto RunTest(const GFCxtWPtr&) -> int; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp index 09390305..e213ccbc 100644 --- a/src/core/function/SecureMemoryAllocator.cpp +++ b/src/core/function/SecureMemoryAllocator.cpp @@ -33,12 +33,24 @@ namespace GpgFrontend { auto SecurityMemoryAllocator::Allocate(std::size_t size) -> void* { - return mi_malloc(size); + auto* addr = mi_malloc(size); + SPDLOG_TRACE("secure memory allocator trys to alloc memory, address: {}", + static_cast<void*>(addr)); + return addr; } auto SecurityMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { - return mi_realloc(ptr, size); + auto* addr = mi_realloc(ptr, size); + SPDLOG_TRACE( + "secure memory allocator trys to realloc memory, " + "old address: {}, new address: {}", + static_cast<void*>(ptr), static_cast<void*>(addr)); + return addr; } -void SecurityMemoryAllocator::Deallocate(void* p) { mi_free(p); } +void SecurityMemoryAllocator::Deallocate(void* p) { + SPDLOG_TRACE("secure memory allocator trys to free memory, address: {}", + static_cast<void*>(p)); + mi_free(p); +} } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/gpg/GpgContext.h b/src/core/function/gpg/GpgContext.h index ea8c0e6a..b53344fd 100644 --- a/src/core/function/gpg/GpgContext.h +++ b/src/core/function/gpg/GpgContext.h @@ -62,7 +62,7 @@ class GPGFRONTEND_CORE_EXPORT GpgContext explicit GpgContext(const GpgContextInitArgs &args, int channel); - ~GpgContext() override; + virtual ~GpgContext() override; [[nodiscard]] auto Good() const -> bool; diff --git a/src/core/model/GpgData.h b/src/core/model/GpgData.h index 695f821f..ed5afc77 100644 --- a/src/core/model/GpgData.h +++ b/src/core/model/GpgData.h @@ -28,6 +28,7 @@ #pragma once +#include "core/GpgFrontendCoreExport.h" #include "core/typedef/CoreTypedef.h" namespace GpgFrontend { @@ -35,7 +36,7 @@ namespace GpgFrontend { * @brief * */ -class GpgData { +class GPGFRONTEND_CORE_EXPORT GpgData { public: /** * @brief Construct a new Gpg Data object diff --git a/src/core/utils/MemoryUtils.h b/src/core/utils/MemoryUtils.h index 4f92b5e9..5261a958 100644 --- a/src/core/utils/MemoryUtils.h +++ b/src/core/utils/MemoryUtils.h @@ -112,7 +112,8 @@ template <typename T, typename... Args> static auto SecureCreateObject(Args &&...args) -> T * { void *mem = SecurityMemoryAllocator::Allocate(sizeof(T)); if (!mem) return nullptr; - SPDLOG_TRACE("alloc secure memnory success, mem: {}", static_cast<void *>(mem)); + SPDLOG_TRACE("alloc secure memnory success, mem: {}", + static_cast<void *>(mem)); try { return new (mem) T(std::forward<Args>(args)...); @@ -136,7 +137,8 @@ static auto SecureCreateUniqueObject(Args &&...args) -> std::unique_ptr<T, SecureObjectDeleter<T>> { void *mem = SecurityMemoryAllocator::Allocate(sizeof(T)); if (!mem) throw std::bad_alloc(); - SPDLOG_TRACE("alloc secure memnory success, unique, mem: {}", mem); + SPDLOG_TRACE("alloc secure memnory success, unique ptr, type: {}, mem: {}", + typeid(T).name(), mem); try { return std::unique_ptr<T, SecureObjectDeleter<T>>( @@ -151,7 +153,8 @@ template <typename T, typename... Args> auto SecureCreateSharedObject(Args &&...args) -> std::shared_ptr<T> { void *mem = SecurityMemoryAllocator::Allocate(sizeof(T)); if (!mem) throw std::bad_alloc(); - SPDLOG_TRACE("alloc secure memnory success, shared, mem: {}", mem); + SPDLOG_TRACE("alloc secure memnory success, shared ptr, type: {}, mem: {}", + typeid(T).name(), mem); try { T *obj = new (mem) T(std::forward<Args>(args)...); diff --git a/src/init.cpp b/src/init.cpp index f9f1801f..77007ff1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -28,6 +28,7 @@ #include "init.h" +#include <qcoreapplication.h> #include <spdlog/async.h> #include <spdlog/common.h> #include <spdlog/sinks/rotating_file_sink.h> @@ -42,10 +43,13 @@ #include "core/utils/MemoryUtils.h" #include "module/GpgFrontendModuleInit.h" #include "module/sdk/Log.h" +#include "test/GpgFrontendTest.h" #include "ui/GpgFrontendUIInit.h" // main -#include "type.h" +#include "GpgFrontendContext.h" + +namespace GpgFrontend { #ifdef WINDOWS int setenv(const char *name, const char *value, int overwrite) { @@ -62,11 +66,11 @@ int setenv(const char *name, const char *value, int overwrite) { void InitMainLoggingSystem(spdlog::level::level_enum level) { // sinks std::vector<spdlog::sink_ptr> sinks; - sinks.push_back(GpgFrontend::SecureCreateSharedObject< - spdlog::sinks::stderr_color_sink_mt>()); + sinks.push_back( + SecureCreateSharedObject<spdlog::sinks::stderr_color_sink_mt>()); // logger - auto main_logger = GpgFrontend::SecureCreateSharedObject<spdlog::logger>( + auto main_logger = SecureCreateSharedObject<spdlog::logger>( "main", begin(sinks), end(sinks)); main_logger->set_pattern( "[%H:%M:%S.%e] [T:%t] [%=6n] %^[%=8l]%$ [%s:%#] [%!] -> %v (+%ius)"); @@ -88,26 +92,39 @@ void InitMainLoggingSystem(spdlog::level::level_enum level) { spdlog::set_default_logger(main_logger); } -void InitLoggingSystem(InitArgs args) { +void InitLoggingSystem(const GFCxtSPtr &ctx) { // init the logging system for main - InitMainLoggingSystem(args.log_level); + InitMainLoggingSystem(ctx->log_level); // init the logging system for core - GpgFrontend::InitCoreLoggingSystem(args.log_level); + InitCoreLoggingSystem(ctx->log_level); + + // shutdown the logging system for modules + Module::LoadGpgFrontendModulesLoggingSystem(ctx->log_level); - // init the logging system for ui - GpgFrontend::UI::InitUILoggingSystem(args.log_level); + // init the logging system for test + Test::InitTestLoggingSystem(ctx->log_level); + + if (ctx->load_ui_env) { + // init the logging system for ui + UI::InitUILoggingSystem(ctx->log_level); + } } -void ShutdownLoggingSystem() { - // shutdown the logging system for ui - GpgFrontend::UI::ShutdownUILoggingSystem(); +void ShutdownLoggingSystem(const GFCxtSPtr &ctx) { + if (ctx->load_ui_env) { + // shutdown the logging system for ui + UI::ShutdownUILoggingSystem(); + } + + // shutdown the logging system for test + Test::ShutdownTestLoggingSystem(); // shutdown the logging system for modules - GpgFrontend::Module::ShutdownGpgFrontendModulesLoggingSystem(); + Module::ShutdownGpgFrontendModulesLoggingSystem(); // shutdown the logging system for core - GpgFrontend::ShutdownCoreLoggingSystem(); + ShutdownCoreLoggingSystem(); #ifdef WINDOWS // Under VisualStudio, this must be called before main finishes to workaround @@ -120,11 +137,11 @@ void ShutdownLoggingSystem() { void InitGlobalPathEnv() { // read settings bool use_custom_gnupg_install_path = - GpgFrontend::GlobalSettingStation::GetInstance().LookupSettings( + GlobalSettingStation::GetInstance().LookupSettings( "general.use_custom_gnupg_install_path", false); std::string custom_gnupg_install_path = - GpgFrontend::GlobalSettingStation::GetInstance().LookupSettings( + GlobalSettingStation::GetInstance().LookupSettings( "general.custom_gnupg_install_path", std::string{}); // add custom gnupg install path into env $PATH @@ -140,3 +157,46 @@ void InitGlobalPathEnv() { SPDLOG_DEBUG("Modified System PATH: {}", modified_path_value); } } + +void InitGlobalBasicalEnv(const GFCxtWPtr &p_ctx) { + GFCxtSPtr ctx = p_ctx.lock(); + if (ctx == nullptr) { + return; + } + + // initialize logging system + InitLoggingSystem(ctx); + + // change path to search for related + InitGlobalPathEnv(); + + if (ctx->load_ui_env) { + ctx->InitGUIApplication(); + } else { + ctx->InitCoreApplication(); + } + + // should load module system first + Module::ModuleInitArgs module_init_args; + module_init_args.log_level = ctx->log_level; + Module::LoadGpgFrontendModules(module_init_args); + + if (ctx->load_ui_env) { + // then preload ui + UI::PreInitGpgFrontendUI(); + } + + // then load core + InitGpgFrontendCore(); +} + +void ShutdownGlobalBasicalEnv(const GFCxtWPtr &p_ctx) { + GFCxtSPtr ctx = p_ctx.lock(); + if (ctx == nullptr) { + return; + } + + ShutdownLoggingSystem(ctx); +} + +} // namespace GpgFrontend @@ -28,7 +28,9 @@ #pragma once -struct InitArgs; +#include "GpgFrontendContext.h" + +namespace GpgFrontend { /** * @brief handle the signal SIGSEGV @@ -38,26 +40,29 @@ struct InitArgs; void HandleSignal(int sig); /** - * @brief processes before exit the program. - * - */ -void BeforeExit(); - -/** * @brief * * @param args */ -void InitLoggingSystem(InitArgs args); +void InitLoggingSystem(const GFCxtSPtr&); /** * @brief initialize the logging system. * */ -void ShutdownLoggingSystem(); +void ShutdownLoggingSystem(const GFCxtSPtr&); /** * @brief init global PATH env * */ void InitGlobalPathEnv(); + +/** + * @brief + * + * @param args + */ +void InitGlobalBasicalEnv(const GFCxtWPtr&); + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 260f6200..a37506a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,9 +35,11 @@ #include <iostream> #include <string> +#include "GpgFrontendContext.h" #include "app.h" #include "cmd.h" -#include "type.h" +#include "init.h" +#include "spdlog/spdlog.h" namespace po = boost::program_options; @@ -48,21 +50,36 @@ namespace po = boost::program_options; * @return */ auto main(int argc, char* argv[]) -> int { +#ifdef RELEASE + // re + signal(SIGSEGV, HandleSignal); + signal(SIGFPE, HandleSignal); + signal(SIGILL, HandleSignal); +#endif + + // initialize qt resources + Q_INIT_RESOURCE(gpgfrontend); + po::options_description desc("Allowed options"); desc.add_options()("help,h", "produce help message")( "version,v", "show version information")( "log-level,l", po::value<std::string>()->default_value("info"), - "set log level (trace, debug, info, warn, error)"); + "set log level (trace, debug, info, warn, error)")( + "test,t", "run all unit test cases"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); - InitArgs args; - args.argc = argc; - args.argv = argv; - args.log_level = spdlog::level::info; + auto p_ctx = GpgFrontend::GpgFrontendContext::CreateInstance(argc, argv); + GpgFrontend::GFCxtSPtr ctx = p_ctx.lock(); + if (ctx == nullptr) { + SPDLOG_ERROR("cannot get gpgfrontend context"); + return -1; + } + ctx->log_level = spdlog::level::info; + ctx->load_ui_env = false; if (vm.count("help") != 0U) { std::cout << desc << "\n"; @@ -70,12 +87,25 @@ auto main(int argc, char* argv[]) -> int { } if (vm.count("version") != 0U) { - return PrintVersion(); + return GpgFrontend::PrintVersion(); } if (vm.count("log-level") != 0U) { - args.log_level = ParseLogLevel(vm); + ctx->log_level = GpgFrontend::ParseLogLevel(vm); } - return StartApplication(args); + if (vm.count("log-level") != 0U) { + ctx->log_level = GpgFrontend::ParseLogLevel(vm); + } + + if (vm.count("test") != 0U) { + InitGlobalBasicalEnv(p_ctx); + return RunTest(ctx); + } + + // default mode + ctx->load_ui_env = true; + InitGlobalBasicalEnv(p_ctx); + + return StartApplication(p_ctx); } diff --git a/src/module/GpgFrontendModuleInit.cpp b/src/module/GpgFrontendModuleInit.cpp index d322a528..5db3b0b6 100644 --- a/src/module/GpgFrontendModuleInit.cpp +++ b/src/module/GpgFrontendModuleInit.cpp @@ -36,11 +36,12 @@ // integrated modules #include "integrated/gnupg_info_gathering_module/GnuPGInfoGatheringModule.h" #include "integrated/version_checking_module/VersionCheckingModule.h" +#include "spdlog/common.h" namespace GpgFrontend::Module { -void LoadGpgFrontendModulesLoggingSystem(ModuleInitArgs args) { - GpgFrontend::Module::SDK::InitModuleLoggingSystem(args.log_level); +void LoadGpgFrontendModulesLoggingSystem(spdlog::level::level_enum log_level) { + GpgFrontend::Module::SDK::InitModuleLoggingSystem(log_level); } void ShutdownGpgFrontendModulesLoggingSystem() { @@ -52,9 +53,6 @@ void LoadGpgFrontendModules(ModuleInitArgs args) { Thread::TaskRunnerGetter::GetInstance().GetTaskRunner()->PostTask( new Thread::Task( [args](const DataObjectPtr&) -> int { - // init the logging system for module system - LoadGpgFrontendModulesLoggingSystem(args); - MODULE_LOG_INFO("loading integrated module..."); // VersionCheckingModule diff --git a/src/module/GpgFrontendModuleInit.h b/src/module/GpgFrontendModuleInit.h index a706bbb4..c7071856 100644 --- a/src/module/GpgFrontendModuleInit.h +++ b/src/module/GpgFrontendModuleInit.h @@ -42,7 +42,7 @@ struct ModuleInitArgs { * @param args */ void GPGFRONTEND_MODULE_EXPORT -LoadGpgFrontendModulesLoggingSystem(ModuleInitArgs args); +LoadGpgFrontendModulesLoggingSystem(spdlog::level::level_enum log_level); /** * @brief diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index c4d7bcf8..1eb710be 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -24,8 +24,8 @@ # SPDX-License-Identifier: GPL-3.0-or-later # Set configure for test -file(COPY ${CMAKE_SOURCE_DIR}/resource/lfs/test/conf DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test/ FOLLOW_SYMLINK_CHAIN) -file(COPY ${CMAKE_SOURCE_DIR}/resource/lfs/test/data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test/ FOLLOW_SYMLINK_CHAIN) +file(COPY ${CMAKE_SOURCE_DIR}/resource/lfs/test/conf DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test/ FOLLOW_SYMLINK_CHAIN) +file(COPY ${CMAKE_SOURCE_DIR}/resource/lfs/test/data DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test/ FOLLOW_SYMLINK_CHAIN) aux_source_directory(./core TEST_SOURCE) aux_source_directory(. TEST_SOURCE) diff --git a/src/test/GpgFrontendTest.cpp b/src/test/GpgFrontendTest.cpp index 24e099a2..18157a00 100644 --- a/src/test/GpgFrontendTest.cpp +++ b/src/test/GpgFrontendTest.cpp @@ -28,14 +28,18 @@ #include "GpgFrontendTest.h" +#include <gtest/gtest.h> +#include <spdlog/sinks/rotating_file_sink.h> +#include <spdlog/sinks/stdout_color_sinks.h> + #include <boost/date_time.hpp> #include <boost/dll.hpp> #include <filesystem> +#include "core/GpgCoreInit.h" #include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgContext.h" #include "spdlog/spdlog.h" -#include "type.h" namespace GpgFrontend::Test { @@ -66,6 +70,15 @@ void InitTestLoggingSystem(spdlog::level::level_enum level) { spdlog::set_default_logger(test_logger); } +void ShutdownTestLoggingSystem() { +#ifdef WINDOWS + // Under VisualStudio, this must be called before main finishes to workaround + // a known VS issue + spdlog::drop_all(); + spdlog::shutdown(); +#endif +} + auto GenerateRandomString(size_t length) -> std::string { const std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -94,6 +107,8 @@ void ConfigureGpgContext() { std::filesystem::create_directory(db_path); } + SPDLOG_DEBUG("DEBUG--------<"); + GpgContext::CreateInstance( kGpgFrontendDefaultChannel, [&]() -> ChannelObjectPtr { GpgContextInitArgs args; @@ -104,11 +119,15 @@ void ConfigureGpgContext() { return ConvertToChannelObjectPtr<>(SecureCreateUniqueObject<GpgContext>( args, kGpgFrontendDefaultChannel)); }); + + SPDLOG_DEBUG("DEBUG-------->"); } -void ExecuteAllTestCase(InitArgs args) { - InitTestLoggingSystem(args.log_level); +auto ExecuteAllTestCase(GpgFrontendContext args) -> int { ConfigureGpgContext(); + + testing::InitGoogleTest(&args.argc, args.argv); + return RUN_ALL_TESTS(); } } // namespace GpgFrontend::Test
\ No newline at end of file diff --git a/src/test/GpgFrontendTest.h b/src/test/GpgFrontendTest.h index 55c6b734..897a8a05 100644 --- a/src/test/GpgFrontendTest.h +++ b/src/test/GpgFrontendTest.h @@ -28,14 +28,23 @@ #pragma once -#include <gtest/gtest.h> -#include <spdlog/sinks/rotating_file_sink.h> -#include <spdlog/sinks/stdout_color_sinks.h> #include <spdlog/spdlog.h> +#include "GpgFrontendTestExport.h" -#include <libconfig.h++> +namespace GpgFrontend::Test { -#include "GpgFrontendTestExport.h" +struct GpgFrontendContext { + int argc; + char **argv; + spdlog::level::level_enum log_level; +}; + +void GPGFRONTEND_TEST_EXPORT +InitTestLoggingSystem(spdlog::level::level_enum level); + +void GPGFRONTEND_TEST_EXPORT ShutdownTestLoggingSystem(); + +auto GPGFRONTEND_TEST_EXPORT ExecuteAllTestCase(GpgFrontendContext args) -> int; -namespace GpgFrontend::Test {} // namespace GpgFrontend::Test +} // namespace GpgFrontend::Test diff --git a/src/test/core/GpgCoreTest.cpp b/src/test/core/GpgCoreTest.cpp index b4cf2f73..f2616573 100644 --- a/src/test/core/GpgCoreTest.cpp +++ b/src/test/core/GpgCoreTest.cpp @@ -30,6 +30,7 @@ #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/utils/IOUtils.h" +#include "spdlog/spdlog.h" namespace GpgFrontend::Test { @@ -54,7 +55,8 @@ void GpgCoreTest::TearDown() {} void GpgCoreTest::SetUp() { libconfig::Config cfg; - ASSERT_NO_THROW(cfg.readFile(config_path_.c_str())); + SPDLOG_INFO("test case config file path: {}", config_path_.string()); + ASSERT_NO_THROW(cfg.readFile(config_path_.string())); auto& root = cfg.getRoot(); import_private_keys(root); } diff --git a/src/test/core/GpgCoreTest.h b/src/test/core/GpgCoreTest.h index bffaf9ad..5730b6e5 100644 --- a/src/test/core/GpgCoreTest.h +++ b/src/test/core/GpgCoreTest.h @@ -28,12 +28,13 @@ #pragma once +#include <gtest/gtest.h> + #include <boost/date_time.hpp> #include <boost/dll.hpp> #include <boost/dll/runtime_symbol_info.hpp> #include <filesystem> -#include "GpgFrontendTest.h" #include "core/function/GlobalSettingStation.h" #include "core/typedef/CoreTypedef.h" @@ -56,7 +57,6 @@ class GpgCoreTest : public ::testing::Test { // Data File Directory Location std::filesystem::path data_path_ = GlobalSettingStation::GetInstance().GetAppDir() / "test" / "data"; - }; } // namespace GpgFrontend::Test
\ No newline at end of file diff --git a/src/ui/GpgFrontendApplication.cpp b/src/ui/GpgFrontendApplication.cpp index 7b079de1..a9e4efd2 100644 --- a/src/ui/GpgFrontendApplication.cpp +++ b/src/ui/GpgFrontendApplication.cpp @@ -53,25 +53,6 @@ GpgFrontendApplication::GpgFrontendApplication(int &argc, char *argv[]) QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8")); } -GpgFrontendApplication *GpgFrontendApplication::GetInstance(int argc, - char *argv[], - bool new_instance) { - static GpgFrontendApplication *instance = nullptr; - static int static_argc = argc; - static char **static_argv = argv; - - if (new_instance || !instance) { - if (instance != nullptr) { - SPDLOG_DEBUG("old application exists, quitting..."); - instance->quit(); - delete instance; - } - SPDLOG_DEBUG("creating new application instance, argc: {}", argc); - instance = new GpgFrontendApplication(static_argc, static_argv); - } - return instance; -} - bool GpgFrontendApplication::notify(QObject *receiver, QEvent *event) { #ifdef RELEASE try { diff --git a/src/ui/GpgFrontendApplication.h b/src/ui/GpgFrontendApplication.h index 76bef259..1ec782cf 100644 --- a/src/ui/GpgFrontendApplication.h +++ b/src/ui/GpgFrontendApplication.h @@ -49,15 +49,6 @@ class GPGFRONTEND_UI_EXPORT GpgFrontendApplication : public QApplication { */ ~GpgFrontendApplication() override = default; - /** - * @brief Get the GpgFrontend Application object - * - * @return GpgFrontendApplication* - */ - static GpgFrontendApplication *GetInstance(int argc = 0, - char *argv[] = nullptr, - bool new_instance = false); - protected: /** * @brief |