/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Andrew Bennetts
  • Date: 2008-09-08 12:59:00 UTC
  • mfrom: (3695 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3756.
  • Revision ID: andrew.bennetts@canonical.com-20080908125900-8ywtsr7jqyyatjz0
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    urlutils,
34
34
    tests,
35
35
    trace,
 
36
    transport,
36
37
    )
37
38
from bzrlib.util.configobj import configobj
38
39
 
146
147
            self.base = "http://example.com/branches/demo"
147
148
        else:
148
149
            self.base = base
149
 
        self.control_files = FakeControlFiles(user_id=user_id)
 
150
        self._transport = self.control_files = \
 
151
            FakeControlFilesAndTransport(user_id=user_id)
150
152
 
151
153
    def lock_write(self):
152
154
        pass
155
157
        pass
156
158
 
157
159
 
158
 
class FakeControlFiles(object):
 
160
class FakeControlFilesAndTransport(object):
159
161
 
160
162
    def __init__(self, user_id=None):
161
 
        self.email = user_id
162
163
        self.files = {}
 
164
        if user_id:
 
165
            self.files['email'] = user_id
 
166
        self._transport = self
163
167
 
164
168
    def get_utf8(self, filename):
165
 
        if filename != 'email':
166
 
            raise NotImplementedError
167
 
        if self.email is not None:
168
 
            return StringIO(self.email)
169
 
        raise errors.NoSuchFile(filename)
 
169
        # from LockableFiles
 
170
        raise AssertionError("get_utf8 should no longer be used")
170
171
 
171
172
    def get(self, filename):
 
173
        # from Transport
172
174
        try:
173
175
            return StringIO(self.files[filename])
174
176
        except KeyError:
175
177
            raise errors.NoSuchFile(filename)
176
178
 
 
179
    def get_bytes(self, filename):
 
180
        # from Transport
 
181
        try:
 
182
            return self.files[filename]
 
183
        except KeyError:
 
184
            raise errors.NoSuchFile(filename)
 
185
 
177
186
    def put(self, filename, fileobj):
178
187
        self.files[filename] = fileobj.read()
179
188
 
 
189
    def put_file(self, filename, fileobj):
 
190
        return self.put(filename, fileobj)
 
191
 
180
192
 
181
193
class InstrumentedConfig(config.Config):
182
194
    """An instrumented config that supplies stubs for template methods."""
576
588
        my_config = self._get_sample_config()
577
589
        self.assertEqual('help', my_config.get_alias('h'))
578
590
 
 
591
    def test_get_aliases(self):
 
592
        my_config = self._get_sample_config()
 
593
        aliases = my_config.get_aliases()
 
594
        self.assertEqual(2, len(aliases))
 
595
        sorted_keys = sorted(aliases)
 
596
        self.assertEqual('help', aliases[sorted_keys[0]])
 
597
        self.assertEqual(sample_long_alias, aliases[sorted_keys[1]])
 
598
 
579
599
    def test_get_no_alias(self):
580
600
        my_config = self._get_sample_config()
581
601
        self.assertEqual(None, my_config.get_alias('foo'))
585
605
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
586
606
 
587
607
 
 
608
class TestGlobalConfigSavingOptions(tests.TestCaseInTempDir):
 
609
 
 
610
    def test_empty(self):
 
611
        my_config = config.GlobalConfig()
 
612
        self.assertEqual(0, len(my_config.get_aliases()))
 
613
 
 
614
    def test_set_alias(self):
 
615
        my_config = config.GlobalConfig()
 
616
        alias_value = 'commit --strict'
 
617
        my_config.set_alias('commit', alias_value)
 
618
        new_config = config.GlobalConfig()
 
619
        self.assertEqual(alias_value, new_config.get_alias('commit'))
 
620
 
 
621
    def test_remove_alias(self):
 
622
        my_config = config.GlobalConfig()
 
623
        my_config.set_alias('commit', 'commit --strict')
 
624
        # Now remove the alias again.
 
625
        my_config.unset_alias('commit')
 
626
        new_config = config.GlobalConfig()
 
627
        self.assertIs(None, new_config.get_alias('commit'))
 
628
 
 
629
 
