Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cd dot command #94

Merged
merged 3 commits into from
Dec 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------

Expand Down
19 changes: 19 additions & 0 deletions awsshell/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ class InputInterrupt(Exception):
pass


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:
self._err.write("invalid syntax, must be: .cd dirname\n")
return
dirname = os.path.expandvars(os.path.expanduser(command[1]))
try:
self._chdir(dirname)
except OSError as e:
self._err.write("cd: %s\n" % e)


class EditHandler(object):
def __init__(self, popen_cls=None, env=None):
if popen_cls is None:
Expand Down Expand Up @@ -85,6 +103,7 @@ def run(self, command, application):
class DotCommandHandler(object):
HANDLER_CLASSES = {
'edit': EditHandler,
'cd': ChangeDirHandler,
}

def __init__(self, output=sys.stdout, err=sys.stderr):
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()