Skip to content

Commit e1c3c49

Browse files
committed
Merge from 3.x: PR #6602
2 parents 5cdeadf + 9811c87 commit e1c3c49

File tree

2 files changed

+54
-89
lines changed

2 files changed

+54
-89
lines changed

Diff for: spyder/plugins/tests/test_ipythonconsole.py

+44-82
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,16 @@
1616
import ipykernel
1717
from pygments.token import Name
1818
import pytest
19-
from qtpy import PYQT4, PYQT5, PYQT_VERSION
20-
from qtpy.QtCore import Qt, QTimer
21-
from qtpy.QtWidgets import QApplication
19+
from qtpy import PYQT4, PYQT5
20+
from qtpy.QtCore import Qt
2221
import zmq
2322

2423
from spyder.config.gui import get_color_scheme
2524
from spyder.config.main import CONF
26-
from spyder.py3compat import PY2, PY3
27-
from spyder.plugins.ipythonconsole import (IPythonConsole,
28-
KernelConnectionDialog)
29-
from spyder.utils.environ import listdict2envdict
25+
from spyder.py3compat import PY2, to_text_string
26+
from spyder.plugins.ipythonconsole import IPythonConsole
3027
from spyder.utils.ipython.style import create_style_class
3128
from spyder.utils.programs import TEMPDIR
32-
from spyder.utils.test import close_message_box
33-
from spyder.widgets.variableexplorer.collectionseditor import CollectionsEditor
3429

3530

3631
#==============================================================================
@@ -44,24 +39,18 @@
4439
#==============================================================================
4540
# Utillity Functions
4641
#==============================================================================
47-
def open_client_from_connection_info(connection_info, qtbot):
48-
top_level_widgets = QApplication.topLevelWidgets()
49-
for w in top_level_widgets:
50-
if isinstance(w, KernelConnectionDialog):
51-
w.cf.setText(connection_info)
52-
qtbot.keyClick(w, Qt.Key_Enter)
53-
5442
def get_console_font_color(syntax_style):
5543
styles = create_style_class(syntax_style).styles
5644
font_color = styles[Name]
57-
5845
return font_color
5946

47+
6048
def get_console_background_color(style_sheet):
6149
background_color = style_sheet.split('background-color:')[1]
6250
background_color = background_color.split(';')[0]
6351
return background_color
6452

53+
6554
#==============================================================================
6655
# Qt Test Fixtures
6756
#==============================================================================
@@ -137,7 +126,6 @@ def test_auto_backend(ipyconsole, qtbot):
137126

138127
@pytest.mark.slow
139128
@flaky(max_runs=3)
140-
@pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
141129
def test_tab_rename_for_slaves(ipyconsole, qtbot):
142130
"""Test slave clients are renamed correctly."""
143131
# Wait until the window is fully up
@@ -159,9 +147,11 @@ def test_tab_rename_for_slaves(ipyconsole, qtbot):
159147

160148
@pytest.mark.slow
161149
@flaky(max_runs=3)
162-
@pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
163150
def test_no_repeated_tabs_name(ipyconsole, qtbot):
164151
"""Test that tabs can't have repeated given names."""
152+
shell = ipyconsole.get_current_shellwidget()
153+
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
154+
165155
# Rename first client
166156
ipyconsole.rename_tabs_after_change('foo')
167157

@@ -176,9 +166,11 @@ def test_no_repeated_tabs_name(ipyconsole, qtbot):
176166

177167
@pytest.mark.slow
178168
@flaky(max_runs=3)
179-
@pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
180169
def test_tabs_preserve_name_after_move(ipyconsole, qtbot):
181170
"""Test that tabs preserve their names after they are moved."""
171+
shell = ipyconsole.get_current_shellwidget()
172+
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
173+
182174
# Create a new client
183175
ipyconsole.create_new_client()
184176

@@ -192,7 +184,6 @@ def test_tabs_preserve_name_after_move(ipyconsole, qtbot):
192184

193185
@pytest.mark.slow
194186
@flaky(max_runs=3)
195-
@pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
196187
def test_conf_env_vars(ipyconsole, qtbot):
197188
"""Test that kernels have env vars set by our kernel spec."""
198189
# Wait until the window is fully up
@@ -210,7 +201,6 @@ def test_conf_env_vars(ipyconsole, qtbot):
210201

