diff options
| author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-01-22 14:53:33 -0500 |
|---|---|---|
| committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-01-22 14:53:33 -0500 |
| commit | 45580056485328338899dd65607c6c57d3797f35 (patch) | |
| tree | 4cb6a10485b440e0b23eb638764fc2ff14faa974 /tests | |
| parent | 49fac04513727a67b0360a9e7560087c64dd557d (diff) | |
| parent | a3b1b6ddf81cdc0b253f15feeb167ff348afd14f (diff) | |
| download | cmd2-git-45580056485328338899dd65607c6c57d3797f35.tar.gz | |
Merge branch 'master' into 2.0
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/conftest.py | 25 | ||||
| -rw-r--r-- | tests/test_argparse.py | 9 | ||||
| -rw-r--r-- | tests/test_argparse_completer.py | 21 | ||||
| -rw-r--r-- | tests/test_argparse_custom.py | 15 | ||||
| -rwxr-xr-x | tests/test_cmd2.py | 15 | ||||
| -rwxr-xr-x | tests/test_completion.py | 15 | ||||
| -rwxr-xr-x | tests/test_history.py | 12 | ||||
| -rwxr-xr-x | tests/test_parsing.py | 11 | ||||
| -rw-r--r-- | tests/test_plugin.py | 7 | ||||
| -rw-r--r-- | tests/test_run_pyscript.py | 12 | ||||
| -rw-r--r-- | tests/test_table_creator.py | 4 | ||||
| -rw-r--r-- | tests/test_transcript.py | 20 | ||||
| -rw-r--r-- | tests/test_utils.py | 68 |
13 files changed, 146 insertions, 88 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 73080b5c..c0b9e385 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,14 +3,27 @@ Cmd2 unit/functional testing """ import sys -from contextlib import redirect_stderr, redirect_stdout -from typing import List, Optional, Union -from unittest import mock - -from pytest import fixture +from contextlib import ( + redirect_stderr, + redirect_stdout, +) +from typing import ( + List, + Optional, + Union, +) +from unittest import ( + mock, +) + +from pytest import ( + fixture, +) import cmd2 -from cmd2.utils import StdSim +from cmd2.utils import ( + StdSim, +) # Prefer statically linked gnureadline if available (for macOS compatibility due to issues with libedit) try: diff --git a/tests/test_argparse.py b/tests/test_argparse.py index 7059e9d3..e7806056 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -4,13 +4,17 @@ Cmd2 testing for argument parsing """ import argparse -from typing import Optional +from typing import ( + Optional, +) import pytest import cmd2 -from .conftest import run_cmd +from .conftest import ( + run_cmd, +) # Prefer statically linked gnureadline if available (for macOS compatibility due to issues with libedit) try: @@ -457,4 +461,3 @@ def test_pytest_mock_invalid(mocker): def test_pytest_mock_valid(mocker, spec_param): mocker.patch.object(ArgparseApp, 'namespace_provider', **spec_param) app = ArgparseApp() - diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py index 5c579b5c..25c7a03a 100644 --- a/tests/test_argparse_completer.py +++ b/tests/test_argparse_completer.py @@ -5,14 +5,27 @@ Unit/functional testing for argparse completer in cmd2 """ import argparse import numbers -from typing import List +from typing import ( + List, +) import pytest import cmd2 -from cmd2 import Cmd2ArgumentParser, CompletionError, CompletionItem, with_argparser -from cmd2.utils import StdSim -from .conftest import complete_tester, run_cmd +from cmd2 import ( + Cmd2ArgumentParser, + CompletionError, + CompletionItem, + with_argparser, +) +from cmd2.utils import ( + StdSim, +) + +from .conftest import ( + complete_tester, + run_cmd, +) # Data and functions for testing standalone choice_provider and completer standalone_choices = ['standalone', 'provider'] diff --git a/tests/test_argparse_custom.py b/tests/test_argparse_custom.py index 3d41fffd..0820f215 100644 --- a/tests/test_argparse_custom.py +++ b/tests/test_argparse_custom.py @@ -7,10 +7,17 @@ import argparse import pytest import cmd2 -from cmd2 import Cmd2ArgumentParser, constants -from cmd2.argparse_custom import generate_range_error - -from .conftest import run_cmd +from cmd2 import ( + Cmd2ArgumentParser, + constants, +) +from cmd2.argparse_custom import ( + generate_range_error, +) + +from .conftest import ( + run_cmd, +) class ApCustomTestApp(cmd2.Cmd): diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 97e10f52..f9109a23 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -9,12 +9,22 @@ import io import os import sys import tempfile -from code import InteractiveConsole +from code import ( + InteractiveConsole, +) import pytest import cmd2 -from cmd2 import COMMAND_NAME, ansi, clipboard, constants, exceptions, plugin, utils +from cmd2 import ( + COMMAND_NAME, + ansi, + clipboard, + constants, + exceptions, + plugin, + utils, +) from .conftest import ( HELP_HISTORY, @@ -47,6 +57,7 @@ def outsim_app(): def test_version(base_app): assert cmd2.__version__ +@pytest.mark.skipif(sys.version_info >= (3, 8), reason="failing in CI systems for Python 3.8 and 3.9") def test_not_in_main_thread(base_app, capsys): import threading cli_thread = threading.Thread(name='cli_thread', target=base_app.cmdloop) diff --git a/tests/test_completion.py b/tests/test_completion.py index a9f2a51b..2f4e34c3 100755 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -20,9 +20,18 @@ import sys import pytest import cmd2 -from cmd2 import utils -from examples.subcommands import SubcommandsExample -from .conftest import complete_tester, normalize, run_cmd +from cmd2 import ( + utils, +) +from examples.subcommands import ( + SubcommandsExample, +) + +from .conftest import ( + complete_tester, + normalize, + run_cmd, +) # List of strings used with completion functions food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato', 'Cheese "Pizza"'] diff --git a/tests/test_history.py b/tests/test_history.py index 86c52592..29ca020a 100755 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -11,9 +11,15 @@ import pytest import cmd2 # Python 3.5 had some regressions in the unitest.mock module, so use # 3rd party mock if available -from cmd2.parsing import StatementParser - -from .conftest import HELP_HISTORY, normalize, run_cmd +from cmd2.parsing import ( + StatementParser, +) + +from .conftest import ( + HELP_HISTORY, + normalize, + run_cmd, +) try: import mock diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 2eccec7c..abf8b9fd 100755 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -7,8 +7,15 @@ import attr import pytest import cmd2 -from cmd2 import constants, exceptions, utils -from cmd2.parsing import StatementParser, shlex_split +from cmd2 import ( + constants, + exceptions, + utils, +) +from cmd2.parsing import ( + StatementParser, + shlex_split, +) @pytest.fixture diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 279f2f79..4a019b15 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -9,7 +9,12 @@ import sys import pytest import cmd2 -from cmd2 import Cmd2ArgumentParser, exceptions, plugin, with_argparser +from cmd2 import ( + Cmd2ArgumentParser, + exceptions, + plugin, + with_argparser, +) # Python 3.5 had some regressions in the unitest.mock module, so use 3rd party mock if available try: diff --git a/tests/test_run_pyscript.py b/tests/test_run_pyscript.py index 8cfd8578..a46a3ca3 100644 --- a/tests/test_run_pyscript.py +++ b/tests/test_run_pyscript.py @@ -8,9 +8,15 @@ import os import pytest -from cmd2 import plugin, utils - -from .conftest import odd_file_names, run_cmd +from cmd2 import ( + plugin, + utils, +) + +from .conftest import ( + odd_file_names, + run_cmd, +) # Python 3.5 had some regressions in the unitest.mock module, so use 3rd party mock if available try: diff --git a/tests/test_table_creator.py b/tests/test_table_creator.py index 0d2edfb2..58dd6fdf 100644 --- a/tests/test_table_creator.py +++ b/tests/test_table_creator.py @@ -5,7 +5,9 @@ Unit testing for cmd2/table_creator.py module """ import pytest -from cmd2 import ansi +from cmd2 import ( + ansi, +) from cmd2.table_creator import ( AlternatingTable, BorderedTable, diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 55d60e18..1b9c71fe 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -9,15 +9,25 @@ import random import re import sys import tempfile -from unittest import mock +from unittest import ( + mock, +) import pytest import cmd2 -from cmd2 import transcript -from cmd2.utils import Settable, StdSim - -from .conftest import run_cmd, verify_help_text +from cmd2 import ( + transcript, +) +from cmd2.utils import ( + Settable, + StdSim, +) + +from .conftest import ( + run_cmd, + verify_help_text, +) class CmdLineApp(cmd2.Cmd): diff --git a/tests/test_utils.py b/tests/test_utils.py index ab3647e4..e6853570 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -10,13 +10,16 @@ import time import pytest +import cmd2.utils as cu +from cmd2.constants import ( + HORIZONTAL_ELLIPSIS, +) + try: import mock except ImportError: from unittest import mock -import cmd2.utils as cu -from cmd2.constants import HORIZONTAL_ELLIPSIS HELLO_WORLD = 'Hello, world!' @@ -641,55 +644,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, 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 |
