]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-progress.c
Merge branch 'jc/unleak-core-excludesfile'
[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 "parse-options.h"
23 #include "progress.h"
24 #include "strbuf.h"
25 #include "string-list.h"
26
27 int cmd__progress(int argc, const char **argv)
28 {
29 const char *const default_title = "Working hard";
30 struct string_list titles = STRING_LIST_INIT_DUP;
31 struct strbuf line = STRBUF_INIT;
32 struct progress *progress = NULL;
33
34 const char *usage[] = {
35 "test-tool progress <stdin",
36 NULL
37 };
38 struct option options[] = {
39 OPT_END(),
40 };
41
42 argc = parse_options(argc, argv, NULL, options, usage, 0);
43 if (argc)
44 usage_with_options(usage, options);
45
46 progress_testing = 1;
47 while (strbuf_getline(&line, stdin) != EOF) {
48 char *end;
49
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)) {
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);
85 } else if (!strcmp(line.buf, "update")) {
86 progress_test_force_update();
87 } else if (!strcmp(line.buf, "stop")) {
88 stop_progress(&progress);
89 } else {
90 die("invalid input: '%s'\n", line.buf);
91 }
92 }
93 strbuf_release(&line);
94 string_list_clear(&titles, 0);
95
96 return 0;
97 }