File: test_option_groups.py

package info (click to toggle)
python-cloup 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 936 kB
  • sloc: python: 5,371; makefile: 120
file content (298 lines) | stat: -rw-r--r-- 8,563 bytes parent folder | download | duplicates (2)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""Test for the "option groups" feature/module."""
from textwrap import dedent
from typing import cast

import click
import pytest
from click import pass_context

import cloup
from cloup import OptionGroup, option, option_group
from cloup._util import pick_non_missing, reindent
from cloup.constraints import RequireAtLeast, mutually_exclusive
from cloup.typing import MISSING
from tests.util import (make_options, new_dummy_func, parametrize, pick_first_bool)


def test_error_message_if_first_arg_is_not_a_string():
    with pytest.raises(
        TypeError, match="the first argument of `@option_group` must be its title"
    ):
        @option_group(
            option('--one'),
            option('--two'),
        )
        def f(one, two):
            pass


@parametrize(
    ['tabular_help', 'align_option_groups'],
    pytest.param(True, True, id='tabular-aligned'),
    pytest.param(True, False, id='tabular-non_aligned'),
    pytest.param(False, None, id='linear'),
)
def test_option_groups_are_correctly_displayed_in_help(
    runner, tabular_help, align_option_groups, get_example_command
):
    cmd = get_example_command(
        tabular_help=tabular_help,
        align_option_groups=align_option_groups
    )
    result = runner.invoke(cmd, args=('--help',))
    assert result.exit_code == 0
    assert result.output.strip() == cmd.expected_help


def test_option_group_constraints_are_checked(runner, get_example_command):
    cmd = get_example_command(align_option_groups=False)

    result = runner.invoke(cmd, args='arg1 --one=1')
    assert result.exit_code == 0

    result = runner.invoke(cmd, args='arg1 --one=1 --three=3 --five=4')
    assert result.exit_code == 0

    result = runner.invoke(cmd, args='arg1 --one=1 --three=3')
    assert result.exit_code == 2
    error_prefix = ('Error: when --three is set, at least 1 of the following '
                    'parameters must be set')
    assert error_prefix in result.output


def test_option_group_decorator_raises_if_group_is_passed_to_contained_option():
    with pytest.raises(ValueError):
        @option_group(
            'a group',
            cloup.option('--opt', group=OptionGroup('another group'))
        )
        def f(opt):
            pass


def test_option_group_decorator_raises_for_no_options():
    with pytest.raises(ValueError):
        cloup.option_group('grp')


@pytest.mark.parametrize(
    'cmd_value', [MISSING, None, True, False],
    ids=lambda val: f'cmd_{val}'
)
@pytest.mark.parametrize(
    'ctx_value', [MISSING, None, True, False],
    ids=lambda val: f'ctx_{val}'
)
def test_align_option_groups_context_setting(runner, ctx_value, cmd_value):
    should_align = pick_first_bool([cmd_value, ctx_value], default=True)
    cxt_settings = pick_non_missing(dict(
        align_option_groups=ctx_value,
        terminal_width=80,
    ))
    cmd_kwargs = pick_non_missing(dict(
        align_option_groups=cmd_value,
        context_settings=cxt_settings
    ))

    @cloup.command(**cmd_kwargs)
    @cloup.option_group('First group', option('--opt', help='first option'))
    @cloup.option_group('Second group', option('--much-longer-opt', help='second option'))
    @pass_context
    def cmd(ctx, one, much_longer_opt):
        assert cmd.must_align_groups(ctx) == should_align

    result = runner.invoke(cmd, args=('--help',))
    start = result.output.find('First')
    if should_align:
        expected = """
            First group:
              --opt TEXT              first option

            Second group:
              --much-longer-opt TEXT  second option

            Other options:
              --help                  Show this message and exit."""
    else:
        expected = """
            First group:
              --opt TEXT  first option

            Second group:
              --much-longer-opt TEXT  second option

            Other options:
              --help  Show this message and exit."""

    expected = dedent(expected).strip()
    end = start + len(expected)
    assert result.output[start:end] == expected


def test_context_settings_propagate_to_children(runner):
    @cloup.group(context_settings=dict(align_option_groups=False))
    def grp():
        pass

    @grp.command()
    @pass_context
    def cmd(ctx):
        assert cmd.must_align_option_groups(ctx) is False

    runner.invoke(grp, ('cmd',))


def test_that_neither_optgroup_nor_its_options_are_shown_if_optgroup_is_hidden(runner):
    @cloup.command('name')
    @cloup.option_group(
        'Hidden group',
        cloup.option('--one'),
        hidden=True
    )
    def cmd():
        pass

    result = runner.invoke(cmd, args=('--help',), catch_exceptions=False)
    assert 'Hidden group' not in result.output
    assert '--one' not in result.output


def test_that_optgroup_is_hidden_if_all_its_options_are_hidden(runner):
    @cloup.command('name')
    @cloup.option_group(
        'Hidden group',
        cloup.option('--one', hidden=True),
        cloup.option('--two', hidden=True),
    )
    def cmd():
        pass

    assert cmd.option_groups[0].hidden
    result = runner.invoke(cmd, args=('--help',), catch_exceptions=False)
    assert 'Hidden group' not in result.output


def test_option_group_options_setter_set_the_hidden_attr_of_options():
    opts = make_options('abc')
    group = OptionGroup('name')
    group.options = opts
    assert not any(opt.hidden for opt in opts)
    group.hidden = True
    group.options = opts
    assert all(opt.hidden for opt in opts)


def test_option_group_with_constrained_subgroups(runner):
    @cloup.command()
    @option_group(
        "Some options",
        RequireAtLeast(1)(
            option('-a', is_flag=True),
            option('-b', is_flag=True)
        ),
        mutually_exclusive(
            option('-c', is_flag=True),
            option('-d', is_flag=True),
        ),
        option('-e', is_flag=True),
    )
    def cmd(a, b, c, d, e):
        pass

    cmd = cast(cloup.Command, cmd)
    assert len(cmd.option_groups) == 1
    assert len(cmd.option_groups[0]) == 5

    assert runner.invoke(cmd, ['-abc']).exit_code == 0
    assert 'Error: at least 1' in runner.invoke(cmd, ['-c']).output
    assert 'mutually exclusive' in runner.invoke(cmd, ['-acd']).output

    expected_help = dedent("""
        Usage: cmd [OPTIONS]

        Some options:
          -a
          -b
          -c
          -d
          -e

        Other options:
          --help  Show this message and exit.
    """).lstrip()
    actual_help = runner.invoke(cmd, ['--help']).output
    assert actual_help == expected_help


def test_usage_of_constraints_as_decorators_inside_option_group(runner):
    @cloup.command()
    @cloup.option_group(
        "Options",
        mutually_exclusive(
            cloup.option('-a', is_flag=True),
            cloup.option('-b', is_flag=True),
            cloup.option('-c', is_flag=True),
        ),
        cloup.option('-d', is_flag=True),
    )
    def cmd(a, b, c, d):
        pass

    expected_help = dedent("""
        Usage: cmd [OPTIONS]

        Options:
          -a
          -b
          -c
          -d

        Other options:
          --help  Show this message and exit.
    """).lstrip()

    res = runner.invoke(cmd, args=['--help'])
    assert res.output == expected_help

    assert runner.invoke(cmd, args=[]).exit_code == 0
    assert runner.invoke(cmd, args='-d'.split()).exit_code == 0
    assert runner.invoke(cmd, args='-ad'.split()).exit_code == 0

    res = runner.invoke(cmd, args='-ab'.split())
    assert res.exit_code == click.UsageError.exit_code
    assert 'mutually exclusive' in res.output


def test_option_groups_raises_if_input_decorator_add_an_argument():
    with pytest.raises(
        TypeError, match='only parameter of type `Option` can be added to option groups'
    ):
        option_group(
            'Title',
            mutually_exclusive(
                cloup.argument('arg'),
                cloup.option('--opt')
            )
        )(new_dummy_func())


def test_default_option_group_title_when_the_only_other_section_is_positional_arguments(
    runner
):
    @cloup.command()
    @cloup.argument("arg", help="An argument.")
    @cloup.option("--opt", help="An option.")
    def cmd(**kwargs):
        pass

    res = runner.invoke(cmd, ["--help"])
    assert res.output == reindent("""
        Usage: cmd [OPTIONS] ARG

        Positional arguments:
          ARG         An argument.

        Options:
          --opt TEXT  An option.
          --help      Show this message and exit.
    """)