Skip to content

Commit 62eb740

Browse files
authored
Merge pull request #158 from joguSD/memory-history
Don't append 'aws' twice when invoking the .edit command
2 parents d597b79 + b90312a commit 62eb740

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

awsshell/app.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ def _get_editor_command(self):
8080
else:
8181
return compat.default_editor()
8282

83+
def _generate_edit_history(self, application):
84+
history = list(application.history)
85+
commands = [h for h in history if not h.startswith(('.', '!'))]
86+
return '\n'.join(commands)
87+
8388
def run(self, command, application):
8489
"""Open application's history buffer in an editor.
8590
@@ -91,10 +96,8 @@ def run(self, command, application):
9196
:param application: The application object.
9297
9398
"""
94-
all_commands = '\n'.join(
95-
['aws ' + h for h in list(application.history)
96-
if not h.startswith(('.', '!'))])
9799
with temporary_file('w') as f:
100+
all_commands = self._generate_edit_history(application)
98101
f.write(all_commands)
99102
f.flush()
100103
editor = self._get_editor_command()
@@ -222,7 +225,7 @@ class AWSShell(object):
222225
"""
223226

224227
def __init__(self, completer, model_completer, docs,
225-
input=None, output=None):
228+
input=None, output=None, popen_cls=None):
226229
self.completer = completer
227230
self.model_completer = model_completer
228231
self.history = InMemoryHistory()
@@ -238,6 +241,10 @@ def __init__(self, completer, model_completer, docs,
238241
self._input = input
239242
self._output = output
240243

244+
if popen_cls is None:
245+
popen_cls = subprocess.Popen
246+
self._popen_cls = popen_cls
247+
241248
# These attrs come from the config file.
242249
self.config_obj = None
243250
self.config_section = None
@@ -309,7 +316,7 @@ def run(self):
309316
initial_document=Document(self.current_docs,
310317
cursor_position=0))
311318
self.cli.request_redraw()
312-
p = subprocess.Popen(full_cmd, shell=True, env=self._env)
319+
p = self._popen_cls(full_cmd, shell=True, env=self._env)
313320
p.communicate()
314321

315322
def stop_input_and_refresh_cli(self):

tests/unit/test_app.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,35 @@ def run(self, command, context):
2626
assert call_args == [(['.foo', 'a', 'b', 'c'], context)]
2727

2828

29+
class PopenLogger(object):
30+
def __call__(self, cmd):
31+
self.cmd = cmd
32+
filename = cmd[1]
33+
with open(filename, 'r') as f:
34+
self.contents = f.read()
35+
return mock.Mock()
36+
37+
2938
def test_edit_handler():
3039
env = {'EDITOR': 'my-editor'}
3140
popen_cls = mock.Mock()
32-
context = mock.Mock()
33-
context.history = []
34-
handler = app.EditHandler(popen_cls, env)
35-
handler.run(['.edit'], context)
41+
application = mock.Mock()
42+
application.history = [
43+
'!ls',
44+
'.edit',
45+
'aws ec2 describe-instances',
46+
'aws ec2 allocate-hosts',
47+
]
48+
popen = PopenLogger()
49+
handler = app.EditHandler(popen, env)
50+
handler.run(['.edit'], application)
3651
# Ensure our editor was called with some arbitrary temp filename.
37-
command_run = popen_cls.call_args[0][0]
52+
command_run = popen.cmd
3853
assert len(command_run) == 2
3954
assert command_run[0] == 'my-editor'
55+
# Ensure the contents of the temp file are correct
56+
expected_contents = 'aws ec2 describe-instances\naws ec2 allocate-hosts'
57+
assert popen.contents == expected_contents
4058

4159

4260
def test_error_msg_printed_on_error_handler(errstream):
@@ -132,6 +150,25 @@ def test_error_displayed_when_chdir_fails(errstream):
132150
assert 'FAILED' in errstream.getvalue()
133151

134152

153+
def test_history_stored_correctly():
154+
mock_prompter = mock.Mock()
155+
mock_prompter.buffers = {'clidocs': mock.Mock()}
156+
# Simulate the user entering various commands
157+
quit_document = mock.Mock()
158+
quit_document.text = '.quit'
159+
command_document = mock.Mock()
160+
command_document.text = 'ec2 describe-instances'
161+
mock_prompter.run.side_effect = [command_document, quit_document]
162+
shell = app.AWSShell(mock.Mock(), mock.Mock(), mock.Mock(),
163+
popen_cls=mock.Mock())
164+
shell.create_cli_interface = mock.Mock(return_value=mock_prompter)
165+
shell.run()
166+
167+
# two calls should have been made, history should have added aws
168+
assert mock_prompter.run.call_count == 2
169+
assert list(shell.history) == ['aws ec2 describe-instances']
170+
171+
135172
def test_exit_dot_command_exits_shell():
136173
mock_prompter = mock.Mock()
137174
# Simulate the user entering '.quit'

0 commit comments

Comments
 (0)