Skip to content

Commit bb37645

Browse files
authored
Merge pull request #628 from python-cmd2/simplify_edit
Removed os.system in favor of do_shell
2 parents 086d4db + befeaaf commit bb37645

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
## 0.9.9 (TBD, 2019)
1+
## 0.9.9 (February 21, 2019)
22
* Bug Fixes
33
* Fixed bug where the ``set`` command was not tab completing from the current ``settable`` dictionary.
4+
* Enhancements
5+
* Changed edit command to use do_shell() instead of calling os.system()
46

57
## 0.9.8 (February 06, 2019)
68
* Bug Fixes

cmd2/cmd2.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,7 +3248,7 @@ def do_history(self, args: argparse.Namespace) -> None:
32483248
for command in history:
32493249
fobj.write('{}\n'.format(command))
32503250
try:
3251-
os.system('"{}" "{}"'.format(self.editor, fname))
3251+
self.do_edit(fname)
32523252
self.do_load(fname)
32533253
except Exception:
32543254
raise
@@ -3356,12 +3356,11 @@ def do_edit(self, args: argparse.Namespace) -> None:
33563356
if not self.editor:
33573357
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
33583358

3359-
editor = utils.quote_string_if_needed(self.editor)
3359+
command = utils.quote_string_if_needed(self.editor)
33603360
if args.file_path:
3361-
expanded_path = utils.quote_string_if_needed(os.path.expanduser(args.file_path))
3362-
os.system('{} {}'.format(editor, expanded_path))
3363-
else:
3364-
os.system('{}'.format(editor))
3361+
command += " " + utils.quote_string_if_needed(args.file_path)
3362+
3363+
self.do_shell(command)
33653364

33663365
@property
33673366
def _current_script_dir(self) -> Optional[str]:

tests/test_cmd2.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,15 @@ def test_history_edit(base_app, monkeypatch):
459459
# going to call it due to the mock
460460
base_app.editor = 'fooedit'
461461

462-
# Mock out the os.system call so we don't actually open an editor
463-
m = mock.MagicMock(name='system')
464-
monkeypatch.setattr("os.system", m)
462+
# Mock out the subprocess.Popen call so we don't actually open an editor
463+
m = mock.MagicMock(name='Popen')
464+
monkeypatch.setattr("subprocess.Popen", m)
465465

466466
# Run help command just so we have a command in history
467467
run_cmd(base_app, 'help')
468468
run_cmd(base_app, 'history -e 1')
469469

470-
# We have an editor, so should expect a system call
470+
# We have an editor, so should expect a Popen call
471471
m.assert_called_once()
472472

473473
def test_history_run_all_commands(base_app):
@@ -883,46 +883,44 @@ def test_edit_file(base_app, request, monkeypatch):
883883
base_app.editor = 'fooedit'
884884

885885
# Mock out the os.system call so we don't actually open an editor
886-
m = mock.MagicMock(name='system')
887-
monkeypatch.setattr("os.system", m)
886+
m = mock.MagicMock(name='Popen')
887+
monkeypatch.setattr("subprocess.Popen", m)
888888

889889
test_dir = os.path.dirname(request.module.__file__)
890890
filename = os.path.join(test_dir, 'script.txt')
891891

892892
run_cmd(base_app, 'edit {}'.format(filename))
893893

894-
# We think we have an editor, so should expect a system call
895-
m.assert_called_once_with('{} {}'.format(utils.quote_string_if_needed(base_app.editor),
896-
utils.quote_string_if_needed(filename)))
894+
# We think we have an editor, so should expect a Popen call
895+
m.assert_called_once()
897896

898897
def test_edit_file_with_spaces(base_app, request, monkeypatch):
899898
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
900899
base_app.editor = 'fooedit'
901900

902-
# Mock out the os.system call so we don't actually open an editor
903-
m = mock.MagicMock(name='system')
904-
monkeypatch.setattr("os.system", m)
901+
# Mock out the subprocess.Popen call so we don't actually open an editor
902+
m = mock.MagicMock(name='Popen')
903+
monkeypatch.setattr("subprocess.Popen", m)
905904

906905
test_dir = os.path.dirname(request.module.__file__)
907906
filename = os.path.join(test_dir, 'my commands.txt')
908907

909908
run_cmd(base_app, 'edit "{}"'.format(filename))
910909

911-
# We think we have an editor, so should expect a system call
912-
m.assert_called_once_with('{} {}'.format(utils.quote_string_if_needed(base_app.editor),
913-
utils.quote_string_if_needed(filename)))
910+
# We think we have an editor, so should expect a Popen call
911+
m.assert_called_once()
914912

915913
def test_edit_blank(base_app, monkeypatch):
916914
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
917915
base_app.editor = 'fooedit'
918916

919-
# Mock out the os.system call so we don't actually open an editor
920-
m = mock.MagicMock(name='system')
921-
monkeypatch.setattr("os.system", m)
917+
# Mock out the subprocess.Popen call so we don't actually open an editor
918+
m = mock.MagicMock(name='Popen')
919+
monkeypatch.setattr("subprocess.Popen", m)
922920

923921
run_cmd(base_app, 'edit')
924922

925-
# We have an editor, so should expect a system call
923+
# We have an editor, so should expect a Popen call
926924
m.assert_called_once()
927925

928926

0 commit comments

Comments
 (0)