-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtest_cli_utils.py
More file actions
89 lines (73 loc) · 2.49 KB
/
test_cli_utils.py
File metadata and controls
89 lines (73 loc) · 2.49 KB
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
import pytest
from unittest.mock import Mock
from httpie.cli.argparser import HelpNotCheckingArgumentParser
from httpie.cli.utils import LazyChoices
def test_lazy_choices():
mock = Mock()
getter = mock.getter
getter.return_value = ['a', 'b', 'c']
# On Python 3.14+ LazyChoices only works properly
# with our custom ArgumentParser.
# In httpie code base, all our argument parsers inherit from this class.
parser = HelpNotCheckingArgumentParser()
parser.register('action', 'lazy_choices', LazyChoices)
parser.add_argument(
'--option',
help="the regular option",
default='a',
metavar='SYMBOL',
choices=['a', 'b'],
)
parser.add_argument(
'--lazy-option',
help="the lazy option",
default='a',
metavar='SYMBOL',
action='lazy_choices',
getter=getter,
cache=False # for test purposes
)
# Parser initialization doesn't call it.
getter.assert_not_called()
# If we don't use --lazy-option, we don't retrieve it.
parser.parse_args([])
getter.assert_not_called()
parser.parse_args(['--option', 'b'])
getter.assert_not_called()
# If we pass a value, it will retrieve to verify.
parser.parse_args(['--lazy-option', 'c'])
getter.assert_called()
getter.reset_mock()
with pytest.raises(SystemExit):
parser.parse_args(['--lazy-option', 'z'])
getter.assert_called()
getter.reset_mock()
def test_lazy_choices_help():
mock = Mock()
getter = mock.getter
getter.return_value = ['a', 'b', 'c']
help_formatter = mock.help_formatter
help_formatter.return_value = '<my help>'
parser = HelpNotCheckingArgumentParser()
parser.register('action', 'lazy_choices', LazyChoices)
parser.add_argument(
'--lazy-option',
default='a',
metavar='SYMBOL',
action='lazy_choices',
getter=getter,
help_formatter=help_formatter,
cache=False # for test purposes
)
# Parser initialization doesn't call it.
getter.assert_not_called()
# If we don't use `--help`, we don't use it.
parser.parse_args([])
getter.assert_not_called()
help_formatter.assert_not_called()
parser.parse_args(['--lazy-option', 'b'])
help_formatter.assert_not_called()
# If we use --help, then we call it with styles
with pytest.raises(SystemExit):
parser.parse_args(['--help'])
help_formatter.assert_called_once_with(['a', 'b', 'c'], isolation_mode=False)