feat(flatpak): report the download flatpak never announces
Installing a .flatpakref looked frozen: flatpak prints two lines and then stays silent for the minutes a runtime takes to come down, so the window repeated the same sentence over a stalled bar and the journal recorded nothing at all. It has no meter to print — measured here, on a real 658 MB install, it draws nothing on a pipe nor on a full terminal, only on the deltas the CDN happens to serve — so the provider now measures the transfer itself: the bytes landing in flatpak's own staging directory, reported as they grow. When even those stop, the window takes over and says which step is running and for how long, in the status bar and in the journal. Along the way the runner reads with read() instead of stdio so in-place updates are not held back by a buffer, strips the ANSI escapes some tools colour their output with (the AUR plugin's own copy is gone), and turns a rewritten meter line into a journal entry every 10 %.
This commit is contained in:
11 files changed
+529
-63
No files matched your search
@@ -1,11 +1,15 @@
|
||||
// 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. No package manager, no display and no network is involved: the
|
||||
// tracker only reads output lines.
|
||||
// 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 <cassert>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// 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.
|
||||
@@ -113,6 +117,73 @@ static void testRepeatedLinesReportNoChange() {
|
||||
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<std::string> messages;
|
||||
std::vector<int> 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<std::string> 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();
|
||||
@@ -124,6 +195,10 @@ int main() {
|
||||
testFinalisationClosesInOnTheEnd();
|
||||
testRatiosThatAreNotCountersAreIgnored();
|
||||
testRepeatedLinesReportNoChange();
|
||||
testAnsiEscapesAreStripped();
|
||||
testLastUnterminatedLineIsDelivered();
|
||||
testProgressMeterReportsMilestones();
|
||||
testMilestonesAreThrottled();
|
||||
|
||||
std::cout << "All progress-tracker tests passed." << std::endl;
|
||||
return 0;
|
||||
|
||||
Reference in new issue
Block a user