-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_pathmanager.py
273 lines (228 loc) · 9.59 KB
/
test_pathmanager.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
#
"""
Tests for pathmanager.py
"""
# Standard library imports
import sys
import os
# Test library imports
import pytest
from qtpy.QtCore import Qt, QTimer
from qtpy.QtWidgets import QMessageBox, QPushButton
# Local imports
from spyder.py3compat import PY3
from spyder.utils.programs import is_module_installed
from spyder.widgets import pathmanager as pathmanager_mod
@pytest.fixture
def pathmanager(qtbot, request):
"""Set up PathManager."""
path, read_only_path, not_active_path = request.param
widget = pathmanager_mod.PathManager(
None,
path=tuple(path),
read_only_path=tuple(read_only_path),
not_active_path=tuple(not_active_path))
qtbot.addWidget(widget)
return widget
@pytest.mark.parametrize('pathmanager',
[(sys.path[:-10], sys.path[-10:], ())],
indirect=True)
def test_pathmanager(pathmanager, qtbot):
"""Run PathManager test"""
pathmanager.show()
assert pathmanager
@pytest.mark.parametrize('pathmanager',
[(sys.path[:-10], sys.path[-10:], ())],
indirect=True)
def test_check_uncheck_path(pathmanager):
"""
Test that checking and unchecking a path in the PathManager correctly
update the not active path list.
"""
# Assert that all paths are checked.
for row in range(pathmanager.listwidget.count()):
assert pathmanager.listwidget.item(row).checkState() == Qt.Checked
@pytest.mark.skipif(os.name != 'nt' or not is_module_installed('win32con'),
reason=("This feature is not applicable for Unix "
"systems and pywin32 is needed"))
@pytest.mark.parametrize('pathmanager',
[(['p1', 'p2', 'p3'], ['p4', 'p5', 'p6'], [])],
indirect=True)
def test_synchronize_with_PYTHONPATH(pathmanager, mocker):
# Import here to prevent an ImportError when testing on unix systems
from spyder.utils.environ import (get_user_env, set_user_env,
listdict2envdict)
# Store PYTHONPATH original state
env = get_user_env()
original_pathlist = env.get('PYTHONPATH', [])
# Mock the dialog window and answer "Yes" to clear contents of PYTHONPATH
# before adding Spyder's path list
mocker.patch.object(pathmanager_mod.QMessageBox, 'question',
return_value=pathmanager_mod.QMessageBox.Yes)
# Assert that PYTHONPATH is synchronized correctly with Spyder's path list
pathmanager.synchronize()
expected_pathlist = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6']
env = get_user_env()
assert env['PYTHONPATH'] == expected_pathlist
# Uncheck 'path2' and assert that it is removed from PYTHONPATH when it
# is synchronized with Spyder's path list
pathmanager.listwidget.item(1).setCheckState(Qt.Unchecked)
pathmanager.synchronize()
expected_pathlist = ['p1', 'p3', 'p4', 'p5', 'p6']
env = get_user_env()
assert env['PYTHONPATH'] == expected_pathlist
# Mock the dialog window and answer "No" to clear contents of PYTHONPATH
# before adding Spyder's path list
mocker.patch.object(pathmanager_mod.QMessageBox, 'question',
return_value=pathmanager_mod.QMessageBox.No)
# Uncheck 'path3' and assert that it is kept in PYTHONPATH when it
# is synchronized with Spyder's path list
pathmanager.listwidget.item(2).setCheckState(Qt.Unchecked)
pathmanager.synchronize()
expected_pathlist = ['p3', 'p1', 'p4', 'p5', 'p6']
env = get_user_env()
assert env['PYTHONPATH'] == expected_pathlist
# Restore PYTHONPATH to its original state
env['PYTHONPATH'] = original_pathlist
set_user_env(listdict2envdict(env))
@pytest.mark.parametrize('pathmanager',
[(sys.path[:-10], sys.path[-10:], ())],
indirect=True)
def test_invalid_directories(qtbot, pathmanager):
"""Check [site/dist]-packages are invalid paths."""
if os.name == 'nt':
paths = ['/lib/site-packages/foo',
'/lib/dist-packages/foo']
else:
paths = ['/lib/python3.6/site-packages/foo',
'/lib/python3.6/dist-packages/foo']
def interact_message_box():
child = pathmanager.findChild(QMessageBox)
qtbot.keyPress(child, Qt.Key_Enter)
for path in paths:
timer = QTimer()
timer.setSingleShot(True)
timer.timeout.connect(interact_message_box)
timer.start(300)
assert not pathmanager.check_path(path)
pathmanager.add_path(path)
@pytest.mark.parametrize('pathmanager',
[(('/spam', '/bar'), ('/foo', ), ())],
indirect=True)
def test_remove_item_and_reply_no(qtbot, pathmanager):
"""Check that the item is not removed after answering 'No'."""
pathmanager.show()
count = pathmanager.count()
def interact_message_box():
messagebox = pathmanager.findChild(QMessageBox)
buttons = messagebox.findChildren(QPushButton)
for button in buttons:
if 'no' in button.text().lower():
qtbot.mouseClick(button, Qt.LeftButton)
break
timer = QTimer()
timer.setSingleShot(True)
timer.timeout.connect(interact_message_box)
timer.start(100)
qtbot.mouseClick(pathmanager.remove_button, Qt.LeftButton)
# Back to main thread
assert pathmanager.count() == count
@pytest.mark.parametrize('pathmanager',
[(('/spam', '/bar'), ('/foo', ), ())],
indirect=True)
def test_remove_item_and_reply_yes(qtbot, pathmanager):
"""Check that the item is indeed removed after answering 'Yes'."""
pathmanager.show()
count = pathmanager.count()
def interact_message_box():
messagebox = pathmanager.findChild(QMessageBox)
buttons = messagebox.findChildren(QPushButton)
for button in buttons:
if 'yes' in button.text().lower():
qtbot.mouseClick(button, Qt.LeftButton)
break
timer = QTimer()
timer.setSingleShot(True)
timer.timeout.connect(interact_message_box)
timer.start(100)
qtbot.mouseClick(pathmanager.remove_button, Qt.LeftButton)
# Back to main thread
assert pathmanager.count() == (count - 1)
@pytest.mark.parametrize('pathmanager',
[((), (), ())],
indirect=True)
def test_add_repeated_item(qtbot, pathmanager, tmpdir):
"""
Check behavior when an unchecked item that is already on the list is added.
The checkbox should then be checked and if replying 'yes' to the question,
then the item should be moved to the top.
"""
pathmanager.show()
dir1 = str(tmpdir.mkdir("foo"))
dir2 = str(tmpdir.mkdir("bar"))
dir3 = str(tmpdir.mkdir("spam"))
pathmanager.add_path(dir1)
pathmanager.add_path(dir2)
pathmanager.add_path(dir3)
pathmanager.set_row_check_state(1, Qt.Unchecked)
assert not all(pathmanager.get_path_dict().values())
def interact_message_box():
messagebox = pathmanager.findChild(QMessageBox)
buttons = messagebox.findChildren(QPushButton)
for button in buttons:
if 'yes' in button.text().lower():
qtbot.mouseClick(button, Qt.LeftButton)
break
timer = QTimer()
timer.setSingleShot(True)
timer.timeout.connect(interact_message_box)
timer.start(500)
pathmanager.add_path(dir2)
print(pathmanager.get_path_dict())
# Back to main thread
assert pathmanager.count() == 3
assert list(pathmanager.get_path_dict().keys())[0] == dir2
assert all(pathmanager.get_path_dict().values())
@pytest.mark.skipif(os.name != 'nt' or not is_module_installed('win32con'),
reason=("This feature is not applicable for Unix "
"systems and pywin32 is needed"))
@pytest.mark.parametrize('pathmanager',
[(('/spam', '/bar'), ('/foo', ), ())],
indirect=True)
def test_buttons_state(qtbot, pathmanager, tmpdir):
"""Check buttons are enabled/disabled based on items and position."""
pathmanager.show()
assert not pathmanager.button_ok.isEnabled()
assert not pathmanager.movetop_button.isEnabled()
assert not pathmanager.moveup_button.isEnabled()
assert pathmanager.movebottom_button.isEnabled()
assert pathmanager.movedown_button.isEnabled()
pathmanager.set_current_row(1)
assert not pathmanager.button_ok.isEnabled()
assert pathmanager.movetop_button.isEnabled()
assert pathmanager.moveup_button.isEnabled()
assert not pathmanager.movebottom_button.isEnabled()
assert not pathmanager.movedown_button.isEnabled()
# Check adding a path updates the ok button
path = tmpdir.mkdir("bloop")
pathmanager.add_path(str(path))
assert pathmanager.button_ok.isEnabled()
assert not pathmanager.movetop_button.isEnabled()
assert not pathmanager.moveup_button.isEnabled()
# Check bottom state
pathmanager.movebottom_button.animateClick()
qtbot.waitUntil(pathmanager.movetop_button.isEnabled)
assert pathmanager.movetop_button.isEnabled()
assert pathmanager.moveup_button.isEnabled()
assert not pathmanager.movebottom_button.isEnabled()
assert not pathmanager.movedown_button.isEnabled()
assert pathmanager.current_row() == 2
# Check delete and ok button
pathmanager.remove_path(True)
assert not pathmanager.button_ok.isEnabled()
if __name__ == "__main__":
pytest.main([os.path.basename(__file__)])