// Unit tests for ProgressTracker: the single monotonic percentage the UI bars // are fed, built out of the very different ways package managers report their // progress, and for running a command: the escape sequences that come with it, // and the progress milestones a rewritten meter line is turned into. No package // manager, no display and no network is involved: the commands are /bin/sh // one-liners. #include "progress-tracker.h" #include #include #include #include // pacman picks up the download counter of the packages it fetches; the bar must // stay inside the download half and finish it at the last package. static void testDownloadPhaseStaysInsideItsHalf() { ProgressTracker tracker; tracker.feed(" (1/4) téléchargement de a-1.0-1-x86_64..."); const int first = tracker.percent(); assert(first >= 0 && first < 45); // The file being fetched drives the bar inside the package it belongs to. assert(tracker.feed(" a-1.0-1-x86_64 1.2 MiB 640 KiB/s [####] 50%") == 5); tracker.feed(" (4/4) téléchargement de d-1.0-1-x86_64..."); assert(tracker.feed(" d-1.0-1-x86_64 1.2 MiB 640 KiB/s [####] 100%") == 45); } // The install phase starts where the download phase stopped (pacman restarts // its counter at 1, so the raw numbers would send the bar back to zero). static void testApplyPhaseFollowsTheDownloadPhase() { ProgressTracker tracker; tracker.feed(" (1/1) téléchargement de a-1.0-1-x86_64..."); tracker.feed(" a-1.0-1-x86_64 1.2 MiB 640 KiB/s [####] 100%"); assert(tracker.percent() == 45); assert(tracker.feed(" (1/4) installation de a-1.0-1-x86_64...") == 58); assert(tracker.feed(" (2/4) installation de b-1.0-1-x86_64...") == 71); assert(tracker.feed(" (4/4) installation de d-1.0-1-x86_64...") == 97); } // A transaction with nothing to download (everything already cached) starts // near zero instead of pretending the download half happened. static void testApplyOnlyTransactionStartsLow() { ProgressTracker tracker; assert(tracker.feed(" (1/2) installation de a-1.0-1-x86_64...") == 50); assert(tracker.feed(" (2/2) installation de b-1.0-1-x86_64...") == 97); } // Steps whose share of the work is unknown — pacman's key and integrity // checks, its file conflict analysis — hold the bar instead of moving it back. static void testUnknownStepsHoldTheBar() { ProgressTracker tracker; tracker.feed(" (1/4) installation de a-1.0-1-x86_64..."); const int held = tracker.percent(); assert(tracker.feed(" (1/4) vérification des clés du trousseau") == -1); assert(tracker.feed(" (2/4) analyse des conflits entre fichiers") == -1); assert(tracker.percent() == held); } // A counter that goes backwards (a retry, a second command, a new phase) never // lowers the value: this is what used to make the bar bounce left and right. static void testValueNeverMovesBackwards() { ProgressTracker tracker; assert(tracker.feed(" (3/4) installation de c-1.0-1-x86_64...") == 84); assert(tracker.feed(" (1/4) installation de a-1.0-1-x86_64...") == -1); assert(tracker.percent() == 84); } // flatpak and the AUR helpers print "Installing 1/3…" instead of a parenthesised // counter; both must be understood. static void testFlatpakAndHelperCountersAreRead() { ProgressTracker tracker; assert(tracker.feed("Installing 1/3…") == 34); assert(tracker.feed("Installing 2/3…") == 65); assert(tracker.feed("Installing 3/3…") == 97); ProgressTracker english; assert(english.feed(" (1/2) downloading base-1.0-1-x86_64...") == 22); assert(english.feed(" (2/2) upgrading base-1.0-1-x86_64...") == 71); } // A bare "12 %" with no keyword is a download meter (that is the only place // tools print one without naming the step). static void testBarePercentIsDownloadProgress() { ProgressTracker tracker; assert(tracker.feed(" 12% ░░░░░░░░░░") == 5); assert(tracker.feed(" 50% ░░░░░░░░░░") == 22); } // Finalisation and hooks are the last step before completion, which the caller // reports: the tracker itself never claims 100 %. static void testFinalisationClosesInOnTheEnd() { ProgressTracker tracker; assert(tracker.feed(":: Finalisation de la transaction...") == 98); assert(tracker.feed(" (1/1) hook: installation de a...") == 98); assert(tracker.percent() < 100); } // Nothing that only looks like a counter may be read as one. static void testRatiosThatAreNotCountersAreIgnored() { ProgressTracker tracker; assert(tracker.feed(":: Synchronisation des bases de données du paquet…") == -1); assert(tracker.feed(" https://mirror.example.org/arch/extra/os/x86_64/") == -1); assert(tracker.feed("firefox 128.0.2/1.0 nothing to do") == -1); assert(tracker.percent() == 0); } // Repeating the same line (pacman reprints steps as it goes) costs nothing and // produces no UI update. static void testRepeatedLinesReportNoChange() { ProgressTracker tracker; assert(tracker.feed(" (1/2) installation de a-1.0-1-x86_64...") == 50); assert(tracker.feed(" (1/2) installation de a-1.0-1-x86_64...") == -1); assert(tracker.feed("") == -1); } // The escape sequences some tools colour their output with are noise: they // would reach the status bar and the journal as garbage (yay does it, flatpak // does it as soon as it is on a terminal). static void testAnsiEscapesAreStripped() { assert(stripAnsi("\033[32mDownloading\033[0m 50%") == "Downloading 50%"); assert(stripAnsi("\033[2K\033[1GDownloading") == "Downloading"); assert(stripAnsi("plain text") == "plain text"); // An unfinished sequence (the run ended in the middle of one) drops the // fragment rather than printing half an escape code. assert(stripAnsi("done\033[3") == "done"); } // A command whose last line has no newline still hands that line over: reading // until the end of the stream must not drop it. static void testLastUnterminatedLineIsDelivered() { std::string text; runStreamed("printf 'first\\n'; sleep 0.2; printf 'no trailing newline'", [&](const OutputChunk& chunk) { text += chunk.text; }); assert(text == "firstno trailing newline"); } // An in-place meter carries a percentage but no sentence: every 10 % step of it // becomes a line of its own, which is what makes a tool that only rewrites its // progress bar visible in the status bar and recorded in the journal. static void testProgressMeterReportsMilestones() { std::vector messages; std::vector percents; runWithProgress("printf 'Downloading 30%%\\rDownloading 60%%\\rDownloading 95%%\\rdone\\n'", [&](const std::string& message, int percent) { if (!message.empty()) messages.push_back(message); if (percent >= 0) percents.push_back(percent); }); // Three meter steps, one real line, and a milestone line per 10 % step. assert(messages.size() == 4); assert(messages[0] == "done"); assert(messages[1] == "\xE2\x8F\xB3 13 %"); assert(messages[2] == "\xE2\x8F\xB3 27 %"); assert(messages[3] == "\xE2\x8F\xB3 42 %"); // The bar only ever moves forward, whatever the stream mixes. for (std::size_t i = 1; i < percents.size(); ++i) { assert(percents[i] >= percents[i - 1]); } } // A tool that only prints once in a while must not turn the milestone into a // log flood: five updates under the step are one line, not five. static void testMilestonesAreThrottled() { std::vector milestones; std::string command; for (int percent = 1; percent <= 45; percent += 3) { command += "printf 'Downloading " + std::to_string(percent) + "%%\\r'; "; } runWithProgress(command, [&](const std::string& message, int) { if (!message.empty() && message.rfind("\xE2\x8F\xB3", 0) == 0) { milestones.push_back(message); } }); // The meter reaches 20 % of the download half over those updates, so the // 10 % step is crossed twice at most. assert(milestones.size() <= 2); } int main() { testDownloadPhaseStaysInsideItsHalf(); testApplyPhaseFollowsTheDownloadPhase(); testApplyOnlyTransactionStartsLow(); testUnknownStepsHoldTheBar(); testValueNeverMovesBackwards(); testFlatpakAndHelperCountersAreRead(); testBarePercentIsDownloadProgress(); testFinalisationClosesInOnTheEnd(); testRatiosThatAreNotCountersAreIgnored(); testRepeatedLinesReportNoChange(); testAnsiEscapesAreStripped(); testLastUnterminatedLineIsDelivered(); testProgressMeterReportsMilestones(); testMilestonesAreThrottled(); std::cout << "All progress-tracker tests passed." << std::endl; return 0; }