2267
2267
# The first env var set wins
2268
2268
self.assertEquals('foo', opt.get_default())
2270
def test_not_supported_list_default_value(self):
2271
self.assertRaises(AssertionError, config.Option, 'foo', default=[1])
2273
def test_not_supported_object_default_value(self):
2274
self.assertRaises(AssertionError, config.Option, 'foo',
2278
class TestOptionConverterMixin(object):
2280
def assertConverted(self, expected, opt, value):
2281
self.assertEquals(expected, opt.convert_from_unicode(value))
2283
def assertWarns(self, opt, value):
2286
warnings.append(args[0] % args[1:])
2287
self.overrideAttr(trace, 'warning', warning)
2288
self.assertEquals(None, opt.convert_from_unicode(value))
2289
self.assertLength(1, warnings)
2291
'Value "%s" is not valid for "%s"' % (value, opt.name),
2294
def assertErrors(self, opt, value):
2295
self.assertRaises(errors.ConfigOptionValueError,
2296
opt.convert_from_unicode, value)
2298
def assertConvertInvalid(self, opt, invalid_value):
2300
self.assertEquals(None, opt.convert_from_unicode(invalid_value))
2301
opt.invalid = 'warning'
2302
self.assertWarns(opt, invalid_value)
2303
opt.invalid = 'error'
2304
self.assertErrors(opt, invalid_value)
2307
class TestOptionWithBooleanConverter(tests.TestCase, TestOptionConverterMixin):
2309
def get_option(self):
2310
return config.Option('foo', help='A boolean.',
2311
from_unicode=config.bool_from_store)
2313
def test_convert_invalid(self):
2314
opt = self.get_option()
2315
# A string that is not recognized as a boolean
2316
self.assertConvertInvalid(opt, u'invalid-boolean')
2317
# A list of strings is never recognized as a boolean
2318
self.assertConvertInvalid(opt, [u'not', u'a', u'boolean'])
2320
def test_convert_valid(self):
2321
opt = self.get_option()
2322
self.assertConverted(True, opt, u'True')
2323
self.assertConverted(True, opt, u'1')
2324
self.assertConverted(False, opt, u'False')
2327
class TestOptionWithIntegerConverter(tests.TestCase, TestOptionConverterMixin):
2329
def get_option(self):
2330
return config.Option('foo', help='An integer.',
2331
from_unicode=config.int_from_store)
2333
def test_convert_invalid(self):
2334
opt = self.get_option()
2335
# A string that is not recognized as an integer
2336
self.assertConvertInvalid(opt, u'forty-two')
2337
# A list of strings is never recognized as an integer
2338
self.assertConvertInvalid(opt, [u'a', u'list'])
2340
def test_convert_valid(self):
2341
opt = self.get_option()
2342
self.assertConverted(16, opt, u'16')
2344
class TestOptionWithListConverter(tests.TestCase, TestOptionConverterMixin):
2346
def get_option(self):
2347
return config.Option('foo', help='A list.',
2348
from_unicode=config.list_from_store)
2350
def test_convert_invalid(self):
2351
# No string is invalid as all forms can be converted to a list
2354
def test_convert_valid(self):
2355
opt = self.get_option()
2356
# An empty string is an empty list
2357
self.assertConverted([], opt, '') # Using a bare str() just in case
2358
self.assertConverted([], opt, u'')
2360
self.assertConverted([u'True'], opt, u'True')
2362
self.assertConverted([u'42'], opt, u'42')
2364
self.assertConverted([u'bar'], opt, u'bar')
2365
# A list remains a list (configObj will turn a string containing commas
2366
# into a list, but that's not what we're testing here)
2367
self.assertConverted([u'foo', u'1', u'True'],
2368
opt, [u'foo', u'1', u'True'])
2271
2371
class TestOptionRegistry(tests.TestCase):
3044
3144
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3045
3145
self.registry = config.option_registry
3047
def register_bool_option(self, name, default, invalid=None):
3048
b = config.Option(name, default=default, help='A boolean.',
3049
from_unicode=config.bool_from_store,
3147
def register_bool_option(self, name, default=None, default_from_env=None):
3148
b = config.Option(name, help='A boolean.',
3149
default=default, default_from_env=default_from_env,
3150
from_unicode=config.bool_from_store)
3051
3151
self.registry.register(b)
3053
def test_get_with_bool_not_defined_default_true(self):
3054
self.register_bool_option('foo', True)
3055
self.assertEquals(True, self.conf.get('foo'))
3057
def test_get_with_bool_not_defined_default_false(self):
3058
self.register_bool_option('foo', False)
3059
self.assertEquals(False, self.conf.get('foo'))
3061
def test_get_with_bool_converter_not_default(self):
3062
self.register_bool_option('foo', False)
3063
self.conf.store._load_from_string('foo=yes')
3064
self.assertEquals(True, self.conf.get('foo'))
3066
def test_get_with_bool_converter_invalid_string(self):
3067
self.register_bool_option('foo', False)
3068
self.conf.store._load_from_string('foo=not-a-boolean')
3069
self.assertEquals(False, self.conf.get('foo'))
3071
def test_get_with_bool_converter_invalid_list(self):
3072
self.register_bool_option('foo', False)
3073
self.conf.store._load_from_string('foo=not,a,boolean')
3074
self.assertEquals(False, self.conf.get('foo'))
3076
def test_get_invalid_warns(self):
3077
self.register_bool_option('foo', False, invalid='warning')
3078
self.conf.store._load_from_string('foo=not-a-boolean')
3081
warnings.append(args[0] % args[1:])
3082
self.overrideAttr(trace, 'warning', warning)
3083
self.assertEquals(False, self.conf.get('foo'))
3084
self.assertLength(1, warnings)
3085
self.assertEquals('Value "not-a-boolean" is not valid for "foo"',
3088
def test_get_invalid_errors(self):
3089
self.register_bool_option('foo', False, invalid='error')
3090
self.conf.store._load_from_string('foo=not-a-boolean')
3091
self.assertRaises(errors.ConfigOptionValueError, self.conf.get, 'foo')
3093
def register_integer_option(self, name, default):
3094
i = config.Option(name, default=default, help='An integer.',
3153
def test_get_default_bool_None(self):
3154
self.register_bool_option('foo')
3155
self.assertEquals(None, self.conf.get('foo'))
3157
def test_get_default_bool_True(self):
3158
self.register_bool_option('foo', u'True')
3159
self.assertEquals(True, self.conf.get('foo'))
3161
def test_get_default_bool_False(self):
3162
self.register_bool_option('foo', False)
3163
self.assertEquals(False, self.conf.get('foo'))
3165
def test_get_default_bool_False_as_string(self):
3166
self.register_bool_option('foo', u'False')
3167
self.assertEquals(False, self.conf.get('foo'))
3169
def test_get_default_bool_from_env_converted(self):
3170
self.register_bool_option('foo', u'True', default_from_env=['FOO'])
3171
self.overrideEnv('FOO', 'False')
3172
self.assertEquals(False, self.conf.get('foo'))
3174
def test_get_default_bool_when_conversion_fails(self):
3175
self.register_bool_option('foo', default='True')
3176
self.conf.store._load_from_string('foo=invalid boolean')
3177
self.assertEquals(True, self.conf.get('foo'))
3179
def register_integer_option(self, name,
3180
default=None, default_from_env=None):
3181
i = config.Option(name, help='An integer.',
3182
default=default, default_from_env=default_from_env,
3095
3183
from_unicode=config.int_from_store)
3096
3184
self.registry.register(i)
3098
def test_get_with_integer_not_defined_returns_default(self):
3099
self.register_integer_option('foo', 42)
3100
self.assertEquals(42, self.conf.get('foo'))
3102
def test_get_with_integer_converter_not_default(self):
3103
self.register_integer_option('foo', 42)
3104
self.conf.store._load_from_string('foo=16')
3105
self.assertEquals(16, self.conf.get('foo'))
3107
def test_get_with_integer_converter_invalid_string(self):
3108
# We don't set a default value
3109
self.register_integer_option('foo', None)
3110
self.conf.store._load_from_string('foo=forty-two')
3111
# No default value, so we should get None
3112
self.assertEquals(None, self.conf.get('foo'))
3114
def test_get_with_integer_converter_invalid_list(self):
3115
# We don't set a default value
3116
self.register_integer_option('foo', None)
3117
self.conf.store._load_from_string('foo=a,list')
3118
# No default value, so we should get None
3119
self.assertEquals(None, self.conf.get('foo'))
3121
def register_list_option(self, name, default):
3122
l = config.Option(name, default=default, help='A list.',
3186
def test_get_default_integer_None(self):
3187
self.register_integer_option('foo')
3188
self.assertEquals(None, self.conf.get('foo'))
3190
def test_get_default_integer(self):
3191
self.register_integer_option('foo', 42)
3192
self.assertEquals(42, self.conf.get('foo'))
3194
def test_get_default_integer_as_string(self):
3195
self.register_integer_option('foo', u'42')
3196
self.assertEquals(42, self.conf.get('foo'))
3198
def test_get_default_integer_from_env(self):
3199
self.register_integer_option('foo', default_from_env=['FOO'])
3200
self.overrideEnv('FOO', '18')
3201
self.assertEquals(18, self.conf.get('foo'))
3203
def test_get_default_integer_when_conversion_fails(self):
3204
self.register_integer_option('foo', default='12')
3205
self.conf.store._load_from_string('foo=invalid integer')
3206
self.assertEquals(12, self.conf.get('foo'))
3208
def register_list_option(self, name, default=None, default_from_env=None):
3209
l = config.Option(name, help='A list.',
3210
default=default, default_from_env=default_from_env,
3123
3211
from_unicode=config.list_from_store)
3124
3212
self.registry.register(l)
3126
def test_get_with_list_not_defined_returns_default(self):
3127
self.register_list_option('foo', [])
3214
def test_get_default_list_None(self):
3215
self.register_list_option('foo')
3216
self.assertEquals(None, self.conf.get('foo'))
3218
def test_get_default_list_empty(self):
3219
self.register_list_option('foo', '')
3128
3220
self.assertEquals([], self.conf.get('foo'))
3130
def test_get_with_list_converter_nothing(self):
3131
self.register_list_option('foo', [1])
3132
self.conf.store._load_from_string('foo=')
3222
def test_get_default_list_from_env(self):
3223
self.register_list_option('foo', default_from_env=['FOO'])
3224
self.overrideEnv('FOO', '')
3133
3225
self.assertEquals([], self.conf.get('foo'))
3135
3227
def test_get_with_list_converter_no_item(self):
3136
self.register_list_option('foo', [1])
3228
self.register_list_option('foo', None)
3137
3229
self.conf.store._load_from_string('foo=,')
3138
3230
self.assertEquals([], self.conf.get('foo'))
3140
def test_get_with_list_converter_one_boolean(self):
3141
self.register_list_option('foo', [1])
3142
self.conf.store._load_from_string('foo=True')
3143
# We get a list of one string
3144
self.assertEquals(['True'], self.conf.get('foo'))
3146
def test_get_with_list_converter_one_integer(self):
3147
self.register_list_option('foo', [1])
3148
self.conf.store._load_from_string('foo=2')
3149
# We get a list of one string
3150
self.assertEquals(['2'], self.conf.get('foo'))
3152
def test_get_with_list_converter_one_string(self):
3153
self.register_list_option('foo', ['foo'])
3154
self.conf.store._load_from_string('foo=bar')
3155
# We get a list of one string
3156
self.assertEquals(['bar'], self.conf.get('foo'))
3158
3232
def test_get_with_list_converter_many_items(self):
3159
self.register_list_option('foo', [1])
3233
self.register_list_option('foo', None)
3160
3234
self.conf.store._load_from_string('foo=m,o,r,e')
3161
3235
self.assertEquals(['m', 'o', 'r', 'e'], self.conf.get('foo'))