211202
@pytest.mark.slow
212203
@flaky(max_runs=3)
213-
@pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
214204
@pytest.mark.no_stderr_file
215205
def test_no_stderr_file(ipyconsole, qtbot):
216206
"""Test that consoles can run without an stderr."""
@@ -230,7 +220,7 @@ def test_no_stderr_file(ipyconsole, qtbot):
230220
@pytest.mark.slow
231221
@pytest.mark.non_ascii_dir
232222
@flaky(max_runs=3)
233-
@pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
223+
@pytest.mark.skipif(os.name == 'nt', reason="It fails on Windows")
234224
def test_non_ascii_stderr_file(ipyconsole, qtbot):
235225
"""Test the creation of a console with a stderr file in a non-ascii dir."""
236226
# Wait until the window is fully up
@@ -248,7 +238,6 @@ def test_non_ascii_stderr_file(ipyconsole, qtbot):
248238

249239
@pytest.mark.slow
250240
@flaky(max_runs=3)
251-
@pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
252241
def test_console_import_namespace(ipyconsole, qtbot):
253242
"""Test an import of the form 'from foo import *'."""
254243
# Wait until the window is fully up
@@ -267,6 +256,9 @@ def test_console_import_namespace(ipyconsole, qtbot):
267256
@flaky(max_runs=3)
268257
def test_console_disambiguation(ipyconsole, qtbot):
269258
"""Test the disambiguation of dedicated consoles."""
259+
shell = ipyconsole.get_current_shellwidget()
260+
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
261+
270262
# Create directories and file for TEMP_DIRECTORY/a/b/c.py
271263
# and TEMP_DIRECTORY/a/d/c.py
272264
dir_b = osp.join(TEMP_DIRECTORY, 'a', 'b')
@@ -301,6 +293,9 @@ def test_console_disambiguation(ipyconsole, qtbot):
301293
@pytest.mark.slow
302294
@flaky(max_runs=3)
303295
def test_console_coloring(ipyconsole, qtbot):
296+
"""Test that console gets the same coloring present in the Editor."""
297+
shell = ipyconsole.get_current_shellwidget()
298+
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
304299

305300
config_options = ipyconsole.config_options()
306301

@@ -325,10 +320,8 @@ def test_console_coloring(ipyconsole, qtbot):
325320

326321
@pytest.mark.slow
327322
@flaky(max_runs=3)
328-
@pytest.mark.skipif(os.name == 'nt' or PYQT4,
329-
reason="It doesn't work on Windows and segfaults in PyQt4")
330323
def test_get_env(ipyconsole, qtbot):
331-
"""Test that showing env var contents is working as expected."""
324+
"""Test that getting env vars from the kernel is working as expected."""
332325
shell = ipyconsole.get_current_shellwidget()
333326
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
334327

@@ -337,55 +330,43 @@ def test_get_env(ipyconsole, qtbot):
337330
shell.execute("import os; os.environ['FOO'] = 'bar'" )
338331

339332
# Ask for os.environ contents
340-
with qtbot.waitSignal(shell.sig_show_env):
333+
with qtbot.waitSignal(shell.sig_show_env) as blocker:
341334
shell.get_env()
342335

343-
# Get env contents from the generated widget
344-
top_level_widgets = QApplication.topLevelWidgets()
345-
for w in top_level_widgets:
346-
if isinstance(w, CollectionsEditor):
347-
env_contents = w.get_value()
348-
qtbot.keyClick(w, Qt.Key_Enter)
336+
# Get env contents from the signal
337+
env_contents = blocker.args[0]
349338

350339
# Assert that our added entry is part of os.environ
351-
env_contents = listdict2envdict(env_contents)
352340
assert env_contents['FOO'] == 'bar'
353341

354342

355343
@pytest.mark.slow
356344
@flaky(max_runs=3)
357-
@pytest.mark.skipif(os.name == 'nt' or PYQT4,
358-
reason="It doesn't work on Windows and segfaults in PyQt4")
359-
def test_get_syspath(ipyconsole, qtbot):
360-
"""Test that showing sys.path contents is working as expected."""
345+
@pytest.mark.skipif(os.name == 'nt',
346+
reason="Fails due to differences in path handling")
347+
def test_get_syspath(ipyconsole, qtbot, tmpdir):
348+
"""
349+
Test that getting sys.path contents from the kernel is working as
350+
expected.
351+
"""
361352
shell = ipyconsole.get_current_shellwidget()
362353
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
363354

