test: cover flatpak parsing and plugin discovery

Unit-test the flatpak parsers (search rows, installed/update columns)
and the plugin manager end-to-end: built-in registration, runtime
discovery of the sample plugin, enable/disable persistence, reload and
install-hint exposure.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-08-29 13:34:50 +02:00
1 parent bc737d356e
commit 71eb962b00
3 files changed
+197

No files matched your search

+21
View File
@@ -48,6 +48,27 @@ add_executable(pacman-manager-test tests/pacman-manager.test.cpp src/pacman-mana
target_include_directories(pacman-manager-test PRIVATE include)
add_test(NAME pacman-manager-test COMMAND pacman-manager-test)
add_executable(flatpak-manager-test tests/flatpak-manager.test.cpp src/flatpak-manager.cpp)
target_include_directories(flatpak-manager-test PRIVATE include)
add_test(NAME flatpak-manager-test COMMAND flatpak-manager-test)
# Plugin manager test: needs Qt (QSettings/QLibrary) and the example plugin
# built next to the test binary, so it is only enabled with the plugin option.
if(FWS_BUILD_EXAMPLE_PLUGIN)
add_executable(plugin-manager-test tests/plugin-manager.test.cpp
src/plugin-manager.cpp src/pacman-manager.cpp src/flatpak-manager.cpp)
target_include_directories(plugin-manager-test PRIVATE include)
target_link_libraries(plugin-manager-test PRIVATE Qt6::Core)
add_dependencies(plugin-manager-test example-provider-plugin)
add_custom_command(TARGET plugin-manager-test POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
$<TARGET_FILE_DIR:plugin-manager-test>/plugins
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:example-provider-plugin>
$<TARGET_FILE_DIR:plugin-manager-test>/plugins/)
add_test(NAME plugin-manager-test COMMAND plugin-manager-test)
endif()
# Optional example external provider plugin. Demonstrates the plugin mechanism:
# the produced library is copied next to the executable and discovered at
# runtime by PluginManager (see the Providers page).
+102
View File
@@ -0,0 +1,102 @@
#include "flatpak-manager.h"
#include <cassert>
#include <iostream>
static void testParseSearchOutput_parsesAlignedRows() {
std::string output =
"Name Description Application ID Version Branch Remotes\n"
"Firefox Fast and privacy-aware browser org.mozilla.firefox 128.0.2 stable flathub\n"
"Calculator Simple calculator org.gnome.Calculator 46.0 stable flathub\n";
auto pkgs = FlatpakManager::parseSearchOutput(output);
assert(pkgs.size() == 2);
assert(pkgs[0].name == "org.mozilla.firefox");
assert(pkgs[0].version == "128.0.2");
assert(pkgs[0].repository == "flathub");
// The display name is prefixed to the description.
assert(pkgs[0].description == "Firefox — Fast and privacy-aware browser");
assert(pkgs[1].name == "org.gnome.Calculator");
}
static void testParseSearchOutput_keepsMultiWordDescriptions() {
std::string output =
"Name Description Application ID Version Branch Remotes\n"
"Spotify Desktop music player with sync org.spotify.Client 1.2.3 stable flathub\n";
auto pkgs = FlatpakManager::parseSearchOutput(output);
assert(pkgs.size() == 1);
assert(pkgs[0].name == "org.spotify.Client");
assert(pkgs[0].description == "Spotify — Desktop music player with sync");
}
static void testParseSearchOutput_ignoresVersionLikeTokens() {
// Versions such as 128.0.2 must not be mistaken for an application ID.
std::string output =
"Name Description Application ID Version Branch Remotes\n"
"GIMP Image editor org.gimp.GIMP 2.10.36 stable flathub\n";
auto pkgs = FlatpakManager::parseSearchOutput(output);
assert(pkgs.size() == 1);
assert(pkgs[0].name == "org.gimp.GIMP");
assert(pkgs[0].version == "2.10.36");
}
static void testParseSearchOutput_emptyInput() {
assert(FlatpakManager::parseSearchOutput("").empty());
}
static void testParseInstalledOutput_parsesTabSeparated() {
std::string output =
"org.mozilla.firefox\t128.0.2\tflathub\n"
"org.gnome.Calculator\t46.0\tflathub\n";
auto pkgs = FlatpakManager::parseInstalledOutput(output);
assert(pkgs.size() == 2);
assert(pkgs[0].name == "org.mozilla.firefox");
assert(pkgs[0].version == "128.0.2");
assert(pkgs[0].repository == "flathub");
assert(pkgs[0].installed);
assert(!pkgs[0].updateAvailable);
}
static void testParseInstalledOutput_emptyInput() {
assert(FlatpakManager::parseInstalledOutput("").empty());
}
static void testParseUpdatesOutput_parsesRows() {
std::string output =
"org.mozilla.firefox\t130.0\n"
"org.gnome.Calculator\t47.0\n";
auto pkgs = FlatpakManager::parseUpdatesOutput(output);
assert(pkgs.size() == 2);
assert(pkgs[0].name == "org.mozilla.firefox");
assert(pkgs[0].version == "130.0");
assert(pkgs[0].installed);
assert(pkgs[0].updateAvailable);
}
static void testParseUpdatesOutput_emptyInput() {
assert(FlatpakManager::parseUpdatesOutput("").empty());
}
int main() {
testParseSearchOutput_parsesAlignedRows();
testParseSearchOutput_keepsMultiWordDescriptions();
testParseSearchOutput_ignoresVersionLikeTokens();
testParseSearchOutput_emptyInput();
testParseInstalledOutput_parsesTabSeparated();
testParseInstalledOutput_emptyInput();
testParseUpdatesOutput_parsesRows();
testParseUpdatesOutput_emptyInput();
std::cout << "All flatpak-manager tests passed." << std::endl;
return 0;
}
+74
View File
@@ -0,0 +1,74 @@
#include "plugin-manager.h"
#include "pacman-manager.h"
#include "flatpak-manager.h"
#include <QCoreApplication>
#include <cassert>
#include <iostream>
// Exercises PluginManager end-to-end: built-in registration plus runtime
// discovery of the example plugin (built into build/plugins by CMake when
// FWS_BUILD_EXAMPLE_PLUGIN=ON).
int main(int argc, char* argv[]) {
QCoreApplication app(argc, argv);
app.setOrganizationName("Freebuff");
app.setApplicationName("PacManager");
PluginManager pm;
pm.registerBuiltin({"pacman", "Pacman", "1.0.0", "Natif Arch Linux", "FWS"},
new PacmanManager());
// Flatpak ships an install hint so the app can offer to install the tool
// (pacman -S flatpak) when it is missing on the system.
pm.registerBuiltin({"flatpak", "Flatpak", "1.0.0", "Flatpak", "FWS",
false, "", false, "flatpak"},
new FlatpakManager());
pm.discoverExternal();
// Built-ins + the sample plugin dropped next to the test binary.
assert(pm.providers().size() == 3);
// Install hints are exposed per provider; the sample plugin omits its
// optional hint symbol, which must read back as empty.
const auto& providers = pm.providers();
for (const auto& entry : providers) {
if (entry.info.id == "flatpak") assert(entry.info.installHint == "flatpak");
if (entry.info.id == "sample") assert(entry.info.installHint.empty());
}
assert(pm.findAvailable("flatpak") != nullptr);
// External plugin metadata is exposed through the C ABI.
const auto* sample = pm.findAvailable("sample");
assert(sample != nullptr);
assert(sample->info.name == "Sample Provider");
assert(sample->info.version == "0.1.0");
assert(sample->info.author == "FWS");
assert(!sample->info.builtin);
assert(sample->manager != nullptr);
assert(sample->manager->getName() == "sample");
assert(sample->manager->isAvailable());
// Manager lookup works by id.
IPackageManager* pacman = pm.managerFor("pacman");
assert(pacman != nullptr);
assert(pacman->getName() == "pacman");
// Enable/disable is persisted and filters the available list.
assert(pm.isEnabled("sample"));
pm.setEnabled("sample", false);
assert(!pm.isEnabled("sample"));
assert(pm.findAvailable("sample") == nullptr);
pm.setEnabled("sample", true);
assert(pm.findAvailable("sample") != nullptr);
// Reload keeps built-ins (same instance) and rescans external plugins.
IPackageManager* pacmanBefore = pacman;
pm.reload();
assert(pm.managerFor("pacman") == pacmanBefore);
assert(pm.findAvailable("sample") != nullptr);
// refreshAvailability re-evaluates the presence of every tool.
pm.refreshAvailability();
assert(pm.findAvailable("pacman") != nullptr);
std::cout << "All plugin-manager tests passed." << std::endl;
return 0;
}