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
|
import tempfile
import os
from cStringIO import StringIO
from sys import version_info
from logilab.common.testlib import TestCase, unittest_main
from logilab.common.configuration import Configuration, OptionValueError, \
OptionsManagerMixIn, OptionsProviderMixIn, Method, read_old_config
options = [('dothis', {'type':'yn', 'default': True, 'metavar': '<y or n>'}),
('value', {'type': 'string', 'metavar': '<string>', 'short': 'v'}),
('multiple', {'type': 'csv', 'default': ('yop',),
'metavar': '<comma separated values>',
'help': 'you can also document the option'}),
('number', {'type': 'int', 'default':2, 'metavar':'<int>'}),
('choice', {'type': 'choice', 'default':'yo', 'choices': ('yo', 'ye'),
'metavar':'<yo|ye>'}),
('multiple-choice', {'type': 'multiple_choice', 'default':('yo', 'ye'),
'choices': ('yo', 'ye', 'yu', 'yi', 'ya'),
'metavar':'<yo|ye>'}),
('named', {'type':'named', 'default':Method('get_named'),
'metavar': '<key=val>'}),
]
class MyConfiguration(Configuration):
"""test configuration"""
def get_named(self):
return {'key': 'val'}
class ConfigurationTC(TestCase):
def setUp(self):
self.cfg = MyConfiguration(name='test', options=options, usage='Just do it ! (tm)')
def test_default(self):
cfg = self.cfg
self.assertEquals(cfg['dothis'], True)
self.assertEquals(cfg['value'], None)
self.assertEquals(cfg['multiple'], ('yop',))
self.assertEquals(cfg['number'], 2)
self.assertEquals(cfg['choice'], 'yo')
self.assertEquals(cfg['multiple-choice'], ('yo', 'ye'))
self.assertEquals(cfg['named'], {'key': 'val'})
def test_base(self):
cfg = self.cfg
cfg.set_option('number', '0')
self.assertEquals(cfg['number'], 0)
self.assertRaises(OptionValueError, cfg.set_option, 'number', 'youpi')
self.assertRaises(OptionValueError, cfg.set_option, 'choice', 'youpi')
self.assertRaises(OptionValueError, cfg.set_option, 'multiple-choice', ('yo', 'y', 'ya'))
cfg.set_option('multiple-choice', 'yo, ya')
self.assertEquals(cfg['multiple-choice'], ['yo', 'ya'])
self.assertEquals(cfg.get('multiple-choice'), ['yo', 'ya'])
self.assertEquals(cfg.get('whatever'), None)
def test_load_command_line_configuration(self):
cfg = self.cfg
args = cfg.load_command_line_configuration(['--choice', 'ye', '--number', '4',
'--multiple=1,2,3', '--dothis=n',
'other', 'arguments'])
self.assertEquals(args, ['other', 'arguments'])
self.assertEquals(cfg['dothis'], False)
self.assertEquals(cfg['multiple'], ['1', '2', '3'])
self.assertEquals(cfg['number'], 4)
self.assertEquals(cfg['choice'], 'ye')
self.assertEquals(cfg['value'], None)
args = cfg.load_command_line_configuration(['-v', 'duh'])
self.assertEquals(args, [])
self.assertEquals(cfg['value'], 'duh')
self.assertEquals(cfg['dothis'], False)
self.assertEquals(cfg['multiple'], ['1', '2', '3'])
self.assertEquals(cfg['number'], 4)
self.assertEquals(cfg['choice'], 'ye')
def test_load_configuration(self):
cfg = self.cfg
args = cfg.load_configuration(choice='ye', number='4',
multiple='1,2,3', dothis='n',
multiple_choice=('yo', 'ya'))
self.assertEquals(cfg['dothis'], False)
self.assertEquals(cfg['multiple'], ['1', '2', '3'])
self.assertEquals(cfg['number'], 4)
self.assertEquals(cfg['choice'], 'ye')
self.assertEquals(cfg['value'], None)
self.assertEquals(cfg['multiple-choice'], ('yo', 'ya'))
def test_generate_config(self):
stream = StringIO()
self.cfg.generate_config(stream)
self.assertLinesEquals(stream.getvalue().strip(), """# test configuration
[TEST]
dothis=yes
#value=
# you can also document the option
multiple=yop
number=2
choice=yo
multiple-choice=yo,ye
named=key:val
""")
def test_generate_config_with_space_string(self):
self.cfg['value'] = ' '
stream = StringIO()
self.cfg.generate_config(stream)
self.assertLinesEquals(stream.getvalue().strip(), """# test configuration
[TEST]
dothis=yes
value=' '
# you can also document the option
multiple=yop
number=2
choice=yo
multiple-choice=yo,ye
named=key:val
""")
def test_loopback(self):
cfg = self.cfg
f = tempfile.mktemp()
stream = open(f, 'w')
try:
cfg.generate_config(stream)
stream.close()
new_cfg = MyConfiguration(name='testloop', options=options)
new_cfg.load_file_configuration(f)
self.assertEquals(cfg['dothis'], new_cfg['dothis'])
self.assertEquals(cfg['multiple'], new_cfg['multiple'])
self.assertEquals(cfg['number'], new_cfg['number'])
self.assertEquals(cfg['choice'], new_cfg['choice'])
self.assertEquals(cfg['value'], new_cfg['value'])
self.assertEquals(cfg['multiple-choice'], new_cfg['multiple-choice'])
finally:
os.remove(f)
def test_help(self):
self.cfg.add_help_section('bonus', 'a nice additional help')
help = self.cfg.help().strip()
# at least in python 2.4.2 the output is:
# ' -v <string>, --value=<string>'
# it is not unlikely some optik/optparse versions do print -v<string>
# so accept both
help = help.replace(' -v <string>, ', ' -v<string>, ')
if version_info >= (2, 5):
self.assertLinesEquals(help, """Usage: Just do it ! (tm)
Options:
-h, --help show this help message and exit
--dothis=<y or n>
-v<string>, --value=<string>
--multiple=<comma separated values>
you can also document the option [current: none]
--number=<int>
--choice=<yo|ye>
--multiple-choice=<yo|ye>
--named=<key=val>
Bonus:
a nice additional help
""".strip())
elif version_info >= (2, 4):
self.assertLinesEquals(help, """usage: Just do it ! (tm)
options:
-h, --help show this help message and exit
--dothis=<y or n>
-v<string>, --value=<string>
--multiple=<comma separated values>
you can also document the option [current: none]
--number=<int>
--choice=<yo|ye>
--multiple-choice=<yo|ye>
--named=<key=val>
Bonus:
a nice additional help
""".strip())
else:
self.assertLinesEquals(help, """usage: Just do it ! (tm)
options:
-h, --help show this help message and exit
--dothis=<y or n>
-v<string>, --value=<string>
--multiple=<comma separated values>
you can also document the option
--number=<int>
--choice=<yo|ye>
--multiple-choice=<yo|ye>
--named=<key=val>
Bonus:
a nice additional help
""".strip())
def test_manpage(self):
from logilab.common import __pkginfo__
self.cfg.generate_manpage(__pkginfo__, stream=StringIO())
def test_rewrite_config(self):
changes = [('renamed', 'renamed', 'choice'),
('moved', 'named', 'old', 'test'),
]
read_old_config(self.cfg, changes, 'data/test.ini')
stream = StringIO()
self.cfg.generate_config(stream)
self.assertLinesEquals(stream.getvalue().strip(), """# test configuration
[TEST]
dothis=yes
value=' '
# you can also document the option
multiple=yop
number=2
choice=yo
multiple-choice=yo,ye
named=key:val
""")
class Linter(OptionsManagerMixIn, OptionsProviderMixIn):
options = (
('profile', {'type' : 'yn', 'metavar' : '<y_or_n>',
'default': False,
'help' : 'Profiled execution.'}),
)
def __init__(self):
OptionsManagerMixIn.__init__(self, usage="")
OptionsProviderMixIn.__init__(self)
self.register_options_provider(self)
self.load_provider_defaults()
class RegrTC(TestCase):
def setUp(self):
self.linter = Linter()
def test_load_defaults(self):
self.linter.load_command_line_configuration([])
self.assertEquals(self.linter.config.profile, False)
if __name__ == '__main__':
unittest_main()
|