diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py index 38571f44cd5..6c4f2ae9d76 100644 --- a/spyder/app/tests/test_mainwindow.py +++ b/spyder/app/tests/test_mainwindow.py @@ -28,7 +28,7 @@ from spyder.config.base import get_home_dir from spyder.config.main import CONF from spyder.plugins.runconfig import RunConfiguration -from spyder.py3compat import PY2 +from spyder.py3compat import PY2, to_text_string from spyder.utils.ipython.kernelspec import SpyderKernelSpec from spyder.utils.programs import is_module_installed from spyder.utils.test import close_save_message_box @@ -346,47 +346,59 @@ def test_change_types_in_varexp(main_window, qtbot): @flaky(max_runs=3) -def test_change_cwd_ipython_console(main_window, qtbot, tmpdir): +@pytest.mark.parametrize("test_directory", [u"non_ascii_ñ_í_ç", u"test_dir"]) +def test_change_cwd_ipython_console(main_window, qtbot, tmpdir, test_directory): """ Test synchronization with working directory and File Explorer when changing cwd in the IPython console. """ - # Wait until the window is fully up + wdir = main_window.workingdirectory + treewidget = main_window.explorer.treewidget shell = main_window.ipyconsole.get_current_shellwidget() + + # Wait until the window is fully up qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT) - # Change directory in ipython console using %cd - temp_dir = str(tmpdir.mkdir("test_dir")) + # Create temp dir + temp_dir = to_text_string(tmpdir.mkdir(test_directory)) + + # Change directory in IPython console using %cd with qtbot.waitSignal(shell.executed): - shell.execute("%cd {}".format(temp_dir)) + shell.execute(u"%cd {}".format(temp_dir)) qtbot.wait(1000) - # assert that cwd changed in workingdirectory - assert osp.normpath(main_window.workingdirectory.history[-1]) == osp.normpath(temp_dir) + # Assert that cwd changed in workingdirectory + assert osp.normpath(wdir.history[-1]) == osp.normpath(temp_dir) - # assert that cwd changed in explorer - assert osp.normpath(main_window.explorer.treewidget.get_current_folder()) == osp.normpath(temp_dir) + # Assert that cwd changed in explorer + assert osp.normpath(treewidget.get_current_folder()) == osp.normpath(temp_dir) @flaky(max_runs=3) -def test_change_cwd_explorer(main_window, qtbot, tmpdir): +@pytest.mark.parametrize("test_directory", [u"non_ascii_ñ_í_ç", u"test_dir"]) +def test_change_cwd_explorer(main_window, qtbot, tmpdir, test_directory): """ Test synchronization with working directory and IPython console when changing directories in the File Explorer. """ - # Wait until the window is fully up + wdir = main_window.workingdirectory + explorer = main_window.explorer shell = main_window.ipyconsole.get_current_shellwidget() + + # Wait until the window is fully up qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT) + # Create temp directory + temp_dir = to_text_string(tmpdir.mkdir(test_directory)) + # Change directory in the explorer widget - temp_dir = str(tmpdir.mkdir("test_dir")) - main_window.explorer.chdir(temp_dir) + explorer.chdir(temp_dir) qtbot.wait(1000) - # assert that cwd changed in workingdirectory - assert osp.normpath(main_window.workingdirectory.history[-1]) == osp.normpath(temp_dir) + # Assert that cwd changed in workingdirectory + assert osp.normpath(wdir.history[-1]) == osp.normpath(temp_dir) - # assert that cwd changed in ipythonconsole + # Assert that cwd changed in IPython console assert osp.normpath(temp_dir) == osp.normpath(shell._cwd) diff --git a/spyder/plugins/ipythonconsole.py b/spyder/plugins/ipythonconsole.py index 9551e4d795b..f4e43518b35 100644 --- a/spyder/plugins/ipythonconsole.py +++ b/spyder/plugins/ipythonconsole.py @@ -883,7 +883,6 @@ def set_current_client_working_directory(self, directory): """Set current client working directory.""" shellwidget = self.get_current_shellwidget() if shellwidget is not None: - directory = encoding.to_unicode_from_fs(directory) shellwidget.set_cwd(directory) def set_working_directory(self, dirname): diff --git a/spyder/plugins/workingdirectory.py b/spyder/plugins/workingdirectory.py index c2d7ea99a50..94f3e3612b4 100644 --- a/spyder/plugins/workingdirectory.py +++ b/spyder/plugins/workingdirectory.py @@ -283,14 +283,11 @@ def chdir(self, directory, browsing_history=False, self.histindex = len(self.history)-1 # Changing working directory - os.chdir( to_text_string(directory) ) + os.chdir(directory) self.refresh_plugin() + if refresh_explorer: self.set_explorer_cwd.emit(directory) if refresh_console: - self.set_as_current_console_wd() + self.set_current_console_wd.emit(directory) self.refresh_findinfiles.emit() - - def set_as_current_console_wd(self): - """Set as current console working directory""" - self.set_current_console_wd.emit(getcwd()) diff --git a/spyder/utils/ipython/spyder_kernel.py b/spyder/utils/ipython/spyder_kernel.py index dc94d85cc22..5ae109ee05b 100644 --- a/spyder/utils/ipython/spyder_kernel.py +++ b/spyder/utils/ipython/spyder_kernel.py @@ -241,7 +241,7 @@ def get_source(self, objtxt): # --- Additional methods def set_cwd(self, dirname): """Set current working directory.""" - return os.chdir(dirname) + os.chdir(dirname) def get_cwd(self): """Get current working directory.""" diff --git a/spyder/utils/tests/test_programs.py b/spyder/utils/tests/test_programs.py index 2b936d50d9a..93c36f4943e 100644 --- a/spyder/utils/tests/test_programs.py +++ b/spyder/utils/tests/test_programs.py @@ -18,7 +18,7 @@ if os.name == 'nt': - python_dir = os.environ['PYTHON'] + python_dir = os.environ['PYTHON'] if os.environ.get('CI', None) else '' VALID_INTERPRETER = os.path.join(python_dir, 'python.exe') INVALID_INTERPRETER = os.path.join(python_dir, 'Scripts', 'ipython.exe') else: diff --git a/spyder/widgets/ipythonconsole/shell.py b/spyder/widgets/ipythonconsole/shell.py index 619697e8f64..a50332ca105 100644 --- a/spyder/widgets/ipythonconsole/shell.py +++ b/spyder/widgets/ipythonconsole/shell.py @@ -9,13 +9,15 @@ """ import ast +import os import uuid from qtpy.QtCore import Signal from qtpy.QtWidgets import QMessageBox from spyder.config.base import _ from spyder.config.gui import config_shortcut -from spyder.py3compat import to_text_string +from spyder.py3compat import PY2, to_text_string +from spyder.utils import encoding from spyder.utils import programs from spyder.utils import syntaxhighlighters as sh from spyder.utils.ipython.style import create_qss_style, create_style_class @@ -98,7 +100,11 @@ def is_spyder_kernel(self): def set_cwd(self, dirname): """Set shell current working directory.""" - code = u"get_ipython().kernel.set_cwd(r'{}')".format(dirname) + # Replace single for double backslashes on Windows + if os.name == 'nt': + dirname = dirname.replace(u"\\", u"\\\\") + + code = u"get_ipython().kernel.set_cwd(u'{}')".format(dirname) if self._reading: self.kernel_client.input(u'!' + code) else: @@ -334,6 +340,8 @@ def handle_exec_method(self, msg): elif 'get_cwd' in method: if data is not None and 'text/plain' in data: self._cwd = ast.literal_eval(data['text/plain']) + if PY2: + self._cwd = encoding.to_unicode_from_fs(self._cwd) else: self._cwd = '' self.sig_change_cwd.emit(self._cwd)