Merge branch 'jc/makefile-redirection-stderr' into maint
[alt-git.git] / test-run-command.c
blob30a64a98dc8b53a3a2d7edfaa57a8ee51d5d63e7
1 /*
2 * test-run-command.c: test run command API.
4 * (C) 2009 Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
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 */
11 #include "git-compat-util.h"
12 #include "run-command.h"
13 #include "argv-array.h"
14 #include "strbuf.h"
15 #include <string.h>
16 #include <errno.h>
18 static int number_callbacks;
19 static int parallel_next(struct child_process *cp,
20 struct strbuf *err,
21 void *cb,
22 void **task_cb)
24 struct child_process *d = cb;
25 if (number_callbacks >= 4)
26 return 0;
28 argv_array_pushv(&cp->args, d->argv);
29 strbuf_addf(err, "preloaded output of a child\n");
30 number_callbacks++;
31 return 1;
34 static int no_job(struct child_process *cp,
35 struct strbuf *err,
36 void *cb,
37 void **task_cb)
39 strbuf_addf(err, "no further jobs available\n");
40 return 0;
43 static int task_finished(int result,
44 struct strbuf *err,
45 void *pp_cb,
46 void *pp_task_cb)
48 strbuf_addf(err, "asking for a quick stop\n");
49 return 1;
52 int main(int argc, char **argv)
54 struct child_process proc = CHILD_PROCESS_INIT;
55 int jobs;
57 if (argc < 3)
58 return 1;
59 proc.argv = (const char **)argv + 2;
61 if (!strcmp(argv[1], "start-command-ENOENT")) {
62 if (start_command(&proc) < 0 && errno == ENOENT)
63 return 0;
64 fprintf(stderr, "FAIL %s\n", argv[1]);
65 return 1;
67 if (!strcmp(argv[1], "run-command"))
68 exit(run_command(&proc));
70 jobs = atoi(argv[2]);
71 proc.argv = (const char **)argv + 3;
73 if (!strcmp(argv[1], "run-command-parallel"))
74 exit(run_processes_parallel(jobs, parallel_next,
75 NULL, NULL, &proc));
77 if (!strcmp(argv[1], "run-command-abort"))
78 exit(run_processes_parallel(jobs, parallel_next,
79 NULL, task_finished, &proc));
81 if (!strcmp(argv[1], "run-command-no-jobs"))
82 exit(run_processes_parallel(jobs, no_job,
83 NULL, task_finished, &proc));
85 fprintf(stderr, "check usage\n");
86 return 1;