File: test_sphinxext.py

package info (click to toggle)
python-cliff 4.11.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 672 kB
  • sloc: python: 5,207; makefile: 32; sh: 21
file content (326 lines) | stat: -rw-r--r-- 8,927 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# Copyright (C) 2017, Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import argparse
import textwrap

from cliff import sphinxext
from cliff.tests import base


class TestSphinxExtension(base.TestBase):
    def test_empty_help(self):
        """Handle positional and optional actions without help messages."""
        parser = argparse.ArgumentParser(prog='hello-world', add_help=False)
        parser.add_argument('name', action='store')
        parser.add_argument('--language', dest='lang')

        output = '\n'.join(sphinxext._format_parser(parser))
        self.assertEqual(
            textwrap.dedent(
                """
        .. program:: hello-world
        .. code-block:: shell

            hello-world [--language LANG] name

        .. option:: --language <LANG>

        .. option:: name
        """
            ).lstrip(),
            output,
        )

    def test_nonempty_help(self):
        """Handle positional and optional actions with help messages."""
        parser = argparse.ArgumentParser(prog='hello-world', add_help=False)
        parser.add_argument('name', help='user name')
        parser.add_argument(
            '--language', dest='lang', help='greeting language'
        )

        output = '\n'.join(sphinxext._format_parser(parser))
        self.assertEqual(
            textwrap.dedent(
                """
        .. program:: hello-world
        .. code-block:: shell

            hello-world [--language LANG] name

        .. option:: --language <LANG>

            greeting language

        .. option:: name

            user name
        """
            ).lstrip(),
            output,
        )

    def test_description_epilog(self):
        """Handle a parser description, epilog."""
        parser = argparse.ArgumentParser(
            prog='hello-world',
            add_help=False,
            description='A "Hello, World" app.',
            epilog='What am I doing down here?',
        )
        parser.add_argument('name', action='store')
        parser.add_argument('--language', dest='lang')

        output = '\n'.join(sphinxext._format_parser(parser))
        self.assertEqual(
            textwrap.dedent(
                """
        A "Hello, World" app.

        .. program:: hello-world
        .. code-block:: shell

            hello-world [--language LANG] name

        .. option:: --language <LANG>

        .. option:: name

        What am I doing down here?
        """
            ).lstrip(),
            output,
        )

    def test_flag(self):
        """Handle a boolean argparse action."""
        parser = argparse.ArgumentParser(prog='hello-world', add_help=False)
        parser.add_argument('name', help='user name')
        parser.add_argument(
            '--translate',
            action='store_true',
            help='translate to local language',
        )

        output = '\n'.join(sphinxext._format_parser(parser))
        self.assertEqual(
            textwrap.dedent(
                """
        .. program:: hello-world
        .. code-block:: shell

            hello-world [--translate] name

        .. option:: --translate

            translate to local language

        .. option:: name

            user name
        """
            ).lstrip(),
            output,
        )

    def test_supressed(self):
        """Handle a supressed action."""
        parser = argparse.ArgumentParser(prog='hello-world', add_help=False)
        parser.add_argument('name', help='user name')
        parser.add_argument('--variable', help=argparse.SUPPRESS)

        output = '\n'.join(sphinxext._format_parser(parser))
        self.assertEqual(
            textwrap.dedent(
                """
        .. program:: hello-world
        .. code-block:: shell

            hello-world name


        .. option:: name

            user name
        """
            ).lstrip(),
            output,
        )

    def test_metavar(self):
        """Handle an option with a metavar."""
        parser = argparse.ArgumentParser(prog='hello-world', add_help=False)
        parser.add_argument(
            'names', metavar='<NAME>', nargs='+', help='a user name'
        )

        output = '\n'.join(sphinxext._format_parser(parser))
        self.assertEqual(
            textwrap.dedent(
                """
        .. program:: hello-world
        .. code-block:: shell

            hello-world <NAME> [<NAME> ...]

        .. option:: NAME

            a user name
        """
            ).lstrip(),
            output,
        )

    def test_multiple_opts(self):
        """Correctly output multiple opts on separate lines."""
        parser = argparse.ArgumentParser(prog='hello-world', add_help=False)
        parser.add_argument('name', help='user name')
        parser.add_argument(
            '--language', dest='lang', help='greeting language'
        )
        parser.add_argument(
            '--translate',
            action='store_true',
            help='translate to local language',
        )
        parser.add_argument(
            '--write-to-var-log-something-or-other',
            action='store_true',
            help='a long opt to force wrapping',
        )
        parser.add_argument(
            '--required-arg',
            dest='stuff',
            required=True,
            help='a required argument',
        )
        style_group = parser.add_mutually_exclusive_group(required=True)
        style_group.add_argument(
            '--polite', action='store_true', help='use a polite greeting'
        )
        style_group.add_argument(
            '--profane', action='store_true', help='use a less polite greeting'
        )

        output = '\n'.join(sphinxext._format_parser(parser))
        self.assertEqual(
            textwrap.dedent(
                """
        .. program:: hello-world
        .. code-block:: shell

            hello-world
                [--language LANG]
                [--translate]
                [--write-to-var-log-something-or-other]
                --required-arg STUFF
                (--polite | --profane)
                name

        .. option:: --language <LANG>

            greeting language

        .. option:: --translate

            translate to local language

        .. option:: --write-to-var-log-something-or-other

            a long opt to force wrapping

        .. option:: --required-arg <STUFF>

            a required argument

        .. option:: --polite

            use a polite greeting

        .. option:: --profane

            use a less polite greeting

        .. option:: name

            user name
        """
            ).lstrip(),
            output,
        )

    def test_various_option_names_with_hyphen(self):
        """Handle options whose name and/or metavar contain hyphen(s)"""
        parser = argparse.ArgumentParser(prog='hello-world', add_help=False)
        parser.add_argument(
            '--foo-bar', metavar='<foo-bar>', help='foo bar', required=True
        )
        parser.add_argument(
            '--foo-bar-baz',
            metavar='<foo-bar-baz>',
            help='foo bar baz',
            required=True,
        )
        parser.add_argument(
            '--foo', metavar='<foo>', help='foo', required=True
        )
        parser.add_argument('--alpha', metavar='<A>', help='alpha')
        parser.add_argument('--alpha-beta', metavar='<A-B>', help='alpha beta')
        parser.add_argument(
            '--alpha-beta-gamma', metavar='<A-B-C>', help='alpha beta gamma'
        )

        output = '\n'.join(sphinxext._format_parser(parser))
        self.assertEqual(
            textwrap.dedent(
                """
        .. program:: hello-world
        .. code-block:: shell

            hello-world
                --foo-bar <foo-bar>
                --foo-bar-baz <foo-bar-baz>
                --foo <foo>
                [--alpha <A>]
                [--alpha-beta <A-B>]
                [--alpha-beta-gamma <A-B-C>]

        .. option:: --foo-bar <foo-bar>

            foo bar

        .. option:: --foo-bar-baz <foo-bar-baz>

            foo bar baz

        .. option:: --foo <foo>

            foo

        .. option:: --alpha <A>

            alpha

        .. option:: --alpha-beta <A-B>

            alpha beta

        .. option:: --alpha-beta-gamma <A-B-C>

            alpha beta gamma
        """
            ).lstrip(),
            output,
        )