t / helper / test-run-command.con commit The sixth batch for 2.18 (e144d12)
   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
  11#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>
  18
  19static int number_callbacks;
  20static 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);
  30        strbuf_addstr(err, "preloaded output of a child\n");
  31        number_callbacks++;
  32        return 1;
  33}
  34
  35static int no_job(struct child_process *cp,
  36                  struct strbuf *err,
  37                  void *cb,
  38                  void **task_cb)
  39{
  40        strbuf_addstr(err, "no further jobs available\n");
  41        return 0;
  42}
  43
  44static int task_finished(int result,
  45                         struct strbuf *err,
  46                         void *pp_cb,
  47                         void *pp_task_cb)
  48{
  49        strbuf_addstr(err, "asking for a quick stop\n");
  50        return 1;
  51}
  52
  53int cmd__run_command(int argc, const char **argv)
  54{
  55        struct child_process proc = CHILD_PROCESS_INIT;
  56        int jobs;
  57
  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        }
  67        if (argc < 3)
  68                return 1;
  69        proc.argv = (const char **)argv + 2;
  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        }
  77        if (!strcmp(argv[1], "run-command"))
  78                exit(run_command(&proc));
  79
  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
  95        fprintf(stderr, "check usage\n");
  96        return 1;
  97}