From 252192bb4385e09a420feb9e4a8f8b0ea5741da6 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Wed, 19 Jun 2024 22:12:24 -0600 Subject: [PATCH] add a basic libmain test for the progress bar rendering Hooray for leaky abstraction allowing us to test this particular part of the render pipeline. Change-Id: Ie0f251ff874f63324e6a9c6388b84ec6507eeae2 --- src/libmain/progress-bar.hh | 5 +--- tests/unit/libmain/progress-bar.cc | 43 ++++++++++++++++++++++++++++++ tests/unit/meson.build | 22 +++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 tests/unit/libmain/progress-bar.cc diff --git a/src/libmain/progress-bar.hh b/src/libmain/progress-bar.hh index 1ac7ba97b..76e2ed4ff 100644 --- a/src/libmain/progress-bar.hh +++ b/src/libmain/progress-bar.hh @@ -13,9 +13,8 @@ constexpr const auto A_LONG_TIME = std::chrono::duration_cast; @@ -66,8 +65,6 @@ private: bool printBuildLogs = false; bool isTTY; -public: - ProgressBar(bool isTTY) : isTTY(isTTY) { diff --git a/tests/unit/libmain/progress-bar.cc b/tests/unit/libmain/progress-bar.cc new file mode 100644 index 000000000..34a87dd9d --- /dev/null +++ b/tests/unit/libmain/progress-bar.cc @@ -0,0 +1,43 @@ +#include + +#include "eval.hh" +#include "progress-bar.hh" +#include "logging.hh" +#include "shared.hh" + +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(); + initGC(); + + startProgressBar(); + ASSERT_NE(dynamic_cast(logger), nullptr); + ProgressBar & progressBar = *dynamic_cast(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 autoStatus = progressBar.getStatus(*state); + + ASSERT_EQ(autoStatus, EXPECTED); + } +} diff --git a/tests/unit/meson.build b/tests/unit/meson.build index c3eefeede..9f19e5fd9 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -249,3 +249,25 @@ test( suite : 'check', protocol : 'gtest', ) + +libmain_tester = executable( + 'liblixmain-tests', + files('libmain/progress-bar.cc'), + dependencies : [ + liblixmain, + liblixexpr, + liblixutil, + liblixstore, + gtest, + boost, + ], + cpp_pch : cpp_pch, +) + +test( + 'libmain-unit-tests', + libmain_tester, + args : tests_args, + suite : 'check', + protocol : 'gtest', +)