1 /*-------------------------------------------------------------------------
2  *
3  * pg_regress_main --- regression test for the main backend
4  *
5  * This is a C implementation of the previous shell script for running
6  * the regression tests, and should be mostly compatible with it.
7  * Initial author of C translation: Magnus Hagander
8  *
9  * This code is released under the terms of the PostgreSQL License.
10  *
11  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  * src/test/regress/pg_regress_main.c
15  *
16  *-------------------------------------------------------------------------
17  */
18 
19 #include "pg_regress.h"
20 
21 /*
22  * start a psql test process for specified file (including redirection),
23  * and return process ID
24  */
25 static PID_TYPE
psql_start_test(const char * testname,_stringlist ** resultfiles,_stringlist ** expectfiles,_stringlist ** tags)26 psql_start_test(const char *testname,
27 				_stringlist **resultfiles,
28 				_stringlist **expectfiles,
29 				_stringlist **tags)
30 {
31 	PID_TYPE	pid;
32 	char		infile[MAXPGPATH];
33 	char		outfile[MAXPGPATH];
34 	char		expectfile[MAXPGPATH];
35 	char		psql_cmd[MAXPGPATH * 3];
36 	size_t		offset = 0;
37 
38 	/*
39 	 * Look for files in the output dir first, consistent with a vpath search.
40 	 * This is mainly to create more reasonable error messages if the file is
41 	 * not found.  It also allows local test overrides when running pg_regress
42 	 * outside of the source tree.
43 	 */
44 	snprintf(infile, sizeof(infile), "%s/sql/%s.sql",
45 			 outputdir, testname);
46 	if (!file_exists(infile))
47 		snprintf(infile, sizeof(infile), "%s/sql/%s.sql",
48 				 inputdir, testname);
49 
50 	snprintf(outfile, sizeof(outfile), "%s/results/%s.out",
51 			 outputdir, testname);
52 
53 	snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
54 			 outputdir, testname);
55 	if (!file_exists(expectfile))
56 		snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
57 				 inputdir, testname);
58 
59 	add_stringlist_item(resultfiles, outfile);
60 	add_stringlist_item(expectfiles, expectfile);
61 
62 	if (launcher)
63 	{
64 		offset += snprintf(psql_cmd + offset, sizeof(psql_cmd) - offset,
65 						   "%s ", launcher);
66 		if (offset >= sizeof(psql_cmd))
67 		{
68 			fprintf(stderr, _("command too long\n"));
69 			exit(2);
70 		}
71 	}
72 
73 	offset += snprintf(psql_cmd + offset, sizeof(psql_cmd) - offset,
74 					   "\"%s%spsql\" -X -a -q -d \"%s\" < \"%s\" > \"%s\" 2>&1",
75 					   bindir ? bindir : "",
76 					   bindir ? "/" : "",
77 					   dblist->str,
78 					   infile,
79 					   outfile);
80 	if (offset >= sizeof(psql_cmd))
81 	{
82 		fprintf(stderr, _("command too long\n"));
83 		exit(2);
84 	}
85 
86 	pid = spawn_process(psql_cmd);
87 
88 	if (pid == INVALID_PID)
89 	{
90 		fprintf(stderr, _("could not start process for test %s\n"),
91 				testname);
92 		exit(2);
93 	}
94 
95 	return pid;
96 }
97 
98 static void
psql_init(int argc,char ** argv)99 psql_init(int argc, char **argv)
100 {
101 	/* set default regression database name */
102 	add_stringlist_item(&dblist, "regression");
103 }
104 
105 int
main(int argc,char * argv[])106 main(int argc, char *argv[])
107 {
108 	return regression_main(argc, argv, psql_init, psql_start_test);
109 }
110