2024-06-20 04:12:24 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2024-11-12 23:07:17 +00:00
|
|
|
#include "lix/libexpr/eval.hh"
|
|
|
|
#include "lix/libmain/progress-bar.hh"
|
|
|
|
#include "lix/libmain/loggers.hh"
|
|
|
|
#include "lix/libutil/logging.hh"
|
|
|
|
#include "lix/libmain/shared.hh"
|
2024-06-20 04:12:24 +00:00
|
|
|
|
|
|
|
constexpr std::string_view TEST_URL = "https://github.com/NixOS/nixpkgs/archive/master.tar.gz";
|
|
|
|
// Arbitrary number. We picked the size of a Nixpkgs tarball that we downloaded.
|
|
|
|
constexpr uint64_t TEST_EXPECTED = 43'370'307;
|
|
|
|
// Arbitrary number. We picked the progress made on a Nixpkgs tarball download we interrupted.
|
|
|
|
constexpr uint64_t TEST_DONE = 1'787'251;
|
|
|
|
|
|
|
|
constexpr std::string_view EXPECTED = ANSI_GREEN "1.7" ANSI_NORMAL "/41.4 MiB DL";
|
|
|
|
// Mostly here for informational purposes, but also if we change the way the escape codes
|
|
|
|
// are defined this test might break in some annoying to debug way.
|
|
|
|
constexpr std::string_view EXPECTED_RAW = "\x1b[32;1m1.7\x1b[0m/41.4 MiB DL";
|
|
|
|
static_assert(EXPECTED == EXPECTED_RAW, "Hey, hey, the ANSI escape code definitions prolly changed");
|
|
|
|
|
|
|
|
namespace nix
|
|
|
|
{
|
|
|
|
TEST(ProgressBar, basicStatusRender) {
|
|
|
|
initNix();
|
2024-11-15 15:15:11 +00:00
|
|
|
initLibExpr();
|
2024-06-20 04:12:24 +00:00
|
|
|
|
tree-wide: unify progress bar inactive and paused states
Previously, the progress bar had two subtly different states in which the bar
would not actually render, both with their own shortcomings: inactive (which
was irreversible) and paused (reversible, but swallowing logs). Furthermore,
there was no way of resetting the statistics, so a very bad solution was
implemented (243c0f18dae2a08ea0e46f7ff33277c63f7506d7) that would create a new
logger for each line of the repl, leaking the previous one and discarding the
value of printBuildLogs. Finally, if stderr was not attached to a TTY, the
update thread was started even though the logger was not active, violating the
invariant required by the destructor (which is not observed because the logger
is leaked).
In this commit, the two aforementioned states are unified into a single one,
which can be exited again, correctly upholds the invariant that the update
thread is only running while the progress bar is active, and does not swallow
logs. The latter change in behavior is not expected to be a problems in the
rare cases where the paused state was used before, since other loggers (like
the simple one) don't exhibit it anyway. The startProgressBar/stopProgressBar
API is removed due to being a footgun, and a new method for properly resetting
the progress is added.
Co-Authored-By: Qyriad <qyriad@qyriad.me>
Change-Id: I2b7c3eb17d439cd0c16f7b896cfb61239ac7ff3a
2024-06-29 13:03:44 +00:00
|
|
|
setLogFormat(LogFormat::bar);
|
2024-06-20 04:12:24 +00:00
|
|
|
ASSERT_NE(dynamic_cast<ProgressBar *>(logger), nullptr);
|
|
|
|
ProgressBar & progressBar = dynamic_cast<ProgressBar &>(*logger);
|
|
|
|
|
|
|
|
Activity act(
|
|
|
|
progressBar,
|
|
|
|
lvlDebug,
|
|
|
|
actFileTransfer,
|
|
|
|
fmt("downloading '%s'", TEST_URL),
|
|
|
|
{ "https://github.com/NixOS/nixpkgs/archive/master.tar.gz" }
|
|
|
|
);
|
|
|
|
act.progress(TEST_DONE, TEST_EXPECTED);
|
|
|
|
auto state = progressBar.state_.lock();
|
|
|
|
std::string const renderedStatus = progressBar.getStatus(*state);
|
|
|
|
|
|
|
|
ASSERT_EQ(renderedStatus, EXPECTED);
|
|
|
|
}
|
|
|
|
}
|