git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 10/13] test-tool run-command: learn to run (parts of) the testsuite

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Sep 26, 2019, 08:30 UTC
Message-ID
<ad9ab10ce0ec0615570f5d685f5765b27093dae7.1569486607.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.288.git.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

Git for Windows jumps through hoops to provide a development environment that allows to build Git and to run its test suite. To that end, an entire MSYS2 system, including GNU make and GCC is offered as "the Git for Windows SDK". It does come at a price: an initial download of said SDK weighs in with several hundreds of megabytes, and the unpacked SDK occupies ~2GB of disk space.

A much more native development environment on Windows is Visual Studio. To help contributors use that environment, we already have a Makefile target `vcxproj` that generates a commit with project files (and other generated files), and Git for Windows' `vs/master` branch is continuously re-generated using that target.

The idea is to allow building Git in Visual Studio, and to run individual tests using a Portable Git.

The one missing thing is a way to run the entire test suite: neither `make` nor `prove` are required to run Git, therefore Git for Windows does not support those commands in the Portable Git.

To help with that, add a simple test helper that exercises the `run_processes_parallel()` function to allow for running test scripts in parallel (which is really necessary, especially on Windows, as Git's test suite takes such a long time to run).

This will also come in handy for the upcoming change to our Azure
Pipeline: we will use this helper in a Portable Git to test the Visual
Studio build of Git.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/helper/test-run-command.c | 153 ++++++++++++++++++++++++++++++++++++
 1 file changed, 153 insertions(+)
diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
index 2cc93bb69c..ead6dc611a 100644
--- a/t/helper/test-run-command.c
+++ b/t/helper/test-run-command.c
@@ -10,9 +10,14 @@
 
 #include "test-tool.h"
 #include "git-compat-util.h"
+#include "cache.h"
 #include "run-command.h"
 #include "argv-array.h"
 #include "strbuf.h"
+#include "parse-options.h"
+#include "string-list.h"
+#include "thread-utils.h"
+#include "wildmatch.h"
 #include <string.h>
 #include <errno.h>
 
@@ -50,11 +55,159 @@ static int task_finished(int result,
 	return 1;
 }
 
+struct testsuite {
+	struct string_list tests, failed;
+	int next;
+	int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
+};
+#define TESTSUITE_INIT \
+	{ STRING_LIST_INIT_DUP, STRING_LIST_INIT_DUP, -1, 0, 0, 0, 0, 0, 0 }
+
+static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
+		     void **task_cb)
+{
+	struct testsuite *suite = cb;
+	const char *test;
+	if (suite->next >= suite->tests.nr)
+		return 0;
+
+	test = suite->tests.items[suite->next++].string;
+	argv_array_pushl(&cp->args, "sh", test, NULL);
+	if (suite->quiet)
+		argv_array_push(&cp->args, "--quiet");
+	if (suite->immediate)
+		argv_array_push(&cp->args, "-i");
+	if (suite->verbose)
+		argv_array_push(&cp->args, "-v");
+	if (suite->verbose_log)
+		argv_array_push(&cp->args, "-V");
+	if (suite->trace)
+		argv_array_push(&cp->args, "-x");
+	if (suite->write_junit_xml)
+		argv_array_push(&cp->args, "--write-junit-xml");
+
+	strbuf_addf(err, "Output of '%s':\n", test);
+	*task_cb = (void *)test;
+
+	return 1;
+}
+
+static int test_finished(int result, struct strbuf *err, void *cb,
+			 void *task_cb)
+{
+	struct testsuite *suite = cb;
+	const char *name = (const char *)task_cb;
+
+	if (result)
+		string_list_append(&suite->failed, name);
+
+	strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
+
+	return 0;
+}
+
+static int test_failed(struct strbuf *out, void *cb, void *task_cb)
+{
+	struct testsuite *suite = cb;
+	const char *name = (const char *)task_cb;
+
+	string_list_append(&suite->failed, name);
+	strbuf_addf(out, "FAILED TO START: '%s'\n", name);
+
+	return 0;
+}
+
+static const char * const testsuite_usage[] = {
+	"test-run-command testsuite [<options>] [<pattern>...]",
+	NULL
+};
+
+static int testsuite(int argc, const char **argv)
+{
+	struct testsuite suite = TESTSUITE_INIT;
+	int max_jobs = 1, i, ret;
+	DIR *dir;
+	struct dirent *d;
+	struct option options[] = {
+		OPT_BOOL('i', "immediate", &suite.immediate,
+			 "stop at first failed test case(s)"),
+		OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
+		OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
+		OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
+		OPT_BOOL('V', "verbose-log", &suite.verbose_log,
+			 "be verbose, redirected to a file"),
+		OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
+		OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
+			 "write JUnit-style XML files"),
+		OPT_END()
+	};
+
+	memset(&suite, 0, sizeof(suite));
+	suite.tests.strdup_strings = suite.failed.strdup_strings = 1;
+
+	argc = parse_options(argc, argv, NULL, options,
+			testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
+
+	if (max_jobs <= 0)
+		max_jobs = online_cpus();
+
+	dir = opendir(".");
+	if (!dir)
+		die("Could not open the current directory");
+	while ((d = readdir(dir))) {
+		const char *p = d->d_name;
+
+		if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
+		    !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
+		    !ends_with(p, ".sh"))
+			continue;
+
+		/* No pattern: match all */
+		if (!argc) {
+			string_list_append(&suite.tests, p);
+			continue;
+		}
+
+		for (i = 0; i < argc; i++)
+			if (!wildmatch(argv[i], p, 0)) {
+				string_list_append(&suite.tests, p);
+				break;
+			}
+	}
+	closedir(dir);
+
+	if (!suite.tests.nr)
+		die("No tests match!");
+	if (max_jobs > suite.tests.nr)
+		max_jobs = suite.tests.nr;
+
+	fprintf(stderr, "Running %d tests (%d at a time)\n",
+		suite.tests.nr, max_jobs);
+
+	ret = run_processes_parallel(max_jobs, next_test, test_failed,
+				     test_finished, &suite);
+
+	if (suite.failed.nr > 0) {
+		ret = 1;
+		fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr);
+		for (i = 0; i < suite.failed.nr; i++)
+			fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
+	}
+
+	string_list_clear(&suite.tests, 0);
+	string_list_clear(&suite.failed, 0);
+
+	return !!ret;
+}
+
 int cmd__run_command(int argc, const char **argv)
 {
 	struct child_process proc = CHILD_PROCESS_INIT;
 	int jobs;
 
+	if (argc > 1 && !strcmp(argv[1], "testsuite"))
+		exit(testsuite(argc - 1, argv + 1));
+
 	if (argc < 3)
 		return 1;
 	while (!strcmp(argv[1], "env")) {
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 14 of 72 in “ci: include a Visual Studio build & test in our Azure Pipeline”
  1. 00/13 ci: include a Visual Studio build & test in our Azure PipelineJohannes Schindelin via GitGitGadget, Sep 26, 2019
  2. 01/13 push: do not pretend to return `int` from `die_push_simple()`Johannes Schindelin via GitGitGadget, Sep 26, 2019
  3. 02/13 msvc: avoid using minus operator on unsigned typesJohannes Schindelin via GitGitGadget, Sep 26, 2019
  4. Denton LiuSep 26, 2019
  5. Johannes SchindelinSep 26, 2019
  6. Denton LiuSep 26, 2019
  7. Johannes SchindelinSep 30, 2019
  8. 03/13 winansi: use FLEX_ARRAY to avoid compiler warningJohannes Schindelin via GitGitGadget, Sep 26, 2019
  9. 04/13 compat/win32/path-utils.h: add #include guardsJohannes Schindelin via GitGitGadget, Sep 26, 2019
  10. 05/13 msvc: ignore some libraries when linkingJohannes Schindelin via GitGitGadget, Sep 26, 2019
  11. 06/13 msvc: handle DEVELOPER=1Johannes Schindelin via GitGitGadget, Sep 26, 2019
  12. 07/13 msvc: work around a bug in GetEnvironmentVariable()Johannes Schindelin via GitGitGadget, Sep 26, 2019
  13. 08/13 vcxproj: only copy `git-remote-http.exe` once it was builtJohannes Schindelin via GitGitGadget, Sep 26, 2019
  14. 10/13 test-tool run-command: learn to run (parts of) the testsuiteJohannes Schindelin via GitGitGadget, Sep 26, 2019
  15. 09/13 vcxproj: include more generated filesJohannes Schindelin via GitGitGadget, Sep 26, 2019
  16. 11/13 tests: let --immediate and --write-junit-xml play well togetherJohannes Schindelin via GitGitGadget, Sep 26, 2019
  17. Junio C HamanoSep 28, 2019
  18. Johannes SchindelinSep 30, 2019
  19. 12/13 ci: really use shallow clones on Azure PipelinesJohannes Schindelin via GitGitGadget, Sep 26, 2019
  20. 13/13 ci: also build and test with MS Visual Studio on Azure PipelinesJohannes Schindelin via GitGitGadget, Sep 26, 2019
  21. 00/13 ci: include a Visual Studio build & test in our Azure PipelineJohannes Schindelin via GitGitGadget, Sep 30, 2019
  22. 01/13 push: do not pretend to return `int` from `die_push_simple()`Johannes Schindelin via GitGitGadget, Sep 30, 2019
  23. Junio C HamanoOct 3, 2019
  24. Johannes SchindelinOct 4, 2019
  25. 03/13 winansi: use FLEX_ARRAY to avoid compiler warningJohannes Schindelin via GitGitGadget, Sep 30, 2019
  26. 02/13 msvc: avoid using minus operator on unsigned typesJohannes Schindelin via GitGitGadget, Sep 30, 2019
  27. Junio C HamanoOct 3, 2019
  28. Johannes SchindelinOct 4, 2019
  29. Johannes SixtOct 4, 2019
  30. Johannes SchindelinOct 4, 2019
  31. Junio C HamanoOct 6, 2019
  32. Johannes SixtOct 6, 2019
  33. Johannes SchindelinOct 8, 2019
  34. Johannes SixtOct 8, 2019
  35. 05/13 msvc: ignore some libraries when linkingJohannes Schindelin via GitGitGadget, Sep 30, 2019
  36. 06/13 msvc: handle DEVELOPER=1Johannes Schindelin via GitGitGadget, Sep 30, 2019
  37. 04/13 compat/win32/path-utils.h: add #include guardsJohannes Schindelin via GitGitGadget, Sep 30, 2019
  38. 07/13 msvc: work around a bug in GetEnvironmentVariable()Johannes Schindelin via GitGitGadget, Sep 30, 2019
  39. 11/13 tests: let --immediate and --write-junit-xml play well togetherJohannes Schindelin via GitGitGadget, Sep 30, 2019
  40. 13/13 ci: also build and test with MS Visual Studio on Azure PipelinesJohannes Schindelin via GitGitGadget, Sep 30, 2019
  41. 10/13 test-tool run-command: learn to run (parts of) the testsuiteJohannes Schindelin via GitGitGadget, Sep 30, 2019
  42. 08/13 vcxproj: only copy `git-remote-http.exe` once it was builtJohannes Schindelin via GitGitGadget, Sep 30, 2019
  43. 09/13 vcxproj: include more generated filesJohannes Schindelin via GitGitGadget, Sep 30, 2019
  44. 12/13 ci: really use shallow clones on Azure PipelinesJohannes Schindelin via GitGitGadget, Sep 30, 2019
  45. 00/13 ci: include a Visual Studio build & test in our Azure PipelineJohannes Schindelin via GitGitGadget, Oct 4, 2019
  46. 01/13 push: do not pretend to return `int` from `die_push_simple()`Johannes Schindelin via GitGitGadget, Oct 4, 2019
  47. 03/13 winansi: use FLEX_ARRAY to avoid compiler warningJohannes Schindelin via GitGitGadget, Oct 4, 2019
  48. Alban GruinOct 7, 2019
  49. 05/13 msvc: ignore some libraries when linkingJohannes Schindelin via GitGitGadget, Oct 4, 2019
  50. 02/13 msvc: avoid using minus operator on unsigned typesJohannes Schindelin via GitGitGadget, Oct 4, 2019
  51. 08/13 vcxproj: only copy `git-remote-http.exe` once it was builtJohannes Schindelin via GitGitGadget, Oct 4, 2019
  52. 07/13 msvc: work around a bug in GetEnvironmentVariable()Johannes Schindelin via GitGitGadget, Oct 4, 2019
  53. 09/13 vcxproj: include more generated filesJohannes Schindelin via GitGitGadget, Oct 4, 2019
  54. 04/13 compat/win32/path-utils.h: add #include guardsJohannes Schindelin via GitGitGadget, Oct 4, 2019
  55. 12/13 ci: really use shallow clones on Azure PipelinesJohannes Schindelin via GitGitGadget, Oct 4, 2019
  56. 06/13 msvc: handle DEVELOPER=1Johannes Schindelin via GitGitGadget, Oct 4, 2019
  57. 11/13 tests: let --immediate and --write-junit-xml play well togetherJohannes Schindelin via GitGitGadget, Oct 4, 2019
  58. 10/13 test-tool run-command: learn to run (parts of) the testsuiteJohannes Schindelin via GitGitGadget, Oct 4, 2019
  59. 13/13 ci: also build and test with MS Visual Studio on Azure PipelinesJohannes Schindelin via GitGitGadget, Oct 4, 2019
  60. Junio C HamanoOct 6, 2019
  61. Johannes SchindelinOct 6, 2019
  62. Johannes SchindelinOct 6, 2019
  63. Junio C HamanoOct 7, 2019
  64. Johannes SchindelinOct 7, 2019
  65. Junio C HamanoOct 8, 2019
  66. Johannes SchindelinOct 8, 2019
  67. Philip OakleyOct 9, 2019
  68. Johannes SchindelinOct 10, 2019
  69. Philip OakleyOct 10, 2019
  70. Junio C HamanoOct 7, 2019
  71. Thomas GummererOct 7, 2019
  72. Johannes SchindelinOct 11, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.