~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-09 12:34:00 UTC
  • mfrom: (6056.2.5 option-registry)
  • Revision ID: pqm@pqm.ubuntu.com-20110809123400-x521f2j9jkxx8ze2
(vila) Introduce OptionRegistry (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2937
2937
 
2938
2938
class TestStackGet(TestStackWithTransport):
2939
2939
 
2940
 
    def setUp(self):
2941
 
        super(TestStackGet, self).setUp()
2942
 
        self.conf = self.get_stack(self)
2943
 
 
2944
2940
    def test_get_for_empty_stack(self):
2945
 
        self.assertEquals(None, self.conf.get('foo'))
 
2941
        conf = self.get_stack(self)
 
2942
        self.assertEquals(None, conf.get('foo'))
2946
2943
 
2947
2944
    def test_get_hook(self):
2948
 
        self.conf.store._load_from_string('foo=bar')
 
2945
        conf = self.get_stack(self)
 
2946
        conf.store._load_from_string('foo=bar')
2949
2947
        calls = []
2950
2948
        def hook(*args):
2951
2949
            calls.append(args)
2952
2950
        config.ConfigHooks.install_named_hook('get', hook, None)
2953
2951
        self.assertLength(0, calls)
2954
 
        value = self.conf.get('foo')
 
2952
        value = conf.get('foo')
2955
2953
        self.assertEquals('bar', value)
2956
2954
        self.assertLength(1, calls)
2957
 
        self.assertEquals((self.conf, 'foo', 'bar'), calls[0])
2958
 
 
2959
 
 
2960
 
class TestStackGetWithConverter(TestStackGet):
2961
 
 
2962
 
    def setUp(self):
2963
 
        super(TestStackGetWithConverter, self).setUp()
2964
 
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
2965
 
        self.registry = config.option_registry
2966
 
 
2967
 
    def register_bool_option(self, name, default, invalid=None):
2968
 
        b = config.Option(name, default=default, help='A boolean.',
2969
 
                          from_unicode=config.bool_from_store,
2970
 
                          invalid=invalid)
2971
 
        self.registry.register(b)
2972
 
 
2973
 
    def test_get_with_bool_not_defined_default_true(self):
2974
 
        self.register_bool_option('foo', True)
2975
 
        self.assertEquals(True, self.conf.get('foo'))
2976
 
 
2977
 
    def test_get_with_bool_not_defined_default_false(self):
2978
 
        self.register_bool_option('foo', False)
2979
 
        self.assertEquals(False, self.conf.get('foo'))
2980
 
 
2981
 
    def test_get_with_bool_converter_not_default(self):
2982
 
        self.register_bool_option('foo', False)
2983
 
        self.conf.store._load_from_string('foo=yes')
2984
 
        self.assertEquals(True, self.conf.get('foo'))
2985
 
 
2986
 
    def test_get_with_bool_converter_invalid_string(self):
2987
 
        self.register_bool_option('foo', False)
2988
 
        self.conf.store._load_from_string('foo=not-a-boolean')
2989
 
        self.assertEquals(False, self.conf.get('foo'))
2990
 
 
2991
 
    def test_get_with_bool_converter_invalid_list(self):
2992
 
        self.register_bool_option('foo', False)
2993
 
        self.conf.store._load_from_string('foo=not,a,boolean')
2994
 
        self.assertEquals(False, self.conf.get('foo'))
2995
 
 
2996
 
    def test_get_invalid_warns(self):
2997
 
        self.register_bool_option('foo', False, invalid='warning')
2998
 
        self.conf.store._load_from_string('foo=not-a-boolean')
2999
 
        warnings = []
3000
 
        def warning(*args):
3001
 
            warnings.append(args[0] % args[1:])
3002
 
        self.overrideAttr(trace, 'warning', warning)
3003
 
        self.assertEquals(False, self.conf.get('foo'))
3004
 
        self.assertLength(1, warnings)
3005
 
        self.assertEquals('Value "not-a-boolean" is not valid for "foo"',
3006
 
                          warnings[0])
3007
 
 
3008
 
    def test_get_invalid_errors(self):
3009
 
        self.register_bool_option('foo', False, invalid='error')
3010
 
        self.conf.store._load_from_string('foo=not-a-boolean')
3011
 
        self.assertRaises(errors.ConfigOptionValueError, self.conf.get, 'foo')
3012
 
 
3013
 
    def register_integer_option(self, name, default):
3014
 
        i = config.Option(name, default=default, help='An integer.',
3015
 
                          from_unicode=config.int_from_store)
3016
 
        self.registry.register(i)
3017
 
 
3018
 
    def test_get_with_integer_not_defined_returns_default(self):
3019
 
        self.register_integer_option('foo', 42)
3020
 
        self.assertEquals(42, self.conf.get('foo'))
3021
 
 
3022
 
    def test_get_with_integer_converter_not_default(self):
3023
 
        self.register_integer_option('foo', 42)
3024
 
        self.conf.store._load_from_string('foo=16')
3025
 
        self.assertEquals(16, self.conf.get('foo'))
3026
 
 
3027
 
    def test_get_with_integer_converter_invalid_string(self):
3028
 
        # We don't set a default value
3029
 
        self.register_integer_option('foo', None)
3030
 
        self.conf.store._load_from_string('foo=forty-two')
3031
 
        # No default value, so we should get None
3032
 
        self.assertEquals(None, self.conf.get('foo'))
3033
 
 
3034
 
    def test_get_with_integer_converter_invalid_list(self):
3035
 
        # We don't set a default value
3036
 
        self.register_integer_option('foo', None)
3037
 
        self.conf.store._load_from_string('foo=a,list')
3038
 
        # No default value, so we should get None
3039
 
        self.assertEquals(None, self.conf.get('foo'))
3040
 
 
3041
 
    def register_list_option(self, name, default):
3042
 
        l = config.Option(name, default=default, help='A list.',
3043
 
                          from_unicode=config.list_from_store)
3044
 
        self.registry.register(l)
3045
 
 
3046
 
    def test_get_with_list_not_defined_returns_default(self):
3047
 
        self.register_list_option('foo', [])
3048
 
        self.assertEquals([], self.conf.get('foo'))
3049
 
 
3050
 
    def test_get_with_list_converter_nothing(self):
3051
 
        self.register_list_option('foo', [1])
3052
 
        self.conf.store._load_from_string('foo=')
3053
 
        self.assertEquals([], self.conf.get('foo'))
3054
 
 
3055
 
    def test_get_with_list_converter_no_item(self):
3056
 
        self.register_list_option('foo', [1])
3057
 
        self.conf.store._load_from_string('foo=,')
3058
 
        self.assertEquals([], self.conf.get('foo'))
3059
 
 
3060
 
    def test_get_with_list_converter_one_boolean(self):
3061
 
        self.register_list_option('foo', [1])
3062
 
        self.conf.store._load_from_string('foo=True')
3063
 
        # We get a list of one string
3064
 
        self.assertEquals(['True'], self.conf.get('foo'))
3065
 
 
3066
 
    def test_get_with_list_converter_one_integer(self):
3067
 
        self.register_list_option('foo', [1])
3068
 
        self.conf.store._load_from_string('foo=2')
3069
 
        # We get a list of one string
3070
 
        self.assertEquals(['2'], self.conf.get('foo'))
3071
 
 
3072
 
    def test_get_with_list_converter_one_string(self):
3073
 
        self.register_list_option('foo', ['foo'])
3074
 
        self.conf.store._load_from_string('foo=bar')
3075
 
        # We get a list of one string
3076
 
        self.assertEquals(['bar'], self.conf.get('foo'))
3077
 
 
3078
 
    def test_get_with_list_converter_many_items(self):
3079
 
        self.register_list_option('foo', [1])
3080
 
        self.conf.store._load_from_string('foo=m,o,r,e')
3081
 
        self.assertEquals(['m', 'o', 'r', 'e'], self.conf.get('foo'))
 
2955
        self.assertEquals((conf, 'foo', 'bar'), calls[0])
3082
2956
 
3083
2957
 
3084
2958
class TestStackSet(TestStackWithTransport):
3310
3184
        conf = config.AuthenticationConfig(_file=StringIO(
3311
3185
                'foo = bar\xff'))
3312
3186
        self.assertRaises(errors.ConfigContentError, conf._get_config)
3313
 
 
 
3187
        
3314
3188
    def test_missing_auth_section_header(self):
3315
3189
        conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
3316
3190
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')