-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_progress_render.cpp
More file actions
105 lines (95 loc) · 3.99 KB
/
Copy pathtest_progress_render.cpp
File metadata and controls
105 lines (95 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <gtest/gtest.h>
import std;
import mcpp.ui;
import mcpp.fetcher.progress;
import mcpp.platform.process;
// W11: one renderer for every acquisition, with a terminal mode (redrawn in
// place) and a plain mode; either prints one line per item (#734).
TEST(GitProgress, ParsesThePhaseAndThePercentage) {
auto r = mcpp::fetcher::parse_git_progress(
"Receiving objects: 45% (450/1000), 1.20 MiB | 800.00 KiB/s");
ASSERT_TRUE(r.has_value());
EXPECT_EQ(r->phase, "Receiving objects");
EXPECT_EQ(r->percent, 45u);
auto remote = mcpp::fetcher::parse_git_progress("remote: Counting objects: 100% (5/5), done.");
ASSERT_TRUE(remote.has_value());
EXPECT_EQ(remote->phase, "Counting objects");
EXPECT_EQ(remote->percent, 100u);
EXPECT_FALSE(mcpp::fetcher::parse_git_progress("Cloning into 'x'...").has_value());
EXPECT_FALSE(mcpp::fetcher::parse_git_progress("fatal: repository not found").has_value());
EXPECT_FALSE(mcpp::fetcher::parse_git_progress("").has_value());
}
TEST(ProgressBarPlain, OneCompletionLineWithoutRepaints) {
mcpp::ui::disable_color();
mcpp::ui::set_live_progress(false);
testing::internal::CaptureStdout();
{
mcpp::ui::ProgressBar bar("Fetching", "example");
bar.update_bytes(10, 100, 0.1);
bar.update_bytes(50, 100, 0.5);
bar.update_bytes(100, 100, 1.0);
bar.finish();
}
auto out = testing::internal::GetCapturedStdout();
EXPECT_EQ(out.find('\r'), std::string::npos) << out;
EXPECT_EQ(out.find('\x1b'), std::string::npos) << out;
// #734: one line per item, stating the size and the time.
EXPECT_EQ(std::ranges::count(out, '\n'), 1) << out;
EXPECT_NE(out.find("Fetching example done, 100 B in"), std::string::npos) << out;
}
TEST(ProgressBarPlain, AFailedItemIsNotReportedDone) {
mcpp::ui::disable_color();
mcpp::ui::set_live_progress(false);
testing::internal::CaptureStdout();
{
mcpp::ui::ProgressBar bar("Installing", "xim:example");
bar.update_indeterminate(0, 0.2);
bar.finish_failed("xim:example");
}
auto out = testing::internal::GetCapturedStdout();
EXPECT_NE(out.find("did not complete"), std::string::npos) << out;
EXPECT_EQ(out.find(" done"), std::string::npos) << out;
}
TEST(DownloadProgressPlain, AnInterruptedDownloadIsNotReportedDone) {
mcpp::ui::disable_color();
mcpp::ui::set_live_progress(false);
testing::internal::CaptureStdout();
{
mcpp::ui::DownloadProgress dl;
const mcpp::ui::DownloadFile f{"xim-index.tar.gz", 10, 100, true, false};
dl.update(std::span{&f, 1}, 0.2);
dl.finish_failed();
}
auto out = testing::internal::GetCapturedStdout();
EXPECT_NE(out.find("xim-index.tar.gz did not complete"), std::string::npos) << out;
EXPECT_EQ(out.find(" done"), std::string::npos) << out;
}
TEST(ProgressBarLive, RedrawsInPlace) {
mcpp::ui::disable_color();
mcpp::ui::set_live_progress(true);
testing::internal::CaptureStdout();
{
mcpp::ui::ProgressBar bar("Fetching", "example");
bar.update(100);
bar.finish();
}
auto out = testing::internal::GetCapturedStdout();
mcpp::ui::set_live_progress(false);
EXPECT_NE(out.find('\r'), std::string::npos) << out;
}
#if !defined(_WIN32)
TEST(StreamingBounded, ACarriageReturnEndsALineOnlyWhenAsked) {
const auto cmd = std::string("printf 'a\\rb\\r\\nc\\n'");
std::vector<std::string> plain, split;
bool timedOut = false;
mcpp::platform::process::run_streaming_bounded(cmd,
[&](std::string_view l) { plain.emplace_back(l); },
std::chrono::milliseconds{0}, std::chrono::seconds{30}, &timedOut);
mcpp::platform::process::run_streaming_bounded(cmd,
[&](std::string_view l) { split.emplace_back(l); },
std::chrono::milliseconds{0}, std::chrono::seconds{30}, &timedOut,
/*split_on_cr=*/true);
EXPECT_EQ(plain, (std::vector<std::string>{"a\rb", "c"}));
EXPECT_EQ(split, (std::vector<std::string>{"a", "b", "c"}));
}
#endif