From cb15663c364867c7d475cb9cce9361406632755e Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Thu, 9 Jul 2020 18:34:09 -0500 Subject: [PATCH 1/4] Percent encode/decode cwd to encode spaces and special characters --- spyder_terminal/server/logic/term_manager.py | 4 ++++ spyder_terminal/server/web/main_handler.py | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/spyder_terminal/server/logic/term_manager.py b/spyder_terminal/server/logic/term_manager.py index 69988229..35954b4c 100644 --- a/spyder_terminal/server/logic/term_manager.py +++ b/spyder_terminal/server/logic/term_manager.py @@ -9,6 +9,7 @@ import tornado.gen import tornado.ioloop from terminado.management import TermManagerBase, PtyWithClients +from urllib.parse import unquote WINDOWS = os.name == 'nt' @@ -70,6 +71,9 @@ def client_disconnected(self, pid, socket): def create_term(self, rows, cols, cwd=None): """Create a new virtual terminal.""" pid = hashlib.md5(str(time.time()).encode('utf-8')).hexdigest()[0:6] + # We need to do percent decoding for reading the info through a cookie + # For further information see spyder-ide/spyder-terminal#225 + cwd = unquote(cwd) pty = self.new_terminal(cwd=cwd, height=rows, width=cols) pty.resize_to_smallest(rows, cols) self.consoles[pid] = pty diff --git a/spyder_terminal/server/web/main_handler.py b/spyder_terminal/server/web/main_handler.py index 31ebf03a..3da03aca 100644 --- a/spyder_terminal/server/web/main_handler.py +++ b/spyder_terminal/server/web/main_handler.py @@ -1,10 +1,10 @@ # -*- coding: iso-8859-15 -*- """Basic static index.html HTTP handler.""" - import tornado.web import tornado.escape from os import getcwd +from urllib.parse import quote class MainHandler(tornado.web.RequestHandler): @@ -18,7 +18,9 @@ def initialize(self, db=None): def get(self): """Get static index.html page.""" cwd = self.get_argument('path', getcwd()) - self.set_cookie('cwd', cwd) + # We need to do percent encoding for sending the info through a cookie + # For further information see spyder-ide/spyder-terminal#225 + self.set_cookie('cwd', quote(cwd)) self.render('../static/build/index.html') @tornado.gen.coroutine From 1d88681291ad5e829f5bea29769bd4217a9964d1 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Fri, 10 Jul 2020 11:15:22 -0500 Subject: [PATCH 2/4] add test for creating a new terminal in a cwd with special characters --- spyder_terminal/server/logic/term_manager.py | 2 +- spyder_terminal/server/web/main_handler.py | 2 +- spyder_terminal/tests/test_terminal.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/spyder_terminal/server/logic/term_manager.py b/spyder_terminal/server/logic/term_manager.py index 35954b4c..6e540886 100644 --- a/spyder_terminal/server/logic/term_manager.py +++ b/spyder_terminal/server/logic/term_manager.py @@ -71,7 +71,7 @@ def client_disconnected(self, pid, socket): def create_term(self, rows, cols, cwd=None): """Create a new virtual terminal.""" pid = hashlib.md5(str(time.time()).encode('utf-8')).hexdigest()[0:6] - # We need to do percent decoding for reading the info through a cookie + # We need to do percent decoding for reading the cwd through a cookie # For further information see spyder-ide/spyder-terminal#225 cwd = unquote(cwd) pty = self.new_terminal(cwd=cwd, height=rows, width=cols) diff --git a/spyder_terminal/server/web/main_handler.py b/spyder_terminal/server/web/main_handler.py index 3da03aca..a3e5278e 100644 --- a/spyder_terminal/server/web/main_handler.py +++ b/spyder_terminal/server/web/main_handler.py @@ -18,7 +18,7 @@ def initialize(self, db=None): def get(self): """Get static index.html page.""" cwd = self.get_argument('path', getcwd()) - # We need to do percent encoding for sending the info through a cookie + # We need to do percent encoding for sending the cwd through a cookie # For further information see spyder-ide/spyder-terminal#225 self.set_cookie('cwd', quote(cwd)) self.render('../static/build/index.html') diff --git a/spyder_terminal/tests/test_terminal.py b/spyder_terminal/tests/test_terminal.py index e92d8f65..04cb2176 100644 --- a/spyder_terminal/tests/test_terminal.py +++ b/spyder_terminal/tests/test_terminal.py @@ -198,6 +198,21 @@ def test_terminal_color(setup_terminal, qtbot_module): qtbot_module.waitUntil(lambda: check_hex_to_rgb(term), timeout=TERM_UP) +def test_terminal_cwd(setup_terminal, qtbot_module): + """Test if the a new terminal supports cwd with especial characters.""" + new_dir = osp.join(os.getcwd(), '"this is dir with spaces"') + os.mkdir(new_dir) + os.chdir(new_dir) + + terminal = setup_terminal + qtbot_module.waitUntil(lambda: terminal.server_is_ready(), timeout=TERM_UP) + qtbot_module.wait(1000) + + port = terminal.port + status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code + assert status_code == 200 + + def test_terminal_find(setup_terminal, qtbot_module): """Test the terminal find next/previous functions.""" terminal = setup_terminal From 328995bbc0c181cd9ff000b52ad21a995bfc77fb Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Fri, 10 Jul 2020 11:43:28 -0500 Subject: [PATCH 3/4] revert cwd after finishing the test --- spyder_terminal/tests/test_terminal.py | 35 +++++++++++++++----------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/spyder_terminal/tests/test_terminal.py b/spyder_terminal/tests/test_terminal.py index 04cb2176..18f30c65 100644 --- a/spyder_terminal/tests/test_terminal.py +++ b/spyder_terminal/tests/test_terminal.py @@ -198,21 +198,6 @@ def test_terminal_color(setup_terminal, qtbot_module): qtbot_module.waitUntil(lambda: check_hex_to_rgb(term), timeout=TERM_UP) -def test_terminal_cwd(setup_terminal, qtbot_module): - """Test if the a new terminal supports cwd with especial characters.""" - new_dir = osp.join(os.getcwd(), '"this is dir with spaces"') - os.mkdir(new_dir) - os.chdir(new_dir) - - terminal = setup_terminal - qtbot_module.waitUntil(lambda: terminal.server_is_ready(), timeout=TERM_UP) - qtbot_module.wait(1000) - - port = terminal.port - status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code - assert status_code == 200 - - def test_terminal_find(setup_terminal, qtbot_module): """Test the terminal find next/previous functions.""" terminal = setup_terminal @@ -372,3 +357,23 @@ def test_close_terminal_manually(setup_terminal, qtbot_module): timeout=TERM_UP) final_num = len(terminal.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.server_is_ready(), timeout=TERM_UP) + qtbot_module.wait(1000) + + port = terminal.port + status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code + assert status_code == 200 + + # Revert cwd + os.chdir(start_dir) From 7f97f48be14477f98165570196a8f5438e4fd53a Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Fri, 10 Jul 2020 12:15:20 -0500 Subject: [PATCH 4/4] fix path for Windows --- spyder_terminal/tests/test_terminal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyder_terminal/tests/test_terminal.py b/spyder_terminal/tests/test_terminal.py index 18f30c65..9a218f66 100644 --- a/spyder_terminal/tests/test_terminal.py +++ b/spyder_terminal/tests/test_terminal.py @@ -362,7 +362,7 @@ def test_close_terminal_manually(setup_terminal, qtbot_module): 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"') + 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)