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.
 
  11 #include "test-tool.h"
 
  12 #include "git-compat-util.h"
 
  14 #include "run-command.h"
 
  15 #include "argv-array.h"
 
  17 #include "parse-options.h"
 
  18 #include "string-list.h"
 
  19 #include "thread-utils.h"
 
  20 #include "wildmatch.h"
 
  24 static int number_callbacks;
 
  25 static int parallel_next(struct child_process *cp,
 
  30         struct child_process *d = cb;
 
  31         if (number_callbacks >= 4)
 
  34         argv_array_pushv(&cp->args, d->argv);
 
  35         strbuf_addstr(err, "preloaded output of a child\n");
 
  40 static int no_job(struct child_process *cp,
 
  45         strbuf_addstr(err, "no further jobs available\n");
 
  49 static int task_finished(int result,
 
  54         strbuf_addstr(err, "asking for a quick stop\n");
 
  59         struct string_list tests, failed;
 
  61         int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
 
  63 #define TESTSUITE_INIT \
 
  64         { STRING_LIST_INIT_DUP, STRING_LIST_INIT_DUP, -1, 0, 0, 0, 0, 0, 0 }
 
  66 static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
 
  69         struct testsuite *suite = cb;
 
  71         if (suite->next >= suite->tests.nr)
 
  74         test = suite->tests.items[suite->next++].string;
 
  75         argv_array_pushl(&cp->args, "sh", test, NULL);
 
  77                 argv_array_push(&cp->args, "--quiet");
 
  79                 argv_array_push(&cp->args, "-i");
 
  81                 argv_array_push(&cp->args, "-v");
 
  82         if (suite->verbose_log)
 
  83                 argv_array_push(&cp->args, "-V");
 
  85                 argv_array_push(&cp->args, "-x");
 
  86         if (suite->write_junit_xml)
 
  87                 argv_array_push(&cp->args, "--write-junit-xml");
 
  89         strbuf_addf(err, "Output of '%s':\n", test);
 
  90         *task_cb = (void *)test;
 
  95 static int test_finished(int result, struct strbuf *err, void *cb,
 
  98         struct testsuite *suite = cb;
 
  99         const char *name = (const char *)task_cb;
 
 102                 string_list_append(&suite->failed, name);
 
 104         strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
 
 109 static int test_failed(struct strbuf *out, void *cb, void *task_cb)
 
 111         struct testsuite *suite = cb;
 
 112         const char *name = (const char *)task_cb;
 
 114         string_list_append(&suite->failed, name);
 
 115         strbuf_addf(out, "FAILED TO START: '%s'\n", name);
 
 120 static const char * const testsuite_usage[] = {
 
 121         "test-run-command testsuite [<options>] [<pattern>...]",
 
 125 static int testsuite(int argc, const char **argv)
 
 127         struct testsuite suite = TESTSUITE_INIT;
 
 128         int max_jobs = 1, i, ret;
 
 131         struct option options[] = {
 
 132                 OPT_BOOL('i', "immediate", &suite.immediate,
 
 133                          "stop at first failed test case(s)"),
 
 134                 OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
 
 135                 OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
 
 136                 OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
 
 137                 OPT_BOOL('V', "verbose-log", &suite.verbose_log,
 
 138                          "be verbose, redirected to a file"),
 
 139                 OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
 
 140                 OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
 
 141                          "write JUnit-style XML files"),
 
 145         memset(&suite, 0, sizeof(suite));
 
 146         suite.tests.strdup_strings = suite.failed.strdup_strings = 1;
 
 148         argc = parse_options(argc, argv, NULL, options,
 
 149                         testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
 
 152                 max_jobs = online_cpus();
 
 156                 die("Could not open the current directory");
 
 157         while ((d = readdir(dir))) {
 
 158                 const char *p = d->d_name;
 
 160                 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
 
 161                     !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
 
 162                     !ends_with(p, ".sh"))
 
 165                 /* No pattern: match all */
 
 167                         string_list_append(&suite.tests, p);
 
 171                 for (i = 0; i < argc; i++)
 
 172                         if (!wildmatch(argv[i], p, 0)) {
 
 173                                 string_list_append(&suite.tests, p);
 
 180                 die("No tests match!");
 
 181         if (max_jobs > suite.tests.nr)
 
 182                 max_jobs = suite.tests.nr;
 
 184         fprintf(stderr, "Running %d tests (%d at a time)\n",
 
 185                 suite.tests.nr, max_jobs);
 
 187         ret = run_processes_parallel(max_jobs, next_test, test_failed,
 
 188                                      test_finished, &suite);
 
 190         if (suite.failed.nr > 0) {
 
 192                 fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr);
 
 193                 for (i = 0; i < suite.failed.nr; i++)
 
 194                         fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
 
 197         string_list_clear(&suite.tests, 0);
 
 198         string_list_clear(&suite.failed, 0);
 
 203 int cmd__run_command(int argc, const char **argv)
 
 205         struct child_process proc = CHILD_PROCESS_INIT;
 
 208         if (argc > 1 && !strcmp(argv[1], "testsuite"))
 
 209                 exit(testsuite(argc - 1, argv + 1));
 
 213         while (!strcmp(argv[1], "env")) {
 
 215                         die("env specifier without a value");
 
 216                 argv_array_push(&proc.env_array, argv[2]);
 
 222         proc.argv = (const char **)argv + 2;
 
 224         if (!strcmp(argv[1], "start-command-ENOENT")) {
 
 225                 if (start_command(&proc) < 0 && errno == ENOENT)
 
 227                 fprintf(stderr, "FAIL %s\n", argv[1]);
 
 230         if (!strcmp(argv[1], "run-command"))
 
 231                 exit(run_command(&proc));
 
 233         jobs = atoi(argv[2]);
 
 234         proc.argv = (const char **)argv + 3;
 
 236         if (!strcmp(argv[1], "run-command-parallel"))
 
 237                 exit(run_processes_parallel(jobs, parallel_next,
 
 240         if (!strcmp(argv[1], "run-command-abort"))
 
 241                 exit(run_processes_parallel(jobs, parallel_next,
 
 242                                             NULL, task_finished, &proc));
 
 244         if (!strcmp(argv[1], "run-command-no-jobs"))
 
 245                 exit(run_processes_parallel(jobs, no_job,
 
 246                                             NULL, task_finished, &proc));
 
 248         fprintf(stderr, "check usage\n");