Files
FWS-PacManage/tests/pacman-manager.test.cpp
T
BlackAngelTV 632cdf1dea feat(cleanup): add a maintenance page for unused and legacy packages
Leftovers pile up on a machine: dependencies that no installed package
requires any more, and installs coming from the AUR or manual builds that the
sync databases know nothing about. The new Nettoyage page lists both (orphans
first, since they are the ones that can be removed in bulk), shows which
provider reported each row, and frees the space held by the caches: paccache
(or pacman -Sc) and flatpak's unused runtimes.

Cleanup is an optional capability of IPackageManager, so providers that only
install packages keep working unchanged.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-09-12 22:17:08 +02:00

249 lines
8.3 KiB
C++

#include "pacman-manager.h"
#include <cassert>
#include <iostream>
static void testParseInstalledOutput_parsesSimpleLines() {
std::string output = "firefox 128.0\nvim 9.1.0000\n";
auto pkgs = PacmanManager::parseInstalledOutput(output);
assert(pkgs.size() == 2);
assert(pkgs[0].name == "firefox");
assert(pkgs[0].version == "128.0");
assert(pkgs[0].installed);
assert(pkgs[1].name == "vim");
assert(pkgs[1].version == "9.1.0000");
}
static void testParseInstalledOutput_keepsHyphensInNames() {
// Hyphenated package names must not be split on the dash (python-pip, not python)
std::string output = "python-pip 24.0-1\n";
auto pkgs = PacmanManager::parseInstalledOutput(output);
assert(pkgs.size() == 1);
assert(pkgs[0].name == "python-pip");
assert(pkgs[0].version == "24.0-1");
}
static void testParseInstalledOutput_skipsEmptyLines() {
std::string output = "firefox 128.0\n\nvim 9.1.0000\n";
auto pkgs = PacmanManager::parseInstalledOutput(output);
assert(pkgs.size() == 2);
}
static void testParseInstalledOutput_emptyInput() {
auto pkgs = PacmanManager::parseInstalledOutput("");
assert(pkgs.empty());
}
static void testParseInstalledNames_parsesNames() {
std::string output = "firefox 128.0\npython-pip 24.0-1\nvim 9.1.0000\n";
auto names = PacmanManager::parseInstalledNames(output);
assert(names.size() == 3);
assert(names.count("firefox") == 1);
assert(names.count("python-pip") == 1);
assert(names.count("vim") == 1);
}
static void testParseInstalledNames_skipsEmptyLines() {
std::string output = "firefox 128.0\n\nvim 9.1.0000\n";
auto names = PacmanManager::parseInstalledNames(output);
assert(names.size() == 2);
assert(names.count("firefox") == 1);
assert(names.count("vim") == 1);
}
static void testParseInstalledNames_emptyInput() {
assert(PacmanManager::parseInstalledNames("").empty());
}
static void testParseInstalledSizes_parsesBlocks() {
std::string output =
"Name : firefox\n"
"Version : 128.0-1\n"
"Installed Size : 263.47 MiB\n"
"Install Date : 2026-01-01\n"
"\n"
"Name : vim\n"
"Installed Size : 12.3 MiB\n";
auto sizes = PacmanManager::parseInstalledSizes(output);
assert(sizes.size() == 2);
assert(sizes["firefox"] == "263.47 MiB");
assert(sizes["vim"] == "12.3 MiB");
}
static void testParseInstalledSizes_emptyInput() {
assert(PacmanManager::parseInstalledSizes("").empty());
}
static void testParseSyncRepos_skipsHeader() {
std::string output =
"Repository Name Version\n"
"core linux 6.6-1\n"
"extra firefox 128.0-1\n"
"multilib gcc-libs 13.2-1\n";
auto repos = PacmanManager::parseSyncRepos(output);
assert(repos.size() == 3);
assert(repos["linux"] == "core");
assert(repos["firefox"] == "extra");
assert(repos["gcc-libs"] == "multilib");
}
static void testParseSyncRepos_emptyInput() {
assert(PacmanManager::parseSyncRepos("").empty());
}
static void testParseCleanupOutput_tagsOrphans() {
auto pkgs = PacmanManager::parseCleanupOutput("libedit 20240808-1\n", "");
assert(pkgs.size() == 1);
assert(pkgs[0].name == "libedit");
assert(pkgs[0].cleanupReason == "Orphelin");
assert(pkgs[0].orphan);
}
static void testParseCleanupOutput_tagsForeignAsLegacy() {
auto pkgs = PacmanManager::parseCleanupOutput("", "yay-bin 12.4.2-1\n");
assert(pkgs.size() == 1);
assert(pkgs[0].name == "yay-bin");
// Legacy installs are listed for review, never bulk-removed.
assert(!pkgs[0].orphan);
assert(pkgs[0].cleanupReason.find("tranger") != std::string::npos);
}
static void testParseCleanupOutput_orphanWinsOverForeign() {
// A foreign package can also be orphaned: it stays bulk-removable, which is
// the classification the cleanup page acts on.
auto pkgs = PacmanManager::parseCleanupOutput("yay-bin 12.4.2-1\n",
"yay-bin 12.4.2-1\n");
assert(pkgs.size() == 1);
assert(pkgs[0].orphan);
}
static void testParseCleanupOutput_keepsBothSources() {
auto pkgs = PacmanManager::parseCleanupOutput("libedit 20240808-1\n",
"yay-bin 12.4.2-1\n");
assert(pkgs.size() == 2);
assert(pkgs[0].name == "libedit" && pkgs[0].orphan);
assert(pkgs[1].name == "yay-bin" && !pkgs[1].orphan);
}
static void testParseCleanupOutput_emptyInput() {
assert(PacmanManager::parseCleanupOutput("", "").empty());
}
static void testRemovePackagesCommand_listsEveryPackage() {
std::string cmd = PacmanManager::removePackagesCommand({"libedit", "python-pip"});
assert(cmd == "pacman -Rns --noconfirm 'libedit' 'python-pip'");
}
static void testRemovePackagesCommand_emptyIsNoOp() {
// Nothing to remove: the caller then has no command to run at all.
assert(PacmanManager::removePackagesCommand({}).empty());
}
static void testRemovePackagesCommand_quotesNames() {
// Package names are data: spaces and quotes must not reach the shell as
// syntax, whatever the cleanup page lists.
assert(PacmanManager::removePackagesCommand({"libedit; rm -rf /"})
== "pacman -Rns --noconfirm 'libedit; rm -rf /'");
assert(PacmanManager::removePackagesCommand({"a'b"})
== "pacman -Rns --noconfirm 'a'\\''b'");
}
static void testCleanCacheCommand_prefersPaccache() {
assert(PacmanManager::cleanCacheCommand(true) == "paccache -rk1");
}
static void testCleanCacheCommand_fallsBackToPacman() {
assert(PacmanManager::cleanCacheCommand(false) == "pacman -Sc --noconfirm");
}
static void testAuthFailed_detectsWrongPassword() {
assert(PacmanManager::authFailed(
"[sudo] password for alice: Sorry, try again.\nsudo: 1 incorrect password attempt"));
}
static void testAuthFailed_detectsMissingPassword() {
assert(PacmanManager::authFailed("sudo: a password is required"));
}
static void testAuthFailed_ignoresPacmanErrors() {
assert(!PacmanManager::authFailed("error: target not found: firefox"));
assert(!PacmanManager::authFailed("warning: firefox is up to date"));
}
static void testDbLocked_detectsEnglishLock() {
assert(PacmanManager::dbLocked(
"error: failed to synchronize all databases (unable to lock database)"));
}
static void testDbLocked_detectsFrenchLock() {
assert(PacmanManager::dbLocked(
"erreur : la synchronisation de toutes les bases de données a échoué "
"(verrouillage de la base de données impossible)"));
}
static void testDbLocked_detectsLockFile() {
assert(PacmanManager::dbLocked(
"error: failed to init transaction (unable to lock database)\n"
" if you're sure a package manager is not already running, you can "
"remove /var/lib/pacman/db.lck"));
}
static void testDbLocked_ignoresUnrelatedErrors() {
assert(!PacmanManager::dbLocked("error: target not found: firefox"));
assert(!PacmanManager::dbLocked("Mise à jour terminée"));
}
int main() {
testParseInstalledOutput_parsesSimpleLines();
testParseInstalledOutput_keepsHyphensInNames();
testParseInstalledOutput_skipsEmptyLines();
testParseInstalledOutput_emptyInput();
testParseInstalledNames_parsesNames();
testParseInstalledNames_skipsEmptyLines();
testParseInstalledNames_emptyInput();
testParseInstalledSizes_parsesBlocks();
testParseInstalledSizes_emptyInput();
testParseSyncRepos_skipsHeader();
testParseSyncRepos_emptyInput();
testParseCleanupOutput_tagsOrphans();
testParseCleanupOutput_tagsForeignAsLegacy();
testParseCleanupOutput_orphanWinsOverForeign();
testParseCleanupOutput_keepsBothSources();
testParseCleanupOutput_emptyInput();
testRemovePackagesCommand_listsEveryPackage();
testRemovePackagesCommand_emptyIsNoOp();
testRemovePackagesCommand_quotesNames();
testCleanCacheCommand_prefersPaccache();
testCleanCacheCommand_fallsBackToPacman();
testAuthFailed_detectsWrongPassword();
testAuthFailed_detectsMissingPassword();
testAuthFailed_ignoresPacmanErrors();
testDbLocked_detectsEnglishLock();
testDbLocked_detectsFrenchLock();
testDbLocked_detectsLockFile();
testDbLocked_ignoresUnrelatedErrors();
std::cout << "All pacman-manager tests passed." << std::endl;
return 0;
}