Skip to content

Commit 2221e08

Browse files
authored
Merge pull request #890 from python-cmd2/complete_redirection
Only tab complete after redirection tokens if redirection is allowed
2 parents 729e152 + b6971fb commit 2221e08

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Setting the following pyscript variables:
1313
* `__name__`: __main__
1414
* `__file__`: script path (as typed, ~ will be expanded)
15+
* Only tab complete after redirection tokens if redirection is allowed
1516
* Other
1617
* Removed undocumented `py run` command since it was replaced by `run_pyscript` a while ago
1718

cmd2/cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,8 @@ def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, com
10501050
in_pipe = False
10511051
in_file_redir = True
10521052

1053-
# Not a redirection token
1054-
else:
1053+
# Only tab complete after redirection tokens if redirection is allowed
1054+
elif self.allow_redirection:
10551055
do_shell_completion = False
10561056
do_path_completion = False
10571057

tests/test_completion.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -902,11 +902,6 @@ class RedirCompType(enum.Enum):
902902
DEFAULT = 3,
903903
NONE = 4
904904

905-
"""
906-
fake > >
907-
fake | grep > file
908-
fake | grep > file >
909-
"""
910905

911906
@pytest.mark.parametrize('line, comp_type', [
912907
('fake', RedirCompType.DEFAULT),
@@ -933,31 +928,40 @@ class RedirCompType(enum.Enum):
933928
('fake > file >>', RedirCompType.NONE),
934929
])
935930
def test_redirect_complete(cmd2_app, monkeypatch, line, comp_type):
936-
shell_cmd_complete_mock = mock.MagicMock(name='shell_cmd_complete')
937-
monkeypatch.setattr("cmd2.Cmd.shell_cmd_complete", shell_cmd_complete_mock)
938-
939-
path_complete_mock = mock.MagicMock(name='path_complete')
940-
monkeypatch.setattr("cmd2.Cmd.path_complete", path_complete_mock)
941-
942-
default_complete_mock = mock.MagicMock(name='fake_completer')
943-
944-
text = ''
945-
line = '{} {}'.format(line, text)
946-
endidx = len(line)
947-
begidx = endidx - len(text)
931+
# Test both cases of allow_redirection
932+
cmd2_app.allow_redirection = True
933+
for count in range(2):
934+
shell_cmd_complete_mock = mock.MagicMock(name='shell_cmd_complete')
935+
monkeypatch.setattr("cmd2.Cmd.shell_cmd_complete", shell_cmd_complete_mock)
936+
937+
path_complete_mock = mock.MagicMock(name='path_complete')
938+
monkeypatch.setattr("cmd2.Cmd.path_complete", path_complete_mock)
939+
940+
default_complete_mock = mock.MagicMock(name='fake_completer')
941+
942+
text = ''
943+
line = '{} {}'.format(line, text)
944+
endidx = len(line)
945+
begidx = endidx - len(text)
946+
947+
cmd2_app._redirect_complete(text, line, begidx, endidx, default_complete_mock)
948+
949+
if comp_type == RedirCompType.SHELL_CMD:
950+
shell_cmd_complete_mock.assert_called_once()
951+
elif comp_type == RedirCompType.PATH:
952+
path_complete_mock.assert_called_once()
953+
elif comp_type == RedirCompType.DEFAULT:
954+
default_complete_mock.assert_called_once()
955+
else:
956+
shell_cmd_complete_mock.assert_not_called()
957+
path_complete_mock.assert_not_called()
958+
default_complete_mock.assert_not_called()
948959

949-
cmd2_app._redirect_complete(text, line, begidx, endidx, default_complete_mock)
960+
# Do the next test with allow_redirection as False
961+
cmd2_app.allow_redirection = False
962+
if comp_type != RedirCompType.DEFAULT:
963+
comp_type = RedirCompType.NONE
950964

951-
if comp_type == RedirCompType.SHELL_CMD:
952-
shell_cmd_complete_mock.assert_called_once()
953-
elif comp_type == RedirCompType.PATH:
954-
path_complete_mock.assert_called_once()
955-
elif comp_type == RedirCompType.DEFAULT:
956-
default_complete_mock.assert_called_once()
957-
else:
958-
shell_cmd_complete_mock.assert_not_called()
959-
path_complete_mock.assert_not_called()
960-
default_complete_mock.assert_not_called()
961965

962966
def test_complete_set_value(cmd2_app):
963967
text = ''

0 commit comments

Comments
 (0)