588
630
class TestLocationConfig(tests.TestCaseInTempDir):
589
631
 
590
632
    def test_constructs(self):
909
951
        self.assertIs(self.my_config.get_user_option('foo'), None)
910
952
        self.my_config.set_user_option('foo', 'bar')
911
953
        self.assertEqual(
912
 
            self.my_config.branch.control_files.files['branch.conf'],
913
 
            'foo = bar\n')
 
954
            self.my_config.branch.control_files.files['branch.conf'].strip(),
 
955
            'foo = bar')
914
956
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
915
957
        self.my_config.set_user_option('foo', 'baz',
916
958
                                       store=config.STORE_LOCATION)
960
1002
        my_config = config.BranchConfig(branch)
961
1003
        self.assertEqual("Robert Collins <robertc@example.net>",
962
1004
                         my_config.username())
963
 
        branch.control_files.email = "John"
 
1005
        my_config.branch.control_files.files['email'] = "John"
964
1006
        my_config.set_user_option('email',
965
1007
                                  "Robert Collins <robertc@example.org>")
966
1008
        self.assertEqual("John", my_config.username())
967
 
        branch.control_files.email = None
 
1009
        del my_config.branch.control_files.files['email']
968
1010
        self.assertEqual("Robert Collins <robertc@example.org>",
969
1011
                         my_config.username())
970
1012
 
971
1013
    def test_not_set_in_branch(self):
972
1014
        my_config = self.get_branch_config(sample_config_text)
973
 
        my_config.branch.control_files.email = None
974
1015
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
975
1016
                         my_config._get_user_id())
976
 
        my_config.branch.control_files.email = "John"
 
1017
        my_config.branch.control_files.files['email'] = "John"
977
1018
        self.assertEqual("John", my_config._get_user_id())
978
1019
 
979
1020
    def test_BZR_EMAIL_OVERRIDES(self):
1136
1177
        self.assertEqual(value, 'value3-section')
1137
1178
 
1138
1179
 
 
1180
class TestTransportConfig(tests.TestCaseWithTransport):
 
1181
 
 
1182
    def test_get_value(self):
 
1183
        """Test that retreiving a value from a section is possible"""
 
1184
        bzrdir_config = config.TransportConfig(transport.get_transport('.'),
 
1185
                                               'control.conf')
 
1186
        bzrdir_config.set_option('value', 'key', 'SECTION')
 
1187
        bzrdir_config.set_option('value2', 'key2')
 
1188
        bzrdir_config.set_option('value3-top', 'key3')
 
1189
        bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
 
1190
        value = bzrdir_config.get_option('key', 'SECTION')
 
1191
        self.assertEqual(value, 'value')
 
1192
        value = bzrdir_config.get_option('key2')
 
1193
        self.assertEqual(value, 'value2')
 
1194
        self.assertEqual(bzrdir_config.get_option('non-existant'), None)
 
1195
        value = bzrdir_config.get_option('non-existant', 'SECTION')
 
1196
        self.assertEqual(value, None)
 
1197
        value = bzrdir_config.get_option('non-existant', default='default')
 
1198
        self.assertEqual(value, 'default')
 
1199
        self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
 
1200
        value = bzrdir_config.get_option('key2', 'NOSECTION',
 
1201
                                         default='default')
 
1202
        self.assertEqual(value, 'default')
 
1203
        value = bzrdir_config.get_option('key3')
 
1204
        self.assertEqual(value, 'value3-top')
 
1205
        value = bzrdir_config.get_option('key3', 'SECTION')
 
1206
        self.assertEqual(value, 'value3-section')
 
1207
 
 
1208
    def test_set_unset_default_stack_on(self):
 
1209
        my_dir = self.make_bzrdir('.')
 
1210
        bzrdir_config = config.BzrDirConfig(my_dir.transport)
 
1211
        self.assertIs(None, bzrdir_config.get_default_stack_on())
 
1212
        bzrdir_config.set_default_stack_on('Foo')
 
1213
        self.assertEqual('Foo', bzrdir_config._config.get_option(
 
1214
                         'default_stack_on'))
 
1215
        self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
 
1216
        bzrdir_config.set_default_stack_on(None)
 
