Skip to content

Commit 791afae

Browse files
avargitster
authored andcommitted
progress.c tests: make start/stop commands on stdin
Change the usage of the "test-tool progress" introduced in 2bb74b5 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 587c3d0 commit 791afae

File tree

2 files changed

+70
-32
lines changed

2 files changed

+70
-32
lines changed

t/helper/test-progress.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
*
44
* Reads instructions from standard input, one instruction per line:
55
*
6+
* "start <total>[ <title>]" - Call start_progress(title, total),
7+
* Uses the default title of "Working hard"
8+
* if the " <title>" is omitted.
69
* "progress <items>" - Call display_progress() with the given item count
710
* as parameter.
811
* "throughput <bytes> <millis> - Call display_throughput() with the given
912
* byte count as parameter. The 'millis'
1013
* specify the time elapsed since the
1114
* start_progress() call.
1215
* "update" - Set the 'progress_update' flag.
16+
* "stop" - Call stop_progress().
1317
*
1418
* See 't0500-progress-display.sh' for examples.
1519
*/
@@ -19,34 +23,50 @@
1923
#include "parse-options.h"
2024
#include "progress.h"
2125
#include "strbuf.h"
26+
#include "string-list.h"
2227

2328
int cmd__progress(int argc, const char **argv)
2429
{
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;
2732
struct strbuf line = STRBUF_INIT;
28-
struct progress *progress;
33+
struct progress *progress = NULL;
2934

3035
const char *usage[] = {
31-
"test-tool progress [--total=<n>] <progress-title>",
36+
"test-tool progress <stdin",
3237
NULL
3338
};
3439
struct option options[] = {
35-
OPT_INTEGER(0, "total", &total, "total number of items"),
3640
OPT_END(),
3741
};
3842

3943
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);
4346

4447
progress_testing = 1;
45-
progress = start_progress(title, total);
4648
while (strbuf_getline(&line, stdin) != EOF) {
4749
char *end;
4850

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)) {
5070
uint64_t item_count = strtoull(end, &end, 10);
5171
if (*end != '\0')
5272
die("invalid input: '%s'\n", line.buf);
@@ -65,12 +85,14 @@ int cmd__progress(int argc, const char **argv)
6585
display_throughput(progress, byte_count);
6686
} else if (!strcmp(line.buf, "update")) {
6787
progress_test_force_update();
88+
} else if (!strcmp(line.buf, "stop")) {
89+
stop_progress(&progress);
6890
} else {
6991
die("invalid input: '%s'\n", line.buf);
7092
}
7193
}
72-
stop_progress(&progress);
7394
strbuf_release(&line);
95+
string_list_clear(&titles, 0);
7496

7597
return 0;
7698
}

