~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: 2009-08-14 05:49:27 UTC
  • mfrom: (4476.3.86 inventory-delta)
  • Revision ID: pqm@pqm.ubuntu.com-20090814054927-k0k18dn46ax4b91f
(andrew) Add inventory-delta streaming for cross-format fetch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
25
25
    branch,
26
26
    bzrdir,
27
27
    config,
28
 
    diff,
29
28
    errors,
30
29
    osutils,
31
30
    mail_client,
35
34
    trace,
36
35
    transport,
37
36
    )
38
 
from bzrlib.tests import features
39
37
from bzrlib.util.configobj import configobj
40
38
 
41
39
 
44
42
[DEFAULT]
45
43
email=Erik B\u00e5gfors <erik@bagfors.nu>
46
44
editor=vim
47
 
change_editor=vimdiff -of @new_path @old_path
48
45
gpg_signing_command=gnome-gpg
49
46
log_format=short
50
47
user_global_option=something
211
208
        self._calls.append('_get_signature_checking')
212
209
        return self._signatures
213
210
 
214
 
    def _get_change_editor(self):
215
 
        self._calls.append('_get_change_editor')
216
 
        return 'vimdiff -fo @new_path @old_path'
217
 
 
218
211
 
219
212
bool_config = """[DEFAULT]
220
213
active = true
321
314
        my_config = config.Config()
322
315
        self.assertEqual('long', my_config.log_format())
323
316
 
324
 
    def test_get_change_editor(self):
325
 
        my_config = InstrumentedConfig()
326
 
        change_editor = my_config.get_change_editor('old_tree', 'new_tree')
327
 
        self.assertEqual(['_get_change_editor'], my_config._calls)
328
 
        self.assertIs(diff.DiffFromTool, change_editor.__class__)
329
 
        self.assertEqual(['vimdiff', '-fo', '@new_path', '@old_path'],
330
 
                         change_editor.command_template)
331
 
 
332
317
 
333
318
class TestConfigPath(tests.TestCase):
334
319
 
335
320
    def setUp(self):
336
321
        super(TestConfigPath, self).setUp()
337
322
        os.environ['HOME'] = '/home/bogus'
338
 
        os.environ['XDG_CACHE_DIR'] = ''
339
323
        if sys.platform == 'win32':
340
324
            os.environ['BZR_HOME'] = \
341
325
                r'C:\Documents and Settings\bogus\Application Data'
363
347
        self.assertEqual(config.authentication_config_filename(),
364
348
                         self.bzr_home + '/authentication.conf')
365
349
 
366
 
    def test_xdg_cache_dir(self):
367
 
        self.assertEqual(config.xdg_cache_dir(),
368
 
            '/home/bogus/.cache')
369
 
 
370
 
 
371
 
class TestIniConfig(tests.TestCaseInTempDir):
372
 
 
373
 
    def make_config_parser(self, s):
374
 
        conf = config.IniBasedConfig(None)
375
 
        parser = conf._get_parser(file=StringIO(s.encode('utf-8')))
376
 
        return conf, parser
377
 
 
378
 
 
379
 
class TestIniConfigBuilding(TestIniConfig):
 
350
 
 
351
class TestIniConfig(tests.TestCase):
380
352
 
381
353
    def test_contructs(self):
382
354
        my_config = config.IniBasedConfig("nothing")
394
366
        parser = my_config._get_parser(file=config_file)
395
367
        self.failUnless(my_config._get_parser() is parser)
396
368
 
397
 
    def _dummy_chown(self, path, uid, gid):
398
 
        self.path, self.uid, self.gid = path, uid, gid
399
 
 
400
 
    def test_ini_config_ownership(self):
401
 
        """Ensure that chown is happening during _write_config_file.
402
 
        """
403
 
        self.requireFeature(features.chown_feature)
404
 
        self.overrideAttr(os, 'chown', self._dummy_chown)
405
 
        self.path = self.uid = self.gid = None
406
 
        def get_filename():
407
 
            return 'foo.conf'
408
 
        conf = config.IniBasedConfig(get_filename)
409
 
        conf._write_config_file()
410
 
        self.assertEquals(self.path, 'foo.conf')
411
 
        self.assertTrue(isinstance(self.uid, int))
412
 
        self.assertTrue(isinstance(self.gid, int))
413
 
 
414
 
class TestGetUserOptionAs(TestIniConfig):
415
 
 
416
369
    def test_get_user_option_as_bool(self):
417
 
        conf, parser = self.make_config_parser("""
 
370
        config_file = StringIO("""
418
371
a_true_bool = true
419
372
a_false_bool = 0
420
373
an_invalid_bool = maybe
421
 
a_list = hmm, who knows ? # This is interpreted as a list !
422
 
""")
423
 
        get_bool = conf.get_user_option_as_bool
424
 
        self.assertEqual(True, get_bool('a_true_bool'))
425
 
        self.assertEqual(False, get_bool('a_false_bool'))
