-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
test_terminal.py
462 lines (366 loc) · 13.8 KB
/
test_terminal.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
#
"""Tests for the plugin."""
# Test library imports
import os
import os.path as osp
import pytest
import requests
import sys
from flaky import flaky
from pytestqt.plugin import QtBot
from unittest.mock import Mock
from qtpy.QtWidgets import QMainWindow, QApplication
from spyder.config.manager import CONF
os.environ['SPYDER_DEV'] = 'True'
# Local imports
import spyder_terminal.terminalplugin
from spyder_terminal.terminalplugin import TerminalPlugin
LOCATION = os.path.realpath(os.path.join(os.getcwd(),
os.path.dirname(__file__)))
LOCATION_SLASH = LOCATION.replace('\\', '/')
TERM_UP = 10000
WINDOWS = os.name == 'nt'
EXIT = 'exit'
CLEAR = 'clear'
if WINDOWS:
CLEAR = 'cls'
PWD = 'pwd'
if WINDOWS:
PWD = 'cd'
PREFIX = 'spyder_terminal.default.'
def check_pwd(termwidget):
"""Check if pwd command is executed."""
def callback(data):
global html
html = data
termwidget.body.runJavaScript(PREFIX + "getTerminalLines()", callback)
try:
return LOCATION in html
except NameError:
return False
def check_paste(termwidget, expected):
"""Check if pasting is correct in the terminal."""
def callback(data):
global text
text = data
termwidget.body.runJavaScript(PREFIX + "getTerminalLines()", callback)
try:
return all([x in text for x in expected])
except NameError:
return False
def check_output(termwidget, expected):
"""Check if the output includes the expected string."""
def callback(data):
global text
text = data
termwidget.body.runJavaScript(PREFIX + "getTerminalLines()", callback)
try:
return expected in text
except NameError:
return False
def check_increase_font_size(term):
def callback(data):
global font_size
font_size = data
expected = 15
term.body.runJavaScript(PREFIX + "increaseFontSize()", callback)
try:
return font_size > expected
except NameError:
return False
def check_decrease_font_size(term):
def callback(data):
global font_size
font_size = data
expected = 16
term.body.runJavaScript(PREFIX + "decreaseFontSize()", callback)
try:
return font_size < expected
except NameError:
return False
def check_fonts(term, expected):
"""Check if terminal fonts were updated."""
def callback(data):
global term_fonts
term_fonts = data
term.body.runJavaScript(PREFIX + "getFonts()", callback)
try:
return term_fonts == expected
except NameError:
return False
def check_hex_to_rgb(term):
"""Check if terminal is converting hexa colors to rgb correctly."""
def callback(data):
global hex_to_rgb
hex_to_rgb = data
expected = 'rgba(170, 171, 33, 0.2)'
color = '#aaab21'
term.body.runJavaScript(PREFIX + "hexToRGB('{}')".format(color),
callback)
try:
return hex_to_rgb == expected
except NameError:
return False
def check_num_tabs(terminal, ref_value):
"""Check if total number of terminal tabs has changed."""
value = len(terminal.get_widget().get_terms())
return value != ref_value
@pytest.fixture(scope="module")
def qtbot_module(qapp, request):
"""Module fixture for qtbot."""
result = QtBot(request)
return result
@pytest.fixture(scope='module')
def setup_terminal(qtbot_module, request):
"""Set up the Notebook plugin."""
class MainMock(QMainWindow):
def __getattr__(self, attr):
return Mock()
def register_shortcut(self, *args, **kwargs):
pass
main = MainMock()
CONF.register_plugin(TerminalPlugin)
terminal = TerminalPlugin(main, CONF)
terminal.update_font()
qtbot_module.addWidget(terminal)
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(5000)
terminal.create_new_term()
terminal.get_widget().show()
def teardown():
terminal.on_close()
request.addfinalizer(teardown)
return terminal
@flaky(max_runs=3)
@pytest.mark.skipif((os.environ.get('CI') and
sys.platform.startswith('linux')),
reason="Doesn't work on Linux CIs")
def test_terminal_paste(setup_terminal, qtbot_module):
"""Test the paste action in the terminal."""
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
term = terminal.get_widget().get_current_term()
port = terminal.get_widget().port
status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code
assert status_code == 200
term.exec_cmd(f'{os.linesep}' * 2)
separator = os.linesep
expected = ['prueba']
QApplication.clipboard().clear()
QApplication.clipboard().setText(separator.join(expected))
term.view.paste()
qtbot_module.waitUntil(lambda: check_paste(term, expected),
timeout=TERM_UP)
expected = ['this', 'a', 'test']
QApplication.clipboard().setText(separator.join(expected))
term.view.paste()
qtbot_module.waitUntil(lambda: check_paste(term, expected),
timeout=TERM_UP)
def test_terminal_color(setup_terminal, qtbot_module):
"""Test if the terminal color is converting to rgba correctly."""
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
term = terminal.get_widget().get_current_term()
port = terminal.get_widget().port
status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code
assert status_code == 200
qtbot_module.waitUntil(lambda: check_hex_to_rgb(term), timeout=TERM_UP)
def test_terminal_find(setup_terminal, qtbot_module):
"""Test the terminal find next/previous functions."""
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
term = terminal.get_widget().get_current_term()
port = terminal.get_widget().port
status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code
assert status_code == 200
term.exec_cmd('ls')
qtbot_module.wait(1000)
# Search without any special parameters
text = 'ls'
found = term.search_next(text)
assert found != -1
found = term.search_previous(text)
assert found != -1
# Search with case sensitive search
text = 'ls'
found = term.search_next(text, case=True)
assert found != -1
found = term.search_previous(text, case=True)
assert found != -1
# Search with the regex option
text = r'/\((.*?)\)/'
found = term.search_next(text, regex=True)
assert found != -1
found = term.search_previous(text, regex=True)
assert found != -1
# Search whole word option
text = 'ls'
found = term.search_next(text, word=True)
assert found != -1
found = term.search_previous(text, word=True)
assert found != -1
def test_terminal_font(setup_terminal, qtbot_module):
"""Test if terminal loads a custom font."""
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
term = terminal.get_widget().get_current_term()
port = terminal.get_widget().port
status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code
assert status_code == 200
term.set_font('Ubuntu Mono')
expected = '\'Ubuntu Mono\', monospace'
qtbot_module.waitUntil(lambda: check_fonts(term, expected),
timeout=TERM_UP)
# Verify increase of size of font
qtbot_module.waitUntil(lambda: check_increase_font_size(term),
timeout=TERM_UP)
# Verify decrease of size of font
qtbot_module.waitUntil(lambda: check_decrease_font_size(term),
timeout=TERM_UP)
def test_terminal_tab_title(setup_terminal, qtbot_module):
"""Test if terminal tab titles are numbered sequentially."""
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
terminal.create_new_term()
terminal.create_new_term()
num_1 = int(terminal.get_widget().tabwidget.tabText(1)[-1])
num_2 = int(terminal.get_widget().tabwidget.tabText(2)[-1])
assert num_2 == num_1 + 1
@flaky(max_runs=3)
@pytest.mark.skipif((os.environ.get('CI') and
sys.platform.startswith('linux')),
reason="Doesn't work on Linux CIs")
def test_new_terminal(setup_terminal, qtbot_module):
"""Test if a new terminal is added."""
# Setup widget
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
# Test if server is running
port = terminal.get_widget().port
status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code
assert status_code == 200
terminal.create_new_term()
term = terminal.get_widget().get_current_term()
qtbot_module.wait(1000)
# Move to LOCATION
# qtbot_module.keyClicks(term.view, 'cd {}'.format(LOCATION))
# qtbot_module.keyPress(term.view, Qt.Key_Return)
term.exec_cmd('cd {}'.format(LOCATION_SLASH))
# Clear
# qtbot_module.keyClicks(term.view, 'clear')
# qtbot_module.keyPress(term.view, Qt.Key_Return)
term.exec_cmd(CLEAR)
# Run pwd
# qtbot_module.keyClicks(term.view, 'pwd')
# qtbot_module.keyPress(term.view, Qt.Key_Return)
qtbot_module.wait(3000)
term.exec_cmd(PWD)
qtbot_module.wait(1000)
# Assert pwd is LOCATION
term.resize(900, 700)
qtbot_module.waitUntil(lambda: check_pwd(term), timeout=TERM_UP)
def test_output_redirection(setup_terminal, qtbot_module):
"""Test if stdout and stderr are redirected on DEV mode."""
assert spyder_terminal.widgets.main_widget.get_debug_level() > 0
stdout = osp.join(os.getcwd(), 'spyder_terminal_out.log')
stderr = osp.join(os.getcwd(), 'spyder_terminal_err.log')
assert osp.exists(stdout) and osp.exists(stderr)
@flaky(max_runs=3)
@pytest.mark.skipif(os.name == 'nt', reason="It hangs on Windows")
def test_close_terminal_manually(setup_terminal, qtbot_module):
"""Test if terminal tab is closed after process was finished manually."""
# Setup widget
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
terminal.create_new_term()
initial_num = len(terminal.get_widget().get_terms())
term = terminal.get_widget().get_current_term()
qtbot_module.wait(3000)
term.exec_cmd(EXIT)
qtbot_module.waitUntil(lambda: check_num_tabs(terminal, initial_num),
timeout=TERM_UP)
final_num = len(terminal.get_widget().get_terms())
assert final_num == initial_num - 1
def test_terminal_cwd(setup_terminal, qtbot_module):
"""Test if the a new terminal supports cwd with especial characters."""
start_dir = os.getcwd()
new_dir = osp.join(start_dir, 'this is dir with spaces')
if not osp.exists(new_dir):
os.mkdir(new_dir)
os.chdir(new_dir)
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
port = terminal.get_widget().port
status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code
assert status_code == 200
# Revert cwd
os.chdir(start_dir)
@flaky(max_runs=3)
@pytest.mark.skipif(sys.platform == 'darwin', reason="It hangs on macOS")
def test_conda_path(setup_terminal, qtbot_module):
"""Test if conda is correctly added to the path of the terminal."""
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
term = terminal.get_widget().get_current_term()
# Check that conda list works
term.exec_cmd("conda env list")
qtbot_module.waitUntil(lambda: check_output(term, "test"), timeout=TERM_UP)
# Clear the terminal
term.exec_cmd("clear")
# Try to deactivate the current environment
term.exec_cmd("conda deactivate")
qtbot_module.waitUntil(lambda: check_output(term, "base"), timeout=TERM_UP)
def test_zoom(setup_terminal, qtbot_module):
"""Test zoom level is increasing and decreasing."""
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
term = terminal.get_widget().get_current_term()
original_zoom = term.get_conf("zoom")
term.view.increase_font()
term.view.increase_font()
term.view.increase_font()
# Check the zoom increases three times
assert term.get_conf("zoom") == original_zoom + 3
term.view.decrease_font()
term.view.decrease_font()
# Check the zoom decreases two times
assert term.get_conf("zoom") == original_zoom + 1
def test_zoom_new_term(setup_terminal, qtbot_module):
"""Test zoom level is correct when creating a new terminal."""
terminal = setup_terminal
qtbot_module.waitUntil(
lambda: terminal.get_widget().server_is_ready(), timeout=TERM_UP)
qtbot_module.wait(1000)
term = terminal.get_widget().get_current_term()
term.view.increase_font()
term.view.increase_font()
term1_zoom = term.get_conf("zoom")
terminal.create_new_term()
term = terminal.get_widget().get_current_term()
new_zoom = term.get_conf("zoom")
assert term1_zoom == new_zoom