364355
# Add a new entry to sys.path
365356
with qtbot.waitSignal(shell.executed):
366-
tmp_dir = tempfile.mkdtemp()
367-
shell.execute("import sys, tempfile; sys.path.append('%s')" % tmp_dir)
357+
tmp_dir = to_text_string(tmpdir)
358+
shell.execute("import sys; sys.path.append('%s')" % tmp_dir)
368359

369360
# Ask for sys.path contents
370-
with qtbot.waitSignal(shell.sig_show_syspath):
361+
with qtbot.waitSignal(shell.sig_show_syspath) as blocker:
371362
shell.get_syspath()
372363

373-
# Get sys.path contents from the generated widget
374-
top_level_widgets = QApplication.topLevelWidgets()
375-
for w in top_level_widgets:
376-
if isinstance(w, CollectionsEditor):
377-
syspath_contents = w.get_value()
378-
qtbot.keyClick(w, Qt.Key_Enter)
364+
# Get sys.path contents from the signal
365+
syspath_contents = blocker.args[0]
379366

380367
# Assert that our added entry is part of sys.path
381368
assert tmp_dir in syspath_contents
382369

383-
# Remove temporary directory
384-
try:
385-
os.rmdir(tmp_dir)
386-
except:
387-
pass
388-
389370

390371
@pytest.mark.slow
391372
@flaky(max_runs=10)
@@ -423,8 +404,7 @@ def test_browse_history_dbg(ipyconsole, qtbot):
423404

424405
@pytest.mark.slow
425406
@flaky(max_runs=3)
426-
@pytest.mark.skipif(os.name == 'nt' or PY2,
427-
reason="It times out sometimes on Windows and doesn't work on PY2")
407+
@pytest.mark.skipif(PY2, reason="It doesn't work on PY2")
428408
def test_unicode_vars(ipyconsole, qtbot):
429409
"""
430410
Test that the Variable Explorer Works with unicode variables.
@@ -546,7 +526,6 @@ def test_plot_magic_dbg(ipyconsole, qtbot):
546526

547527
@pytest.mark.slow
548528
@flaky(max_runs=3)
549-
@pytest.mark.skipif(os.name == 'nt', reason="It times out on Windows")
550529
def test_run_doctest(ipyconsole, qtbot):
551530
"""
552531
Test that doctests can be run without problems
@@ -684,8 +663,6 @@ def test_clear_and_reset_magics_dbg(ipyconsole, qtbot):
684663

685664
@pytest.mark.slow
686665
@flaky(max_runs=3)
687-
@pytest.mark.skipif(os.name == 'nt' or PYQT4,
688-
reason="It doesn't work on Windows and segfaults in PyQt4")
689666
def test_restart_kernel(ipyconsole, qtbot):
690667
"""
691668
Test that kernel is restarted correctly
@@ -700,17 +677,15 @@ def test_restart_kernel(ipyconsole, qtbot):
700677

701678
# Restart kernel and wait until it's up again
702679
shell._prompt_html = None
703-
QTimer.singleShot(1000, lambda: close_message_box(qtbot))
704680
client.restart_kernel()
705681
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
706682

683+
assert 'Restarting kernel...' in shell._control.toPlainText()
707684
assert not shell.is_defined('a')
708685

709686

710687
@pytest.mark.slow
711688
@flaky(max_runs=3)
712-
@pytest.mark.skipif(os.name == 'nt' or PYQT4,
713-
reason="It doesn't work on Windows and segfaults in PyQt4")
714689
def test_load_kernel_file_from_id(ipyconsole, qtbot):
715690
"""
716691
Test that a new client is created using its id
@@ -722,9 +697,7 @@ def test_load_kernel_file_from_id(ipyconsole, qtbot):
722697
connection_file = osp.basename(client.connection_file)
723698
id_ = connection_file.split('kernel-')[-1].split('.json')[0]
724699

725-
QTimer.singleShot(2000, lambda: open_client_from_connection_info(
726-
id_, qtbot))
727-
ipyconsole.create_client_for_kernel()
700+
ipyconsole._create_client_for_kernel(id_, None, None, None)
728701
qtbot.wait(1000)
729702

730703
new_client = ipyconsole.get_clients()[1]
@@ -733,9 +706,7 @@ def test_load_kernel_file_from_id(ipyconsole, qtbot):
733706

