root / logilab.pylintinstaller / logilab / common / test / unittest_configuration.py

Revision 202:d67e86292521, 8.6 kB (checked in by tziade@…, 11 months ago)

added logilab.pylintinstaller

Line 
1import tempfile
2import os
3from cStringIO import StringIO
4from sys import version_info
5
6from logilab.common.testlib import TestCase, unittest_main
7from logilab.common.configuration import Configuration, OptionValueError, \
8     OptionsManagerMixIn, OptionsProviderMixIn, Method, read_old_config
9
10options = [('dothis', {'type':'yn', 'default': True, 'metavar': '<y or n>'}),
11           ('value', {'type': 'string', 'metavar': '<string>', 'short': 'v'}),
12           ('multiple', {'type': 'csv', 'default': ('yop',),
13                         'metavar': '<comma separated values>',
14                         'help': 'you can also document the option'}),
15           ('number', {'type': 'int', 'default':2, 'metavar':'<int>'}),
16           ('choice', {'type': 'choice', 'default':'yo', 'choices': ('yo', 'ye'),
17                       'metavar':'<yo|ye>'}),
18           ('multiple-choice', {'type': 'multiple_choice', 'default':('yo', 'ye'),
19                                'choices': ('yo', 'ye', 'yu', 'yi', 'ya'),
20                                'metavar':'<yo|ye>'}),
21           ('named', {'type':'named', 'default':Method('get_named'),
22                      'metavar': '<key=val>'}),
23           ]
24
25class MyConfiguration(Configuration):
26    """test configuration"""
27    def get_named(self):
28        return {'key': 'val'}
29   
30class ConfigurationTC(TestCase):
31   
32    def setUp(self):
33        self.cfg = MyConfiguration(name='test', options=options, usage='Just do it ! (tm)')
34
35    def test_default(self):
36        cfg = self.cfg
37        self.assertEquals(cfg['dothis'], True)
38        self.assertEquals(cfg['value'], None)
39        self.assertEquals(cfg['multiple'], ('yop',))
40        self.assertEquals(cfg['number'], 2)
41        self.assertEquals(cfg['choice'], 'yo')
42        self.assertEquals(cfg['multiple-choice'], ('yo', 'ye'))
43        self.assertEquals(cfg['named'], {'key': 'val'})
44
45    def test_base(self):
46        cfg = self.cfg
47        cfg.set_option('number', '0')
48        self.assertEquals(cfg['number'], 0)
49        self.assertRaises(OptionValueError, cfg.set_option, 'number', 'youpi')
50        self.assertRaises(OptionValueError, cfg.set_option, 'choice', 'youpi')
51        self.assertRaises(OptionValueError, cfg.set_option, 'multiple-choice', ('yo', 'y', 'ya'))
52        cfg.set_option('multiple-choice', 'yo, ya')
53        self.assertEquals(cfg['multiple-choice'], ['yo', 'ya'])
54        self.assertEquals(cfg.get('multiple-choice'), ['yo', 'ya'])
55        self.assertEquals(cfg.get('whatever'), None)
56
57    def test_load_command_line_configuration(self):
58        cfg = self.cfg
59        args = cfg.load_command_line_configuration(['--choice', 'ye', '--number', '4',
60                                                    '--multiple=1,2,3', '--dothis=n',
61                                                    'other', 'arguments'])
62        self.assertEquals(args, ['other', 'arguments'])
63        self.assertEquals(cfg['dothis'], False)
64        self.assertEquals(cfg['multiple'], ['1', '2', '3'])
65        self.assertEquals(cfg['number'], 4)
66        self.assertEquals(cfg['choice'], 'ye')
67        self.assertEquals(cfg['value'], None)
68        args = cfg.load_command_line_configuration(['-v', 'duh'])
69        self.assertEquals(args, [])
70        self.assertEquals(cfg['value'], 'duh')
71        self.assertEquals(cfg['dothis'], False)
72        self.assertEquals(cfg['multiple'], ['1', '2', '3'])
73        self.assertEquals(cfg['number'], 4)
74        self.assertEquals(cfg['choice'], 'ye')
75       
76    def test_load_configuration(self):
77        cfg = self.cfg
78        args = cfg.load_configuration(choice='ye', number='4',
79                                      multiple='1,2,3', dothis='n',
80                                      multiple_choice=('yo', 'ya'))
81        self.assertEquals(cfg['dothis'], False)
82        self.assertEquals(cfg['multiple'], ['1', '2', '3'])
83        self.assertEquals(cfg['number'], 4)
84        self.assertEquals(cfg['choice'], 'ye')
85        self.assertEquals(cfg['value'], None)
86        self.assertEquals(cfg['multiple-choice'], ('yo', 'ya'))
87       
88    def test_generate_config(self):
89        stream = StringIO()
90        self.cfg.generate_config(stream)
91        self.assertLinesEquals(stream.getvalue().strip(), """# test configuration
92[TEST]
93
94dothis=yes
95
96#value=
97
98# you can also document the option
99multiple=yop
100
101number=2
102
103choice=yo
104
105multiple-choice=yo,ye
106
107named=key:val
108""")
109       
110    def test_generate_config_with_space_string(self):
111        self.cfg['value'] = '    '
112        stream = StringIO()
113        self.cfg.generate_config(stream)
114        self.assertLinesEquals(stream.getvalue().strip(), """# test configuration
115[TEST]
116
117dothis=yes
118
119value='    '
120
121# you can also document the option
122multiple=yop
123
124number=2
125
126choice=yo
127
128multiple-choice=yo,ye
129
130named=key:val
131""")
132       
133
134    def test_loopback(self):
135        cfg = self.cfg
136        f = tempfile.mktemp()
137        stream = open(f, 'w')
138        try:
139            cfg.generate_config(stream)
140            stream.close()
141            new_cfg = MyConfiguration(name='testloop', options=options)
142            new_cfg.load_file_configuration(f)
143            self.assertEquals(cfg['dothis'], new_cfg['dothis'])
144            self.assertEquals(cfg['multiple'], new_cfg['multiple'])
145            self.assertEquals(cfg['number'], new_cfg['number'])
146            self.assertEquals(cfg['choice'], new_cfg['choice'])
147            self.assertEquals(cfg['value'], new_cfg['value'])
148            self.assertEquals(cfg['multiple-choice'], new_cfg['multiple-choice'])
149        finally:
150            os.remove(f)
151       
152    def test_help(self):
153        self.cfg.add_help_section('bonus', 'a nice additional help')
154        help = self.cfg.help().strip()
155        # at least in python 2.4.2 the output is:
156        # '  -v <string>, --value=<string>'
157        # it is not unlikely some optik/optparse versions do print -v<string>
158        # so accept both
159        help = help.replace(' -v <string>, ', ' -v<string>, ')
160        if version_info >= (2, 5):
161            self.assertLinesEquals(help, """Usage: Just do it ! (tm)
162
163Options:
164  -h, --help            show this help message and exit
165  --dothis=<y or n>     
166  -v<string>, --value=<string>
167  --multiple=<comma separated values>
168                        you can also document the option [current: none]
169  --number=<int>       
170  --choice=<yo|ye>     
171  --multiple-choice=<yo|ye>
172  --named=<key=val>     
173
174  Bonus:
175    a nice additional help
176""".strip())
177        elif version_info >= (2, 4):
178            self.assertLinesEquals(help, """usage: Just do it ! (tm)
179
180options:
181  -h, --help            show this help message and exit
182  --dothis=<y or n>     
183  -v<string>, --value=<string>
184  --multiple=<comma separated values>
185                        you can also document the option [current: none]
186  --number=<int>       
187  --choice=<yo|ye>     
188  --multiple-choice=<yo|ye>
189  --named=<key=val>     
190
191  Bonus:
192    a nice additional help
193""".strip())
194        else:
195            self.assertLinesEquals(help, """usage: Just do it ! (tm)
196
197options:
198  -h, --help            show this help message and exit
199  --dothis=<y or n>     
200  -v<string>, --value=<string>
201  --multiple=<comma separated values>
202                        you can also document the option
203  --number=<int>       
204  --choice=<yo|ye>     
205  --multiple-choice=<yo|ye>
206  --named=<key=val>     
207
208  Bonus:
209    a nice additional help
210""".strip())
211
212
213    def test_manpage(self):
214        from logilab.common import __pkginfo__
215        self.cfg.generate_manpage(__pkginfo__, stream=StringIO())
216
217    def test_rewrite_config(self):
218        changes = [('renamed', 'renamed', 'choice'),
219                   ('moved', 'named', 'old', 'test'),
220                   ]
221        read_old_config(self.cfg, changes, 'data/test.ini')
222        stream = StringIO()
223        self.cfg.generate_config(stream)
224        self.assertLinesEquals(stream.getvalue().strip(), """# test configuration
225[TEST]
226
227dothis=yes
228
229value='    '
230
231# you can also document the option
232multiple=yop
233
234number=2
235
236choice=yo
237
238multiple-choice=yo,ye
239
240named=key:val
241""")
242       
243class Linter(OptionsManagerMixIn, OptionsProviderMixIn):
244    options = (
245        ('profile', {'type' : 'yn', 'metavar' : '<y_or_n>',
246                     'default': False,
247                     'help' : 'Profiled execution.'}),
248        )
249    def __init__(self):
250        OptionsManagerMixIn.__init__(self, usage="")
251        OptionsProviderMixIn.__init__(self)
252        self.register_options_provider(self)
253        self.load_provider_defaults()
254
255class RegrTC(TestCase):
256
257    def setUp(self):
258        self.linter = Linter()
259       
260    def test_load_defaults(self):
261        self.linter.load_command_line_configuration([])
262        self.assertEquals(self.linter.config.profile, False)
263       
264       
265if __name__ == '__main__':
266    unittest_main()
Note: See TracBrowser for help on using the browser.