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*/
1011
#include "test-tool.h"
12#include "git-compat-util.h"
13#include "run-command.h"
14#include "argv-array.h"
15#include "strbuf.h"
16#include <string.h>
17#include <errno.h>
1819
static int number_callbacks;
20static int parallel_next(struct child_process *cp,
21struct strbuf *err,
22void *cb,
23void **task_cb)
24{
25struct child_process *d = cb;
26if (number_callbacks >= 4)
27return 0;
2829
argv_array_pushv(&cp->args, d->argv);
30strbuf_addstr(err, "preloaded output of a child\n");
31number_callbacks++;
32return 1;
33}
3435
static int no_job(struct child_process *cp,
36struct strbuf *err,
37void *cb,
38void **task_cb)
39{
40strbuf_addstr(err, "no further jobs available\n");
41return 0;
42}
4344
static int task_finished(int result,
45struct strbuf *err,
46void *pp_cb,
47void *pp_task_cb)
48{
49strbuf_addstr(err, "asking for a quick stop\n");
50return 1;
51}
5253
int cmd__run_command(int argc, const char **argv)
54{
55struct child_process proc = CHILD_PROCESS_INIT;
56int jobs;
5758
if (argc < 3)
59return 1;
60while (!strcmp(argv[1], "env")) {
61if (!argv[2])
62die("env specifier without a value");
63argv_array_push(&proc.env_array, argv[2]);
64argv += 2;
65argc -= 2;
66}
67if (argc < 3)
68return 1;
69proc.argv = (const char **)argv + 2;
7071
if (!strcmp(argv[1], "start-command-ENOENT")) {
72if (start_command(&proc) < 0 && errno == ENOENT)
73return 0;
74fprintf(stderr, "FAIL %s\n", argv[1]);
75return 1;
76}
77if (!strcmp(argv[1], "run-command"))
78exit(run_command(&proc));
7980
jobs = atoi(argv[2]);
81proc.argv = (const char **)argv + 3;
8283
if (!strcmp(argv[1], "run-command-parallel"))
84exit(run_processes_parallel(jobs, parallel_next,
85NULL, NULL, &proc));
8687
if (!strcmp(argv[1], "run-command-abort"))
88exit(run_processes_parallel(jobs, parallel_next,
89NULL, task_finished, &proc));
9091
if (!strcmp(argv[1], "run-command-no-jobs"))
92exit(run_processes_parallel(jobs, no_job,
93NULL, task_finished, &proc));
9495
fprintf(stderr, "check usage\n");
96return 1;
97}