1217
        self.assertIs(None, bzrdir_config.get_default_stack_on())
 
1218
 
 
1219
 
1139
1220
class TestAuthenticationConfigFile(tests.TestCase):
1140
1221
    """Test the authentication.conf file matching"""
1141
1222
 
1156
1237
        self.assertEquals({}, conf._get_config())
1157
1238
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1158
1239
 
1159
 
    def test_broken_config(self):
 
1240
    def test_missing_auth_section_header(self):
 
1241
        conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
 
1242
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
 
1243
 
 
1244
    def test_auth_section_header_not_closed(self):
1160
1245
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1161
1246
        self.assertRaises(errors.ParseConfigError, conf._get_config)
1162
1247
 
 
1248
    def test_auth_value_not_boolean(self):
1163
1249
        conf = config.AuthenticationConfig(_file=StringIO(
1164
1250
                """[broken]
1165
1251
scheme=ftp
1167
1253
verify_certificates=askme # Error: Not a boolean
1168
1254
"""))
1169
1255
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
 
1256
 
 
1257
    def test_auth_value_not_int(self):
1170
1258
        conf = config.AuthenticationConfig(_file=StringIO(
1171
1259
                """[broken]
1172
1260
scheme=ftp
1291
1379
        self._got_user_passwd(None, None,
1292
1380
                              conf, 'http', 'bar.org', user='georges')
1293
1381
 
 
1382
    def test_credentials_for_user_without_password(self):
 
1383
        conf = config.AuthenticationConfig(_file=StringIO(
 
1384
                """
 
1385
[without password]
 
1386
scheme=http
 
1387
host=bar.org
 
1388
user=jim
 
1389
"""))
 
1390
        # Get user but no password
 
1391
        self._got_user_passwd('jim', None,
 
1392
                              conf, 'http', 'bar.org')
 
1393
 
1294
1394
    def test_verify_certificates(self):
1295
1395
        conf = config.AuthenticationConfig(_file=StringIO(
1296
1396
                """
1354
1454
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1355
1455
            'smtp', port=10025)
1356
1456
 
 
1457
    def test_ssh_password_emits_warning(self):
 
1458
        conf = config.AuthenticationConfig(_file=StringIO(
 
1459
                """
 
1460
[ssh with password]
 
1461
scheme=ssh
 
1462
host=bar.org
 
1463
user=jim
 
1464
password=jimpass
 
1465
"""))
 
1466
        entered_password = 'typed-by-hand'
 
1467
        stdout = tests.StringIOWrapper()
 
1468
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
 
1469
                                            stdout=stdout)
 
1470
 
 
1471
        # Since the password defined in the authentication config is ignored,
 
1472
        # the user is prompted
 
1473
        self.assertEquals(entered_password,
 
1474
                          conf.get_password('ssh', 'bar.org', user='jim'))
 
1475
        self.assertContainsRe(
 
1476
            self._get_log(keep_log_file=True),
 
1477
            'password ignored in section \[ssh with password\]')
 
1478
 
 
1479
    def test_ssh_without_password_doesnt_emit_warning(self):
 
1480
        conf = config.AuthenticationConfig(_file=StringIO(
 
1481
                """
 
1482
[ssh with password]
 
1483
scheme=ssh
 
1484
host=bar.org
 
1485
user=jim
 
1486
"""))
 
1487
        entered_password = 'typed-by-hand'
 
1488
        stdout = tests.StringIOWrapper()
 
1489
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
 
1490
                                            stdout=stdout)
 
1491
 
 
1492
        # Since the password defined in the authentication config is ignored,
 
1493
        # the user is prompted
 
1494
        self.assertEquals(entered_password,
 
1495
                          conf.get_password('ssh', 'bar.org', user='jim'))
 
1496
        # No warning shoud be emitted since there is no password. We are only
 
1497
        # providing "user".
 
1498
        self.assertNotContainsRe(
 
1499
            self._get_log(keep_log_file=True),
 
1500
            'password ignored in section \[ssh with password\]')
 
1501
 
1357
1502
 
1358
1503
# FIXME: Once we have a way to declare authentication to all test servers, we
1359
1504
# can implement generic tests.