File: runtests.py

package info (click to toggle)
python-xlib 0.33-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,040 kB
  • sloc: python: 23,022; awk: 89; makefile: 60; sh: 10
file content (134 lines) | stat: -rwxr-xr-x 3,627 bytes parent folder | download | duplicates (4)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python

# Python 2/3 compatibility.
from __future__ import print_function

import os
import signal
import subprocess
import sys
import tempfile
import textwrap

from pkg_resources import load_entry_point


class SigException(BaseException):
    def __init__(self, signum):
        super(SigException, self).__init__()
        self.signum = signum

def xsession_sighandler(signum, frame):
    raise SigException(signum)

def xserver_start(display, executable='Xvfb', authfile=None):
    pid = os.fork()
    if pid != 0:
        return pid
    if authfile is None:
        authfile = os.devnull
    # This will make the xserver send us a SIGUSR1 when ready.
    signal.signal(signal.SIGUSR1, signal.SIG_IGN)
    cmd = [
        executable,
        '-auth', authfile,
        '-noreset',
        display,
    ]
    print('starting xserver: `{0}`'.format(' '.join(cmd)))
    os.execlp(cmd[0], *cmd)

def tests_run(display, authfile=None):
    pid = os.fork()
    if pid != 0:
        return pid
    if authfile is None:
        authfile = os.devnull
    os.environ['DISPLAY'] = display
    os.environ['XAUTHORITY'] = authfile
    cmd = [
        sys.executable,
        '-c', textwrap.dedent(
            '''
            from pkg_resources import load_entry_point
            sys.exit(load_entry_point(
                'nose', 'console_scripts', 'nosetests',
            )())
            '''
        ).lstrip(),
        '--exe', '--with-xunit', '--verbosity=3',
    ]
    has_custom_tests = False
    for arg in sys.argv[1:]:
        if not arg.startswith('-'):
            has_custom_tests = True
        cmd.append(arg)
    if not has_custom_tests:
        cmd.extend(('test/', 'examples/run_examples.py'))
    print('running tests: `{0}`'.format(' '.join(cmd)))
    sys.argv = cmd
    try:
        load_entry_point('nose', 'console_scripts', 'nosetests')()
    except SystemExit as err:
        code = err.code
    else:
        code = 0
    os._exit(code)


def runtests():

    cleanup_funcs = []

    try:
        if hasattr(sys, 'pypy_version_info'):
            server_display = ':8'
        else:
            server_display = ':9'
        server_display += ''.join(str(n) for n in sys.version_info[:3])

        # Setup a temporary authentication file.
        cookie = subprocess.check_output('mcookie').strip()
        authfile = tempfile.NamedTemporaryFile(delete=False)
        cleanup_funcs.append(lambda: os.unlink(authfile.name))
        authfile.close()
        subprocess.check_call((
            'xauth',
            '-f', authfile.name,
            'add', server_display, '.', cookie,
        ))

        # Setup signal handler to wait for xserver to be ready.
        signal.signal(signal.SIGUSR1, xsession_sighandler)

        # Start xserver.
        server_pid = xserver_start(server_display, authfile=authfile.name)
        cleanup_funcs.append(lambda: os.waitpid(server_pid, 0))
        cleanup_funcs.append(lambda: os.kill(server_pid, signal.SIGTERM))

        # Give the server 3 seconds to start.
        signal.alarm(3)

        # Wait for server to be ready.
        try:
            signal.pause()
        except SigException as err:
            assert signal.SIGUSR1 == err.signum
            signal.alarm(0)

        # Run tests.
        tests_pid = tests_run(server_display, authfile=authfile.name)
        pid, status = os.waitpid(tests_pid, 0)
        assert pid == tests_pid
        sys.exit(status >> 8)

    except KeyboardInterrupt:
        sys.exit(1)

    finally:
        for func in reversed(cleanup_funcs):
            func()


if __name__ == '__main__':
    runtests()