PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_regress_main.c
Go to the documentation of this file.
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 }
char * str
Definition: pg_regress.h:25
static PID_TYPE psql_start_test(const char *testname, _stringlist **resultfiles, _stringlist **expectfiles, _stringlist **tags)
static void static void static void void add_stringlist_item(_stringlist **listhead, const char *str)
Definition: pg_regress.c:152
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PID_TYPE
Definition: pg_regress.h:15
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static void psql_init(int argc, char **argv)
#define putenv(x)
Definition: win32.h:421
char * launcher
Definition: pg_regress.c:75
PID_TYPE spawn_process(const char *cmdline)
Definition: pg_regress.c:1124
static bool file_exists(const char *name)
Definition: dfmgr.c:450
#define MAXPGPATH
_stringlist * dblist
Definition: pg_regress.c:70
#define INVALID_PID
Definition: pg_regress.h:16
int main(int argc, char *argv[])
char * outputdir
Definition: pg_regress.c:73
char * inputdir
Definition: pg_regress.c:72
static void infile(const char *filename)
Definition: zic.c:1151
#define free(a)
Definition: header.h:60
char * bindir
Definition: pg_regress.c:74
#define unsetenv(x)
Definition: win32.h:422
static char * outfile
#define _(x)
Definition: elog.c:84
int regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc)
Definition: pg_regress.c:2018