#include #include #include #include #include #include #include #include using namespace Void::Plugin; #if defined(VOID_TEST_PLUGIN_PATH) && defined(VOID_STATEFUL_V1_PATH) namespace { #if defined(_WIN32) constexpr std::string_view kPluginExtension = ".dll"; #else constexpr std::string_view kPluginExtension = ".so"; #endif } // namespace Void::Testing::Test discovery_loads_all_plugins_in_filename_order() { Void::FileSystem::TempDir tmp{"void_disc"}; std::filesystem::copy_file(VOID_STATEFUL_V1_PATH, tmp.path() / ("b_stateful" + std::string{kPluginExtension})); std::filesystem::copy_file(VOID_TEST_PLUGIN_PATH, tmp.path() / ("a_test" + std::string{kPluginExtension})); { std::ofstream decoy{tmp.path() / "readme.txt"}; decoy << "not a plugin"; } std::filesystem::create_directory(tmp.path() / "subdir"); PluginManager mgr; const auto results = mgr.load_directory(tmp.path()); // Only the two plugin files count, sorted by filename. Void::Testing::expect_eq(results.size(), std::size_t{2}); if (results.size() != 2) { return; } Void::Testing::expect_eq( results[0].first.filename().string(), std::string{"a_test"} + std::string{kPluginExtension}); Void::Testing::expect(results[0].second.has_value()); Void::Testing::expect_eq(*results[0].second, std::string{"TestPlugin"}); Void::Testing::expect_eq( results[1].first.filename().string(), std::string{"b_stateful"} + std::string{kPluginExtension}); Void::Testing::expect(results[1].second.has_value()); Void::Testing::expect_eq(*results[1].second, std::string{"StatefulPlugin"}); Void::Testing::expect(static_cast(mgr.acquire("TestPlugin"))); Void::Testing::expect(static_cast(mgr.acquire("StatefulPlugin"))); } Void::Testing::Test discovery_reports_per_file_errors() { Void::FileSystem::TempDir tmp{"void_disc"}; std::filesystem::copy_file(VOID_TEST_PLUGIN_PATH, tmp.path() / ("ok" + std::string{kPluginExtension})); { std::ofstream bogus{tmp.path() / ("broken" + std::string{kPluginExtension})}; bogus << "ELF? no."; } PluginManager mgr; const auto results = mgr.load_directory(tmp.path()); Void::Testing::expect_eq(results.size(), std::size_t{2}); if (results.size() != 2) { return; } // broken sorts first and fails to dlopen; ok loads. Void::Testing::expect(!results[0].second.has_value()); Void::Testing::expect(results[0].second.error() == LoadError::DlopenFailed); Void::Testing::expect(results[1].second.has_value()); } Void::Testing::Test discovery_missing_directory_yields_empty() { PluginManager mgr; const auto results = mgr.load_directory("/nonexistent/void_discovery_test"); Void::Testing::expect(results.empty()); } #endif // test plugins available