734707
@pytest.mark.slow
735708
@flaky(max_runs=3)
736-
@pytest.mark.skipif(os.name == 'nt' or PYQT4,
737-
reason="It segfaults frequently")
738-
def test_load_kernel_file_from_location(ipyconsole, qtbot):
709+
def test_load_kernel_file_from_location(ipyconsole, qtbot, tmpdir):
739710
"""
740711
Test that a new client is created using a connection file
741712
placed in a different location from jupyter_runtime_dir
@@ -744,24 +715,19 @@ def test_load_kernel_file_from_location(ipyconsole, qtbot):
744715
client = ipyconsole.get_current_client()
745716
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
746717

747-
connection_file = osp.join(tempfile.gettempdir(),
748-
osp.basename(client.connection_file))
718+
fname = osp.basename(client.connection_file)
719+
connection_file = to_text_string(tmpdir.join(fname))
749720
shutil.copy2(client.connection_file, connection_file)
750721

751-
QTimer.singleShot(2000, lambda: open_client_from_connection_info(
752-
connection_file,
753-
qtbot))
754-
ipyconsole.create_client_for_kernel()
722+
ipyconsole._create_client_for_kernel(connection_file, None, None, None)
755723
qtbot.wait(1000)
756724

757725
assert len(ipyconsole.get_clients()) == 2
758726

759727

760728
@pytest.mark.slow
761729
@flaky(max_runs=3)
762-
@pytest.mark.skipif(os.name == 'nt' or PYQT4,
763-
reason="It segfaults frequently")
764-
def test_load_kernel_file(ipyconsole, qtbot):
730+
def test_load_kernel_file(ipyconsole, qtbot, tmpdir):
765731
"""
766732
Test that a new client is created using the connection file
767733
of an existing client
@@ -770,10 +736,7 @@ def test_load_kernel_file(ipyconsole, qtbot):
770736
client = ipyconsole.get_current_client()
771737
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
772738

773-
QTimer.singleShot(2000, lambda: open_client_from_connection_info(
774-
client.connection_file,
775-
qtbot))
776-
ipyconsole.create_client_for_kernel()
739+
ipyconsole._create_client_for_kernel(client.connection_file, None, None, None)
777740
qtbot.wait(1000)
778741

779742
new_client = ipyconsole.get_clients()[1]
@@ -787,7 +750,6 @@ def test_load_kernel_file(ipyconsole, qtbot):
787750

788751
@pytest.mark.slow
789752
@flaky(max_runs=3)
790-
@pytest.mark.skipif(os.name == 'nt', reason="It times out on Windows")
791753
def test_sys_argv_clear(ipyconsole, qtbot):
792754
"""Test that sys.argv is cleared up correctly"""
793755
shell = ipyconsole.get_current_shellwidget()

Diff for: spyder/widgets/ipythonconsole/client.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
QToolButton, QVBoxLayout, QWidget)
2828

2929
# Local imports
30-
from spyder.config.base import _, get_image_path, get_module_source_path
30+
from spyder.config.base import (_, get_image_path, get_module_source_path,
31+
running_under_pytest)
3132
from spyder.config.gui import get_font, get_shortcut
3233
from spyder.utils import icon_manager as ima
3334
from spyder.utils import sourcecode
3435
from spyder.utils.encoding import get_coding
3536
from spyder.utils.environ import RemoteEnvDialog
36-
from spyder.utils.ipython.style import create_qss_style
3737
from spyder.utils.programs import TEMPDIR
3838
from spyder.utils.qthelpers import (add_actions, create_action,
3939
create_toolbutton, DialogManager,
@@ -460,12 +460,15 @@ def restart_kernel(self):
460460
"""
461461
sw = self.shellwidget
462462

463-
message = _('Are you sure you want to restart the kernel?')
464-
buttons = QMessageBox.Yes | QMessageBox.No
465-
result = QMessageBox.question(self, _('Restart kernel?'),
466-
message, buttons)
463+
if not running_under_pytest():
464+
message = _('Are you sure you want to restart the kernel?')
465+
buttons = QMessageBox.Yes | QMessageBox.No
466+
result = QMessageBox.question(self, _('Restart kernel?'),
467+
message, buttons)
468+
else:
469+
result = None
467470

468-
if result == QMessageBox.Yes:
471+
if result == QMessageBox.Yes or running_under_pytest():
469472
if sw.kernel_manager:
470473
if self.infowidget.isVisible():
471474
self.infowidget.hide()

0 commit comments

Comments
 (0)