File: conftest.py

package info (click to toggle)
glueviz 1.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,340 kB
  • sloc: python: 44,462; makefile: 138; sh: 70
file content (127 lines) | stat: -rw-r--r-- 3,493 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
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
import os
import sys
import warnings

import pytest

try:
    from qtpy import PYSIDE2
except Exception:
    PYSIDE2 = False

from glue.config import CFG_DIR as CFG_DIR_ORIG

try:
    import objgraph
except ImportError:
    OBJGRAPH_INSTALLED = False
else:
    OBJGRAPH_INSTALLED = True


STDERR_ORIGINAL = sys.stderr

ON_APPVEYOR = os.environ.get('APPVEYOR', 'False') == 'True'


def pytest_runtest_teardown(item, nextitem):
    sys.stderr = STDERR_ORIGINAL
    global start_dir
    os.chdir(start_dir)


def pytest_addoption(parser):
    parser.addoption("--no-optional-skip", action="store_true", default=False,
                     help="don't skip any tests with optional dependencies")


start_dir = None


def pytest_configure(config):

    global start_dir
    start_dir = os.path.abspath('.')

    os.environ['GLUE_TESTING'] = 'True'

    if config.getoption('no_optional_skip'):
        from glue.tests import helpers
        for attr in helpers.__dict__:
            if attr.startswith('requires_'):
                # The following line replaces the decorators with a function
                # that does noting, effectively disabling it.
                setattr(helpers, attr, lambda f: f)

    # Make sure we don't affect the real glue config dir
    import tempfile
    from glue import config
    config.CFG_DIR = tempfile.mkdtemp()

    # Start up QApplication, if the Qt code is present
    try:
        from glue.utils.qt import get_qapp
    except Exception:
        # Note that we catch any exception, not just ImportError, because
        # QtPy can raise a PythonQtError.
        pass
    else:
        get_qapp()

    # Force loading of plugins
    from glue.main import load_plugins
    load_plugins()


def pytest_report_header(config):
    from glue import __version__
    glue_version = "%20s:\t%s" % ("glue", __version__)
    from glue._deps import get_status
    return os.linesep + glue_version + os.linesep + os.linesep + get_status()


def pytest_unconfigure(config):

    os.environ.pop('GLUE_TESTING')

    # Reset configuration directory to original one
    from glue import config
    config.CFG_DIR = CFG_DIR_ORIG

    # Remove reference to QApplication to prevent segmentation fault on PySide
    try:
        from glue.utils.qt import app
        app.qapp = None
    except Exception:  # for when we run the tests without the qt directories
        # Note that we catch any exception, not just ImportError, because
        # QtPy can raise a PythonQtError.
        pass

    if OBJGRAPH_INSTALLED and not ON_APPVEYOR:

        # Make sure there are no lingering references to GlueApplication
        obj = objgraph.by_type('GlueApplication')
        if len(obj) > 0:
            objgraph.show_backrefs(objgraph.by_type('GlueApplication'))
            warnings.warn('There are {0} remaining references to GlueApplication'.format(len(obj)))

        # Uncomment when checking for memory leaks
        # objgraph.show_most_common_types(limit=100)


# With PySide2, tests can fail in a non-deterministic way with the following
# error:
#
#   AttributeError: 'PySide2.QtGui.QStandardItem' object has no attribute 'connect'
#
# Until this can be properly debugged and fixed, we xfail any test that fails
# with this exception

if PYSIDE2:

    def pytest_runtest_call(__multicall__):
        try:
            __multicall__.execute()
        except AttributeError as exc:
            if 'PySide2.QtGui.QStandardItem' in str(exc):
                pytest.xfail()