1 import tempfile
2 import os
3 from cStringIO import StringIO
4 from sys import version_info
5
6 from logilab.common.testlib import TestCase, unittest_main
7 from logilab.common.optik_ext import OptionValueError
8 from logilab.common.configuration import Configuration, \
9 OptionsManagerMixIn, OptionsProviderMixIn, Method, read_old_config
10
11 options = [('dothis', {'type':'yn', 'action': 'store', 'default': True, 'metavar': '<y or n>'}),
12 ('value', {'type': 'string', 'metavar': '<string>', 'short': 'v'}),
13 ('multiple', {'type': 'csv', 'default': ('yop','yep'),
14 'metavar': '<comma separated values>',
15 'help': 'you can also document the option'}),
16 ('number', {'type': 'int', 'default':2, 'metavar':'<int>', 'help': 'boom'}),
17 ('choice', {'type': 'choice', 'default':'yo', 'choices': ('yo', 'ye'),
18 'metavar':'<yo|ye>'}),
19 ('multiple-choice', {'type': 'multiple_choice', 'default':('yo', 'ye'),
20 'choices': ('yo', 'ye', 'yu', 'yi', 'ya'),
21 'metavar':'<yo|ye>'}),
22 ('named', {'type':'named', 'default':Method('get_named'),
23 'metavar': '<key=val>'}),
24
25 ('diffgroup', {'type':'string', 'default':'pouet', 'metavar': '<key=val>',
26 'group': 'agroup'}),
27
28 ]
29
31 """test configuration"""
34
36
39
41 cfg = self.cfg
42 self.assertEquals(cfg['dothis'], True)
43 self.assertEquals(cfg['value'], None)
44 self.assertEquals(cfg['multiple'], ('yop','yep'))
45 self.assertEquals(cfg['number'], 2)
46 self.assertEquals(cfg['choice'], 'yo')
47 self.assertEquals(cfg['multiple-choice'], ('yo', 'ye'))
48 self.assertEquals(cfg['named'], {'key': 'val'})
49
51 cfg = self.cfg
52 cfg.set_option('number', '0')
53 self.assertEquals(cfg['number'], 0)
54 self.assertRaises(OptionValueError, cfg.set_option, 'number', 'youpi')
55 self.assertRaises(OptionValueError, cfg.set_option, 'choice', 'youpi')
56 self.assertRaises(OptionValueError, cfg.set_option, 'multiple-choice', ('yo', 'y', 'ya'))
57 cfg.set_option('multiple-choice', 'yo, ya')
58 self.assertEquals(cfg['multiple-choice'], ['yo', 'ya'])
59 self.assertEquals(cfg.get('multiple-choice'), ['yo', 'ya'])
60 self.assertEquals(cfg.get('whatever'), None)
61
63 cfg = self.cfg
64 args = cfg.load_command_line_configuration(['--choice', 'ye', '--number', '4',
65 '--multiple=1,2,3', '--dothis=n',
66 'other', 'arguments'])
67 self.assertEquals(args, ['other', 'arguments'])
68 self.assertEquals(cfg['dothis'], False)
69 self.assertEquals(cfg['multiple'], ['1', '2', '3'])
70 self.assertEquals(cfg['number'], 4)
71 self.assertEquals(cfg['choice'], 'ye')
72 self.assertEquals(cfg['value'], None)
73 args = cfg.load_command_line_configuration(['-v', 'duh'])
74 self.assertEquals(args, [])
75 self.assertEquals(cfg['value'], 'duh')
76 self.assertEquals(cfg['dothis'], False)
77 self.assertEquals(cfg['multiple'], ['1', '2', '3'])
78 self.assertEquals(cfg['number'], 4)
79 self.assertEquals(cfg['choice'], 'ye')
80
82 cfg = self.cfg
83 args = cfg.load_configuration(choice='ye', number='4',
84 multiple='1,2,3', dothis='n',
85 multiple_choice=('yo', 'ya'))
86 self.assertEquals(cfg['dothis'], False)
87 self.assertEquals(cfg['multiple'], ['1', '2', '3'])
88 self.assertEquals(cfg['number'], 4)
89 self.assertEquals(cfg['choice'], 'ye')
90 self.assertEquals(cfg['value'], None)
91 self.assertEquals(cfg['multiple-choice'], ('yo', 'ya'))
92
94 file = tempfile.mktemp()
95 stream = open(file, 'w')
96 try:
97 stream.write("""# test configuration
98 [Test]
99
100 dothis=no
101
102 #value=
103
104 # you can also document the option
105 multiple=yop,yepii
106
107 # boom
108 number=3
109
110 choice=yo
111
112 multiple-choice=yo,ye
113
114 named=key:val
115
116
117 [agroup]
118
119 diffgroup=zou
120 """)
121 stream.close()
122 self.cfg.load_file_configuration(file)
123 self.assertEquals(self.cfg['dothis'], False)
124 self.assertEquals(self.cfg['value'], None)
125 self.assertEquals(self.cfg['multiple'], ['yop','yepii'])
126 self.assertEquals(self.cfg['diffgroup'], 'zou')
127 finally:
128 os.remove(file)
129
131 stream = StringIO()
132 self.cfg.generate_config(stream)
133 self.assertLinesEquals(stream.getvalue().strip(), """# test configuration
134 [TEST]
135
136 dothis=yes
137
138 #value=
139
140 # you can also document the option
141 multiple=yop,yep
142
143 # boom
144 number=2
145
146 choice=yo
147
148 multiple-choice=yo,ye
149
150 named=key:val
151
152
153 [AGROUP]
154
155 diffgroup=pouet
156 """)
157
159 self.cfg['value'] = ' '
160 stream = StringIO()
161 self.cfg.generate_config(stream)
162 self.assertLinesEquals(stream.getvalue().strip(), """# test configuration
163 [TEST]
164
165 dothis=yes
166
167 value=' '
168
169 # you can also document the option
170 multiple=yop,yep
171
172 # boom
173 number=2
174
175 choice=yo
176
177 multiple-choice=yo,ye
178
179 named=key:val
180
181
182 [AGROUP]
183
184 diffgroup=pouet
185 """)
186
187
189 cfg = self.cfg
190 f = tempfile.mktemp()
191 stream = open(f, 'w')
192 try:
193 cfg.generate_config(stream)
194 stream.close()
195 new_cfg = MyConfiguration(name='testloop', options=options)
196 new_cfg.load_file_configuration(f)
197 self.assertEquals(cfg['dothis'], new_cfg['dothis'])
198 self.assertEquals(cfg['multiple'], new_cfg['multiple'])
199 self.assertEquals(cfg['number'], new_cfg['number'])
200 self.assertEquals(cfg['choice'], new_cfg['choice'])
201 self.assertEquals(cfg['value'], new_cfg['value'])
202 self.assertEquals(cfg['multiple-choice'], new_cfg['multiple-choice'])
203 finally:
204 os.remove(f)
205
207 self.assertRaises(OptionValueError,
208 self.cfg.__setitem__, 'multiple-choice', ('a', 'b'))
209 self.cfg['multiple-choice'] = ('yi', 'ya')
210 self.assertEquals(self.cfg['multiple-choice'], ('yi', 'ya'))
211
213 self.cfg.add_help_section('bonus', 'a nice additional help')
214 help = self.cfg.help().strip()
215
216
217
218
219 help = help.replace(' -v <string>, ', ' -v<string>, ')
220 if version_info >= (2, 5):
221 self.assertLinesEquals(help, """Usage: Just do it ! (tm)
222
223 Options:
224 -h, --help show this help message and exit
225 --dothis=<y or n>
226 -v<string>, --value=<string>
227 --multiple=<comma separated values>
228 you can also document the option [current: yop,yep]
229 --number=<int> boom [current: 2]
230 --choice=<yo|ye>
231 --multiple-choice=<yo|ye>
232 --named=<key=val>
233
234 Agroup:
235 --diffgroup=<key=val>
236
237 Bonus:
238 a nice additional help
239 """, striplines=True)
240 elif version_info >= (2, 4):
241 self.assertLinesEquals(help, """usage: Just do it ! (tm)
242
243 options:
244 -h, --help show this help message and exit
245 --dothis=<y or n>
246 -v<string>, --value=<string>
247 --multiple=<comma separated values>
248 you can also document the option [current: yop,yep]
249 --number=<int> boom [current: 2]
250 --choice=<yo|ye>
251 --multiple-choice=<yo|ye>
252 --named=<key=val>
253
254 Bonus:
255 a nice additional help
256 """, striplines=True)
257 else:
258 self.assertLinesEquals(help, """usage: Just do it ! (tm)
259
260 options:
261 -h, --help show this help message and exit
262 --dothis=<y or n>
263 -v<string>, --value=<string>
264 --multiple=<comma separated values>
265 you can also document the option
266 --number=<int>
267 --choice=<yo|ye>
268 --multiple-choice=<yo|ye>
269 --named=<key=val>
270
271 Bonus:
272 a nice additional help
273 """, striplines=True)
274
275
276 - def test_manpage(self):
277 from logilab.common import __pkginfo__
278 self.cfg.generate_manpage(__pkginfo__, stream=StringIO())
279
281 changes = [('renamed', 'renamed', 'choice'),
282 ('moved', 'named', 'old', 'test'),
283 ]
284 read_old_config(self.cfg, changes, 'data/test.ini')
285 stream = StringIO()
286 self.cfg.generate_config(stream)
287 self.assertLinesEquals(stream.getvalue().strip(), """# test configuration
288 [TEST]
289
290 dothis=yes
291
292 value=' '
293
294 # you can also document the option
295 multiple=yop
296
297 # boom
298 number=2
299
300 choice=yo
301
302 multiple-choice=yo,ye
303
304 named=key:val
305
306
307 [AGROUP]
308
309 diffgroup=pouet
310 """)
311
312 -class Linter(OptionsManagerMixIn, OptionsProviderMixIn):
313 options = (
314 ('profile', {'type' : 'yn', 'metavar' : '<y_or_n>',
315 'default': False,
316 'help' : 'Profiled execution.'}),
317 )
323
332
333
334 if __name__ == '__main__':
335 unittest_main()
336