Merge branch 'eb/mboxrd-doc'
[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  *   "progress <items>" - Call display_progress() with the given item count
7  *                        as parameter.
8  *   "throughput <bytes> <millis> - Call display_throughput() with the given
9  *                                  byte count as parameter.  The 'millis'
10  *                                  specify the time elapsed since the
11  *                                  start_progress() call.
12  *   "update" - Set the 'progress_update' flag.
13  *
14  * See 't0500-progress-display.sh' for examples.
15  */
16 #include "test-tool.h"
17 #include "gettext.h"
18 #include "parse-options.h"
19 #include "progress.h"
20 #include "strbuf.h"
21
22 /*
23  * These are defined in 'progress.c', but are not exposed in 'progress.h',
24  * because they are exclusively for testing.
25  */
26 extern int progress_testing;
27 extern uint64_t progress_test_ns;
28 void progress_test_force_update(void);
29
30 int cmd__progress(int argc, const char **argv)
31 {
32         int total = 0;
33         const char *title;
34         struct strbuf line = STRBUF_INIT;
35         struct progress *progress;
36
37         const char *usage[] = {
38                 "test-tool progress [--total=<n>] <progress-title>",
39                 NULL
40         };
41         struct option options[] = {
42                 OPT_INTEGER(0, "total", &total, "total number of items"),
43                 OPT_END(),
44         };
45
46         argc = parse_options(argc, argv, NULL, options, usage, 0);
47         if (argc != 1)
48                 die("need a title for the progress output");
49         title = argv[0];
50
51         progress_testing = 1;
52         progress = start_progress(title, total);
53         while (strbuf_getline(&line, stdin) != EOF) {
54                 char *end;
55
56                 if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
57                         uint64_t item_count = strtoull(end, &end, 10);
58                         if (*end != '\0')
59                                 die("invalid input: '%s'\n", line.buf);
60                         display_progress(progress, item_count);
61                 } else if (skip_prefix(line.buf, "throughput ",
62                                        (const char **) &end)) {
63                         uint64_t byte_count, test_ms;
64
65                         byte_count = strtoull(end, &end, 10);
66                         if (*end != ' ')
67                                 die("invalid input: '%s'\n", line.buf);
68                         test_ms = strtoull(end + 1, &end, 10);
69                         if (*end != '\0')
70                                 die("invalid input: '%s'\n", line.buf);
71                         progress_test_ns = test_ms * 1000 * 1000;
72                         display_throughput(progress, byte_count);
73                 } else if (!strcmp(line.buf, "update"))
74                         progress_test_force_update();
75                 else
76                         die("invalid input: '%s'\n", line.buf);
77         }
78         stop_progress(&progress);
79
80         return 0;
81 }