Files
FWS-PacManage/tests/cli-command.test.cpp
T
BlackAngelTV 3e4ed92c0a feat(cli): add a simplified command line over the logic layer
The window needs a display, which kept the package manager out of a script and
off a headless machine. The command line runs the same operations through
AppCore, before any widget exists, so providers, the pacman lock, the journal
and the history behave exactly as in the window.

Refs #22.
2026-10-03 22:53:47 +02:00

141 lines
5.5 KiB
C++

// Unit tests for the command-line grammar (cli/cli-command.*): which arguments
// ask for the command line rather than the window, and what each keyword parses
// into. Pure parsing — no package manager, no display, nothing spawned.
#include <cassert>
#include <iostream>
#include <QCoreApplication>
#include <QString>
#include <QStringList>
#include "cli-command.h"
static QStringList args(std::initializer_list<const char*> words) {
QStringList list;
for (const char* word : words) list << QString::fromUtf8(word);
return list;
}
static void testInvocationRouting() {
// No arguments, or a file the desktop handed over: the window is wanted.
assert(!cli::isCliInvocation({}));
assert(!cli::isCliInvocation(args({"/home/user/app.flatpak"})));
assert(!cli::isCliInvocation(args({"app.flatpakref"})));
assert(!cli::isCliInvocation(args({"./bundle.fwspacm"})));
// A keyword, or any leading option, is aimed at the command line.
assert(cli::isCliInvocation(args({"recherche", "vlc"})));
assert(cli::isCliInvocation(args({"install", "vlc"})));
assert(cli::isCliInvocation(args({"search", "vlc"})));
assert(cli::isCliInvocation(args({"--help"})));
assert(cli::isCliInvocation(args({"-y", "install", "vlc"})));
// A bare word that is not a path is a mistyped command, not a file: the
// command line reports it instead of opening a window that cannot use it.
assert(cli::isCliInvocation(args({"frobnicate"})));
}
static void testSearchParsing() {
const cli::Parsed parsed = cli::parse(args({"recherche", "vlc"}));
assert(parsed.command == cli::Command::Search);
assert(parsed.operands == args({"vlc"}));
// The search word may hold spaces: the term is everything that follows.
const cli::Parsed multi = cli::parse(args({"search", "gtk", "theme"}));
assert(multi.command == cli::Command::Search);
assert(multi.operands.join(QLatin1Char(' ')) == QLatin1String("gtk theme"));
// The keyword is not a search term on its own.
const cli::Parsed missing = cli::parse(args({"recherche"}));
assert(missing.command == cli::Command::Unknown);
assert(!missing.error.isEmpty());
}
static void testInstallAndRemoveParsing() {
const cli::Parsed install = cli::parse(args({"install", "vlc"}));
assert(install.command == cli::Command::Install);
assert(install.operands == args({"vlc"}));
const cli::Parsed several = cli::parse(args({"installer", "vlc", "vim"}));
assert(several.command == cli::Command::Install);
assert(several.operands == args({"vlc", "vim"}));
const cli::Parsed remove = cli::parse(args({"supprimer", "-y", "vlc"}));
assert(remove.command == cli::Command::Remove);
assert(remove.assumeYes);
assert(remove.operands == args({"vlc"}));
assert(cli::parse(args({"remove"})).command == cli::Command::Unknown);
}
static void testUpdateParsing() {
// No operand means everything; one operand means that package.
const cli::Parsed all = cli::parse(args({"maj"}));
assert(all.command == cli::Command::Update);
assert(all.operands.isEmpty());
const cli::Parsed one = cli::parse(args({"update", "vlc"}));
assert(one.command == cli::Command::Update);
assert(one.operands == args({"vlc"}));
assert(cli::parse(args({"maj", "vlc", "vim"})).command == cli::Command::Unknown);
}
static void testReadOnlyCommands() {
assert(cli::parse(args({"liste"})).command == cli::Command::List);
assert(cli::parse(args({"list"})).command == cli::Command::List);
assert(cli::parse(args({"maj-dispo"})).command == cli::Command::Updates);
assert(cli::parse(args({"cleanup"})).command == cli::Command::Cleanup);
assert(cli::parse(args({"refresh"})).command == cli::Command::Refresh);
const cli::Parsed info = cli::parse(args({"info", "vlc"}));
assert(info.command == cli::Command::Info);
assert(info.operands == args({"vlc"}));
assert(cli::parse(args({"info"})).command == cli::Command::Unknown);
// A listing takes no operand.
assert(cli::parse(args({"liste", "vlc"})).command == cli::Command::Unknown);
}
static void testHelpAndVersion() {
assert(cli::parse(args({"--help"})).command == cli::Command::Help);
assert(cli::parse(args({"aide"})).command == cli::Command::Help);
// An option wins wherever it stands.
assert(cli::parse(args({"install", "--help"})).command == cli::Command::Help);
assert(cli::parse(args({"--version"})).command == cli::Command::Version);
assert(cli::parse(args({"-v"})).command == cli::Command::Version);
// The help names the keywords the parser accepts.
assert(cli::helpText().contains(QLatin1String("recherche")));
assert(cli::helpText().contains(QLatin1String("install")));
}
static void testUnknownCommands() {
const cli::Parsed word = cli::parse(args({"frobnicate"}));
assert(word.command == cli::Command::Unknown);
assert(word.error.contains(QLatin1String("frobnicate")));
const cli::Parsed option = cli::parse(args({"--nope"}));
assert(option.command == cli::Command::Unknown);
assert(option.error.contains(QLatin1String("--nope")));
// No argument at all is not an error: it is the window.
assert(cli::parse({}).command == cli::Command::None);
}
int main(int argc, char* argv[]) {
QCoreApplication app(argc, argv);
testInvocationRouting();
testSearchParsing();
testInstallAndRemoveParsing();
testUpdateParsing();
testReadOnlyCommands();
testHelpAndVersion();
testUnknownCommands();
std::cout << "All cli-command tests passed" << std::endl;
return 0;
}