diff options
author | Mikhail Ushanov <gm.mephisto@gmail.com> | 2020-11-17 01:05:15 +0300 |
---|---|---|
committer | Mikhail Ushanov <gm.mephisto@gmail.com> | 2020-11-17 01:05:15 +0300 |
commit | 6c5eea6e8eb4ed6dbd3deaeba5e1ff86161553ee (patch) | |
tree | f01a944b92839319f4a75ad534ffe4ab03da9958 /tests | |
parent | baf0392007659d069a7fed543335ac5e0e937556 (diff) | |
download | cmd2-git-6c5eea6e8eb4ed6dbd3deaeba5e1ff86161553ee.tar.gz |
feat(utils): probe editors in system path
Signed-off-by: Mikhail Ushanov <gm.mephisto@gmail.com>
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test_cmd2.py | 18 | ||||
-rw-r--r-- | tests/test_utils.py | 59 |
2 files changed, 59 insertions, 18 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 554f2ba7..2f24f4d7 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -1275,24 +1275,6 @@ optional arguments: -s, --shout N00B EMULATION MODE """ -@pytest.mark.skipif(sys.platform.startswith('win'), - reason="utils.which function only used on Mac and Linux") -def test_which_editor_good(): - editor = cmd2.Cmd.DEFAULT_EDITOR - path = utils.which(editor) - - # Assert that the editor was found because some editor should exist on all Mac and Linux systems - assert path - -@pytest.mark.skipif(sys.platform.startswith('win'), - reason="utils.which function only used on Mac and Linux") -def test_which_editor_bad(): - nonexistent_editor = 'this_editor_does_not_exist.exe' - path = utils.which(nonexistent_editor) - # Assert that the non-existent editor wasn't found - assert path is None - - class MultilineApp(cmd2.Cmd): def __init__(self, *args, **kwargs): super().__init__(*args, multiline_commands=['orate'], **kwargs) 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 |