t / helper / test-run-command.con commit t/helper: merge test-read-cache into test-tool (5fbe600)
   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 "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>
  17
  18static int number_callbacks;
  19static int parallel_next(struct child_process *cp,
  20                         struct strbuf *err,
  21                         void *cb,
  22                         void **task_cb)
  23{
  24        struct child_process *d = cb;
  25        if (number_callbacks >= 4)
  26                return 0;
  27
  28        argv_array_pushv(&cp->args, d->argv);
  29        strbuf_addstr(err, "preloaded output of a child\n");
  30        number_callbacks++;
  31        return 1;
  32}
  33
  34static int no_job(struct child_process *cp,
  35                  struct strbuf *err,
  36                  void *cb,
  37                  void **task_cb)
  38{
  39        strbuf_addstr(err, "no further jobs available\n");
  40        return 0;
  41}
  42
  43static int task_finished(int result,
  44                         struct strbuf *err,
  45                         void *pp_cb,
  46                         void *pp_task_cb)
  47{
  48        strbuf_addstr(err, "asking for a quick stop\n");
  49        return 1;
  50}
  51
  52int cmd_main(int argc, const char **argv)
  53{
  54        struct child_process proc = CHILD_PROCESS_INIT;
  55        int jobs;
  56
  57        if (argc < 3)
  58                return 1;
  59        while (!strcmp(argv[1], "env")) {
  60                if (!argv[2])
  61                        die("env specifier without a value");
  62                argv_array_push(&proc.env_array, argv[2]);
  63                argv += 2;
  64                argc -= 2;
  65        }
  66        if (argc < 3)
  67                return 1;
  68        proc.argv = (const char **)argv + 2;
  69
  70        if (!strcmp(argv[1], "start-command-ENOENT")) {
  71                if (start_command(&proc) < 0 && errno == ENOENT)
  72                        return 0;
  73                fprintf(stderr, "FAIL %s\n", argv[1]);
  74                return 1;
  75        }
  76        if (!strcmp(argv[1], "run-command"))
  77                exit(run_command(&proc));
  78
  79        jobs = atoi(argv[2]);
  80        proc.argv = (const char **)argv + 3;
  81
  82        if (!strcmp(argv[1], "run-command-parallel"))
  83                exit(run_processes_parallel(jobs, parallel_next,
  84                                            NULL, NULL, &proc));
  85
  86        if (!strcmp(argv[1], "run-command-abort"))
  87                exit(run_processes_parallel(jobs, parallel_next,
  88                                            NULL, task_finished, &proc));
  89
  90        if (!strcmp(argv[1], "run-command-no-jobs"))
  91                exit(run_processes_parallel(jobs, no_job,
  92                                            NULL, task_finished, &proc));
  93
  94        fprintf(stderr, "check usage\n");
  95        return 1;
  96}