]>
Commit | Line | Data |
---|---|---|
2b541bf8 JS |
1 | /* |
2 | * test-run-command.c: test run command API. | |
3 | * | |
4 | * (C) 2009 Ilari Liusvaara <ilari.liusvaara@elisanet.fi> | |
5 | * | |
6 | * This code is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
ae6a51f5 | 11 | #include "test-tool.h" |
2b541bf8 JS |
12 | #include "git-compat-util.h" |
13 | #include "run-command.h" | |
c553c72e SB |
14 | #include "argv-array.h" |
15 | #include "strbuf.h" | |
2b541bf8 JS |
16 | #include <string.h> |
17 | #include <errno.h> | |
18 | ||
c553c72e SB |
19 | static int number_callbacks; |
20 | static int parallel_next(struct child_process *cp, | |
21 | struct strbuf *err, | |
22 | void *cb, | |
23 | void **task_cb) | |
24 | { | |
25 | struct child_process *d = cb; | |
26 | if (number_callbacks >= 4) | |
27 | return 0; | |
28 | ||
29 | argv_array_pushv(&cp->args, d->argv); | |
02962d36 | 30 | strbuf_addstr(err, "preloaded output of a child\n"); |
c553c72e SB |
31 | number_callbacks++; |
32 | return 1; | |
33 | } | |
34 | ||
35 | static int no_job(struct child_process *cp, | |
36 | struct strbuf *err, | |
37 | void *cb, | |
38 | void **task_cb) | |
39 | { | |
02962d36 | 40 | strbuf_addstr(err, "no further jobs available\n"); |
c553c72e SB |
41 | return 0; |
42 | } | |
43 | ||
44 | static int task_finished(int result, | |
c553c72e SB |
45 | struct strbuf *err, |
46 | void *pp_cb, | |
47 | void *pp_task_cb) | |
48 | { | |
02962d36 | 49 | strbuf_addstr(err, "asking for a quick stop\n"); |
c553c72e SB |
50 | return 1; |
51 | } | |
52 | ||
ae6a51f5 | 53 | int cmd__run_command(int argc, const char **argv) |
2b541bf8 | 54 | { |
d3180279 | 55 | struct child_process proc = CHILD_PROCESS_INIT; |
c553c72e | 56 | int jobs; |
2b541bf8 | 57 | |
c61a975d NTND |
58 | if (argc < 3) |
59 | return 1; | |
60 | while (!strcmp(argv[1], "env")) { | |
61 | if (!argv[2]) | |
62 | die("env specifier without a value"); | |
63 | argv_array_push(&proc.env_array, argv[2]); | |
64 | argv += 2; | |
65 | argc -= 2; | |
66 | } | |
2b541bf8 JS |
67 | if (argc < 3) |
68 | return 1; | |
c553c72e | 69 | proc.argv = (const char **)argv + 2; |
2b541bf8 JS |
70 | |
71 | if (!strcmp(argv[1], "start-command-ENOENT")) { | |
72 | if (start_command(&proc) < 0 && errno == ENOENT) | |
73 | return 0; | |
74 | fprintf(stderr, "FAIL %s\n", argv[1]); | |
75 | return 1; | |
76 | } | |
c0f19bf3 JN |
77 | if (!strcmp(argv[1], "run-command")) |
78 | exit(run_command(&proc)); | |
2b541bf8 | 79 | |
c553c72e SB |
80 | jobs = atoi(argv[2]); |
81 | proc.argv = (const char **)argv + 3; | |
82 | ||
83 | if (!strcmp(argv[1], "run-command-parallel")) | |
84 | exit(run_processes_parallel(jobs, parallel_next, | |
85 | NULL, NULL, &proc)); | |
86 | ||
87 | if (!strcmp(argv[1], "run-command-abort")) | |
88 | exit(run_processes_parallel(jobs, parallel_next, | |
89 | NULL, task_finished, &proc)); | |
90 | ||
91 | if (!strcmp(argv[1], "run-command-no-jobs")) | |
92 | exit(run_processes_parallel(jobs, no_job, | |
93 | NULL, task_finished, &proc)); | |
94 | ||
2b541bf8 JS |
95 | fprintf(stderr, "check usage\n"); |
96 | return 1; | |
97 | } |