/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: John Arbash Meinel
  • Date: 2008-06-05 16:27:16 UTC
  • mfrom: (3475 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3476.
  • Revision ID: john@arbash-meinel.com-20080605162716-a3hn238tnctbfd8j
merge bzr.dev, resolve NEWS

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):
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
 
1139
1209
class TestAuthenticationConfigFile(tests.TestCase):
1140
1210
    """Test the authentication.conf file matching"""
1141
1211
 
1156
1226
        self.assertEquals({}, conf._get_config())
1157
1227
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1158
1228
 
1159
 
    def test_broken_config(self):
 
1229
    def test_missing_auth_section_header(self):
 
1230
        conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
 
1231
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
 
1232
 
 
1233
    def test_auth_section_header_not_closed(self):
1160
1234
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1161
1235
        self.assertRaises(errors.ParseConfigError, conf._get_config)
1162
1236
 
 
1237
    def test_auth_value_not_boolean(self):
1163
1238
        conf = config.AuthenticationConfig(_file=StringIO(
1164
1239
                """[broken]
1165
1240
scheme=ftp
1167
1242
verify_certificates=askme # Error: Not a boolean
1168
1243
"""))
1169
1244
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
 
1245
 
 
1246
    def test_auth_value_not_int(self):
1170
1247
        conf = config.AuthenticationConfig(_file=StringIO(
1171
1248
                """[broken]
1172
1249
scheme=ftp
1291
1368
        self._got_user_passwd(None, None,
1292
1369
                              conf, 'http', 'bar.org', user='georges')
1293
1370
 
 
1371
    def test_credentials_for_user_without_password(self):
 
1372
        conf = config.AuthenticationConfig(_file=StringIO(
 
1373
                """
 
1374
[without password]
 
1375
scheme=http
 
1376
host=bar.org
 
1377
user=jim
 
1378
"""))
 
1379
        # Get user but no password
 
1380
        self._got_user_passwd('jim', None,
 
1381
                              conf, 'http', 'bar.org')
 
1382
 
1294
1383
    def test_verify_certificates(self):
1295
1384
        conf = config.AuthenticationConfig(_file=StringIO(
1296
1385
                """
1354
1443
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1355
1444
            'smtp', port=10025)
1356
1445
 
 
1446
    def test_ssh_password_emits_warning(self):
 
1447
        conf = config.AuthenticationConfig(_file=StringIO(
 
1448
                """
 
1449
[ssh with password]
 
1450
scheme=ssh
 
1451
host=bar.org
 
1452
user=jim
 
1453
password=jimpass
 
1454
"""))
 
1455
        entered_password = 'typed-by-hand'
 
1456
        stdout = tests.StringIOWrapper()
 
1457
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
 
1458
                                            stdout=stdout)
 
1459
 
 
1460
        # Since the password defined in the authentication config is ignored,
 
1461
        # the user is prompted
 
1462
        self.assertEquals(entered_password,
 
1463
                          conf.get_password('ssh', 'bar.org', user='jim'))
 
1464
        self.assertContainsRe(
 
1465
            self._get_log(keep_log_file=True),
 
1466
            'password ignored in section \[ssh with password\]')
 
1467
 
 
1468
    def test_ssh_without_password_doesnt_emit_warning(self):
 
1469
        conf = config.AuthenticationConfig(_file=StringIO(
 
1470
                """
 
1471
[ssh with password]
 
1472
scheme=ssh
 
1473
host=bar.org
 
1474
user=jim
 
1475
"""))
 
1476
        entered_password = 'typed-by-hand'
 
1477
        stdout = tests.StringIOWrapper()
 
1478
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
 
1479
                                            stdout=stdout)
 
1480
 
 
1481
        # Since the password defined in the authentication config is ignored,
 
1482
        # the user is prompted
 
1483
        self.assertEquals(entered_password,
 
1484
                          conf.get_password('ssh', 'bar.org', user='jim'))
 
1485
        # No warning shoud be emitted since there is no password. We are only
 
1486
        # providing "user".
 
1487
        self.assertNotContainsRe(
 
1488
            self._get_log(keep_log_file=True),
 
1489
            'password ignored in section \[ssh with password\]')
 
1490
 
1357
1491
 
1358
1492
# FIXME: Once we have a way to declare authentication to all test servers, we
1359
1493
# can implement generic tests.