progress.c tests: make start/stop commands on stdin

Change the usage of the "test-tool progress" introduced in 2bb74b53a49 (Test the progress display, 2019-09-16) to take command like "start" and "stop" on stdin, instead of running them implicitly. This makes for tests that are easier to read, since the recipe will mirror the API usage, and allows for easily testing invalid usage that would yield (or should yield) a BUG(), e.g. providing two "start" calls in a row. A subsequent commit will add such tests. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 3, 2022 at 22:40 UTC 791afae2924d032186bfad557c462c92025ac901
2 files changed +70 -32
t/helper/test-progress.c
+33 -11
@@ -3,6 +3,9 @@
3 *
4 * Reads instructions from standard input, one instruction per line:
5 *
6 + * "start <total>[ <title>]" - Call start_progress(title, total),
7 + * Uses the default title of "Working hard"
8 + * if the " <title>" is omitted.
9 * "progress <items>" - Call display_progress() with the given item count
10 * as parameter.
11 * "throughput <bytes> <millis> - Call display_throughput() with the given
@@ -10,6 +13,7 @@
13 * specify the time elapsed since the
14 * start_progress() call.
15 * "update" - Set the 'progress_update' flag.
16 + * "stop" - Call stop_progress().
17 *
18 * See 't0500-progress-display.sh' for examples.
19 */
@@ -19,34 +23,50 @@
23 #include "parse-options.h"
24 #include "progress.h"
25 #include "strbuf.h"
26 +#include "string-list.h"
27
28 int cmd__progress(int argc, const char **argv)
29 {
25 - int total = 0;
26 - const char *title;
30 + const char *const default_title = "Working hard";
31 + struct string_list titles = STRING_LIST_INIT_DUP;
32 struct strbuf line = STRBUF_INIT;
28 - struct progress *progress;
33 + struct progress *progress = NULL;
34
35 const char *usage[] = {
31 - "test-tool progress [--total=<n>] <progress-title>",
36 + "test-tool progress <stdin",
37 NULL
38 };
39 struct option options[] = {
35 - OPT_INTEGER(0, "total", &total, "total number of items"),
40 OPT_END(),
41 };
42
43 argc = parse_options(argc, argv, NULL, options, usage, 0);
40 - if (argc != 1)
41 - die("need a title for the progress output");
42 - title = argv[0];
44 + if (argc)
45 + usage_with_options(usage, options);
46
47 progress_testing = 1;
45 - progress = start_progress(title, total);
48 while (strbuf_getline(&line, stdin) != EOF) {
49 char *end;
50
49 - if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
51 + if (skip_prefix(line.buf, "start ", (const char **) &end)) {
52 + uint64_t total = strtoull(end, &end, 10);
53 + const char *title;
54 +
55 + /*
56 + * We can't use "end + 1" as an argument to
57 + * start_progress(), it doesn't xstrdup() its
58 + * "title" argument. We need to hold onto a
59 + * valid "char *" for it until the end.
60 + */
61 + if (!*end)
62 + title = default_title;
63 + else if (*end == ' ')
64 + title = string_list_insert(&titles, end + 1)->string;
65 + else
66 + die("invalid input: '%s'\n", line.buf);
67 +
68 + progress = start_progress(title, total);
69 + } else if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
70 uint64_t item_count = strtoull(end, &end, 10);
71 if (*end != '\0')
72 die("invalid input: '%s'\n", line.buf);
@@ -65,12 +85,14 @@ int cmd__progress(int argc, const char **argv)
85 display_throughput(progress, byte_count);
86 } else if (!strcmp(line.buf, "update")) {
87 progress_test_force_update();
88 + } else if (!strcmp(line.buf, "stop")) {
89 + stop_progress(&progress);
90 } else {
91 die("invalid input: '%s'\n", line.buf);
92 }
93 }
72 - stop_progress(&progress);
94 strbuf_release(&line);
95 + string_list_clear(&titles, 0);
96
97 return 0;
98 }
t/t0500-progress-display.sh
+37 -21
@@ -18,6 +18,7 @@ test_expect_success 'simple progress display' '
18 EOF
19
20 cat >in <<-\EOF &&
21 + start 0
22 update
23 progress 1
24 update
@@ -26,8 +27,9 @@ test_expect_success 'simple progress display' '
27 progress 4
28 update
29 progress 5
30 + stop
31 EOF
30 - test-tool progress "Working hard" <in 2>stderr &&
32 + test-tool progress <in 2>stderr &&
33
34 show_cr <stderr >out &&
35 test_cmp expect out
@@ -42,11 +44,13 @@ test_expect_success 'progress display with total' '
44 EOF
45
46 cat >in <<-\EOF &&
47 + start 3
48 progress 1
49 progress 2
50 progress 3
51 + stop
52 EOF
49 - test-tool progress --total=3 "Working hard" <in 2>stderr &&
53 + test-tool progress <in 2>stderr &&
54
55 show_cr <stderr >out &&
56 test_cmp expect out
@@ -63,14 +67,14 @@ Working hard.......2.........3.........4.........5.........6:
67 EOF
68
69 cat >in <<-\EOF &&
70 + start 100000 Working hard.......2.........3.........4.........5.........6
71 progress 100
72 progress 1000
73 progress 10000
74 progress 100000
75 + stop
76 EOF
71 - test-tool progress --total=100000 \
72 - "Working hard.......2.........3.........4.........5.........6" \
73 - <in 2>stderr &&
77 + test-tool progress <in 2>stderr &&
78
79 show_cr <stderr >out &&
80 test_cmp expect out
@@ -89,16 +93,16 @@ Working hard.......2.........3.........4.........5.........6:
93 EOF
94
95 cat >in <<-\EOF &&
96 + start 100000 Working hard.......2.........3.........4.........5.........6
97 update
98 progress 1
99 update
100 progress 2
101 progress 10000
102 progress 100000
103 + stop
104 EOF
99 - test-tool progress --total=100000 \
100 - "Working hard.......2.........3.........4.........5.........6" \
101 - <in 2>stderr &&
105 + test-tool progress <in 2>stderr &&
106
107 show_cr <stderr >out &&
108 test_cmp expect out
@@ -117,14 +121,14 @@ Working hard.......2.........3.........4.........5.........6:
121 EOF
122
123 cat >in <<-\EOF &&
124 + start 100000 Working hard.......2.........3.........4.........5.........6
125 progress 25000
126 progress 50000
127 progress 75000
128 progress 100000
129 + stop
130 EOF
125 - test-tool progress --total=100000 \
126 - "Working hard.......2.........3.........4.........5.........6" \
127 - <in 2>stderr &&
131 + test-tool progress <in 2>stderr &&
132
133 show_cr <stderr >out &&
134 test_cmp expect out
@@ -141,14 +145,14 @@ Working hard.......2.........3.........4.........5.........6.........7.........:
145 EOF
146
147 cat >in <<-\EOF &&
148 + start 100000 Working hard.......2.........3.........4.........5.........6.........7.........
149 progress 25000
150 progress 50000
151 progress 75000
152 progress 100000
153 + stop
154 EOF
149 - test-tool progress --total=100000 \
150 - "Working hard.......2.........3.........4.........5.........6.........7........." \
151 - <in 2>stderr &&
155 + test-tool progress <in 2>stderr &&
156
157 show_cr <stderr >out &&
158 test_cmp expect out
@@ -165,12 +169,14 @@ test_expect_success 'progress shortens - crazy caller' '
169 EOF
170
171 cat >in <<-\EOF &&
172 + start 1000
173 progress 100
174 progress 200
175 progress 1
176 progress 1000
177 + stop
178 EOF
173 - test-tool progress --total=1000 "Working hard" <in 2>stderr &&
179 + test-tool progress <in 2>stderr &&
180
181 show_cr <stderr >out &&
182 test_cmp expect out
@@ -186,6 +192,7 @@ test_expect_success 'progress display with throughput' '
192 EOF
193
194 cat >in <<-\EOF &&
195 + start 0
196 throughput 102400 1000
197 update
198 progress 10
@@ -198,8 +205,9 @@ test_expect_success 'progress display with throughput' '
205 throughput 409600 4000
206 update
207 progress 40
208 + stop
209 EOF
202 - test-tool progress "Working hard" <in 2>stderr &&
210 + test-tool progress <in 2>stderr &&
211
212 show_cr <stderr >out &&
213 test_cmp expect out
@@ -215,6 +223,7 @@ test_expect_success 'progress display with throughput and total' '
223 EOF
224
225 cat >in <<-\EOF &&
226 + start 40
227 throughput 102400 1000
228 progress 10
229 throughput 204800 2000
@@ -223,8 +232,9 @@ test_expect_success 'progress display with throughput and total' '
232 progress 30
233 throughput 409600 4000
234 progress 40
235 + stop
236 EOF
227 - test-tool progress --total=40 "Working hard" <in 2>stderr &&
237 + test-tool progress <in 2>stderr &&
238
239 show_cr <stderr >out &&
240 test_cmp expect out
@@ -240,6 +250,7 @@ test_expect_success 'cover up after throughput shortens' '
250 EOF
251
252 cat >in <<-\EOF &&
253 + start 0
254 throughput 409600 1000
255 update
256 progress 1
@@ -252,8 +263,9 @@ test_expect_success 'cover up after throughput shortens' '
263 throughput 1638400 4000
264 update
265 progress 4
266 + stop
267 EOF
256 - test-tool progress "Working hard" <in 2>stderr &&
268 + test-tool progress <in 2>stderr &&
269
270 show_cr <stderr >out &&
271 test_cmp expect out
@@ -268,6 +280,7 @@ test_expect_success 'cover up after throughput shortens a lot' '
280 EOF
281
282 cat >in <<-\EOF &&
283 + start 0
284 throughput 1 1000
285 update
286 progress 1
@@ -277,8 +290,9 @@ test_expect_success 'cover up after throughput shortens a lot' '
290 throughput 3145728 3000
291 update
292 progress 3
293 + stop
294 EOF
281 - test-tool progress "Working hard" <in 2>stderr &&
295 + test-tool progress <in 2>stderr &&
296
297 show_cr <stderr >out &&
298 test_cmp expect out
@@ -286,6 +300,7 @@ test_expect_success 'cover up after throughput shortens a lot' '
300
301 test_expect_success 'progress generates traces' '
302 cat >in <<-\EOF &&
303 + start 40
304 throughput 102400 1000
305 update
306 progress 10
@@ -298,10 +313,11 @@ test_expect_success 'progress generates traces' '
313 throughput 409600 4000
314 update
315 progress 40
316 + stop
317 EOF
318
303 - GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool progress --total=40 \
304 - "Working hard" <in 2>stderr &&
319 + GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool progress \
320 + <in 2>stderr &&
321
322 # t0212/parse_events.perl intentionally omits regions and data.
323 test_region progress "Working hard" trace.event &&