aboutsummaryrefslogtreecommitdiff
blob: cb4ffdf7fe4f1c330cd0c0ce07ffe7bd8aad01c2 (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright: 2006 Marien Zwart <marienz@gentoo.org>
# License: BSD/GPL2

import sys

from snakeoil.test import TestCase

from pkgcore.config import configurable, basics, errors
from pkgcore.scripts import pconfig
from pkgcore.test.scripts.helpers import ArgParseMixin


@configurable({'reff': 'ref:spork'})
def spork(reff):
    """Test thing."""

def foon():
    pass

@configurable(typename='spork')
def pseudospork():
    pass

@configurable(typename='multi', allow_unknowns=True, types={
        'string': 'str', 'boolean': 'bool', 'list': 'list',
        'callable': 'callable', 'lazy_ref': 'lazy_ref:spork',
        'ref': 'ref:spork', 'lazy_refs': 'lazy_refs:spork',
        'refs': 'refs:spork',
        })
def multi(**kwargs):
    """Just something taking all kinds of args."""

# "in positional but not in required" is an error.
@configurable(positional=['foon'])
def broken_type(*args):
    """Noop."""

@configurable(types={'inc': 'list'}, allow_unknowns=True)
def increment(inc=()):
    """Noop."""


class DescribeClassTest(TestCase, ArgParseMixin):

    _argparser = pconfig.describe_class

    def test_parser(self):
        self.assertError(
            'the following arguments are required: target_class')

        if sys.hexversion >= 0x03050000:
            module = "module 'pkgcore'"
        else:
            module = "'module' object"

        self.assertError(
            "argument target_class: Failed importing target 'pkgcore.spork': '%s has no attribute 'spork''" % module,
            'pkgcore.spork')
        self.assertError(
            "argument target_class: Failed importing target 'pkgcore.a': '%s has no attribute 'a''" % module,
            'pkgcore.a', 'pkgcore.b')
        self.parse('pkgcore.scripts')

    def test_describe_class(self):
        self.assertOut(
            ['typename is spork',
             'Test thing.',
             '',
             'reff: ref:spork (required)'],
            'pkgcore.test.scripts.test_pconfig.spork')
        self.assertOut(
            ['typename is increment',
             'Noop.',
             'values not listed are handled as strings',
             '',
             'inc: list'],
            'pkgcore.test.scripts.test_pconfig.increment')

    def test_broken_type(self):
        self.assertErr(
            ['Not a valid type!'],
            'pkgcore.test.scripts.test_pconfig.broken_type')


class ClassesTest(TestCase, ArgParseMixin):

    _argparser = pconfig.classes

    def test_classes(self):
        sections = []
        for i in xrange(10):
            @configurable(typename='spork')
            def noop():
                """noop"""
            noop.__name__ = str(i)
            sections.append(basics.HardCodedConfigSection({'class': noop}))
        self.assertOut(
            ['pkgcore.test.scripts.test_pconfig.foon'],
            spork=basics.HardCodedConfigSection({'class': foon}))
        self.assertOut([
                'pkgcore.test.scripts.test_pconfig.0',
                'pkgcore.test.scripts.test_pconfig.1',
                'pkgcore.test.scripts.test_pconfig.2',
                'pkgcore.test.scripts.test_pconfig.3',
                'pkgcore.test.scripts.test_pconfig.4',
                'pkgcore.test.scripts.test_pconfig.5',
                'pkgcore.test.scripts.test_pconfig.multi',
                'pkgcore.test.scripts.test_pconfig.pseudospork',
                'pkgcore.test.scripts.test_pconfig.spork',
                ],
            bork=basics.HardCodedConfigSection({
                    'class': pseudospork, 'bork': True, 'inherit-only': True}),
            multi=basics.HardCodedConfigSection({
                    'class': multi,
                    'ref': sections[0],
                    'refs': sections[1:3],
                    'lazy_ref': sections[3],
                    'lazy_refs': sections[4:6],
                    'random': 'unknown',
                    }),
            spork=basics.HardCodedConfigSection({
                    'class': spork,
                    'reff': basics.HardCodedConfigSection({
                            'class': pseudospork})}))


class DumpTest(TestCase, ArgParseMixin):

    _argparser = pconfig.dump

    def test_dump(self):
        self.assertOut(
            ["'spork' {",
             '    # typename of this section: foon',
             '    class pkgcore.test.scripts.test_pconfig.foon;',
             '}',
             ''],
            spork=basics.HardCodedConfigSection({'class': foon}))

    def test_default(self):
        self.assertOut(
            ["'spork' {",
             '    # typename of this section: foon',
             '    class pkgcore.test.scripts.test_pconfig.foon;',
             '    default true;',
             '}',
             ''],
            spork=basics.HardCodedConfigSection({'class': foon,
                                                 'default': True}))

    def test_uncollapsable(self):
        self.assertOut(
            '',
            spork=basics.HardCodedConfigSection({
                    'class': foon, 'broken': True, 'inherit-only': True}))

    def test_serialise(self):
        nest = basics.HardCodedConfigSection({'class': pseudospork})
        self.assertOut(
            ["'spork' {",
             '    # typename of this section: multi',
             '    class pkgcore.test.scripts.test_pconfig.multi;',
             '    # type: bool',
             '    boolean True;',
             '    # type: callable',
             '    callable pkgcore.test.scripts.test_pconfig.multi;',
             '    # type: lazy_ref:spork',
             '    lazy_ref {',
             '        # typename of this section: spork',
             '        class pkgcore.test.scripts.test_pconfig.pseudospork;',
             '    };',
             '    # type: lazy_refs:spork',
             '    lazy_refs {',
             '        # typename of this section: spork',
             '        class pkgcore.test.scripts.test_pconfig.pseudospork;',
             '    } {',
             '        # typename of this section: spork',
             '        class pkgcore.test.scripts.test_pconfig.pseudospork;',
             '    };',
             '    # type: list',
             "    list 'a' 'b\\' \"c';",
             '    # type: ref:spork',
             '    ref {',
             '        # typename of this section: spork',
             '        class pkgcore.test.scripts.test_pconfig.pseudospork;',
             '    };',
             '    # type: refs:spork',
             '    refs {',
             '        # typename of this section: spork',
             '        class pkgcore.test.scripts.test_pconfig.pseudospork;',
             '    } {',
             '        # typename of this section: spork',
             '        class pkgcore.test.scripts.test_pconfig.pseudospork;',
             '    };',
             '    # type: str',
             '    string \'it is a "stringy" \\\'string\\\'\';',
             '    # type: str',
             "    unknown 'random';",
             '}',
             ''],
            spork=basics.HardCodedConfigSection({
                    'class': multi,
                    'string': 'it is a "stringy" \'string\'',
                    'boolean': True,
                    'list': ['a', 'b\' "c'],
                    'callable': multi,
                    'ref': nest,
                    'lazy_ref': nest,
                    'refs': [nest, nest],
                    'lazy_refs': [nest, nest],
                    'unknown': 'random',
                    }))

    def test_one_typename(self):
        self.assertOut(
            ["'spork' {",
             '    # typename of this section: spork',
             '    class pkgcore.test.scripts.test_pconfig.pseudospork;',
             '}',
             '',
             ],
            'spork',
            spork=basics.HardCodedConfigSection({'class': pseudospork}),
            foon=basics.HardCodedConfigSection({'class': foon}),
            )


class UncollapsableTest(TestCase, ArgParseMixin):

    _argparser = pconfig.uncollapsable

    def test_uncollapsable(self):
        self.assertOut(
            ["section foon:",
            " Collapsing section named 'foon'",
            " cannot collapse inherit-only section"
            "",
            "",
            "section spork:",
            " Collapsing section named 'spork'",
            " type pkgcore.test.scripts.test_pconfig.spork needs settings for 'reff'"
            "",
            "",
            ],
            spork=basics.HardCodedConfigSection({'class': spork}),
            foon=basics.HardCodedConfigSection({'class': spork,
                                                'inherit-only': True}),
            )


class ConfigurablesTest(TestCase, ArgParseMixin):

    _argparser = pconfig.configurables

    def test_configurables(self):
        self.assertError(
            'unrecognized arguments: bar',
            'foo', 'bar')


class WeirdSection(basics.ConfigSection):

    def __contains__(self, key):
        return key == 'sects'

    def keys(self):
        return ['sects']

    def render_value(self, central, name, arg_type):
        if name != 'sects':
            raise KeyError(name)
        if arg_type != 'repr':
            raise errors.ConfigurationError('%r unsupported' % (arg_type,))
        return 'refs', [
            ['spork', basics.HardCodedConfigSection({'foo': 'bar'})],
            None, None]


class DumpUncollapsedTest(TestCase, ArgParseMixin):

    _argparser = pconfig.dump_uncollapsed

    def test_dump_uncollapsed(self):
        self.assertOut(
            ['# Warning:',
             '# Do not copy this output to a configuration file directly,',
             '# because the types you see here are only guesses.',
             '# A value used as "list" in the collapsed config will often',
             '# show up as "string" here and may need to be converted',
             '# (for example from space-separated to comma-separated)',
             '# to work in a config file with a different format.',
             '',
             '********',
             'Source 1',
             '********',
             '',
             'foon',
             '====',
             '# type: callable',
             "'class' = pkgcore.test.scripts.test_pconfigspork",
             '# type: bool',
             "'inherit-only' = True",
             '# type: refs',
             "'refs' = ",
             '    nested section 1',
             '    ================',
             '    # type: str',
             "    'crystal' = 'clear'",
             '',
             '    nested section 2',
             '    ================',
             '    # type: refs',
             "    'sects.prepend' = ",
             '        nested section 1',
             '        ================',
             "        named section 'spork'",
             '',
             '        nested section 2',
             '        ================',
             '        # type: str',
             "        'foo' = 'bar'",
             '',
             '',
             '# type: list',
             "'seq' = 'a' 'b c'",
             '# type: str',
             "'str' = 'quote \\'\" unquote'",
             '',
             'spork',
             '=====',
             '# type: callable',
             "'class' = pkgcore.test.scripts.test_pconfigspork",
             '',
             ],
            spork=basics.HardCodedConfigSection({'class': spork}),
            foon=basics.HardCodedConfigSection({
                    'class': spork,
                    'inherit-only': True,
                    'refs': [
                        basics.HardCodedConfigSection({'crystal': 'clear'}),
                        WeirdSection(),
                        ],
                    'seq': ['a', 'b c'],
                    'str': 'quote \'" unquote',
                    }),
            )