Skip to content

Commit df09c85

Browse files
committed
Identified and marked a few blocks of code that can't be reached during unit tests due to the lack of a real terminal. Some more comments.
1 parent c218633 commit df09c85

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

cmd2/argparse_completer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def my_completer(text: str, line: str, begidx: int, endidx:int, extra_param: str
5252
The subcommand group dictionary maps subcommand names to tuple(arg_choices, subcmd_args_lookup)
5353
5454
For more details of this more complex approach see tab_autocompletion.py in the examples
55+
56+
Copyright 2018 Eric Lin <[email protected]>
57+
Released under MIT license, see LICENSE file
5558
"""
5659

5760
import argparse
@@ -262,6 +265,12 @@ def complete_command(self, tokens: List[str], text: str, line: str, begidx: int,
262265
current_is_positional = False
263266
consumed_arg_values = {} # dict(arg_name -> [values, ...])
264267

268+
# the following are nested functions that have full access to all variables in the parent
269+
# function including variables declared and updated after this function. Variable values
270+
# are current at the point the nested functions are invoked (as in, they do not receive a
271+
# snapshot of these values, they directly access the current state of variables in the
272+
# parent function)
273+
265274
def consume_flag_argument() -> None:
266275
"""Consuming token as a flag argument"""
267276
# we're consuming flag arguments

cmd2/cmd2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ def _redirect_complete(self, text, line, begidx, endidx, compfunc):
15911591
return compfunc(text, line, begidx, endidx)
15921592

15931593
@staticmethod
1594-
def _pad_matches_to_display(matches_to_display):
1594+
def _pad_matches_to_display(matches_to_display): # pragma: no cover
15951595
"""
15961596
Adds padding to the matches being displayed as tab completion suggestions.
15971597
The default padding of readline/pyreadine is small and not visually appealing
@@ -1613,7 +1613,7 @@ def _pad_matches_to_display(matches_to_display):
16131613

16141614
return [cur_match + padding for cur_match in matches_to_display], len(padding)
16151615

1616-
def _display_matches_gnu_readline(self, substitution, matches, longest_match_length):
1616+
def _display_matches_gnu_readline(self, substitution, matches, longest_match_length): # pragma: no cover
16171617
"""
16181618
Prints a match list using GNU readline's rl_display_match_list()
16191619
This exists to print self.display_matches if it has data. Otherwise matches prints.
@@ -1664,7 +1664,7 @@ def _display_matches_gnu_readline(self, substitution, matches, longest_match_len
16641664
# Redraw prompt and input line
16651665
rl_force_redisplay()
16661666

1667-
def _display_matches_pyreadline(self, matches):
1667+
def _display_matches_pyreadline(self, matches): # pragma: no cover
16681668
"""
16691669
Prints a match list using pyreadline's _display_completions()
16701670
This exists to print self.display_matches if it has data. Otherwise matches prints.
@@ -3344,7 +3344,7 @@ def do_load(self, arglist):
33443344
# self._script_dir list when done.
33453345
with open(expanded_path, encoding='utf-8') as target:
33463346
self.cmdqueue = target.read().splitlines() + ['eos'] + self.cmdqueue
3347-
except IOError as e:
3347+
except IOError as e: # pragma: no cover
33483348
self.perror('Problem accessing script from {}:\n{}'.format(expanded_path, e))
33493349
return
33503350

@@ -3371,7 +3371,7 @@ def is_text_file(file_path):
33713371
# noinspection PyUnusedLocal
33723372
if sum(1 for line in f) > 0:
33733373
valid_text_file = True
3374-
except IOError:
3374+
except IOError: # pragma: no cover
33753375
pass
33763376
except UnicodeDecodeError:
33773377
# The file is not ASCII. Check if it is UTF-8.
@@ -3381,7 +3381,7 @@ def is_text_file(file_path):
33813381
# noinspection PyUnusedLocal
33823382
if sum(1 for line in f) > 0:
33833383
valid_text_file = True
3384-
except IOError:
3384+
except IOError: # pragma: no cover
33853385
pass
33863386
except UnicodeDecodeError:
33873387
# Not UTF-8

cmd2/rl_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
try:
1919
# noinspection PyUnresolvedReferences
2020
import readline
21-
except ImportError:
21+
except ImportError: # pragma: no cover
2222
pass
2323

2424

@@ -51,7 +51,8 @@ def rl_force_redisplay() -> None:
5151
"""
5252
if not sys.stdout.isatty():
5353
return
54-
if rl_type == RlType.GNU:
54+
55+
if rl_type == RlType.GNU: # pragma: no cover
5556
# rl_forced_update_display() is the proper way to redraw the prompt and line, but we
5657
# have to use ctypes to do it since Python's readline API does not wrap the function
5758
readline_lib.rl_forced_update_display()
@@ -60,6 +61,6 @@ def rl_force_redisplay() -> None:
6061
display_fixed = ctypes.c_int.in_dll(readline_lib, "rl_display_fixed")
6162
display_fixed.value = 1
6263

63-
elif rl_type == RlType.PYREADLINE:
64+
elif rl_type == RlType.PYREADLINE: # pragma: no cover
6465
# noinspection PyProtectedMember
6566
readline.rl.mode._print_prompt()

examples/tab_autocompletion.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env python
22
# coding=utf-8
3-
"""A simple example demonstrating how to use flag and index based tab-completion functions
3+
"""
4+
A example usage of the AutoCompleter
5+
6+
Copyright 2018 Eric Lin <[email protected]>
7+
Released under MIT license, see LICENSE file
48
"""
59
import argparse
610
import itertools

0 commit comments

Comments
 (0)