Debian Codegraph @

rpython/config/test/test_config.py

raw symbols SCIP
1 from rpython.config.config import *
2 import py, sys
3
4 def make_description():
5 gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
6 gcdummy = BoolOption('dummy', 'dummy', default=False)
7 booloption = BoolOption('bool', 'Test boolean option', default=True)
8 intoption = IntOption('int', 'Test int option', default=0)
9 floatoption = FloatOption('float', 'Test float option', default=2.3)
10 stroption = StrOption('str', 'Test string option', default="abc")
11
12 wantref_option = BoolOption('wantref', 'Test requires', default=False,
13 requires=[('gc.name', 'ref')])
14 wantframework_option = BoolOption('wantframework', 'Test requires',
15 default=False,
16 requires=[('gc.name', 'framework')])
17
18 gcgroup = OptionDescription('gc', '', [gcoption, gcdummy, floatoption])
19 descr = OptionDescription('pypy', '', [gcgroup, booloption,
20 wantref_option, stroption,
21 wantframework_option,
22 intoption])
23 return descr
24
25 def test_base_config():
26 descr = make_description()
27 config = Config(descr, bool=False)
28
29 assert config.gc.name == 'ref'
30 config.gc.name = 'framework'
31 assert config.gc.name == 'framework'
32 assert getattr(config, "gc.name") == 'framework'
33
34 assert config.gc.float == 2.3
35 assert config.int == 0
36 config.gc.float = 3.4
37 config.int = 123
38 assert config.gc.float == 3.4
39 assert config.int == 123
40
41 assert not config.wantref
42
43 assert config.str == "abc"
44 config.str = "def"
45 assert config.str == "def"
46
47 py.test.raises(ConfigError, 'config.gc.name = "foo"')
48 py.test.raises(AttributeError, 'config.gc.foo = "bar"')
49 py.test.raises(ConfigError, 'config.bool = 123')
50 py.test.raises(ConfigError, 'config.int = "hello"')
51 py.test.raises(ConfigError, 'config.gc.float = None')
52
53 config = Config(descr, bool=False)
54 assert config.gc.name == 'ref'
55 config.wantframework = True
56 py.test.raises(ConfigError, 'config.gc.name = "ref"')
57 config.gc.name = "framework"
58
59 def test___dir__():
60 descr = make_description()
61 config = Config(descr, bool=False)
62 attrs = dir(config)
63 assert '__repr__' in attrs # from the type
64 assert '_cfgimpl_values' in attrs # from self
65 if sys.version_info >= (2, 6):
66 assert 'gc' in attrs # custom attribute
67 #
68 attrs = dir(config.gc)
69 if sys.version_info >= (2, 6):
70 assert 'name' in attrs
71 assert 'dummy' in attrs
72 assert 'float' in attrs
73
74 def test_arbitrary_option():
75 descr = OptionDescription("top", "", [
76 ArbitraryOption("a", "no help", default=None)
77 ])
78 config = Config(descr)
79 config.a = []
80 config.a.append(1)
81 assert config.a == [1]
82
83 descr = OptionDescription("top", "", [
84 ArbitraryOption("a", "no help", defaultfactory=list)
85 ])
86 c1 = Config(descr)
87 c2 = Config(descr)
88 c1.a.append(1)
89 assert c2.a == []
90 assert c1.a == [1]
91
92 def test_annotator_folding():
93 from rpython.translator.interactive import Translation
94
95 gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
96 gcgroup = OptionDescription('gc', '', [gcoption])
97 descr = OptionDescription('pypy', '', [gcgroup])
98 config = Config(descr)
99
100 def f(x):
101 if config.gc.name == 'ref':
102 return x + 1
103 else:
104 return 'foo'
105
106 t = Translation(f, [int])
107 t.rtype()
108
109 block = t.context.graphs[0].startblock
110 assert len(block.exits[0].target.operations) == 0
111 assert len(block.operations) == 1
112 assert len(block.exits) == 1
113 assert block.operations[0].opname == 'int_add'
114
115 assert config._freeze_()
116 # does not raise, since it does not change the attribute
117 config.gc.name = "ref"
118 py.test.raises(TypeError, 'config.gc.name = "framework"')
119
120 def test_compare_configs():
121 descr = make_description()
122 conf1 = Config(descr)
123 conf2 = Config(descr, wantref=True)
124 assert conf1 != conf2
125 assert hash(conf1) != hash(conf2)
126 assert conf1.getkey() != conf2.getkey()
127 conf1.wantref = True
128 assert conf1 == conf2
129 assert hash(conf1) == hash(conf2)
130 assert conf1.getkey() == conf2.getkey()
131
132 def test_loop():
133 descr = make_description()
134 conf = Config(descr)
135 for (name, value), (gname, gvalue) in \
136 zip(conf.gc, [("name", "ref"), ("dummy", False)]):
137 assert name == gname
138 assert value == gvalue
139
140 def test_to_optparse():
141 gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref',
142 cmdline='--gc -g')
143 gcgroup = OptionDescription('gc', '', [gcoption])
144 descr = OptionDescription('pypy', '', [gcgroup])
145 config = Config(descr)
146
147 parser = to_optparse(config, ['gc.name'])
148 (options, args) = parser.parse_args(args=['--gc=framework'])
149
150 assert config.gc.name == 'framework'
151
152
153 config = Config(descr)
154 parser = to_optparse(config, ['gc.name'])
155 (options, args) = parser.parse_args(args=['-g ref'])
156 assert config.gc.name == 'ref'
157
158 # XXX strange exception
159 py.test.raises(SystemExit,
160 "(options, args) = parser.parse_args(args=['-g foobar'])")
161
162 def test_to_optparse_number():
163 intoption = IntOption('int', 'Int option test', cmdline='--int -i')
164 floatoption = FloatOption('float', 'Float option test',
165 cmdline='--float -f')
166 descr = OptionDescription('test', '', [intoption, floatoption])
167 config = Config(descr)
168
169 parser = to_optparse(config, ['int', 'float'])
170 (options, args) = parser.parse_args(args=['-i 2', '--float=0.1'])
171
172 assert config.int == 2
173 assert config.float == 0.1
174
175 py.test.raises(SystemExit,
176 "(options, args) = parser.parse_args(args=['--int=foo', '-f bar'])")
177
178 def test_to_optparse_bool():
179 booloption1 = BoolOption('bool1', 'Boolean option test', default=False,
180 cmdline='--bool1 -b')
181 booloption2 = BoolOption('bool2', 'Boolean option test', default=True,
182 cmdline='--with-bool2 -c')
183 booloption3 = BoolOption('bool3', 'Boolean option test', default=True,
184 cmdline='--bool3')
185 booloption4 = BoolOption('bool4', 'Boolean option test', default=True,
186 cmdline='--bool4', negation=False)
187 descr = OptionDescription('test', '', [booloption1, booloption2,
188 booloption3, booloption4])
189 config = Config(descr)
190
191 parser = to_optparse(config, ['bool1', 'bool2'])
192 (options, args) = parser.parse_args(args=['-b'])
193
194 assert config.bool1
195 assert config.bool2
196
197 config = Config(descr)
198 parser = to_optparse(config, ['bool1', 'bool2', 'bool3', 'bool4'])
199 (options, args) = parser.parse_args(args=['--without-bool2', '--no-bool3',
200 '--bool4'])
201 assert not config.bool1
202 assert not config.bool2
203 assert not config.bool3
204
205 py.test.raises(SystemExit,
206 "(options, args) = parser.parse_args(args=['-bfoo'])")
207 py.test.raises(SystemExit,
208 "(options, args) = parser.parse_args(args=['--no-bool4'])")
209
210 def test_config_start():
211 descr = make_description()
212 config = Config(descr)
213 parser = to_optparse(config, ["gc.*"])
214
215 options, args = parser.parse_args(args=["--gc-name=framework", "--gc-dummy"])
216 assert config.gc.name == "framework"
217 assert config.gc.dummy
218
219 def test_star_works_recursively():
220 descr = OptionDescription("top", "", [
221 OptionDescription("a", "", [
222 BoolOption("b1", "", default=False, cmdline="--b1"),
223 OptionDescription("sub", "", [
224 BoolOption("b2", "", default=False, cmdline="--b2")
225 ])
226 ]),
227 BoolOption("b3", "", default=False, cmdline="--b3"),
228 ])
229 config = Config(descr)
230 assert not config.a.b1
231 assert not config.a.sub.b2
232 parser = to_optparse(config, ['a.*'])
233 options, args = parser.parse_args(args=["--b1", "--b2"])
234 assert config.a.b1
235 assert config.a.sub.b2
236 py.test.raises(SystemExit,
237 "(options, args) = parser.parse_args(args=['--b3'])")
238
239 config = Config(descr)
240 assert not config.a.b1
241 assert not config.a.sub.b2
242 # does not lead to an option conflict
243 parser = to_optparse(config, ['a.*', 'a.sub.*'])
244 options, args = parser.parse_args(args=["--b1", "--b2"])
245 assert config.a.b1
246 assert config.a.sub.b2
247
248 def test_optparse_path_options():
249 gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
250 gcgroup = OptionDescription('gc', '', [gcoption])
251 descr = OptionDescription('pypy', '', [gcgroup])
252 config = Config(descr)
253
254 parser = to_optparse(config, ['gc.name'])
255 (options, args) = parser.parse_args(args=['--gc-name=framework'])
256
257 assert config.gc.name == 'framework'
258
259 def test_getpaths():
260 descr = make_description()
261 config = Config(descr)
262
263 assert config.getpaths() == ['gc.name', 'gc.dummy', 'gc.float', 'bool',
264 'wantref', 'str', 'wantframework',
265 'int']
266 assert config.getpaths() == descr.getpaths()
267 assert config.gc.getpaths() == ['name', 'dummy', 'float']
268 assert config.gc.getpaths() == descr.gc.getpaths()
269 assert config.getpaths(include_groups=True) == [
270 'gc', 'gc.name', 'gc.dummy', 'gc.float',
271 'bool', 'wantref', 'str', 'wantframework', 'int']
272 assert config.getpaths(True) == descr.getpaths(True)
273
274 def test_underscore_in_option_name():
275 descr = OptionDescription("opt", "", [
276 BoolOption("_foobar", "", default=False),
277 ])
278 config = Config(descr)
279 parser = to_optparse(config)
280 assert parser.has_option("--_foobar")
281
282 def test_none():
283 dummy1 = BoolOption('dummy1', 'doc dummy', default=False, cmdline=None)
284 dummy2 = BoolOption('dummy2', 'doc dummy', default=False, cmdline='--dummy')
285 group = OptionDescription('group', '', [dummy1, dummy2])
286 config = Config(group)
287
288 parser = to_optparse(config)
289 py.test.raises(SystemExit,
290 "(options, args) = parser.parse_args(args=['--dummy1'])")
291
292 def test_requirements_from_top():
293 descr = OptionDescription("test", '', [
294 BoolOption("toplevel", "", default=False),
295 OptionDescription("sub", '', [
296 BoolOption("opt", "", default=False,
297 requires=[("toplevel", True)])
298 ])
299 ])
300 config = Config(descr)
301 config.sub.opt = True
302 assert config.toplevel
303
304 def test_requirements_for_choice():
305 descr = OptionDescription("test", '', [
306 BoolOption("toplevel", "", default=False),
307 OptionDescription("s", '', [
308 ChoiceOption("type_system", "", ["ll", "oo"], "ll"),
309 ChoiceOption("backend", "",
310 ["c", "cli"], "c",
311 requires={
312 "c": [("s.type_system", "ll"),
313 ("toplevel", True)],
314 "cli": [("s.type_system", "oo")],
315 })
316 ])
317 ])
318 config = Config(descr)
319 config.s.backend = "cli"
320 assert config.s.type_system == "oo"
321
322 def test_choice_with_no_default():
323 descr = OptionDescription("test", "", [
324 ChoiceOption("backend", "", ["c", "cli"])])
325 config = Config(descr)
326 assert config.backend is None
327 config.backend = "c"
328
329 def test_overrides_are_defaults():
330 descr = OptionDescription("test", "", [
331 BoolOption("b1", "", default=False, requires=[("b2", False)]),
332 BoolOption("b2", "", default=False),
333 ])
334 config = Config(descr, b2=True)
335 assert config.b2
336 config.b1 = True
337 assert not config.b2
338 print config._cfgimpl_value_owners
339
340 def test_overrides_require_as_default():
341 descr = OptionDescription("test", "", [
342 ChoiceOption("backend", "", ['c', 'cli'], 'c',
343 requires={'c': [('type_system', 'll')],
344 'cli': [('type_system', 'oo')]}),
345 ChoiceOption("type_system", "", ['ll', 'oo'], 'll')
346 ])
347 config = Config(descr, backend='c')
348 config.set(backend=None, type_system=None)
349 config = Config(descr, backend='c')
350 config.set(backend='cli')
351 assert config.backend == 'cli'
352 assert config.type_system == 'oo'
353
354 def test_overrides_require_as_default_boolopt():
355 descr = OptionDescription("test", "", [
356 BoolOption("backend", "", default=False,
357 requires=[('type_system', True)]),
358 BoolOption("type_system", "", default=False)
359 ])
360 config = Config(descr, backend=True)
361 config.set(backend=False)
362 config.set(type_system=False)
363 assert config.backend == False
364 assert config.type_system == False
365
366 def test_overrides_dont_change_user_options():
367 descr = OptionDescription("test", "", [
368 BoolOption("b", "", default=False)])
369 config = Config(descr)
370 config.b = True
371 config.override({'b': False})
372 assert config.b
373
374 def test_str():
375 descr = make_description()
376 c = Config(descr)
377 print c # does not crash
378
379 def test_dwim_set():
380 descr = OptionDescription("opt", "", [
381 OptionDescription("sub", "", [
382 BoolOption("b1", ""),
383 ChoiceOption("c1", "", ['a', 'b', 'c'], 'a'),
384 BoolOption("d1", ""),
385 ]),
386 BoolOption("b2", ""),
387 BoolOption("d1", ""),
388 ])
389 c = Config(descr)
390 c.set(b1=False, c1='b')
391 assert not c.sub.b1
392 assert c.sub.c1 == 'b'
393 # new config, because you cannot change values once they are set
394 c = Config(descr)
395 c.set(b2=False, **{'sub.c1': 'c'})
396 assert not c.b2
397 assert c.sub.c1 == 'c'
398 py.test.raises(AmbigousOptionError, "c.set(d1=True)")
399 py.test.raises(NoMatchingOptionFound, "c.set(unknown='foo')")
400
401 def test_more_set():
402 descr = OptionDescription("opt", "", [
403 OptionDescription("s1", "", [
404 BoolOption("a", "", default=False)]),
405 IntOption("int", "", default=42)])
406 d = {'s1.a': True, 'int': 23}
407 config = Config(descr)
408 config.set(**d)
409 assert config.s1.a
410 assert config.int == 23
411
412 def test_optparse_help():
413 import cStringIO
414 descr = OptionDescription("opt", "", [
415 BoolOption("bool1", 'do bool1', default=False, cmdline='--bool1'),
416 BoolOption("bool2", 'do bool2', default=False, cmdline='--bool2', negation=False),
417 BoolOption("bool3", 'do bool3', default=True, cmdline='--bool3'),
418 ChoiceOption("choice", "choose!", ['a', 'b', 'c'], 'a', '--choice'),
419 ChoiceOption("choice2", "choose2!", ['x', 'y', 'z'], None, '--choice2'),
420 StrOption("str", 'specify xyz', default='hello', cmdline='--str'),
421 ])
422 conf = Config(descr)
423 parser = to_optparse(conf)
424 out = cStringIO.StringIO()
425 parser.print_help(out)
426 help = out.getvalue()
427 #print help
428 assert "do bool1\n" in help
429 assert "unset option set by --bool1 [default]" in help
430 assert "do bool2\n" in help
431 assert "do bool3 [default]" in help
432 assert "choose! [CHOICE=a|b|c, default: a]" in help
433 assert "choose2! [CHOICE2=x|y|z]" in help
434 assert "specify xyz [default: hello]" in help
435
436 def test_make_dict():
437 descr = OptionDescription("opt", "", [
438 OptionDescription("s1", "", [
439 BoolOption("a", "", default=False)]),
440 IntOption("int", "", default=42)])
441 config = Config(descr)
442 d = make_dict(config)
443 assert d == {"s1.a": False, "int": 42}
444 config.int = 43
445 config.s1.a = True
446 d = make_dict(config)
447 assert d == {"s1.a": True, "int": 43}
448
449 def test_copy():
450 descr = OptionDescription("opt", "", [
451 OptionDescription("s1", "", [
452 BoolOption("a", "", default=False)]),
453 IntOption("int", "", default=42)])
454 c1 = Config(descr)
455 c1.int = 43
456 c2 = c1.copy()
457 assert c2.int == 43
458 assert not c2.s1.a
459 c2.s1.a = True
460 assert c2.s1.a
461 py.test.raises(ConfigError, "c2.int = 44")
462 c2 = c1.copy(as_default=True)
463 assert c2.int == 43
464 assert not c2.s1.a
465 c2.s1.a = True
466 assert c2.s1.a
467 c2.int = 44 # does not crash
468
469 def test_bool_suggests():
470 descr = OptionDescription("test", '', [
471 BoolOption("toplevel", "", default=False),
472 BoolOption("opt", "", default=False,
473 suggests=[("toplevel", True)])
474 ])
475 c = Config(descr)
476 assert not c.toplevel
477 assert not c.opt
478 c.opt = True
479 assert c.opt
480 assert c.toplevel
481 # does not crash
482 c.toplevel = False
483 assert not c.toplevel
484
485 c = Config(descr)
486 c.toplevel = False
487 assert not c.toplevel
488 # does not crash
489 c.opt = True
490 assert c.opt
491 assert not c.toplevel
492
493 def test_suggests_can_fail():
494 descr = OptionDescription("test", '', [
495 BoolOption("t1", "", default=False),
496 BoolOption("t2", "", default=False,
497 requires=[("t3", True)]),
498 BoolOption("t3", "", default=False),
499 BoolOption("opt", "", default=False,
500 suggests=[("t1", True), ("t2", True)])
501 ])
502 c = Config(descr)
503 assert not c.t1
504 assert not c.t2
505 assert not c.t3
506 assert not c.opt
507 c.opt = True
508 assert c.opt
509 assert c.t1
510 assert c.t2
511 assert c.t3
512 # does not crash
513 c.t2 = False
514 assert not c.t2
515
516 c = Config(descr)
517 c.t3 = False
518 assert not c.t3
519 # does not crash
520 c.opt = True
521 assert c.opt
522 assert not c.t3
523 assert not c.t2
524
525 def test_suggests_can_fail_choiceopt():
526 # this is what occurs in "./translate.py --gcrootfinder=asmgcc --jit"
527 # with --jit suggesting the boehm gc, but --gcrootfinder requiring the
528 # framework gctransformer.
529 descr = OptionDescription("test", '', [
530 ChoiceOption("t1", "", ["a", "b"], default="a"),
531 ChoiceOption("t2", "", ["c", "d"], default="c",
532 requires={"d": [("t3", "f")]}),
533 ChoiceOption("t3", "", ["e", "f"], default="e"),
534 ChoiceOption("opt", "", ["g", "h"], default="g",
535 suggests={"h": [("t1", "b"), ("t2", "d")]})
536 ])
537 c = Config(descr)
538 assert c.t1 == 'a'
539 assert c.t2 == 'c'
540 assert c.t3 == 'e'
541 assert c.opt == 'g'
542 c.opt = "h"
543 assert c.opt == 'h'
544 assert c.t1 == 'b'
545 assert c.t2 == 'd'
546 assert c.t3 == 'f'
547 # does not crash
548 c.t2 = 'c'
549 assert c.t2 == 'c'
550
551 c = Config(descr)
552 c.t3 = 'e'
553 assert c.t3 == 'e'
554 # does not crash
555 c.opt = 'h'
556 assert c.opt == 'h'
557 assert c.t3 == 'e'
558 assert c.t2 == 'c'
559
560
561 def test_choice_suggests():
562 descr = OptionDescription("test", '', [
563 BoolOption("toplevel", "", default=False),
564 ChoiceOption("opt", "", ["a", "b", "c"],
565 "a",
566 suggests={"b": [("toplevel", True)]})
567 ])
568 c = Config(descr)
569 assert not c.toplevel
570 assert c.opt == "a"
571 c.opt = "b"
572 assert c.opt == "b"
573 assert c.toplevel
574 # does not crash
575 c.toplevel = False
576 assert not c.toplevel
577
578 c = Config(descr)
579 c.toplevel = False
580 assert not c.toplevel
581 # does not crash
582 c.opt = "b"
583 assert c.opt == "b"
584 assert not c.toplevel
585
586
587 def test_bogus_suggests():
588 descr = OptionDescription("test", '', [
589 BoolOption("toplevel", "", suggests=[("opt", "bogusvalue")]),
590 ChoiceOption("opt", "", ["a", "b", "c"], "a"),
591 ])
592 c = Config(descr)
593 py.test.raises(ConfigError, "c.toplevel = True")
594
595
596 def test_delattr():
597 descr = OptionDescription("opt", "", [
598 OptionDescription("s1", "", [
599 BoolOption("a", "", default=False)]),
600 IntOption("int", "", default=42)])
601 c = Config(descr)
602 c.int = 45
603 assert c.int == 45
604 del c.int
605 assert c.int == 42
606 c.int = 45
607 assert c.int == 45
608
609 def test_validator():
610 def my_validator_1(config):
611 assert config is c
612
613 def my_validator_2(config):
614 assert config is c
615 raise ConflictConfigError
616
617 descr = OptionDescription("opt", "", [
618 BoolOption('booloption1', 'option test1', default=False,
619 validator=my_validator_1),
620 BoolOption('booloption2', 'option test2', default=False,
621 validator=my_validator_2),
622 BoolOption('booloption3', 'option test3', default=False,
623 requires=[("booloption2", True)]),
624 BoolOption('booloption4', 'option test4', default=False,
625 suggests=[("booloption2", True)]),
626 ])
627 c = Config(descr)
628 c.booloption1 = True
629 py.test.raises(ConfigError, "c.booloption2 = True")
630 assert c.booloption2 is False
631 py.test.raises(ConfigError, "c.booloption3 = True")
632 assert c.booloption2 is False
633 c.booloption4 = True
634 assert c.booloption2 is False
635 c.booloption2 = False
636 assert c.booloption2 is False
637
638 def test_suggested_owner_does_not_override():
639 descr = OptionDescription("test", '', [
640 BoolOption("toplevel", "", default=False),
641 BoolOption("opt", "", default=False,
642 suggests=[("toplevel", False)]),
643 BoolOption("opt2", "", default=False,
644 suggests=[("toplevel", True)]),
645 ])
646 c = Config(descr)
647 c.toplevel = False
648 c.opt = True # bug: sets owner of toplevel back to 'suggested'
649 c.opt2 = True # and this overrides toplevel because it's only suggested
650 assert c.toplevel == False
651 assert c.opt == True
652 assert c.opt2 == True