diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2020-11-17 21:48:50 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-17 21:48:50 -0500 |
commit | 31c2d8fcdad3b4f8be96ea604622e5b47d031a56 (patch) | |
tree | f01a944b92839319f4a75ad534ffe4ab03da9958 /tests/test_utils.py | |
parent | baf0392007659d069a7fed543335ac5e0e937556 (diff) | |
parent | 6c5eea6e8eb4ed6dbd3deaeba5e1ff86161553ee (diff) | |
download | cmd2-git-31c2d8fcdad3b4f8be96ea604622e5b47d031a56.tar.gz |
Merge pull request #1018 from gmmephisto/fix-find-editor
feat(utils): probe editors in system path
Diffstat (limited to 'tests/test_utils.py')
-rw-r--r-- | tests/test_utils.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py index 2c94466c..ab3647e4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -3,12 +3,18 @@ """ Unit testing for cmd2/utils.py module. """ +import os import signal import sys import time import pytest +try: + import mock +except ImportError: + from unittest import mock + import cmd2.utils as cu from cmd2.constants import HORIZONTAL_ELLIPSIS @@ -634,3 +640,56 @@ def test_str_to_bool_invalid(): def test_str_to_bool_bad_input(): with pytest.raises(ValueError): cu.str_to_bool(1) + +@mock.patch('cmd2.utils.probe_editors') +def test_find_editor_specified(mock_probe_editors): + expected_editor = 'vim' + with mock.patch.dict(os.environ, {'EDITOR': expected_editor}): + editor = cu.find_editor() + assert editor == expected_editor + mock_probe_editors.assert_not_called() + +@pytest.mark.skipif(sys.platform.startswith('win'), + reason="test 'find_editor' unix codepath") +def test_find_editor_not_specified_unix(): + expected_editor = 'vim' + with mock.patch.dict(os.environ, {'EDITOR': ''}): + with mock.patch( + 'cmd2.utils.probe_editors', + return_value=expected_editor + ) as mock_probe_editors: + editor = cu.find_editor() + assert editor == expected_editor + mock_probe_editors.assert_called_once() + +@pytest.mark.skipif(not sys.platform.startswith('win'), + reason="test 'find_editor' win codepath") +def test_find_editor_not_specified_win(): + expected_editor = 'notepad' + with mock.patch.dict(os.environ, {'EDITOR': ''}): + with mock.patch('cmd2.utils.probe_editors') as mock_probe_editors: + editor = cu.find_editor() + assert editor == expected_editor + mock_probe_editors.assert_not_called() + +@pytest.mark.skipif(sys.platform.startswith('win'), + reason="test 'probe_editors' codepath") +def test_probe_editors(tmpdir): + path = tmpdir.mkdir('bin') + vi_path = str(path.join('vi')) + with mock.patch.dict(os.environ, {'PATH': str(path)}): + editor = cu.probe_editors() + assert not editor + + def mock_is_executable(p): + print(p, vi_path) + if p == vi_path: + return True + + with mock.patch.dict(os.environ, {'PATH': str(path)}): + with mock.patch( + 'cmd2.utils.is_executable', + mock_is_executable + ): + editor = cu.probe_editors() + assert editor == vi_path |