From 5326608d5131be2ab069af8d935e3bb65f9b3edc Mon Sep 17 00:00:00 2001 From: James Saryerwinnie Date: Sat, 19 Dec 2015 10:25:49 -0800 Subject: [PATCH 1/3] Proof of concept for .cd command Proposed in #76. Still needs discussion for whether or not this should be a dot command. If we go with this approach the only thing left is tests. --- awsshell/app.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/awsshell/app.py b/awsshell/app.py index 218e2b0..220e2cb 100644 --- a/awsshell/app.py +++ b/awsshell/app.py @@ -45,6 +45,19 @@ class InputInterrupt(Exception): pass +class ChangeDirHandler(object): + def run(self, command, application): + # command is a list of parsed commands + if len(command) != 2: + sys.stderr.write("invalid syntax, must be: .cd dirname\n") + return + dirname = os.path.expandvars(os.path.expanduser(command[1])) + try: + os.chdir(dirname) + except OSError as e: + sys.stderr.write("cd: %s\n" % e) + + class EditHandler(object): def __init__(self, popen_cls=None, env=None): if popen_cls is None: @@ -85,6 +98,7 @@ def run(self, command, application): class DotCommandHandler(object): HANDLER_CLASSES = { 'edit': EditHandler, + 'cd': ChangeDirHandler, } def __init__(self, output=sys.stdout, err=sys.stderr): From b06e27ad95ebd2059bdc05b44ec8d2edbcf175b4 Mon Sep 17 00:00:00 2001 From: James Saryerwinnie Date: Tue, 29 Dec 2015 17:20:49 -0800 Subject: [PATCH 2/3] Add tests for .cd dot command --- awsshell/app.py | 11 ++++++++--- tests/unit/test_app.py | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/awsshell/app.py b/awsshell/app.py index 220e2cb..b8c29a7 100644 --- a/awsshell/app.py +++ b/awsshell/app.py @@ -46,16 +46,21 @@ class InputInterrupt(Exception): class ChangeDirHandler(object): + def __init__(self, output=sys.stdout, err=sys.stderr, chdir=os.chdir): + self._output = output + self._err = err + self._chdir = chdir + def run(self, command, application): # command is a list of parsed commands if len(command) != 2: - sys.stderr.write("invalid syntax, must be: .cd dirname\n") + self._err.write("invalid syntax, must be: .cd dirname\n") return dirname = os.path.expandvars(os.path.expanduser(command[1])) try: - os.chdir(dirname) + self._chdir(dirname) except OSError as e: - sys.stderr.write("cd: %s\n" % e) + self._err.write("cd: %s\n" % e) class EditHandler(object): diff --git a/tests/unit/test_app.py b/tests/unit/test_app.py index 9702545..f8397a8 100644 --- a/tests/unit/test_app.py +++ b/tests/unit/test_app.py @@ -42,3 +42,26 @@ def test_prints_error_message_on_unknown_dot_command(errstream): handler = app.DotCommandHandler(err=errstream) handler.handle_cmd(".unknown foo bar", None) assert errstream.getvalue() == "Unknown dot command: .unknown\n" + + +def test_cd_handler_can_chdir(): + chdir = mock.Mock() + handler = app.ChangeDirHandler(chdir=chdir) + handler.run(['.cd', 'foo/bar'], None) + assert chdir.call_args == mock.call('foo/bar') + + +def test_chdir_syntax_error_prints_err_msg(errstream): + chdir = mock.Mock() + handler = app.ChangeDirHandler(err=errstream, chdir=chdir) + handler.run(['.cd'], None) + assert 'invalid syntax' in errstream.getvalue() + assert not chdir.called + + +def test_error_displayed_when_chdir_fails(errstream): + chdir = mock.Mock() + chdir.side_effect = OSError("FAILED") + handler = app.ChangeDirHandler(err=errstream, chdir=chdir) + handler.run(['.cd', 'foo'], None) + assert 'FAILED' in errstream.getvalue() From 4e84552f367f4da647e10be05795b870c112e3bb Mon Sep 17 00:00:00 2001 From: James Saryerwinnie Date: Tue, 29 Dec 2015 17:44:02 -0800 Subject: [PATCH 3/3] Update README with docs for .cd command --- README.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.rst b/README.rst index 223e1f1..bfbecb0 100644 --- a/README.rst +++ b/README.rst @@ -219,6 +219,19 @@ variable before defaulting to ``notepad`` on Windows and aws> .edit +.cd +~~~ + +You can change the current working directory of the aws-shell by using +the ``.cd`` command:: + + aws> !pwd + /usr + aws> .cd /tmp + aws> !pwd + /tmp + + Executing Shell Commands ------------------------