Skip to content

Commit b06e27a

Browse files
committed
Add tests for .cd dot command
1 parent 5326608 commit b06e27a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

awsshell/app.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,21 @@ class InputInterrupt(Exception):
4646

4747

4848
class ChangeDirHandler(object):
49+
def __init__(self, output=sys.stdout, err=sys.stderr, chdir=os.chdir):
50+
self._output = output
51+
self._err = err
52+
self._chdir = chdir
53+
4954
def run(self, command, application):
5055
# command is a list of parsed commands
5156
if len(command) != 2:
52-
sys.stderr.write("invalid syntax, must be: .cd dirname\n")
57+
self._err.write("invalid syntax, must be: .cd dirname\n")
5358
return
5459
dirname = os.path.expandvars(os.path.expanduser(command[1]))
5560
try:
56-
os.chdir(dirname)
61+
self._chdir(dirname)
5762
except OSError as e:
58-
sys.stderr.write("cd: %s\n" % e)
63+
self._err.write("cd: %s\n" % e)
5964

6065

6166
class EditHandler(object):

tests/unit/test_app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,26 @@ def test_prints_error_message_on_unknown_dot_command(errstream):
4242
handler = app.DotCommandHandler(err=errstream)
4343
handler.handle_cmd(".unknown foo bar", None)
4444
assert errstream.getvalue() == "Unknown dot command: .unknown\n"
45+
46+
47+
def test_cd_handler_can_chdir():
48+
chdir = mock.Mock()
49+
handler = app.ChangeDirHandler(chdir=chdir)
50+
handler.run(['.cd', 'foo/bar'], None)
51+
assert chdir.call_args == mock.call('foo/bar')
52+
53+
54+
def test_chdir_syntax_error_prints_err_msg(errstream):
55+
chdir = mock.Mock()
56+
handler = app.ChangeDirHandler(err=errstream, chdir=chdir)
57+
handler.run(['.cd'], None)
58+
assert 'invalid syntax' in errstream.getvalue()
59+
assert not chdir.called
60+
61+
62+
def test_error_displayed_when_chdir_fails(errstream):
63+
chdir = mock.Mock()
64+
chdir.side_effect = OSError("FAILED")
65+
handler = app.ChangeDirHandler(err=errstream, chdir=chdir)
66+
handler.run(['.cd', 'foo'], None)
67+
assert 'FAILED' in errstream.getvalue()

0 commit comments

Comments
 (0)