~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: John Arbash Meinel
  • Date: 2010-08-13 19:08:57 UTC
  • mto: (5050.17.7 2.2)
  • mto: This revision was merged to the branch mainline in revision 5379.
  • Revision ID: john@arbash-meinel.com-20100813190857-mvzwnimrxvm0zimp
Lots of documentation updates.

We had a lot of http links pointing to the old domain. They should
all now be properly updated to the new domain. (only bazaar-vcs.org
entry left is for pqm, which seems to still reside at the old url.)

Also removed one 'TODO' doc entry about switching to binary xdelta, since
we basically did just that with groupcompress.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
 
1
# Copyright (C) 2005-2010 Canonical Ltd
3
2
#
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
18
17
"""Tests for finding and reading the bzr config file[s]."""
19
18
# import system imports here
20
19
from cStringIO import StringIO
21
 
import getpass
22
20
import os
23
21
import sys
24
22
 
27
25
    branch,
28
26
    bzrdir,
29
27
    config,
 
28
    diff,
30
29
    errors,
31
30
    osutils,
32
31
    mail_client,
36
35
    trace,
37
36
    transport,
38
37
    )
 
38
from bzrlib.tests import features
39
39
from bzrlib.util.configobj import configobj
40
40
 
41
41
 
44
44
[DEFAULT]
45
45
email=Erik B\u00e5gfors <erik@bagfors.nu>
46
46
editor=vim
 
47
change_editor=vimdiff -of @new_path @old_path
47
48
gpg_signing_command=gnome-gpg
48
49
log_format=short
49
50
user_global_option=something
210
211
        self._calls.append('_get_signature_checking')
211
212
        return self._signatures
212
213
 
 
214
    def _get_change_editor(self):
 
215
        self._calls.append('_get_change_editor')
 
216
        return 'vimdiff -fo @new_path @old_path'
 
217
 
213
218
 
214
219
bool_config = """[DEFAULT]
215
220
active = true
316
321
        my_config = config.Config()
317
322
        self.assertEqual('long', my_config.log_format())
318
323
 
 
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
 
319
332
 
320
333
class TestConfigPath(tests.TestCase):
321
334
 
322
335
    def setUp(self):
323
336
        super(TestConfigPath, self).setUp()
324
337
        os.environ['HOME'] = '/home/bogus'
 
338
        os.environ['XDG_CACHE_DIR'] = ''
325
339
        if sys.platform == 'win32':
326
340
            os.environ['BZR_HOME'] = \
327
341
                r'C:\Documents and Settings\bogus\Application Data'
349
363
        self.assertEqual(config.authentication_config_filename(),
350
364
                         self.bzr_home + '/authentication.conf')
351
365
 
352
 
 
353
 
class TestIniConfig(tests.TestCase):
 
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):
354
380
 
355
381
    def test_contructs(self):
356
382
        my_config = config.IniBasedConfig("nothing")
368
394
        parser = my_config._get_parser(file=config_file)
369
395
        self.failUnless(my_config._get_parser() is parser)
370
396
 
 
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
    def test_get_user_option_as_bool(self):
 
417
        conf, parser = self.make_config_parser("""
 
418
a_true_bool = true
 
419
a_false_bool = 0
 
420
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
 
371
467
 
372
468
class TestGetConfig(tests.TestCase):
373
469
 
608
704
        my_config = self._get_sample_config()
609
705
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
610
706
 
 
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
 
611
719
 
612
720
class TestGlobalConfigSavingOptions(tests.TestCaseInTempDir):
613
721
 
1548
1656
"""))
1549
1657
        entered_password = 'typed-by-hand'
1550
1658
        stdout = tests.StringIOWrapper()
 
1659
        stderr = tests.StringIOWrapper()
1551
1660
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1552
 
                                            stdout=stdout)
 
1661
                                            stdout=stdout, stderr=stderr)
1553
1662
 
1554
1663
        # Since the password defined in the authentication config is ignored,
1555
1664
        # the user is prompted
1556
1665
        self.assertEquals(entered_password,
1557
1666
                          conf.get_password('ssh', 'bar.org', user='jim'))
1558
1667
        self.assertContainsRe(
1559
 
            self._get_log(keep_log_file=True),
 
1668
            self.get_log(),
1560
1669
            'password ignored in section \[ssh with password\]')
1561
1670
 
1562
1671
    def test_ssh_without_password_doesnt_emit_warning(self):
1569
1678
"""))
1570
1679
        entered_password = 'typed-by-hand'
1571
1680
        stdout = tests.StringIOWrapper()
 
1681
        stderr = tests.StringIOWrapper()
1572
1682
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1573
 
                                            stdout=stdout)
 
1683
                                            stdout=stdout,
 
1684
                                            stderr=stderr)
1574
1685
 
1575
1686
        # Since the password defined in the authentication config is ignored,
1576
1687
        # the user is prompted
1579
1690
        # No warning shoud be emitted since there is no password. We are only
1580
1691
        # providing "user".
1581
1692
        self.assertNotContainsRe(
1582
 
            self._get_log(keep_log_file=True),
 
1693
            self.get_log(),
1583
1694
            'password ignored in section \[ssh with password\]')
1584
1695
 
1585
1696
    def test_uses_fallback_stores(self):
1586
 
        self._old_cs_registry = config.credential_store_registry
1587
 
        def restore():
1588
 
            config.credential_store_registry = self._old_cs_registry
1589
 
        self.addCleanup(restore)
1590
 
        config.credential_store_registry = config.CredentialStoreRegistry()
 
1697
        self.overrideAttr(config, 'credential_store_registry',
 
1698
                          config.CredentialStoreRegistry())
1591
1699
        store = StubCredentialStore()
1592
1700
        store.add_credentials("http", "example.com", "joe", "secret")
1593
1701
        config.credential_store_registry.register("stub", store, fallback=True)