wrapper: add a helper to generate numbers from a CSPRNG
[git.git] / t / helper / test-tool.c
blobe6ec69cf326a3babac7bac7df9a03026b0c16d5a
1 #include "git-compat-util.h"
2 #include "test-tool.h"
3 #include "trace2.h"
4 #include "parse-options.h"
6 static const char * const test_tool_usage[] = {
7 "test-tool [-C <directory>] <command [<arguments>...]]",
8 NULL
9 };
11 struct test_cmd {
12 const char *name;
13 int (*fn)(int argc, const char **argv);
16 static struct test_cmd cmds[] = {
17 { "advise", cmd__advise_if_enabled },
18 { "bitmap", cmd__bitmap },
19 { "bloom", cmd__bloom },
20 { "chmtime", cmd__chmtime },
21 { "config", cmd__config },
22 { "crontab", cmd__crontab },
23 { "csprng", cmd__csprng },
24 { "ctype", cmd__ctype },
25 { "date", cmd__date },
26 { "delta", cmd__delta },
27 { "dir-iterator", cmd__dir_iterator },
28 { "drop-caches", cmd__drop_caches },
29 { "dump-cache-tree", cmd__dump_cache_tree },
30 { "dump-fsmonitor", cmd__dump_fsmonitor },
31 { "dump-split-index", cmd__dump_split_index },
32 { "dump-untracked-cache", cmd__dump_untracked_cache },
33 { "example-decorate", cmd__example_decorate },
34 { "fast-rebase", cmd__fast_rebase },
35 { "genrandom", cmd__genrandom },
36 { "genzeros", cmd__genzeros },
37 { "getcwd", cmd__getcwd },
38 { "hashmap", cmd__hashmap },
39 { "hash-speed", cmd__hash_speed },
40 { "index-version", cmd__index_version },
41 { "json-writer", cmd__json_writer },
42 { "lazy-init-name-hash", cmd__lazy_init_name_hash },
43 { "match-trees", cmd__match_trees },
44 { "mergesort", cmd__mergesort },
45 { "mktemp", cmd__mktemp },
46 { "oid-array", cmd__oid_array },
47 { "oidmap", cmd__oidmap },
48 { "oidtree", cmd__oidtree },
49 { "online-cpus", cmd__online_cpus },
50 { "parse-options", cmd__parse_options },
51 { "parse-pathspec-file", cmd__parse_pathspec_file },
52 { "partial-clone", cmd__partial_clone },
53 { "path-utils", cmd__path_utils },
54 { "pcre2-config", cmd__pcre2_config },
55 { "pkt-line", cmd__pkt_line },
56 { "prio-queue", cmd__prio_queue },
57 { "proc-receive", cmd__proc_receive },
58 { "progress", cmd__progress },
59 { "reach", cmd__reach },
60 { "read-cache", cmd__read_cache },
61 { "read-graph", cmd__read_graph },
62 { "read-midx", cmd__read_midx },
63 { "ref-store", cmd__ref_store },
64 { "reftable", cmd__reftable },
65 { "dump-reftable", cmd__dump_reftable },
66 { "regex", cmd__regex },
67 { "repository", cmd__repository },
68 { "revision-walking", cmd__revision_walking },
69 { "run-command", cmd__run_command },
70 { "scrap-cache-tree", cmd__scrap_cache_tree },
71 { "serve-v2", cmd__serve_v2 },
72 { "sha1", cmd__sha1 },
73 { "sha256", cmd__sha256 },
74 { "sigchain", cmd__sigchain },
75 { "simple-ipc", cmd__simple_ipc },
76 { "strcmp-offset", cmd__strcmp_offset },
77 { "string-list", cmd__string_list },
78 { "submodule-config", cmd__submodule_config },
79 { "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
80 { "subprocess", cmd__subprocess },
81 { "trace2", cmd__trace2 },
82 { "userdiff", cmd__userdiff },
83 { "urlmatch-normalization", cmd__urlmatch_normalization },
84 { "xml-encode", cmd__xml_encode },
85 { "wildmatch", cmd__wildmatch },
86 #ifdef GIT_WINDOWS_NATIVE
87 { "windows-named-pipe", cmd__windows_named_pipe },
88 #endif
89 { "write-cache", cmd__write_cache },
92 static NORETURN void die_usage(void)
94 size_t i;
96 fprintf(stderr, "usage: test-tool <toolname> [args]\n");
97 for (i = 0; i < ARRAY_SIZE(cmds); i++)
98 fprintf(stderr, " %s\n", cmds[i].name);
99 exit(128);
102 int cmd_main(int argc, const char **argv)
104 int i;
105 const char *working_directory = NULL;
106 struct option options[] = {
107 OPT_STRING('C', NULL, &working_directory, "directory",
108 "change the working directory"),
109 OPT_END()
112 BUG_exit_code = 99;
113 argc = parse_options(argc, argv, NULL, options, test_tool_usage,
114 PARSE_OPT_STOP_AT_NON_OPTION |
115 PARSE_OPT_KEEP_ARGV0);
117 if (argc < 2)
118 die_usage();
120 if (working_directory && chdir(working_directory) < 0)
121 die("Could not cd to '%s'", working_directory);
123 for (i = 0; i < ARRAY_SIZE(cmds); i++) {
124 if (!strcmp(cmds[i].name, argv[1])) {
125 argv++;
126 argc--;
127 trace2_cmd_name(cmds[i].name);
128 trace2_cmd_list_config();
129 trace2_cmd_list_env_vars();
130 return cmds[i].fn(argc, argv);
133 error("there is no tool named '%s'", argv[1]);
134 die_usage();