Debian Codegraph @

test/test_metaconfig.py

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 # BSD Licence
2 # Copyright (c) 2010, Science & Technology Facilities Council (STFC)
3 # All rights reserved.
4 #
5 # See the LICENSE file in the source distribution of this software for
6 # the full license text.
7
8 """
9 Metaconfig tests.
10
11 Requires nose.
12
13 """
14
15 import os
16
17 from unittest import TestCase
18 import tempfile
19 from cStringIO import StringIO
20
21 import ConfigParser
22
23 from metaconfig import MetaConfig, Error
24
25 def _make_test_config(**kwargs):
26 conf = ConfigParser.RawConfigParser()
27
28 conf.add_section('foo')
29 conf.set('foo', 'a', '42')
30 conf.set('foo', 'b', 'line 1\nline 2')
31
32 conf.add_section('bar')
33 for k, v in kwargs.items():
34 conf.set('bar', k, v)
35
36 return conf
37
38 _metaconfig_pat = """
39
40 [metaconfig]
41 configs = p1 p1.p2
42
43 [p1:foo]
44 a = 42
45 b = line 1
46 line 2
47
48 [p1:bar]
49 %s
50
51 [p1.p2:foo]
52 a = 12
53 b = no
54 c = yes
55
56 """
57
58 def _make_metaconfig(**kwargs):
59 args = '\n'.join('%s = %s' % (k, kwargs[k]) for k in kwargs)
60 text = _metaconfig_pat % args
61 fh = StringIO()
62 fh.write(text)
63 fh.seek(0)
64
65 mf = ConfigParser.ConfigParser()
66 mf.readfp(fh)
67
68 return mf
69
70
71 class Test1(TestCase):
72 def setUp(self):
73 self.mf = MetaConfig()
74 self.mf.add_config('p1', _make_test_config(x='1'))
75 self.mf.add_config('p1.p2', _make_test_config(x='2'))
76
77 def test_1(self):
78 conf = self.mf.get_config('p1')
79
80 assert conf.get('foo', 'a') == '42'
81
82 def test_2(self):
83 conf = self.mf.get_config('x')
84 assert conf.sections() == []
85
86 def test_3(self):
87 conf = self.mf.get_config('p1.p2')
88
89 assert conf.get('bar', 'x') == '2'
90
91 def test_4(self):
92 # Request non-existent conf with existing parent.
93 conf = self.mf.get_config('p1.p3')
94
95 assert conf.get('bar', 'x') == '1'
96
97 class Test2(TestCase):
98 def setUp(self):
99 self.mf = MetaConfig()
100
101 fd, self.config_file = tempfile.mkstemp()
102 fh = os.fdopen(fd, 'w')
103 conf = _make_test_config(x='1')
104 conf.write(fh)
105 fh.close()
106
107
108 def tearDown(self):
109 os.remove(self.config_file)
110
111 def test_1(self):
112 self.mf.add_config_fh('p1', open(self.config_file))
113
114 conf = self.mf.get_config('p1')
115
116 assert conf.get('foo', 'a') == '42'
117 assert conf.get('bar', 'x') == '1'
118
119 def test_2(self):
120 self.mf.add_config_file('p1', self.config_file)
121
122 conf = self.mf.get_config('p1')
123
124 assert conf.get('foo', 'a') == '42'
125 assert conf.get('bar', 'x') == '1'
126
127 class Test3(TestCase):
128 def setUp(self):
129 self.mf = MetaConfig.from_config(_make_metaconfig(x='1'))
130
131 def test_1(self):
132 conf = self.mf.get_config('p1')
133
134 assert conf.get('foo', 'a') == '42'
135 assert conf.get('bar', 'x') == '1'