1872
1872
class TestTransportConfig(tests.TestCaseWithTransport):
1874
def test_load_utf8(self):
1875
"""Ensure we can load an utf8-encoded file."""
1876
t = self.get_transport()
1877
unicode_user = u'b\N{Euro Sign}ar'
1878
unicode_content = u'user=%s' % (unicode_user,)
1879
utf8_content = unicode_content.encode('utf8')
1880
# Store the raw content in the config file
1881
t.put_bytes('foo.conf', utf8_content)
1882
conf = config.TransportConfig(t, 'foo.conf')
1883
self.assertEquals(unicode_user, conf.get_option('user'))
1885
def test_load_non_ascii(self):
1886
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
1887
t = self.get_transport()
1888
t.put_bytes('foo.conf', 'user=foo\n#\xff\n')
1889
conf = config.TransportConfig(t, 'foo.conf')
1890
self.assertRaises(errors.ConfigContentError, conf._get_configobj)
1892
def test_load_erroneous_content(self):
1893
"""Ensure we display a proper error on content that can't be parsed."""
1894
t = self.get_transport()
1895
t.put_bytes('foo.conf', '[open_section\n')
1896
conf = config.TransportConfig(t, 'foo.conf')
1897
self.assertRaises(errors.ParseConfigError, conf._get_configobj)
1874
1899
def test_get_value(self):
1875
1900
"""Test that retreiving a value from a section is possible"""
1876
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1901
bzrdir_config = config.TransportConfig(self.get_transport('.'),
1877
1902
'control.conf')
1878
1903
bzrdir_config.set_option('value', 'key', 'SECTION')
1879
1904
bzrdir_config.set_option('value2', 'key2')
2328
2353
self.assertRaises(AssertionError, store._load_from_string, 'bar=baz')
2356
class TestIniFileStoreContent(tests.TestCaseWithTransport):
2357
"""Simulate loading a config store without content of various encodings.
2359
All files produced by bzr are in utf8 content.
2361
Users may modify them manually and end up with a file that can't be
2362
loaded. We need to issue proper error messages in this case.
2365
invalid_utf8_char = '\xff'
2367
def test_load_utf8(self):
2368
"""Ensure we can load an utf8-encoded file."""
2369
t = self.get_transport()
2370
# From http://pad.lv/799212
2371
unicode_user = u'b\N{Euro Sign}ar'
2372
unicode_content = u'user=%s' % (unicode_user,)
2373
utf8_content = unicode_content.encode('utf8')
2374
# Store the raw content in the config file
2375
t.put_bytes('foo.conf', utf8_content)
2376
store = config.IniFileStore(t, 'foo.conf')
2378
stack = config.Stack([store.get_sections], store)
2379
self.assertEquals(unicode_user, stack.get('user'))
2381
def test_load_non_ascii(self):
2382
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
2383
t = self.get_transport()
2384
t.put_bytes('foo.conf', 'user=foo\n#%s\n' % (self.invalid_utf8_char,))
2385
store = config.IniFileStore(t, 'foo.conf')
2386
self.assertRaises(errors.ConfigContentError, store.load)
2388
def test_load_erroneous_content(self):
2389
"""Ensure we display a proper error on content that can't be parsed."""
2390
t = self.get_transport()
2391
t.put_bytes('foo.conf', '[open_section\n')
2392
store = config.IniFileStore(t, 'foo.conf')
2393
self.assertRaises(errors.ParseConfigError, store.load)
2396
class TestIniConfigContent(tests.TestCaseWithTransport):
2397
"""Simulate loading a IniBasedConfig without content of various encodings.
2399
All files produced by bzr are in utf8 content.
2401
Users may modify them manually and end up with a file that can't be
2402
loaded. We need to issue proper error messages in this case.
2405
invalid_utf8_char = '\xff'
2407
def test_load_utf8(self):
2408
"""Ensure we can load an utf8-encoded file."""
2409
# From http://pad.lv/799212
2410
unicode_user = u'b\N{Euro Sign}ar'
2411
unicode_content = u'user=%s' % (unicode_user,)
2412
utf8_content = unicode_content.encode('utf8')
2413
# Store the raw content in the config file
2414
with open('foo.conf', 'wb') as f:
2415
f.write(utf8_content)
2416
conf = config.IniBasedConfig(file_name='foo.conf')
2417
self.assertEquals(unicode_user, conf.get_user_option('user'))
2419
def test_load_badly_encoded_content(self):
2420
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
2421
with open('foo.conf', 'wb') as f:
2422
f.write('user=foo\n#%s\n' % (self.invalid_utf8_char,))
2423
conf = config.IniBasedConfig(file_name='foo.conf')
2424
self.assertRaises(errors.ConfigContentError, conf._get_parser)
2426
def test_load_erroneous_content(self):
2427
"""Ensure we display a proper error on content that can't be parsed."""
2428
with open('foo.conf', 'wb') as f:
2429
f.write('[open_section\n')
2430
conf = config.IniBasedConfig(file_name='foo.conf')
2431
self.assertRaises(errors.ParseConfigError, conf._get_parser)
2331
2434
class TestMutableStore(TestStore):
2333
2436
scenarios = [(key, {'store_id': key, 'get_store': builder}) for key, builder
3036
3139
self.assertEquals({}, conf._get_config())
3037
3140
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
3142
def test_non_utf8_config(self):
3143
conf = config.AuthenticationConfig(_file=StringIO(
3145
self.assertRaises(errors.ConfigContentError, conf._get_config)
3039
3147
def test_missing_auth_section_header(self):
3040
3148
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
3041
3149
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')