Skip to content

Commit 1c6f9b7

Browse files
committed
Fail gracefully when we can't lauch editor in .edit
Fixes awslabs#88.
1 parent bfdd312 commit 1c6f9b7

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

awsshell/app.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ def run(self, command, application):
6565

6666

6767
class EditHandler(object):
68-
def __init__(self, popen_cls=None, env=None):
68+
def __init__(self, popen_cls=None, env=None, err=sys.stderr):
6969
if popen_cls is None:
7070
popen_cls = subprocess.Popen
7171
self._popen_cls = popen_cls
7272
if env is None:
7373
env = os.environ
7474
self._env = env
75+
self._err = err
7576

7677
def _get_editor_command(self):
7778
if 'EDITOR' in self._env:
@@ -97,8 +98,14 @@ def run(self, command, application):
9798
f.write(all_commands)
9899
f.flush()
99100
editor = self._get_editor_command()
100-
p = self._popen_cls([editor, f.name])
101-
p.communicate()
101+
try:
102+
p = self._popen_cls([editor, f.name])
103+
p.communicate()
104+
except OSError:
105+
self._err.write("Unable to launch editor: %s\n"
106+
"You can configure which editor to use by "
107+
"exporting the EDITOR environment variable.\n"
108+
% editor)
102109

103110

104111
class ProfileHandler(object):

tests/unit/test_app.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ def test_edit_handler():
3939
assert command_run[0] == 'my-editor'
4040

4141

42+
def test_error_msg_printed_on_error_handler(errstream):
43+
env = {'EDITOR': 'my-editor'}
44+
popen_cls = mock.Mock()
45+
popen_cls.side_effect = OSError()
46+
context = mock.Mock()
47+
context.history = []
48+
handler = app.EditHandler(popen_cls, env, errstream)
49+
handler.run(['.edit'], context)
50+
51+
# Then we should not propagate an exception, and we
52+
# should print a helpful error message.
53+
assert 'Unable to launch editor: my-editor' in errstream.getvalue()
54+
55+
4256
def test_profile_handler_prints_profile():
4357
shell = mock.Mock(spec=app.AWSShell)
4458
shell.profile = 'myprofile'

0 commit comments

Comments
 (0)