aboutsummaryrefslogtreecommitdiff
path: root/src/tools/testwrap
blob: 02f1951ad7e949d77cb2a347b6c9141a09abf598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3

import argparse
import shutil
import subprocess
import os
import sys

parser = argparse.ArgumentParser()

parser.add_argument('--srcdir', help='source directory of test', type=str)
parser.add_argument('--basedir', help='base directory of test', type=str)
parser.add_argument('--testgroup', help='test group', type=str)
parser.add_argument('--testname', help='test name', type=str)
parser.add_argument('--skip', help='skip test (with reason)', type=str)
parser.add_argument('--pg-test-extra', help='extra tests', type=str)
parser.add_argument('test_command', nargs='*')

args = parser.parse_args()

testdir = '{}/testrun/{}/{}'.format(
    args.basedir, args.testgroup, args.testname)

print('# executing test in {} group {} test {}'.format(
    testdir, args.testgroup, args.testname))
sys.stdout.flush()

if args.skip is not None:
    print('1..0 # Skipped: ' + args.skip)
    sys.exit(0)

if os.path.exists(testdir) and os.path.isdir(testdir):
    shutil.rmtree(testdir)
os.makedirs(testdir)

os.chdir(args.srcdir)

# mark test as having started
open(os.path.join(testdir, 'test.start'), 'x')

env_dict = {**os.environ,
            'TESTDATADIR': os.path.join(testdir, 'data'),
            'TESTLOGDIR': os.path.join(testdir, 'log')}


# The configuration time value of PG_TEST_EXTRA is supplied via argument
# --pg-test-extra. But it can be overridden by environment variable
# PG_TEST_EXTRA at the time of running a test. Hence use value from arguments
# only if PG_TEST_EXTRA is not set in the test environment, which already
# contains all the environment variables at the time of running the test.
if "PG_TEST_EXTRA" not in env_dict and args.pg_test_extra:
    env_dict["PG_TEST_EXTRA"] = args.pg_test_extra

sp = subprocess.Popen(args.test_command, env=env_dict, stdout=subprocess.PIPE)
# Meson categorizes a passing TODO test point as bad
# (https://github.com/mesonbuild/meson/issues/13183).  Remove the TODO
# directive, so Meson computes the file result like Perl does.  This could
# have the side effect of delaying stdout lines relative to stderr.  That
# doesn't affect the log file, and the TAP protocol uses stdout only.
for line in sp.stdout:
    if line.startswith(b'ok '):
        line = line.replace(b' # TODO ', b' # testwrap-overridden-TODO ', 1)
    sys.stdout.buffer.write(line)
    sys.stdout.flush()
returncode = sp.wait()

if returncode == 0:
    print('# test succeeded')
    open(os.path.join(testdir, 'test.success'), 'x')
else:
    print('# test failed')
    open(os.path.join(testdir, 'test.fail'), 'x')
sys.exit(returncode)