Merge branch 'en/dir-traversal'
[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 #define GIT_TEST_PROGRESS_ONLY
17 #include "test-tool.h"
18 #include "gettext.h"
19 #include "parse-options.h"
20 #include "progress.h"
21 #include "strbuf.h"
22
23 int cmd__progress(int argc, const char **argv)
24 {
25         int total = 0;
26         const char *title;
27         struct strbuf line = STRBUF_INIT;
28         struct progress *progress;
29
30         const char *usage[] = {
31                 "test-tool progress [--total=<n>] <progress-title>",
32                 NULL
33         };
34         struct option options[] = {
35                 OPT_INTEGER(0, "total", &total, "total number of items"),
36                 OPT_END(),
37         };
38
39         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];
43
44         progress_testing = 1;
45         progress = start_progress(title, total);
46         while (strbuf_getline(&line, stdin) != EOF) {
47                 char *end;
48
49                 if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
50                         uint64_t item_count = strtoull(end, &end, 10);
51                         if (*end != '\0')
52                                 die("invalid input: '%s'\n", line.buf);
53                         display_progress(progress, item_count);
54                 } else if (skip_prefix(line.buf, "throughput ",
55                                        (const char **) &end)) {
56                         uint64_t byte_count, test_ms;
57
58                         byte_count = strtoull(end, &end, 10);
59                         if (*end != ' ')
60                                 die("invalid input: '%s'\n", line.buf);
61                         test_ms = strtoull(end + 1, &end, 10);
62                         if (*end != '\0')
63                                 die("invalid input: '%s'\n", line.buf);
64                         progress_test_ns = test_ms * 1000 * 1000;
65                         display_throughput(progress, byte_count);
66                 } else if (!strcmp(line.buf, "update"))
67                         progress_test_force_update();
68                 else
69                         die("invalid input: '%s'\n", line.buf);
70         }
71         stop_progress(&progress);
72
73         return 0;
74 }