]> granicus.if.org Git - postgresql/blob - src/test/regress/pg_regress_main.c
6106c6bbda24eb922e8a386148723de70dbc88d2
[postgresql] / src / test / regress / pg_regress_main.c
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-2017, 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
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         char       *appnameenv;
38
39         /*
40          * Look for files in the output dir first, consistent with a vpath search.
41          * This is mainly to create more reasonable error messages if the file is
42          * not found.  It also allows local test overrides when running pg_regress
43          * outside of the source tree.
44          */
45         snprintf(infile, sizeof(infile), "%s/sql/%s.sql",
46                          outputdir, testname);
47         if (!file_exists(infile))
48                 snprintf(infile, sizeof(infile), "%s/sql/%s.sql",
49                                  inputdir, testname);
50
51         snprintf(outfile, sizeof(outfile), "%s/results/%s.out",
52                          outputdir, testname);
53
54         snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
55                          outputdir, testname);
56         if (!file_exists(expectfile))
57                 snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
58                                  inputdir, testname);
59
60         add_stringlist_item(resultfiles, outfile);
61         add_stringlist_item(expectfiles, expectfile);
62
63         if (launcher)
64                 offset += snprintf(psql_cmd + offset, sizeof(psql_cmd) - offset,
65                                                    "%s ", launcher);
66
67         appnameenv = psprintf("PGAPPNAME=pg_regress/%s", testname);
68         putenv(appnameenv);
69
70         snprintf(psql_cmd + offset, sizeof(psql_cmd) - offset,
71                          "\"%s%spsql\" -X -a -q -d \"%s\" < \"%s\" > \"%s\" 2>&1",
72                          bindir ? bindir : "",
73                          bindir ? "/" : "",
74                          dblist->str,
75                          infile,
76                          outfile);
77
78         pid = spawn_process(psql_cmd);
79
80         if (pid == INVALID_PID)
81         {
82                 fprintf(stderr, _("could not start process for test %s\n"),
83                                 testname);
84                 exit(2);
85         }
86
87         unsetenv("PGAPPNAME");
88         free(appnameenv);
89
90         return pid;
91 }
92
93 static void
94 psql_init(int argc, char **argv)
95 {
96         /* set default regression database name */
97         add_stringlist_item(&dblist, "regression");
98 }
99
100 int
101 main(int argc, char *argv[])
102 {
103         return regression_main(argc, argv, psql_init, psql_start_test);
104 }