Skip to content

Commit 8adf480

Browse files
committed
Merge branch 'cd-dot-command'
* 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 awslabs#94.
2 parents 65d4917 + 4e84552 commit 8adf480

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

README.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,19 @@ will be printed.
261261
Current shell profile: demo
262262

263263

264+
.cd
265+
~~~
266+
267+
You can change the current working directory of the aws-shell by using
268+
the ``.cd`` command::
269+
270+
aws> !pwd
271+
/usr
272+
aws> .cd /tmp
273+
aws> !pwd
274+
/tmp
275+
276+
264277
Executing Shell Commands
265278
------------------------
266279

awsshell/app.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ class InputInterrupt(Exception):
4545
pass
4646

4747

48+
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+
54+
def run(self, command, application):
55+
# command is a list of parsed commands
56+
if len(command) != 2:
57+
self._err.write("invalid syntax, must be: .cd dirname\n")
58+
return
59+
dirname = os.path.expandvars(os.path.expanduser(command[1]))
60+
try:
61+
self._chdir(dirname)
62+
except OSError as e:
63+
self._err.write("cd: %s\n" % e)
64+
65+
4866
class EditHandler(object):
4967
def __init__(self, popen_cls=None, env=None):
5068
if popen_cls is None:
@@ -121,6 +139,7 @@ class DotCommandHandler(object):
121139
HANDLER_CLASSES = {
122140
'edit': EditHandler,
123141
'profile': ProfileHandler,
142+
'cd': ChangeDirHandler,
124143
}
125144

126145
def __init__(self, output=sys.stdout, err=sys.stderr):

tests/unit/test_app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,26 @@ def test_delegates_to_complete_changing_profile():
9393
shell.profile = 'mynewprofile'
9494
assert completer.change_profile.call_args == mock.call('mynewprofile')
9595
assert shell.profile == 'mynewprofile'
96+
97+
98+
def test_cd_handler_can_chdir():
99+
chdir = mock.Mock()
100+
handler = app.ChangeDirHandler(chdir=chdir)
101+
handler.run(['.cd', 'foo/bar'], None)
102+
assert chdir.call_args == mock.call('foo/bar')
103+
104+
105+
def test_chdir_syntax_error_prints_err_msg(errstream):
106+
chdir = mock.Mock()
107+
handler = app.ChangeDirHandler(err=errstream, chdir=chdir)
108+
handler.run(['.cd'], None)
109+
assert 'invalid syntax' in errstream.getvalue()
110+
assert not chdir.called
111+
112+
113+
def test_error_displayed_when_chdir_fails(errstream):
114+
chdir = mock.Mock()
115+
chdir.side_effect = OSError("FAILED")
116+
handler = app.ChangeDirHandler(err=errstream, chdir=chdir)
117+
handler.run(['.cd', 'foo'], None)
118+
assert 'FAILED' in errstream.getvalue()

0 commit comments

Comments
 (0)