forked from spyder-ide/qtpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
183 lines (153 loc) · 4.5 KB
/
test_main.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import contextlib
import os
import subprocess
import sys
import pytest
from qtpy import API_NAMES, QtCore, QtGui, QtWidgets
with contextlib.suppress(Exception):
# removed in qt 6.0
from qtpy import QtWebEngineWidgets
def assert_pyside2():
"""
Make sure that we are using PySide
"""
import PySide2
assert QtCore.QEvent is PySide2.QtCore.QEvent
assert QtGui.QPainter is PySide2.QtGui.QPainter
assert QtWidgets.QWidget is PySide2.QtWidgets.QWidget
assert (
QtWebEngineWidgets.QWebEnginePage
is PySide2.QtWebEngineWidgets.QWebEnginePage
)
assert os.environ["QT_API"] == "pyside2"
def assert_pyside6():
"""
Make sure that we are using PySide
"""
import PySide6
assert QtCore.QEvent is PySide6.QtCore.QEvent
assert QtGui.QPainter is PySide6.QtGui.QPainter
assert QtWidgets.QWidget is PySide6.QtWidgets.QWidget
# Only valid for qt>=6.2
# assert QtWebEngineWidgets.QWebEnginePage is PySide6.QtWebEngineCore.QWebEnginePage
assert os.environ["QT_API"] == "pyside6"
def assert_pyqt5():
"""
Make sure that we are using PyQt5
"""
import PyQt5
assert QtCore.QEvent is PyQt5.QtCore.QEvent
assert QtGui.QPainter is PyQt5.QtGui.QPainter
assert QtWidgets.QWidget is PyQt5.QtWidgets.QWidget
assert os.environ["QT_API"] == "pyqt5"
def assert_pyqt6():
"""
Make sure that we are using PyQt6
"""
import PyQt6
assert QtCore.QEvent is PyQt6.QtCore.QEvent
assert QtGui.QPainter is PyQt6.QtGui.QPainter
assert QtWidgets.QWidget is PyQt6.QtWidgets.QWidget
assert os.environ["QT_API"] == "pyqt6"
def assert_qt5():
try:
import PyQt5
except ImportError:
try:
import PySide2
except ImportError:
pytest.fail("No Qt5 API available.")
else:
assert_pyside2()
else:
assert_pyqt5()
def assert_qt6():
try:
import PyQt6
except ImportError:
try:
import PySide6
except ImportError:
pytest.fail("No Qt6 API available.")
else:
assert_pyside6()
else:
assert_pyqt6()
def test_qt_api():
"""
If QT_API is specified, we check that the correct Qt wrapper was used
"""
QT_API = os.environ.get("QT_API", "").lower()
if QT_API == "pyqt5":
assert_pyqt5()
elif QT_API == "pyside2":
assert_pyside2()
elif QT_API == "pyqt6":
assert_pyqt6()
elif QT_API == "pyside6":
assert_pyside6()
else:
# If the tests are run locally, USE_QT_API and QT_API may not be
# defined, but we still want to make sure qtpy is behaving sensibly.
# We should then be loading, in order of decreasing preference, PyQt5,
# PySide2, PyQt6 and PySide6.
try:
import PyQt5
except ImportError:
try:
import PySide2
except ImportError:
try:
import PyQt6
except ImportError:
import PySide6
assert_pyside6()
else:
assert_pyqt6()
else:
assert_pyside2()
else:
assert_pyqt5()
@pytest.mark.parametrize("api", API_NAMES.values())
def test_qt_api_environ(api):
"""
If no QT_API is specified but some Qt is imported, ensure QT_API is set properly.
"""
mod = f"{api}.QtCore"
pytest.importorskip(mod, reason=f"Requires {api}")
# clean env
env = os.environ.copy()
for key in ("QT_API", "USE_QT_API"):
if key in env:
del env[key]
cmd = f"""
import {mod}
from qtpy import API
import os
print(API)
print(os.environ['QT_API'])
"""
output = subprocess.check_output([sys.executable, "-c", cmd], env=env)
got_api, env_qt_api = output.strip().decode("utf-8").splitlines()
assert got_api == api.lower()
assert env_qt_api == api.lower()
# Also ensure we raise a nice error
env["QT_API"] = "bad"
cmd = """
try:
import qtpy
except ValueError as exc:
assert 'Specified QT_API' in str(exc), str(exc)
else:
raise AssertionError('QtPy imported despite bad QT_API')
"""
subprocess.check_call([sys.executable, "-Oc", cmd], env=env)
def test_qt_version():
"""
If QT_VERSION is specified, check that one of the correct Qt wrappers was used
"""
QT_VERSION = os.environ.get("QT_VERSION", "").lower()
if QT_VERSION == "qt5":
assert_qt5()
elif QT_VERSION == "qt6":
assert_qt6()