426
 
        warnings = []
427
 
        def warning(*args):
428
 
            warnings.append(args[0] % args[1:])
429
 
        self.overrideAttr(trace, 'warning', warning)
430
 
        msg = 'Value "%s" is not a boolean for "%s"'
431
 
        self.assertIs(None, get_bool('an_invalid_bool'))
432
 
        self.assertEquals(msg % ('maybe', 'an_invalid_bool'), warnings[0])
433
 
        warnings = []
434
 
        self.assertIs(None, get_bool('not_defined_in_this_config'))
435
 
        self.assertEquals([], warnings)
436
 
 
437
 
    def test_get_user_option_as_list(self):
438
 
        conf, parser = self.make_config_parser("""
439
 
a_list = a,b,c
440
 
length_1 = 1,
441
 
one_item = x
442
 
""")
443
 
        get_list = conf.get_user_option_as_list
444
 
        self.assertEqual(['a', 'b', 'c'], get_list('a_list'))
445
 
        self.assertEqual(['1'], get_list('length_1'))
446
 
        self.assertEqual('x', conf.get_user_option('one_item'))
447
 
        # automatically cast to list
448
 
        self.assertEqual(['x'], get_list('one_item'))
449
 
 
450
 
 
451
 
class TestSupressWarning(TestIniConfig):
452
 
 
453
 
    def make_warnings_config(self, s):
454
 
        conf, parser = self.make_config_parser(s)
455
 
        return conf.suppress_warning
456
 
 
457
 
    def test_suppress_warning_unknown(self):
458
 
        suppress_warning = self.make_warnings_config('')
459
 
        self.assertEqual(False, suppress_warning('unknown_warning'))
460
 
 
461
 
    def test_suppress_warning_known(self):
462
 
        suppress_warning = self.make_warnings_config('suppress_warnings=a,b')
463
 
        self.assertEqual(False, suppress_warning('c'))
464
 
        self.assertEqual(True, suppress_warning('a'))
465
 
        self.assertEqual(True, suppress_warning('b'))
466
 
 
 
374
a_list = hmm, who knows ? # This interpreted as a list !
 
375
""".encode('utf-8'))
 
376
        my_config = config.IniBasedConfig(None)
 
377
        parser = my_config._get_parser(file=config_file)
 
378
        get_option = my_config.get_user_option_as_bool
 
379
        self.assertEqual(True, get_option('a_true_bool'))
 
380
        self.assertEqual(False, get_option('a_false_bool'))
 
381
        self.assertIs(None, get_option('an_invalid_bool'))
 
382
        self.assertIs(None, get_option('not_defined_in_this_config'))
467
383
 
468
384
class TestGetConfig(tests.TestCase):
469
385
 
704
620
        my_config = self._get_sample_config()
705
621
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
706
622
 
707
 
    def test_get_change_editor(self):
708
 
        my_config = self._get_sample_config()
709
 
        change_editor = my_config.get_change_editor('old', 'new')
710
 
        self.assertIs(diff.DiffFromTool, change_editor.__class__)
711
 
        self.assertEqual('vimdiff -of @new_path @old_path',
712
 
                         ' '.join(change_editor.command_template))
713
 
 
714
 
    def test_get_no_change_editor(self):
715
 
        my_config = self._get_empty_config()
716
 
        change_editor = my_config.get_change_editor('old', 'new')
717
 
        self.assertIs(None, change_editor)
718
 
 
719
623
 
720
624
class TestGlobalConfigSavingOptions(tests.TestCaseInTempDir):
721
625
 
1665
1569
        self.assertEquals(entered_password,
1666
1570
                          conf.get_password('ssh', 'bar.org', user='jim'))
1667
1571
        self.assertContainsRe(
1668
 
            self.get_log(),
 
1572
            self._get_log(keep_log_file=True),
1669
1573
            'password ignored in section \[ssh with password\]')
1670
1574
 
1671
1575
    def test_ssh_without_password_doesnt_emit_warning(self):
1690
1594
        # No warning shoud be emitted since there is no password. We are only
1691
1595
        # providing "user".
1692
1596
        self.assertNotContainsRe(
1693
 
            self.get_log(),
 
1597
            self._get_log(keep_log_file=True),
1694
1598
            'password ignored in section \[ssh with password\]')
1695
1599
 
1696
1600
    def test_uses_fallback_stores(self):
1697
 
        self.overrideAttr(config, 'credential_store_registry',
1698
 
                          config.CredentialStoreRegistry())
 
1601
        self._old_cs_registry = config.credential_store_registry
 
1602
        def restore():
 
1603
            config.credential_store_registry = self._old_cs_registry
 
1604
        self.addCleanup(restore)
 
1605
        config.credential_store_registry = config.CredentialStoreRegistry()
1699
1606
        store = StubCredentialStore()
1700
1607
        store.add_credentials("http", "example.com", "joe", "secret")
1701
1608
        config.credential_store_registry.register("stub", store, fallback=True)