File: run-tests.py

package info (click to toggle)
python-i3ipc 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 580 kB
  • sloc: python: 2,968; makefile: 222; sh: 4
file content (102 lines) | stat: -rwxr-xr-x 2,589 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3

import subprocess
from subprocess import Popen, call
import os
from os import listdir, path
from os.path import isfile, join
import sys
import re
import time
import random
from shutil import which

here = os.path.abspath(os.path.dirname(__file__))

XVFB = 'Xvfb'
I3_BINARY = 'i3'
SOCKETDIR = '/tmp/.X11-unix'


def check_dependencies():
    if not which(XVFB):
        # TODO make this optional
        print('Xvfb is required to run tests')
        print('Command "%s" not found in PATH' % XVFB)
        sys.exit(127)

    if not which(I3_BINARY):
        print('i3 binary is required to run tests')
        print('Command "%s" not found in PATH' % I3_BINARY)
        sys.exit(127)


def get_open_display():
    if not os.path.isdir(SOCKETDIR):
        sys.stderr.write(
            'warning: could not find the X11 socket directory at {}. Using display 0.\n'
            .format(SOCKETDIR))
        sys.stderr.flush()
        return 0
    socket_re = re.compile(r'^X([0-9]+)$')
    socket_files = [f for f in listdir(SOCKETDIR) if socket_re.match(f)]
    displays = [int(socket_re.search(f).group(1)) for f in socket_files]
    open_display = min(
        [i for i in range(0,
                          max(displays or [0]) + 2) if i not in displays])
    return open_display


def start_server(display):
    xvfb = Popen([XVFB, ':%d' % display])
    # wait for the socket to make sure the server is running
    socketfile = path.join(SOCKETDIR, 'X%d' % display)
    tries = 0
    while True:
        if path.exists(socketfile):
            break
        else:
            tries += 1

            if tries > 100:
                print('could not start x server')
                xvfb.kill()
                sys.exit(1)

            time.sleep(0.1)

    return xvfb


def run_pytest(display):
    version_info = sys.version_info

    if version_info[0] < 3:
        raise NotImplementedError('tests are not implemented for python < 3')

    cmd = ['python3', '-m', 'pytest', '-s']

    if version_info[1] < 6:
        cmd += ['--ignore', 'test/aio']

    env = os.environ.copy()
    env['DISPLAY'] = ':%d' % display
    env['PYTHONPATH'] = here
    env['I3SOCK'] = '/tmp/i3ipc-test-sock-{display}'.format(display=display)
    return subprocess.run(cmd + sys.argv[1:], env=env)


def main():
    check_dependencies()
    call([I3_BINARY, '--version'])
    display = get_open_display()

    with start_server(display) as server:
        result = run_pytest(display)
        server.terminate()

    sys.exit(result.returncode)


if __name__ == '__main__':
    main()