Skip to content

Commit

Permalink
Add tests for .cd dot command
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesls committed Dec 30, 2015
1 parent 5326608 commit b06e27a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
11 changes: 8 additions & 3 deletions awsshell/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
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()

0 comments on commit b06e27a

Please sign in to comment.