]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-progress.c
Merge branch 'ds/remove-idx-before-pack'
[thirdparty/git.git] / t / helper / test-progress.c
CommitLineData
2bb74b53
SG
1/*
2 * A test helper to exercise the progress display.
3 *
4 * Reads instructions from standard input, one instruction per line:
5 *
791afae2
ÆAB
6 * "start <total>[ <title>]" - Call start_progress(title, total),
7 * Uses the default title of "Working hard"
8 * if the " <title>" is omitted.
2bb74b53
SG
9 * "progress <items>" - Call display_progress() with the given item count
10 * as parameter.
11 * "throughput <bytes> <millis> - Call display_throughput() with the given
12 * byte count as parameter. The 'millis'
13 * specify the time elapsed since the
14 * start_progress() call.
15 * "update" - Set the 'progress_update' flag.
791afae2 16 * "stop" - Call stop_progress().
2bb74b53
SG
17 *
18 * See 't0500-progress-display.sh' for examples.
19 */
3cacb9aa 20#define GIT_TEST_PROGRESS_ONLY
2bb74b53 21#include "test-tool.h"
2bb74b53
SG
22#include "parse-options.h"
23#include "progress.h"
24#include "strbuf.h"
791afae2 25#include "string-list.h"
2bb74b53 26
2bb74b53
SG
27int cmd__progress(int argc, const char **argv)
28{
791afae2
ÆAB
29 const char *const default_title = "Working hard";
30 struct string_list titles = STRING_LIST_INIT_DUP;
2bb74b53 31 struct strbuf line = STRBUF_INIT;
791afae2 32 struct progress *progress = NULL;
2bb74b53
SG
33
34 const char *usage[] = {
791afae2 35 "test-tool progress <stdin",
2bb74b53
SG
36 NULL
37 };
38 struct option options[] = {
2bb74b53
SG
39 OPT_END(),
40 };
41
42 argc = parse_options(argc, argv, NULL, options, usage, 0);
791afae2
ÆAB
43 if (argc)
44 usage_with_options(usage, options);
2bb74b53
SG
45
46 progress_testing = 1;
2bb74b53
SG
47 while (strbuf_getline(&line, stdin) != EOF) {
48 char *end;
49
791afae2
ÆAB
50 if (skip_prefix(line.buf, "start ", (const char **) &end)) {
51 uint64_t total = strtoull(end, &end, 10);
52 const char *title;
53
54 /*
55 * We can't use "end + 1" as an argument to
56 * start_progress(), it doesn't xstrdup() its
57 * "title" argument. We need to hold onto a
58 * valid "char *" for it until the end.
59 */
60 if (!*end)
61 title = default_title;
62 else if (*end == ' ')
63 title = string_list_insert(&titles, end + 1)->string;
64 else
65 die("invalid input: '%s'\n", line.buf);
66
67 progress = start_progress(title, total);
68 } else if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
2bb74b53
SG
69 uint64_t item_count = strtoull(end, &end, 10);
70 if (*end != '\0')
71 die("invalid input: '%s'\n", line.buf);
72 display_progress(progress, item_count);
73 } else if (skip_prefix(line.buf, "throughput ",
74 (const char **) &end)) {
75 uint64_t byte_count, test_ms;
76
77 byte_count = strtoull(end, &end, 10);
78 if (*end != ' ')
79 die("invalid input: '%s'\n", line.buf);
80 test_ms = strtoull(end + 1, &end, 10);
81 if (*end != '\0')
82 die("invalid input: '%s'\n", line.buf);
83 progress_test_ns = test_ms * 1000 * 1000;
84 display_throughput(progress, byte_count);
587c3d0d 85 } else if (!strcmp(line.buf, "update")) {
2bb74b53 86 progress_test_force_update();
791afae2
ÆAB
87 } else if (!strcmp(line.buf, "stop")) {
88 stop_progress(&progress);
587c3d0d 89 } else {
2bb74b53 90 die("invalid input: '%s'\n", line.buf);
587c3d0d 91 }
2bb74b53 92 }
8266e0c0 93 strbuf_release(&line);
791afae2 94 string_list_clear(&titles, 0);
2bb74b53
SG
95
96 return 0;
97}