From 47362559ef3d51ca1706d769ad8c42418bdaa825 Mon Sep 17 00:00:00 2001 From: iTrooz Date: Mon, 26 Feb 2024 20:51:08 +0100 Subject: [PATCH] tests: Add infrastructure for testing plugins (#1538) This PR adds a test architecture to be able to test plugins Main infrastructure done by @WerWolv --------- Co-authored-by: WerWolv --- .github/workflows/tests.yml | 4 +- CMakeLists.txt | 7 ++- cmake/modules/ImHexPlugin.cmake | 6 +++ dist/rpm/imhex.spec | 4 -- lib/libimhex/CMakeLists.txt | 2 + .../include/hex/api/event_manager.hpp | 2 + .../include/hex/api/plugin_manager.hpp | 1 + lib/libimhex/include/hex/helpers/logger.hpp | 2 +- .../include/hex/providers/provider.hpp | 2 +- .../include/hex/test/test_provider.hpp | 0 .../libimhex}/include/hex/test/tests.hpp | 12 ++++-- lib/libimhex/source/api/plugin_manager.cpp | 9 ++++ lib/libimhex/source/helpers/logger.cpp | 1 + lib/libimhex/source/providers/provider.cpp | 7 ++- lib/libimhex/source/test/tests.cpp | 23 ++++++++++ plugins/builtin/CMakeLists.txt | 2 - .../include/content/helpers/notification.hpp | 8 ---- .../source/content/helpers/notification.cpp | 16 ------- plugins/builtin/source/content/project.cpp | 17 +++++--- plugins/builtin/source/content/providers.cpp | 30 ++++++------- plugins/builtin/tests/CMakeLists.txt | 19 ++++++++ plugins/builtin/tests/source/main.cpp | 43 +++++++++++++++++++ .../ui/include/toasts/toast_notification.hpp | 14 ++++-- tests/CMakeLists.txt | 3 +- tests/common/CMakeLists.txt | 1 - tests/common/source/main.cpp | 7 ++- tests/plugins/CMakeLists.txt | 16 +++++++ tests/plugins/source/plugins.cpp | 16 +++++++ 28 files changed, 205 insertions(+), 69 deletions(-) rename {tests/common => lib/libimhex}/include/hex/test/test_provider.hpp (100%) rename {tests/common => lib/libimhex}/include/hex/test/tests.hpp (87%) create mode 100644 lib/libimhex/source/test/tests.cpp delete mode 100644 plugins/builtin/include/content/helpers/notification.hpp delete mode 100644 plugins/builtin/source/content/helpers/notification.cpp create mode 100644 plugins/builtin/tests/CMakeLists.txt create mode 100644 plugins/builtin/tests/source/main.cpp create mode 100644 tests/plugins/CMakeLists.txt create mode 100644 tests/plugins/source/plugins.cpp diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9333456c7..c98f7f403 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,9 +2,8 @@ name: "Unit Tests" on: push: - branches: [ master ] + branches: ["*"] pull_request: - branches: [ master ] workflow_dispatch: jobs: @@ -49,6 +48,7 @@ jobs: cd build CC=gcc-12 CXX=g++-12 cmake \ -DCMAKE_BUILD_TYPE=Debug \ + -DIMHEX_ENABLE_UNIT_TESTS=ON \ -DCMAKE_C_COMPILER_LAUNCHER=ccache \ -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ -DCMAKE_C_FLAGS="-fuse-ld=lld -fsanitize=address,leak,undefined -fno-sanitize-recover=all" \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 48fcde92a..4115e978a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ option(IMHEX_ENABLE_UNITY_BUILD "Enables building ImHex as a unity build option(IMHEX_GENERATE_PDBS "Enable generating PDB files in non-debug builds (Windows only)" OFF) option(IMHEX_REPLACE_DWARF_WITH_PDB "Remove DWARF information from binaries when generating PDBS (Windows only)" OFF) option(IMHEX_ENABLE_STD_ASSERTS "Enable debug asserts in the C++ std library. (Breaks Plugin ABI!)" OFF) +option(IMHEX_ENABLE_UNIT_TESTS "Enable building unit tests" OFF) # Basic compiler and cmake configurations set(CMAKE_CXX_STANDARD 23) @@ -61,8 +62,10 @@ add_subdirectory(lib/libimhex) add_subdirectory(main) # Add unit tests -enable_testing() -add_subdirectory(tests EXCLUDE_FROM_ALL) +if (IMHEX_ENABLE_UNIT_TESTS) + enable_testing() + add_subdirectory(tests EXCLUDE_FROM_ALL) +endif () # Configure more resources that will be added to the install package createPackage() diff --git a/cmake/modules/ImHexPlugin.cmake b/cmake/modules/ImHexPlugin.cmake index 0431b5cb4..a170eed61 100644 --- a/cmake/modules/ImHexPlugin.cmake +++ b/cmake/modules/ImHexPlugin.cmake @@ -88,6 +88,12 @@ macro(add_imhex_plugin) elseif (UNIX) set_target_properties(${IMHEX_PLUGIN_NAME} PROPERTIES INSTALL_RPATH_USE_ORIGIN ON INSTALL_RPATH "$ORIGIN/") endif() + + if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt AND IMHEX_ENABLE_UNIT_TESTS) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests) + target_link_libraries(${IMHEX_PLUGIN_NAME} PUBLIC ${IMHEX_PLUGIN_NAME}_tests) + target_compile_definitions(${IMHEX_PLUGIN_NAME}_tests PRIVATE IMHEX_PROJECT_NAME="${IMHEX_PLUGIN_NAME}-tests") + endif() endmacro() macro(add_romfs_resource input output) diff --git a/dist/rpm/imhex.spec b/dist/rpm/imhex.spec index 2a08531ff..4b8f5f94f 100644 --- a/dist/rpm/imhex.spec +++ b/dist/rpm/imhex.spec @@ -95,10 +95,6 @@ CXXFLAGS+=" -std=gnu++2b" %set_build_flags CXXFLAGS+=" -std=gnu++2b" %endif -# build binaries required for tests -%cmake_build --target unit_tests -%ctest --exclude-regex '(Helpers/StoreAPI|Helpers/TipsAPI|Helpers/ContentAPI)' -# Helpers/*API exclude tests that require network access %install diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index 3f4417f64..df7f73592 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -38,6 +38,8 @@ set(LIBIMHEX_SOURCES source/helpers/tar.cpp source/helpers/debugging.cpp + source/test/tests.cpp + source/providers/provider.cpp source/providers/memory_provider.cpp source/providers/undo/stack.cpp diff --git a/lib/libimhex/include/hex/api/event_manager.hpp b/lib/libimhex/include/hex/api/event_manager.hpp index 85690ff5f..4847755ed 100644 --- a/lib/libimhex/include/hex/api/event_manager.hpp +++ b/lib/libimhex/include/hex/api/event_manager.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -308,4 +309,5 @@ namespace hex { * The 'from' provider should not have any per provider data after this, and should be immediately deleted */ EVENT_DEF(MovePerProviderData, prv::Provider *, prv::Provider *); + } \ No newline at end of file diff --git a/lib/libimhex/include/hex/api/plugin_manager.hpp b/lib/libimhex/include/hex/api/plugin_manager.hpp index a4528436f..0a736a24c 100644 --- a/lib/libimhex/include/hex/api/plugin_manager.hpp +++ b/lib/libimhex/include/hex/api/plugin_manager.hpp @@ -111,6 +111,7 @@ namespace hex { static void addPlugin(const std::string &name, PluginFunctions functions); + static Plugin* getPlugin(const std::string &name); static const std::list& getPlugins(); static const std::vector& getPluginPaths(); static const std::vector& getPluginLoadPaths(); diff --git a/lib/libimhex/include/hex/helpers/logger.hpp b/lib/libimhex/include/hex/helpers/logger.hpp index ec3d778fa..013128131 100644 --- a/lib/libimhex/include/hex/helpers/logger.hpp +++ b/lib/libimhex/include/hex/helpers/logger.hpp @@ -8,6 +8,7 @@ #include #include +#include namespace hex::log { @@ -88,7 +89,6 @@ namespace hex::log { hex::log::impl::print(fg(impl::color::fatal()) | fmt::emphasis::bold, "[FATAL]", fmt, args...); } - [[maybe_unused]] void print(const std::string &fmt, auto && ... args) { std::scoped_lock lock(impl::getLoggerMutex()); diff --git a/lib/libimhex/include/hex/providers/provider.hpp b/lib/libimhex/include/hex/providers/provider.hpp index 8ef87d93f..86bb376ee 100644 --- a/lib/libimhex/include/hex/providers/provider.hpp +++ b/lib/libimhex/include/hex/providers/provider.hpp @@ -161,7 +161,7 @@ namespace hex::prv { */ [[nodiscard]] virtual std::string getName() const = 0; - void resize(u64 newSize); + bool resize(u64 newSize); void insert(u64 offset, u64 size); void remove(u64 offset, u64 size); diff --git a/tests/common/include/hex/test/test_provider.hpp b/lib/libimhex/include/hex/test/test_provider.hpp similarity index 100% rename from tests/common/include/hex/test/test_provider.hpp rename to lib/libimhex/include/hex/test/test_provider.hpp diff --git a/tests/common/include/hex/test/tests.hpp b/lib/libimhex/include/hex/test/tests.hpp similarity index 87% rename from tests/common/include/hex/test/tests.hpp rename to lib/libimhex/include/hex/test/tests.hpp index 6fac3d61a..00a44ef98 100644 --- a/tests/common/include/hex/test/tests.hpp +++ b/lib/libimhex/include/hex/test/tests.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -28,16 +29,20 @@ } \ } while (0) +#define INIT_PLUGIN(name) \ + if (!hex::test::initPluginImpl(name)) TEST_FAIL(); + namespace hex::test { + using Function = int(*)(); struct Test { - std::function function; + Function function; bool shouldFail; }; class Tests { public: - static auto addTest(const std::string &name, const std::function &func, bool shouldFail) noexcept { + static auto addTest(const std::string &name, Function func, bool shouldFail) noexcept { s_tests.insert({ name, {func, shouldFail} }); @@ -50,7 +55,7 @@ namespace hex::test { } private: - static inline std::map s_tests; + static std::map s_tests; }; template @@ -86,4 +91,5 @@ namespace hex::test { return TestSequence(executor.getName(), std::forward(f), executor.shouldFail()); } + bool initPluginImpl(std::string name); } diff --git a/lib/libimhex/source/api/plugin_manager.cpp b/lib/libimhex/source/api/plugin_manager.cpp index e5260e40b..eb0209118 100644 --- a/lib/libimhex/source/api/plugin_manager.cpp +++ b/lib/libimhex/source/api/plugin_manager.cpp @@ -321,6 +321,15 @@ namespace hex { return plugins; } + Plugin* PluginManager::getPlugin(const std::string &name) { + for (auto &plugin : getPluginsMutable()) { + if (plugin.getPluginName() == name) + return &plugin; + } + + return nullptr; + } + const std::vector& PluginManager::getPluginPaths() { return s_pluginPaths; } diff --git a/lib/libimhex/source/helpers/logger.cpp b/lib/libimhex/source/helpers/logger.cpp index fad4421cb..7abf811e7 100644 --- a/lib/libimhex/source/helpers/logger.cpp +++ b/lib/libimhex/source/helpers/logger.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/lib/libimhex/source/providers/provider.cpp b/lib/libimhex/source/providers/provider.cpp index 31e68177d..46918908e 100644 --- a/lib/libimhex/source/providers/provider.cpp +++ b/lib/libimhex/source/providers/provider.cpp @@ -74,7 +74,11 @@ namespace hex::prv { } } - void Provider::resize(u64 newSize) { + bool Provider::resize(u64 newSize) { + if (newSize >> 63) { + log::error("new provider size is very large ({}). Is it a negative number ?", newSize); + return false; + } i64 difference = newSize - this->getActualSize(); if (difference > 0) @@ -83,6 +87,7 @@ namespace hex::prv { EventProviderDataRemoved::post(this, this->getActualSize() + difference, -difference); this->markDirty(); + return true; } void Provider::insert(u64 offset, u64 size) { diff --git a/lib/libimhex/source/test/tests.cpp b/lib/libimhex/source/test/tests.cpp new file mode 100644 index 000000000..0bcfdaa86 --- /dev/null +++ b/lib/libimhex/source/test/tests.cpp @@ -0,0 +1,23 @@ +#include + +namespace hex::test { + std::map Tests::s_tests; + + bool initPluginImpl(std::string name) { + if (name != "Built-in") { + if(!initPluginImpl("Built-in")) return false; + } + + hex::Plugin *plugin = hex::PluginManager::getPlugin(name); + if (plugin == nullptr) { + hex::log::fatal("Plugin '{}' was not found !", name); + return false; + }else if (!plugin->initializePlugin()) { + hex::log::fatal("Failed to initialize plugin '{}' !", name); + return false; + } + + hex::log::info("Initialized plugin '{}' successfully", name); + return true; + } +} \ No newline at end of file diff --git a/plugins/builtin/CMakeLists.txt b/plugins/builtin/CMakeLists.txt index e5c3d3e79..59a7573d1 100644 --- a/plugins/builtin/CMakeLists.txt +++ b/plugins/builtin/CMakeLists.txt @@ -114,8 +114,6 @@ add_imhex_plugin( source/content/views/view_achievements.cpp source/content/views/view_highlight_rules.cpp source/content/views/view_tutorials.cpp - - source/content/helpers/notification.cpp INCLUDES include diff --git a/plugins/builtin/include/content/helpers/notification.hpp b/plugins/builtin/include/content/helpers/notification.hpp deleted file mode 100644 index bec1a2127..000000000 --- a/plugins/builtin/include/content/helpers/notification.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -namespace hex::plugin::builtin { - - void showError(const std::string& message); - - void showWarning(const std::string& message); -} diff --git a/plugins/builtin/source/content/helpers/notification.cpp b/plugins/builtin/source/content/helpers/notification.cpp deleted file mode 100644 index 7bcecb005..000000000 --- a/plugins/builtin/source/content/helpers/notification.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include - -#include - -namespace hex::plugin::builtin { - - void showError(const std::string& message){ - ui::ToastError::open(message); - log::error(message); - } - - void showWarning(const std::string& message){ - ui::ToastWarning::open(message); - log::warn(message); - } -} diff --git a/plugins/builtin/source/content/project.cpp b/plugins/builtin/source/content/project.cpp index 5e6b2231e..1d85f07ca 100644 --- a/plugins/builtin/source/content/project.cpp +++ b/plugins/builtin/source/content/project.cpp @@ -13,8 +13,7 @@ #include #include #include - -#include +#include namespace hex::plugin::builtin { @@ -23,26 +22,29 @@ namespace hex::plugin::builtin { bool load(const std::fs::path &filePath) { if (!wolv::io::fs::exists(filePath) || !wolv::io::fs::isRegularFile(filePath)) { - showError(hex::format("hex.builtin.popup.error.project.load"_lang, + ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang, hex::format("hex.builtin.popup.error.project.load.file_not_found"_lang, wolv::util::toUTF8String(filePath) ))); + return false; } Tar tar(filePath, Tar::Mode::Read); if (!tar.isValid()) { - showError(hex::format("hex.builtin.popup.error.project.load"_lang, + ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang, hex::format("hex.builtin.popup.error.project.load.invalid_tar"_lang, tar.getOpenErrorString() ))); + return false; } if (!tar.contains(MetadataPath)) { - showError(hex::format("hex.builtin.popup.error.project.load"_lang, + ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang, hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang) )); + return false; } @@ -50,9 +52,10 @@ namespace hex::plugin::builtin { const auto metadataContent = tar.readVector(MetadataPath); if (!std::string(metadataContent.begin(), metadataContent.end()).starts_with(MetadataHeaderMagic)) { - showError(hex::format("hex.builtin.popup.error.project.load"_lang, + ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang, hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang) )); + return false; } } @@ -176,4 +179,4 @@ namespace hex::plugin::builtin { void registerProjectHandlers() { hex::ProjectFile::setProjectFunctions(load, store); } -} \ No newline at end of file +} diff --git a/plugins/builtin/source/content/providers.cpp b/plugins/builtin/source/content/providers.cpp index 8a59ad955..513a23b04 100644 --- a/plugins/builtin/source/content/providers.cpp +++ b/plugins/builtin/source/content/providers.cpp @@ -11,13 +11,13 @@ #include #include #include -#include "content/helpers/notification.hpp" #include #include #include #include +#include #include @@ -70,9 +70,11 @@ namespace hex::plugin::builtin { if (newProvider == nullptr) { // If a provider is not created, it will be overwritten when saving the project, // so we should prevent the project from loading at all - showError(hex::format("hex.builtin.popup.error.project.load"_lang, - hex::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType) - )); + ui::ToastError::open( + hex::format("hex.builtin.popup.error.project.load"_lang, + hex::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType) + ) + ); success = false; break; } @@ -94,29 +96,27 @@ namespace hex::plugin::builtin { } } - std::string warningMsg; + std::string warningMessage; for (const auto &warning : providerWarnings){ ImHexApi::Provider::remove(warning.first); - warningMsg.append( + warningMessage.append( hex::format("\n - {} : {}", warning.first->getName(), warning.second)); } // If no providers were opened, display an error with // the warnings that happened when opening them - if (ImHexApi::Provider::getProviders().size() == 0) { - showError(hex::format("hex.builtin.popup.error.project.load"_lang, - hex::format("hex.builtin.popup.error.project.load.no_providers"_lang)) + warningMsg); + if (ImHexApi::Provider::getProviders().empty()) { + ui::ToastError::open(hex::format("{}{}", "hex.builtin.popup.error.project.load"_lang, "hex.builtin.popup.error.project.load.no_providers"_lang, warningMessage)); return false; - } else { - - // Else, if are warnings, still display them - if (warningMsg.empty()) { + } else { + // Else, if there are warnings, still display them + if (warningMessage.empty()) { return true; } else { - showWarning( - hex::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMsg)); + ui::ToastWarning::open(hex::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMessage)); } + return success; } }, diff --git a/plugins/builtin/tests/CMakeLists.txt b/plugins/builtin/tests/CMakeLists.txt new file mode 100644 index 000000000..4c82cebd3 --- /dev/null +++ b/plugins/builtin/tests/CMakeLists.txt @@ -0,0 +1,19 @@ +project(${IMHEX_PLUGIN_NAME}_tests) + +# Add new tests here # +set(AVAILABLE_TESTS + Providers/ReadWrite + Providers/InvalidResize +) + +add_library(${PROJECT_NAME} SHARED + source/main.cpp +) + +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/plugins/builtin/include) + +target_link_libraries(${PROJECT_NAME} PRIVATE libimhex) + +foreach (test IN LISTS AVAILABLE_TESTS) + add_test(NAME "Plugin_${IMHEX_PLUGIN_NAME}/${test}" COMMAND $ "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) +endforeach () \ No newline at end of file diff --git a/plugins/builtin/tests/source/main.cpp b/plugins/builtin/tests/source/main.cpp new file mode 100644 index 000000000..4ac8b04c1 --- /dev/null +++ b/plugins/builtin/tests/source/main.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include + +using namespace hex; +using namespace hex::plugin::builtin; + +TEST_SEQUENCE("Providers/ReadWrite") { + INIT_PLUGIN("Built-in"); + + auto &pr = *ImHexApi::Provider::createProvider("hex.builtin.provider.mem_file", true); + + TEST_ASSERT(pr.getSize() == 0); + TEST_ASSERT(!pr.isDirty()); + + pr.resize(50); + TEST_ASSERT(pr.getSize() == 50); + TEST_ASSERT(pr.isDirty()); + + char buf[] = "\x99\x99"; // temporary value that should be overwriten + pr.read(0, buf, 2); + TEST_ASSERT(std::equal(buf, buf+2, "\x00\x00")); + + pr.write(0, "\xFF\xFF", 2); + char buf2[] = "\x99\x99"; // temporary value that should be overwriten + pr.read(0, buf2, 2); + TEST_ASSERT(std::equal(buf2, buf2+2, "\xFF\xFF")); + + TEST_SUCCESS(); +}; + +TEST_SEQUENCE("Providers/InvalidResize") { + INIT_PLUGIN("Built-in"); + + auto &pr = *ImHexApi::Provider::createProvider("hex.builtin.provider.mem_file", true); + + + TEST_ASSERT(!pr.resize(-1)); + TEST_SUCCESS(); +}; \ No newline at end of file diff --git a/plugins/ui/include/toasts/toast_notification.hpp b/plugins/ui/include/toasts/toast_notification.hpp index ead7c5ebc..ab43a376b 100644 --- a/plugins/ui/include/toasts/toast_notification.hpp +++ b/plugins/ui/include/toasts/toast_notification.hpp @@ -1,11 +1,11 @@ #pragma once -#include #include #include #include #include +#include #include @@ -49,17 +49,23 @@ namespace hex::ui { struct ToastInfo : impl::ToastNotification { ToastInfo(std::string message) - : ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerInfo), ICON_VS_INFO, "hex.ui.common.info", std::move(message)) {} + : ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerInfo), ICON_VS_INFO, "hex.ui.common.info", std::move(message)) { + log::info("{}", message); + } }; struct ToastWarning : impl::ToastNotification { ToastWarning(std::string message) - : ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerWarning), ICON_VS_WARNING, "hex.ui.common.warning", std::move(message)) {} + : ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerWarning), ICON_VS_WARNING, "hex.ui.common.warning", std::move(message)) { + log::warn("{}", message); + } }; struct ToastError : impl::ToastNotification { ToastError(std::string message) - : ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerError), ICON_VS_ERROR, "hex.ui.common.error", std::move(message)) {} + : ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerError), ICON_VS_ERROR, "hex.ui.common.error", std::move(message)) { + log::error("{}", message); + } }; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 47fd39d1f..81dade9b7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,9 +1,10 @@ project(unit_tests) -add_custom_target(unit_tests DEPENDS helpers algorithms) +add_custom_target(unit_tests DEPENDS imhex_all helpers algorithms plugins) add_subdirectory(common) target_compile_definitions(tests_common PUBLIC IMHEX_PROJECT_NAME="${PROJECT_NAME}") add_subdirectory(helpers) add_subdirectory(algorithms) +add_subdirectory(plugins) diff --git a/tests/common/CMakeLists.txt b/tests/common/CMakeLists.txt index 5fadc6ab4..98c922020 100644 --- a/tests/common/CMakeLists.txt +++ b/tests/common/CMakeLists.txt @@ -5,5 +5,4 @@ project(tests_common) add_library(tests_common STATIC source/main.cpp ) -target_include_directories(tests_common PUBLIC include) target_link_libraries(tests_common PUBLIC libimhex ${FMT_LIBRARIES} libwolv) diff --git a/tests/common/source/main.cpp b/tests/common/source/main.cpp index 03f2066a3..217e229ba 100644 --- a/tests/common/source/main.cpp +++ b/tests/common/source/main.cpp @@ -3,8 +3,10 @@ #include #include #include - #include +#include +#include +#include #include @@ -48,7 +50,10 @@ int main(int argc, char **argv) { else hex::log::info("Failed!"); + hex::TaskManager::exit(); + hex::EventImHexClosing::post(); hex::EventManager::clear(); + hex::PluginManager::unload(); return result; } \ No newline at end of file diff --git a/tests/plugins/CMakeLists.txt b/tests/plugins/CMakeLists.txt new file mode 100644 index 000000000..35b4da650 --- /dev/null +++ b/tests/plugins/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.16) + +project(plugins_test) + +add_executable(${PROJECT_NAME} + source/plugins.cpp +) + + +# ---- No need to change anything from here downwards unless you know what you're doing ---- # + +target_include_directories(${PROJECT_NAME} PRIVATE include) +target_link_libraries(${PROJECT_NAME} PRIVATE libimhex tests_common ${FMT_LIBRARIES}) + +set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) +add_dependencies(unit_tests ${PROJECT_NAME}) diff --git a/tests/plugins/source/plugins.cpp b/tests/plugins/source/plugins.cpp new file mode 100644 index 000000000..0cdb40167 --- /dev/null +++ b/tests/plugins/source/plugins.cpp @@ -0,0 +1,16 @@ +#include + +#include + +using namespace hex; +class PluginLoader { +public: + PluginLoader() { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Plugins)) { + PluginManager::addLoadPath(dir); + } + + PluginManager::load(); + } +}; +static PluginLoader pluginLoader; \ No newline at end of file