t/t0500-progress-display.sh

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ test_expect_success 'simple progress display' '
1818
EOF
1919
2020
cat >in <<-\EOF &&
21+
start 0
2122
update
2223
progress 1
2324
update
@@ -26,8 +27,9 @@ test_expect_success 'simple progress display' '
2627
progress 4
2728
update
2829
progress 5
30+
stop
2931
EOF
30-
test-tool progress "Working hard" <in 2>stderr &&
32+
test-tool progress <in 2>stderr &&
3133
3234
show_cr <stderr >out &&
3335
test_cmp expect out
@@ -42,11 +44,13 @@ test_expect_success 'progress display with total' '
4244
EOF
4345
4446
cat >in <<-\EOF &&
47+
start 3
4548
progress 1
4649
progress 2
4750
progress 3
51+
stop
4852
EOF
49-
test-tool progress --total=3 "Working hard" <in 2>stderr &&
53+
test-tool progress <in 2>stderr &&
5054
5155
show_cr <stderr >out &&
5256
test_cmp expect out
@@ -63,14 +67,14 @@ Working hard.......2.........3.........4.........5.........6:
6367
EOF
6468
6569
cat >in <<-\EOF &&
70+
start 100000 Working hard.......2.........3.........4.........5.........6
6671
progress 100
6772
progress 1000
6873
progress 10000
6974
progress 100000
75+
stop
7076
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 &&
7478
7579
show_cr <stderr >out &&
7680
test_cmp expect out
@@ -89,16 +93,16 @@ Working hard.......2.........3.........4.........5.........6:
8993
EOF
9094
9195
cat >in <<-\EOF &&
96+
start 100000 Working hard.......2.........3.........4.........5.........6
9297
update
9398
progress 1
9499
update
95100
progress 2
96101
progress 10000
97102
progress 100000
103+
stop
98104
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 &&
102106
103107
show_cr <stderr >out &&
104108
test_cmp expect out
@@ -117,14 +121,14 @@ Working hard.......2.........3.........4.........5.........6:
117121
EOF
118122
119123
cat >in <<-\EOF &&
124+
start 100000 Working hard.......2.........3.........4.........5.........6
120125
progress 25000
121126
progress 50000
122127
progress 75000
123128
progress 100000
129+
stop
124130
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 &&
128132
129133
show_cr <stderr >out &&
130134
test_cmp expect out
@@ -141,14 +145,14 @@ Working hard.......2.........3.........4.........5.........6.........7.........:
141145
EOF
142146
143147
cat >in <<-\EOF &&
148+
start 100000 Working hard.......2.........3.........4.........5.........6.........7.........
144149
progress 25000
145150
progress 50000
146151
progress 75000
147152
progress 100000
153+
stop
148154
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 &&
152156
153157
show_cr <stderr >out &&
154158
test_cmp expect out
@@ -165,12 +169,14 @@ test_expect_success 'progress shortens - crazy caller' '
165169
EOF
166170
167171
cat >in <<-\EOF &&
172+
start 1000
168173
progress 100
169174
progress 200
170175
progress 1
171176
progress 1000
177+
stop
172178
EOF
173-
test-tool progress --total=1000 "Working hard" <in 2>stderr &&
179+
test-tool progress <in 2>stderr &&
174180
175181
show_cr <stderr >out &&
176182
test_cmp expect out
@@ -186,6 +192,7 @@ test_expect_success 'progress display with throughput' '
186192
EOF
187193
188194
cat >in <<-\EOF &&
195+
start 0
189196
throughput 102400 1000
190197
update
191198
progress 10
@@ -198,8 +205,9 @@ test_expect_success 'progress display with throughput' '
198205
throughput 409600 4000
199206
update
200207
progress 40
208+
stop
201209
EOF
202-
test-tool progress "Working hard" <in 2>stderr &&
210+
test-tool progress <in 2>stderr &&
203211
204212
show_cr <stderr >out &&
205213
test_cmp expect out
@@ -215,6 +223,7 @@ test_expect_success 'progress display with throughput and total' '
215223
EOF
216224
217225
cat >in <<-\EOF &&
226+
start 40
218227
throughput 102400 1000
219228
progress 10
220229
throughput 204800 2000
@@ -223,8 +232,9 @@ test_expect_success 'progress display with throughput and total' '
223232
progress 30
224233
throughput 409600 4000
225234
progress 40
235+
stop
226236
EOF
227-
test-tool progress --total=40 "Working hard" <in 2>stderr &&
237+
test-tool progress <in 2>stderr &&
228238
229239
show_cr <stderr >out &&
230240
test_cmp expect out
@@ -240,6 +250,7 @@ test_expect_success 'cover up after throughput shortens' '
240250
EOF
241251
242252
cat >in <<-\EOF &&
253+
start 0
243254
throughput 409600 1000
244255
update
245256
progress 1
@@ -252,8 +263,9 @@ test_expect_success 'cover up after throughput shortens' '
252263
throughput 1638400 4000
253264
update
254265
progress 4
266+
stop
255267
EOF
256-
test-tool progress "Working hard" <in 2>stderr &&
268+
test-tool progress <in 2>stderr &&
257269
258270
show_cr <stderr >out &&
259271
test_cmp expect out
@@ -268,6 +280,7 @@ test_expect_success 'cover up after throughput shortens a lot' '
268280
EOF
269281
270282
cat >in <<-\EOF &&
283+
start 0
271284
throughput 1 1000
272285
update
273286
progress 1
@@ -277,15 +290,17 @@ test_expect_success 'cover up after throughput shortens a lot' '
277290
throughput 3145728 3000
278291
update
279292
progress 3
293+
stop
280294
EOF
281-
test-tool progress "Working hard" <in 2>stderr &&
295+
test-tool progress <in 2>stderr &&
282296
283297
show_cr <stderr >out &&
284298
test_cmp expect out
285299
'
286300

287301
test_expect_success 'progress generates traces' '
288302
cat >in <<-\EOF &&
303+
start 40
289304
throughput 102400 1000
290305
update
291306
progress 10
@@ -298,10 +313,11 @@ test_expect_success 'progress generates traces' '
298313
throughput 409600 4000
299314
update
300315
progress 40
316+
stop
301317
EOF
302318
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 &&
305321
306322
# t0212/parse_events.perl intentionally omits regions and data.
307323
test_region progress "Working hard" trace.event &&

0 commit comments

Comments
 (0)