Skip to content

Commit

Permalink
Merge branch 'cd-dot-command'
Browse files Browse the repository at this point in the history
* cd-dot-command:
  Update README with docs for .cd command
  Add tests for .cd dot command
  Proof of concept for .cd command

Conflicts:
	awsshell/app.py
	tests/unit/test_app.py

Closes #94.
  • Loading branch information
jamesls committed Dec 30, 2015
2 parents 65d4917 + 4e84552 commit 8adf480
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,19 @@ will be printed.
Current shell profile: demo


.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 @@ -121,6 +139,7 @@ class DotCommandHandler(object):
HANDLER_CLASSES = {
'edit': EditHandler,
'profile': ProfileHandler,
'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 @@ -93,3 +93,26 @@ def test_delegates_to_complete_changing_profile():
shell.profile = 'mynewprofile'
assert completer.change_profile.call_args == mock.call('mynewprofile')
assert shell.profile == 'mynewprofile'


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()

0 comments on commit 8adf480

Please sign in to comment.