Dragging a .flatpakref, .flatpakrepo or .flatpak onto the window, or opening it with the file manager, has to reach flatpak without the user going through a search. The provider now owns its formats: it claims the files it handles and builds the matching command, and the core routes the request to whichever provider recognises it, so a new provider can claim its own formats the same way. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
172 lines
6.9 KiB
C++
172 lines
6.9 KiB
C++
#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());
|
|
}
|
|
|
|
// ---- Installing from a file (drag & drop, "Ouvrir avec PacManager") ----
|
|
|
|
static void testClassifyFile_recognisesTheThreeFlatpakFormats() {
|
|
using FileKind = FlatpakManager::FileKind;
|
|
|
|
assert(FlatpakManager::classifyFile("/home/jane/Téléchargements/org.gnome.Dictionary.flatpakref")
|
|
== FileKind::Ref);
|
|
assert(FlatpakManager::classifyFile("flathub.flatpakrepo") == FileKind::Repo);
|
|
assert(FlatpakManager::classifyFile("/tmp/app.flatpak") == FileKind::Bundle);
|
|
// A bundle's extension starts like a ref one's, so it must not be read as
|
|
// a .flatpakref by accident.
|
|
assert(FlatpakManager::classifyFile("/tmp/Org.App.flatpak") != FileKind::Ref);
|
|
|
|
// Case and an URL query string do not change the format.
|
|
assert(FlatpakManager::classifyFile("https://flathub.org/repo/appstream/x.FLATPAKREF")
|
|
== FileKind::Ref);
|
|
assert(FlatpakManager::classifyFile("https://example.org/app.flatpakref?v=2")
|
|
== FileKind::Ref);
|
|
|
|
// Anything else is not ours: a pacman package, a directory named after the
|
|
// format, or a plain document.
|
|
assert(FlatpakManager::classifyFile("/tmp/firefox.pkg.tar.zst") == FileKind::None);
|
|
assert(FlatpakManager::classifyFile("/tmp/flatpak") == FileKind::None);
|
|
assert(FlatpakManager::classifyFile("") == FileKind::None);
|
|
}
|
|
|
|
static void testRepoName_comesFromTheFileName() {
|
|
assert(FlatpakManager::repoNamespace("flathub.flatpakrepo") == "flathub");
|
|
assert(FlatpakManager::repoNamespace("/tmp/My-Repo.FlatpakRepo") == "my-repo");
|
|
// A nameless path still has to register a remote under some name.
|
|
assert(!FlatpakManager::repoNamespace("").empty());
|
|
}
|
|
|
|
static void testInstallFileCommand_buildsTheFlatpakCalls() {
|
|
using FileKind = FlatpakManager::FileKind;
|
|
|
|
// A ref file or bundle is handed to `flatpak install`, non-interactively so
|
|
// a dropped file cannot open a prompt nobody is looking at.
|
|
const std::string ref = FlatpakManager::installFileCommand(
|
|
FileKind::Ref, "/tmp/org.gnome.Dictionary.flatpakref");
|
|
assert(ref.find("flatpak install") == 0);
|
|
assert(ref.find("--noninteractive") != std::string::npos);
|
|
assert(ref.find("--assumeyes") != std::string::npos);
|
|
assert(ref.find("/tmp/org.gnome.Dictionary.flatpakref") != std::string::npos);
|
|
assert(ref.find("--user") == std::string::npos);
|
|
|
|
// The per-user retry (polkit refused, no admin rights) adds --user.
|
|
const std::string userRef = FlatpakManager::installFileCommand(
|
|
FileKind::Ref, "/tmp/org.gnome.Dictionary.flatpakref", /*userScope=*/true);
|
|
assert(userRef.find("--user") != std::string::npos);
|
|
|
|
// A .flatpakrepo registers the remote under the file's name instead.
|
|
const std::string repo = FlatpakManager::installFileCommand(
|
|
FileKind::Repo, "/tmp/flathub.flatpakrepo", /*userScope=*/true);
|
|
assert(repo.find("flatpak remote-add") == 0);
|
|
assert(repo.find("--if-not-exists") != std::string::npos);
|
|
assert(repo.find("flathub ") != std::string::npos);
|
|
assert(repo.find("/tmp/flathub.flatpakrepo") != std::string::npos);
|
|
|
|
// A path with spaces stays a single argument.
|
|
const std::string quoted = FlatpakManager::installFileCommand(
|
|
FileKind::Bundle, "/tmp/mon paquet.flatpak");
|
|
assert(quoted.find("'/tmp/mon paquet.flatpak'") != std::string::npos);
|
|
}
|
|
|
|
int main() {
|
|
testParseSearchOutput_parsesAlignedRows();
|
|
testParseSearchOutput_keepsMultiWordDescriptions();
|
|
testParseSearchOutput_ignoresVersionLikeTokens();
|
|
testParseSearchOutput_emptyInput();
|
|
|
|
testParseInstalledOutput_parsesTabSeparated();
|
|
testParseInstalledOutput_emptyInput();
|
|
|
|
testParseUpdatesOutput_parsesRows();
|
|
testParseUpdatesOutput_emptyInput();
|
|
|
|
testClassifyFile_recognisesTheThreeFlatpakFormats();
|
|
testRepoName_comesFromTheFileName();
|
|
testInstallFileCommand_buildsTheFlatpakCalls();
|
|
|
|
std::cout << "All flatpak-manager tests passed." << std::endl;
|
|
return 0;
|
|
}
|