From 5dc5c694712a63ed5cd0063c67a99b5c0838f2f9 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 19 Nov 2020 13:57:35 -0500 Subject: Updated utils.find_editor() to include more Windows editors --- tests/test_utils.py | 61 +++++++++++------------------------------------------ 1 file changed, 12 insertions(+), 49 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index ab3647e4..f4a082ae 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -641,55 +641,18 @@ 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' +def test_find_editor_specified(): + expected_editor = os.path.join('fake_dir', 'editor') 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 + +def test_find_editor_not_specified(): + # Use existing path env setting. Something in the editor list should be found. + editor = cu.find_editor() + assert editor + + # Overwrite path env setting with invalid path. No editor should be found. + with mock.patch.dict(os.environ, {'PATH': 'fake_dir'}): + editor = cu.find_editor() + assert editor is None -- cgit v1.2.1 From 20951e94a213eaec3a2f46f8089099256329d0e7 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Sat, 21 Nov 2020 14:44:15 -0500 Subject: Fix unit test that was failing when EDITOR environment variable was set --- tests/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index f4a082ae..5336ccfd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -652,7 +652,7 @@ def test_find_editor_not_specified(): editor = cu.find_editor() assert editor - # Overwrite path env setting with invalid path. No editor should be found. - with mock.patch.dict(os.environ, {'PATH': 'fake_dir'}): + # Overwrite path env setting with invalid path, clear all other env vars so no editor should be found. + with mock.patch.dict(os.environ, {'PATH': 'fake_dir'}, clear=True): editor = cu.find_editor() assert editor is None -- cgit v1.2.1