diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-09-30 11:17:04 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-09-30 11:17:04 -0400 |
commit | 81ad085cffc8f8af7fb7884bcc5db6fcede09df6 (patch) | |
tree | a197a6c81767f10b62d6ac6acfd46e42357a8289 /tests/conftest.py | |
parent | 85a21ef39467dc24a6f6f8c4f3ac656f24d6ff90 (diff) | |
parent | 61d5703cd3586b3460669a6260cf903c9863b240 (diff) | |
download | cmd2-git-81ad085cffc8f8af7fb7884bcc5db6fcede09df6.tar.gz |
Merged master into transcript_fixes branch and resolved conflicts
Diffstat (limited to 'tests/conftest.py')
-rw-r--r-- | tests/conftest.py | 86 |
1 files changed, 31 insertions, 55 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index a23c44d0..da7e8b08 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,11 +6,13 @@ Copyright 2016 Federico Ceratto <federico.ceratto@gmail.com> Released under MIT license, see LICENSE file """ import sys +from typing import Optional +from unittest import mock from pytest import fixture -from unittest import mock import cmd2 +from cmd2.utils import StdSim # Prefer statically linked gnureadline if available (for macOS compatibility due to issues with libedit) try: @@ -27,29 +29,29 @@ except ImportError: # Help text for base cmd2.Cmd application BASE_HELP = """Documented commands (type help <topic>): ======================================== -alias help load pyscript set shortcuts -edit history py quit shell unalias +alias help load py quit shell +edit history macro pyscript set shortcuts """ BASE_HELP_VERBOSE = """ Documented commands (type help <topic>): ================================================================================ -alias Define or display aliases +alias Manage aliases edit Edit a file in a text editor -help List available commands with "help" or detailed help with "help cmd" +help List available commands or provide detailed help for a specific command history View, run, edit, save, or clear previously entered commands -load Runs commands in script file that is encoded as either ASCII or UTF-8 text -py Invoke python command, shell, or script -pyscript Runs a python script file inside the console -quit Exits this application -set Sets a settable parameter or shows current settings of parameters +load Run commands in script file that is encoded as either ASCII or UTF-8 text +macro Manage macros +py Invoke Python command or shell +pyscript Run a Python script file inside the console +quit Exit this application +set Set a settable parameter or show current settings of parameters shell Execute a command as if at the OS prompt -shortcuts Lists shortcuts available -unalias Unsets aliases +shortcuts List available shortcuts """ # Help text for the history command -HELP_HISTORY = """Usage: history [arg] [-h] [-r | -e | -s | -o FILE | -t TRANSCRIPT | -c] +HELP_HISTORY = """Usage: history [-h] [-r | -e | -s | -o FILE | -t TRANSCRIPT | -c] [arg] View, run, edit, save, or clear previously entered commands @@ -64,12 +66,12 @@ optional arguments: -h, --help show this help message and exit -r, --run run selected history items -e, --edit edit and then run selected history items - -s, --script script format; no separation lines + -s, --script output commands in script format -o, --output-file FILE output commands to a script file -t, --transcript TRANSCRIPT output commands and results to a transcript file - -c, --clear clears all history + -c, --clear clear all history """ # Output from the shortcuts command with default built-in shortcuts @@ -80,11 +82,8 @@ SHORTCUTS_TXT = """Shortcuts for other commands: @@: _relative_load """ -expect_colors = True -if sys.platform.startswith('win'): - expect_colors = False # Output from the show command with default settings -SHOW_TXT = """colors: {} +SHOW_TXT = """colors: Terminal continuation_prompt: > debug: False echo: False @@ -94,14 +93,10 @@ locals_in_py: False prompt: (Cmd) quiet: False timing: False -""".format(expect_colors) +""" -if expect_colors: - color_str = 'True ' -else: - color_str = 'False' SHOW_LONG = """ -colors: {} # Colorized output (*nix only) +colors: Terminal # Allow colorized output (valid values: Terminal, Always, Never) continuation_prompt: > # On 2nd+ line of input debug: False # Show full error stack on error echo: False # Echo command issued into output @@ -111,23 +106,7 @@ locals_in_py: False # Allow access to your application in py via self prompt: (Cmd) # The prompt issued to solicit input quiet: False # Don't print nonessential feedback timing: False # Report execution times -""".format(color_str) - - -class StdOut(object): - """ Toy class for replacing self.stdout in cmd2.Cmd instances for unit testing. """ - def __init__(self): - self.buffer = '' - - def write(self, s): - self.buffer += s - - def read(self): - raise NotImplementedError - - def clear(self): - self.buffer = '' - +""" def normalize(block): """ Normalize a block of text to perform comparison. @@ -141,10 +120,10 @@ def normalize(block): def run_cmd(app, cmd): - """ Clear StdOut buffer, run the command, extract the buffer contents, """ + """ Clear StdSim buffer, run the command, extract the buffer contents, """ app.stdout.clear() app.onecmd_plus_hooks(cmd) - out = app.stdout.buffer + out = app.stdout.getvalue() app.stdout.clear() return normalize(out) @@ -152,21 +131,21 @@ def run_cmd(app, cmd): @fixture def base_app(): c = cmd2.Cmd() - c.stdout = StdOut() + c.stdout = StdSim(c.stdout) return c -def complete_tester(text, line, begidx, endidx, app): +def complete_tester(text: str, line: str, begidx: int, endidx: int, app) -> Optional[str]: """ This is a convenience function to test cmd2.complete() since in a unit test environment there is no actual console readline is monitoring. Therefore we use mock to provide readline data to complete(). - :param text: str - the string prefix we are attempting to match - :param line: str - the current input line with leading whitespace removed - :param begidx: int - the beginning index of the prefix text - :param endidx: int - the ending index of the prefix text + :param text: the string prefix we are attempting to match + :param line: the current input line with leading whitespace removed + :param begidx: the beginning index of the prefix text + :param endidx: the ending index of the prefix text :param app: the cmd2 app that will run completions :return: The first matched string or None if there are no matches Matches are stored in app.completion_matches @@ -181,11 +160,8 @@ def complete_tester(text, line, begidx, endidx, app): def get_endidx(): return endidx - first_match = None + # Run the readline tab-completion function with readline mocks in place with mock.patch.object(readline, 'get_line_buffer', get_line): with mock.patch.object(readline, 'get_begidx', get_begidx): with mock.patch.object(readline, 'get_endidx', get_endidx): - # Run the readline tab-completion function with readline mocks in place - first_match = app.complete(text, 0) - - return first_match + return app.complete(text, 0) |