|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import time |
| 6 | +import os.path |
| 7 | +import fnmatch |
| 8 | +import tempfile |
| 9 | +import subprocess |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | + |
| 14 | +def xvfb_available(): |
| 15 | + # http://stackoverflow.com/a/28909933/2085149 |
| 16 | + return any( |
| 17 | + os.access(os.path.join(path, 'Xvfb'), os.X_OK) |
| 18 | + for path in os.environ["PATH"].split(os.pathsep) |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +class XvfbExitedError(Exception): |
| 23 | + |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +class Xvfb(object): |
| 28 | + |
| 29 | + def __init__(self, config): |
| 30 | + self.width = int(config.getini('xvfb_width')) |
| 31 | + self.height = int(config.getini('xvfb_height')) |
| 32 | + self.colordepth = int(config.getini('xvfb_colordepth')) |
| 33 | + self.args = config.getini('xvfb_args') or [] |
| 34 | + self.display = None |
| 35 | + self._old_display = None |
| 36 | + self._proc = None |
| 37 | + |
| 38 | + def start(self): |
| 39 | + self._old_display = os.environ.get('DISPLAY', None) |
| 40 | + self.display = self._get_free_display() |
| 41 | + display_str = ':{}'.format(self.display) |
| 42 | + os.environ['DISPLAY'] = display_str |
| 43 | + |
| 44 | + cmd = ['Xvfb', display_str, '-screen', '0', |
| 45 | + '{}x{}x{}'.format(self.width, self.height, self.colordepth)] |
| 46 | + cmd.extend(self.args) |
| 47 | + self._proc = subprocess.Popen(cmd) |
| 48 | + |
| 49 | + time.sleep(0.1) # Give Xvfb a bit of time to start/fail |
| 50 | + ret = self._proc.poll() |
| 51 | + |
| 52 | + if ret is not None: |
| 53 | + raise XvfbExitedError("Xvfb exited with exit code {0}".format(ret)) |
| 54 | + |
| 55 | + def stop(self): |
| 56 | + if self._old_display is None: |
| 57 | + del os.environ['DISPLAY'] |
| 58 | + else: |
| 59 | + os.environ['DISPLAY'] == self._old_display |
| 60 | + try: |
| 61 | + self._proc.terminate() |
| 62 | + self._proc.wait() |
| 63 | + except OSError: |
| 64 | + pass |
| 65 | + |
| 66 | + def _get_free_display(self): |
| 67 | + tmpdir = tempfile.gettempdir() |
| 68 | + pattern = '.X*-lock' |
| 69 | + lockfiles = fnmatch.filter(os.listdir(tmpdir), pattern) |
| 70 | + existing_displays = [int(re.match('^\.X(\d+)-lock$', name).group(1)) |
| 71 | + for name in lockfiles] |
| 72 | + if existing_displays: |
| 73 | + return max(existing_displays) + 1 |
| 74 | + else: |
| 75 | + return 0 |
| 76 | + |
| 77 | + |
| 78 | +def pytest_addoption(parser): |
| 79 | + group = parser.getgroup('xvfb') |
| 80 | + group.addoption( |
| 81 | + '--no-xvfb', |
| 82 | + action='store_true', |
| 83 | + help='Disable Xvfb for tests.' |
| 84 | + ) |
| 85 | + |
| 86 | + parser.addini('xvfb_width', 'Width of the Xvfb display', default='800') |
| 87 | + parser.addini('xvfb_height', 'Height of the Xvfb display', default='600') |
| 88 | + parser.addini('xvfb_colordepth', 'Color depth of the Xvfb display', |
| 89 | + default='16') |
| 90 | + parser.addini('xvfb_args', 'Additional arguments for Xvfb', |
| 91 | + type='linelist') |
| 92 | + |
| 93 | + |
| 94 | +def pytest_configure(config): |
| 95 | + if config.getoption('--no-xvfb') or not xvfb_available(): |
| 96 | + config.xvfb = None |
| 97 | + else: |
| 98 | + config.xvfb = Xvfb(config) |
| 99 | + config.xvfb.start() |
| 100 | + |
| 101 | + |
| 102 | +def pytest_unconfigure(config): |
| 103 | + if config.xvfb is not None: |
| 104 | + config.xvfb.stop() |
0 commit comments