]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-tool.c
Turn `git serve` into a test helper
[thirdparty/git.git] / t / helper / test-tool.c
CommitLineData
efd71f89
NTND
1#include "git-compat-util.h"
2#include "test-tool.h"
6ea18fff
JS
3#include "parse-options.h"
4
5static const char * const test_tool_usage[] = {
6 "test-tool [-C <directory>] <command [<arguments>...]]",
7 NULL
8};
efd71f89
NTND
9
10struct test_cmd {
11 const char *name;
12 int (*fn)(int argc, const char **argv);
13};
14
15static struct test_cmd cmds[] = {
0e496492 16 { "chmtime", cmd__chmtime },
0e2678af 17 { "config", cmd__config },
e4998944 18 { "ctype", cmd__ctype },
a801a7cf 19 { "date", cmd__date },
9153dde5 20 { "delta", cmd__delta },
1c854745 21 { "drop-caches", cmd__drop_caches },
06ccb29e 22 { "dump-cache-tree", cmd__dump_cache_tree },
f1ef0b02 23 { "dump-fsmonitor", cmd__dump_fsmonitor },
8133061e 24 { "dump-split-index", cmd__dump_split_index },
cd780f0b 25 { "dump-untracked-cache", cmd__dump_untracked_cache },
dbceb3ec 26 { "example-decorate", cmd__example_decorate },
c680668d 27 { "genrandom", cmd__genrandom },
d5cfd142 28 { "genzeros", cmd__genzeros },
7c18cbd5 29 { "hashmap", cmd__hashmap },
37649b7f 30 { "hash-speed", cmd__hash_speed },
cc6f663d 31 { "index-version", cmd__index_version },
75459410 32 { "json-writer", cmd__json_writer },
64eb82fe 33 { "lazy-init-name-hash", cmd__lazy_init_name_hash },
9080e75f 34 { "match-trees", cmd__match_trees },
34889d3c 35 { "mergesort", cmd__mergesort },
d9cc2c87 36 { "mktemp", cmd__mktemp },
c033cc15 37 { "online-cpus", cmd__online_cpus },
2f17c78c 38 { "parse-options", cmd__parse_options },
b8d5cf4f 39 { "path-utils", cmd__path_utils },
8ea40cc5 40 { "pkt-line", cmd__pkt_line },
15b75817 41 { "prio-queue", cmd__prio_queue },
ab176ac4 42 { "reach", cmd__reach },
5fbe600c 43 { "read-cache", cmd__read_cache },
4d80560c 44 { "read-midx", cmd__read_midx },
65370d81 45 { "ref-store", cmd__ref_store },
9038531f 46 { "regex", cmd__regex },
dade47c0 47 { "repository", cmd__repository },
77d4b8c8 48 { "revision-walking", cmd__revision_walking },
ae6a51f5 49 { "run-command", cmd__run_command },
ff5fb8b0 50 { "scrap-cache-tree", cmd__scrap_cache_tree },
b7ce24d0 51 { "serve-v2", cmd__serve_v2 },
dae2ff9b 52 { "sha1", cmd__sha1 },
a0fe6e6e 53 { "sha1-array", cmd__sha1_array },
13eeedb5 54 { "sha256", cmd__sha256 },
e154a6f3 55 { "sigchain", cmd__sigchain },
1a5f3d70 56 { "strcmp-offset", cmd__strcmp_offset },
c932a5ff 57 { "string-list", cmd__string_list },
b6188213 58 { "submodule-config", cmd__submodule_config },
2b1257e4 59 { "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
112edd6a 60 { "subprocess", cmd__subprocess },
599fbd87 61 { "urlmatch-normalization", cmd__urlmatch_normalization },
22231908 62 { "xml-encode", cmd__xml_encode },
0489289d 63 { "wildmatch", cmd__wildmatch },
06ba9d03
JH
64#ifdef GIT_WINDOWS_NATIVE
65 { "windows-named-pipe", cmd__windows_named_pipe },
66#endif
c81f843d 67 { "write-cache", cmd__write_cache },
efd71f89
NTND
68};
69
4e26569d
JK
70static NORETURN void die_usage(void)
71{
72 size_t i;
73
74 fprintf(stderr, "usage: test-tool <toolname> [args]\n");
75 for (i = 0; i < ARRAY_SIZE(cmds); i++)
76 fprintf(stderr, " %s\n", cmds[i].name);
77 exit(128);
78}
79
efd71f89
NTND
80int cmd_main(int argc, const char **argv)
81{
82 int i;
6ea18fff
JS
83 const char *working_directory = NULL;
84 struct option options[] = {
85 OPT_STRING('C', NULL, &working_directory, "directory",
86 "change the working directory"),
87 OPT_END()
88 };
efd71f89 89
a86303cb 90 BUG_exit_code = 99;
6ea18fff
JS
91 argc = parse_options(argc, argv, NULL, options, test_tool_usage,
92 PARSE_OPT_STOP_AT_NON_OPTION |
93 PARSE_OPT_KEEP_ARGV0);
94
efd71f89 95 if (argc < 2)
4e26569d 96 die_usage();
efd71f89 97
6ea18fff
JS
98 if (working_directory && chdir(working_directory) < 0)
99 die("Could not cd to '%s'", working_directory);
100
efd71f89
NTND
101 for (i = 0; i < ARRAY_SIZE(cmds); i++) {
102 if (!strcmp(cmds[i].name, argv[1])) {
103 argv++;
104 argc--;
105 return cmds[i].fn(argc, argv);
106 }
107 }
4e26569d
JK
108 error("there is no tool named '%s'", argv[1]);
109 die_usage();
efd71f89 110}