1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
from argparse import Action
from collections import namedtuple
import pytest
from flexmock import flexmock
from borgmatic.commands.completion import fish as module
OptionType = namedtuple('OptionType', ['file', 'choice', 'unknown_required'])
test_data = [
(Action('--flag', 'flag'), OptionType(file=False, choice=False, unknown_required=False)),
*(
(
Action('--flag', 'flag', metavar=metavar),
OptionType(file=True, choice=False, unknown_required=False),
)
for metavar in ('FILENAME', 'PATH')
),
(
Action('--flag', dest='config_paths'),
OptionType(file=True, choice=False, unknown_required=False),
),
(
Action('--flag', 'flag', metavar='OTHER'),
OptionType(file=False, choice=False, unknown_required=False),
),
(
Action('--flag', 'flag', choices=['a', 'b']),
OptionType(file=False, choice=True, unknown_required=False),
),
(
Action('--flag', 'flag', choices=['a', 'b'], type=str),
OptionType(file=False, choice=True, unknown_required=True),
),
(
Action('--flag', 'flag', choices=None),
OptionType(file=False, choice=False, unknown_required=False),
),
(
Action('--flag', 'flag', required=True),
OptionType(file=False, choice=False, unknown_required=True),
),
*(
(
Action('--flag', 'flag', nargs=nargs),
OptionType(file=False, choice=False, unknown_required=True),
)
for nargs in ('+', '*')
),
*(
(
Action('--flag', 'flag', metavar=metavar),
OptionType(file=False, choice=False, unknown_required=True),
)
for metavar in ('PATTERN', 'KEYS', 'N')
),
*(
(
Action('--flag', 'flag', type=flag_type, default=None),
OptionType(file=False, choice=False, unknown_required=True),
)
for flag_type in (int, str)
),
(
Action('--flag', 'flag', type=int, default=1),
OptionType(file=False, choice=False, unknown_required=False),
),
(
Action('--flag', 'flag', type=str, required=True, metavar='PATH'),
OptionType(file=True, choice=False, unknown_required=True),
),
(
Action('--flag', 'flag', type=str, required=True, metavar='PATH', default='/dev/null'),
OptionType(file=True, choice=False, unknown_required=True),
),
(
Action('--flag', 'flag', type=str, required=False, metavar='PATH', default='/dev/null'),
OptionType(file=True, choice=False, unknown_required=False),
),
]
@pytest.mark.parametrize('action, option_type', test_data)
def test_has_file_options_detects_file_options(action: Action, option_type: OptionType):
assert module.has_file_options(action) == option_type.file
@pytest.mark.parametrize('action, option_type', test_data)
def test_has_choice_options_detects_choice_options(action: Action, option_type: OptionType):
assert module.has_choice_options(action) == option_type.choice
@pytest.mark.parametrize('action, option_type', test_data)
def test_has_unknown_required_param_options_detects_unknown_required_param_options(
action: Action,
option_type: OptionType,
):
assert module.has_unknown_required_param_options(action) == option_type.unknown_required
@pytest.mark.parametrize('action, option_type', test_data)
def test_has_exact_options_detects_exact_options(action: Action, option_type: OptionType):
assert module.has_exact_options(action) == (True in option_type)
@pytest.mark.parametrize('action, option_type', test_data)
def test_exact_options_completion_produces_reasonable_completions(
action: Action,
option_type: OptionType,
):
completion = module.exact_options_completion(action)
if True in option_type:
assert completion.startswith('\ncomplete -c borgmatic')
else:
assert completion == ''
def test_exact_options_completion_raises_for_unexpected_action():
flexmock(module).should_receive('has_exact_options').and_return(True)
flexmock(module).should_receive('has_file_options').and_return(False)
flexmock(module).should_receive('has_choice_options').and_return(False)
flexmock(module).should_receive('has_unknown_required_param_options').and_return(False)
with pytest.raises(ValueError):
module.exact_options_completion(Action('--unknown', dest='unknown'))
def test_dedent_strip_as_tuple_does_not_raise():
module.dedent_strip_as_tuple(
'''
a
b
''',
)
|