Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 0.9.9 (TBD, 2019)
## 0.9.9 (February 21, 2019)
* Bug Fixes
* Fixed bug where the ``set`` command was not tab completing from the current ``settable`` dictionary.
* Enhancements
* Changed edit command to use do_shell() instead of calling os.system()

## 0.9.8 (February 06, 2019)
* Bug Fixes
Expand Down
11 changes: 5 additions & 6 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3248,7 +3248,7 @@ def do_history(self, args: argparse.Namespace) -> None:
for command in history:
fobj.write('{}\n'.format(command))
try:
os.system('"{}" "{}"'.format(self.editor, fname))
self.do_edit(fname)
self.do_load(fname)
except Exception:
raise
Expand Down Expand Up @@ -3356,12 +3356,11 @@ def do_edit(self, args: argparse.Namespace) -> None:
if not self.editor:
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")

editor = utils.quote_string_if_needed(self.editor)
command = utils.quote_string_if_needed(self.editor)
if args.file_path:
expanded_path = utils.quote_string_if_needed(os.path.expanduser(args.file_path))
os.system('{} {}'.format(editor, expanded_path))
else:
os.system('{}'.format(editor))
command += " " + utils.quote_string_if_needed(args.file_path)

self.do_shell(command)

@property
def _current_script_dir(self) -> Optional[str]:
Expand Down
36 changes: 17 additions & 19 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,15 @@ def test_history_edit(base_app, monkeypatch):
# going to call it due to the mock
base_app.editor = 'fooedit'

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

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

# We have an editor, so should expect a system call
# We have an editor, so should expect a Popen call
m.assert_called_once()

def test_history_run_all_commands(base_app):
Expand Down Expand Up @@ -883,46 +883,44 @@ def test_edit_file(base_app, request, monkeypatch):
base_app.editor = 'fooedit'

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

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

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

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

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

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

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

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

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

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

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

run_cmd(base_app, 'edit')

# We have an editor, so should expect a system call
# We have an editor, so should expect a Popen call
m.assert_called_once()


Expand Down