]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-progress.c
git-push.txt: fix grammar
[thirdparty/git.git] / t / helper / test-progress.c
1 /*
2 * A test helper to exercise the progress display.
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
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.
16 * "stop" - Call stop_progress().
17 *
18 * See 't0500-progress-display.sh' for examples.
19 */
20 #define GIT_TEST_PROGRESS_ONLY
21 #include "test-tool.h"
22 #include "gettext.h"
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 {
30 const char *const default_title = "Working hard";
31 struct string_list titles = STRING_LIST_INIT_DUP;
32 struct strbuf line = STRBUF_INIT;
33 struct progress *progress = NULL;
34
35 const char *usage[] = {
36 "test-tool progress <stdin",
37 NULL
38 };
39 struct option options[] = {
40 OPT_END(),
41 };
42
43 argc = parse_options(argc, argv, NULL, options, usage, 0);
44 if (argc)
45 usage_with_options(usage, options);
46
47 progress_testing = 1;
48 while (strbuf_getline(&line, stdin) != EOF) {
49 char *end;
50
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);
73 display_progress(progress, item_count);
74 } else if (skip_prefix(line.buf, "throughput ",
75 (const char **) &end)) {
76 uint64_t byte_count, test_ms;
77
78 byte_count = strtoull(end, &end, 10);
79 if (*end != ' ')
80 die("invalid input: '%s'\n", line.buf);
81 test_ms = strtoull(end + 1, &end, 10);
82 if (*end != '\0')
83 die("invalid input: '%s'\n", line.buf);
84 progress_test_ns = test_ms * 1000 * 1000;
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 }
94 strbuf_release(&line);
95 string_list_clear(&titles, 0);
96
97 return 0;
98 }