Skip to content

Commit dc0ad81

Browse files
committed
Ensure editor and the file to edit can have spaces in them
1 parent f49d88a commit dc0ad81

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ def do_edit(self, arg):
16331633
f.write(history_item or '')
16341634
f.close()
16351635

1636-
os.system('%s %s' % (self.editor, filename))
1636+
os.system('"{}" "{}"'.format(self.editor, filename))
16371637

16381638
if self.autorun_on_edit or history_item:
16391639
self.do_load(filename)

examples/transcript_regex.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Run this transcript with "python example.py -t transcript_regex.txt"
2-
# The regex for editor matches any word until first space. The one for colors is because no color on Windows.
2+
# The regex for colors is because no color on Windows.
3+
# The regex for editor will match whatever program you use.
34
(Cmd) set
45
abbrev: True
56
autorun_on_edit: False
67
colors: /(True|False)/
78
continuation_prompt: >
89
debug: False
910
echo: False
10-
editor: /([^\s]+)/
11+
editor: /.*/
1112
feedback_to_output: True
1213
locals_in_py: True
1314
maxrepeats: 3

tests/test_cmd2.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,23 @@ def test_edit_file(base_app, request, monkeypatch):
736736
run_cmd(base_app, 'edit {}'.format(filename))
737737

738738
# We think we have an editor, so should expect a system call
739-
m.assert_called_once_with('{} {}'.format(base_app.editor, filename))
739+
m.assert_called_once_with('"{}" "{}"'.format(base_app.editor, filename))
740+
741+
def test_edit_file_with_spaces(base_app, request, monkeypatch):
742+
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
743+
base_app.editor = 'fooedit'
744+
745+
# Mock out the os.system call so we don't actually open an editor
746+
m = mock.MagicMock(name='system')
747+
monkeypatch.setattr("os.system", m)
748+
749+
test_dir = os.path.dirname(request.module.__file__)
750+
filename = os.path.join(test_dir, 'my commands.txt')
751+
752+
run_cmd(base_app, 'edit {}'.format(filename))
753+
754+
# We think we have an editor, so should expect a system call
755+
m.assert_called_once_with('"{}" "{}"'.format(base_app.editor, filename))
740756

741757
def test_edit_number(base_app, monkeypatch):
742758
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock

tests/transcript_regex.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Run this transcript with "python example.py -t transcript_regex.txt"
2-
# The regex for editor matches any word until first space. The one for colors is because no color on Windows.
2+
# The regex for colors is because no color on Windows.
3+
# The regex for editor will match whatever program you use.
34
(Cmd) set
45
abbrev: True
56
autorun_on_edit: False
67
colors: /(True|False)/
78
continuation_prompt: >
89
debug: False
910
echo: False
10-
editor: /([^\s]+)/
11+
editor: /.*/
1112
feedback_to_output: True
1213
locals_in_py: True
1314
maxrepeats: 3

0 commit comments

Comments
 (0)