Skip to content

Commit 6ea18ff

Browse files
dschogitster
authored andcommitted
test-tool: handle the -C <directory> option just like git
In preparation for moving `git serve` into `test-tool` (because it really is only used by the test suite), we teach the `test-tool` the useful trick to change the working directory before running the test command, which will avoid introducing subshells in the test code. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 31cf4a6 commit 6ea18ff

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

t/helper/test-tool.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#include "git-compat-util.h"
22
#include "test-tool.h"
3+
#include "parse-options.h"
4+
5+
static const char * const test_tool_usage[] = {
6+
"test-tool [-C <directory>] <command [<arguments>...]]",
7+
NULL
8+
};
39

410
struct test_cmd {
511
const char *name;
@@ -73,11 +79,24 @@ static NORETURN void die_usage(void)
7379
int cmd_main(int argc, const char **argv)
7480
{
7581
int i;
82+
const char *working_directory = NULL;
83+
struct option options[] = {
84+
OPT_STRING('C', NULL, &working_directory, "directory",
85+
"change the working directory"),
86+
OPT_END()
87+
};
7688

7789
BUG_exit_code = 99;
90+
argc = parse_options(argc, argv, NULL, options, test_tool_usage,
91+
PARSE_OPT_STOP_AT_NON_OPTION |
92+
PARSE_OPT_KEEP_ARGV0);
93+
7894
if (argc < 2)
7995
die_usage();
8096

97+
if (working_directory && chdir(working_directory) < 0)
98+
die("Could not cd to '%s'", working_directory);
99+
81100
for (i = 0; i < ARRAY_SIZE(cmds); i++) {
82101
if (!strcmp(cmds[i].name, argv[1])) {
83102
argv++;

0 commit comments

Comments
 (0)