~bzr/ubuntu/hardy/bzr/bzr-ppa

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Max Bowsher
  • Date: 2011-02-09 04:09:41 UTC
  • mfrom: (0.5074.21 hardy)
  • Revision ID: maxb@f2s.com-20110209040941-4c6g890kt3pj7vjo
Merge beta-ppa into ppa upon release of 2.3.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
19
19
from cStringIO import StringIO
20
20
import os
21
21
import sys
 
22
import threading
 
23
 
 
24
 
 
25
from testtools import matchers
22
26
 
23
27
#import bzrlib specific imports here
24
28
from bzrlib import (
35
39
    trace,
36
40
    transport,
37
41
    )
38
 
from bzrlib.tests import features
 
42
from bzrlib.tests import (
 
43
    features,
 
44
    scenarios,
 
45
    )
39
46
from bzrlib.util.configobj import configobj
40
47
 
41
48
 
 
49
def lockable_config_scenarios():
 
50
    return [
 
51
        ('global',
 
52
         {'config_class': config.GlobalConfig,
 
53
          'config_args': [],
 
54
          'config_section': 'DEFAULT'}),
 
55
        ('locations',
 
56
         {'config_class': config.LocationConfig,
 
57
          'config_args': ['.'],
 
58
          'config_section': '.'}),]
 
59
 
 
60
 
 
61
load_tests = scenarios.load_tests_apply_scenarios
 
62
 
 
63
 
42
64
sample_long_alias="log -r-15..-1 --line"
43
65
sample_config_text = u"""
44
66
[DEFAULT]
106
128
"""
107
129
 
108
130
 
 
131
def create_configs(test):
 
132
    """Create configuration files for a given test.
 
133
 
 
134
    This requires creating a tree (and populate the ``test.tree`` attribute)
 
135
    and its associated branch and will populate the following attributes:
 
136
 
 
137
    - branch_config: A BranchConfig for the associated branch.
 
138
 
 
139
    - locations_config : A LocationConfig for the associated branch
 
140
 
 
141
    - bazaar_config: A GlobalConfig.
 
142
 
 
143
    The tree and branch are created in a 'tree' subdirectory so the tests can
 
144
    still use the test directory to stay outside of the branch.
 
145
    """
 
146
    tree = test.make_branch_and_tree('tree')
 
147
    test.tree = tree
 
148
    test.branch_config = config.BranchConfig(tree.branch)
 
149
    test.locations_config = config.LocationConfig(tree.basedir)
 
150
    test.bazaar_config = config.GlobalConfig()
 
151
 
 
152
 
 
153
def create_configs_with_file_option(test):
 
154
    """Create configuration files with a ``file`` option set in each.
 
155
 
 
156
    This builds on ``create_configs`` and add one ``file`` option in each
 
157
    configuration with a value which allows identifying the configuration file.
 
158
    """
 
159
    create_configs(test)
 
160
    test.bazaar_config.set_user_option('file', 'bazaar')
 
161
    test.locations_config.set_user_option('file', 'locations')
 
162
    test.branch_config.set_user_option('file', 'branch')
 
163
 
 
164
 
 
165
class TestOptionsMixin:
 
166
 
 
167
    def assertOptions(self, expected, conf):
 
168
        # We don't care about the parser (as it will make tests hard to write
 
169
        # and error-prone anyway)
 
170
        self.assertThat([opt[:4] for opt in conf._get_options()],
 
171
                        matchers.Equals(expected))
 
172
 
 
173
 
109
174
class InstrumentedConfigObj(object):
110
175
    """A config obj look-enough-alike to record calls made to it."""
111
176
 
130
195
        self._calls.append(('keys',))
131
196
        return []
132
197
 
 
198
    def reload(self):
 
199
        self._calls.append(('reload',))
 
200
 
133
201
    def write(self, arg):
134
202
        self._calls.append(('write',))
135
203
 
334
402
 
335
403
    def setUp(self):
336
404
        super(TestConfigPath, self).setUp()
337
 
        os.environ['HOME'] = '/home/bogus'
338
 
        os.environ['XDG_CACHE_DIR'] = ''
 
405
        self.overrideEnv('HOME', '/home/bogus')
 
406
        self.overrideEnv('XDG_CACHE_DIR', '')
339
407
        if sys.platform == 'win32':
340
 
            os.environ['BZR_HOME'] = \
341
 
                r'C:\Documents and Settings\bogus\Application Data'
 
408
            self.overrideEnv(
 
409
                'BZR_HOME', r'C:\Documents and Settings\bogus\Application Data')
342
410
            self.bzr_home = \
343
411
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
344
412
        else:
351
419
        self.assertEqual(config.config_filename(),
352
420
                         self.bzr_home + '/bazaar.conf')
353
421
 
354
 
    def test_branches_config_filename(self):
355
 
        self.assertEqual(config.branches_config_filename(),
356
 
                         self.bzr_home + '/branches.conf')
357
 
 
358
422
    def test_locations_config_filename(self):
359
423
        self.assertEqual(config.locations_config_filename(),
360
424
                         self.bzr_home + '/locations.conf')
368
432
            '/home/bogus/.cache')
369
433
 
370
434
 
 
435
class TestXDGConfigDir(tests.TestCaseInTempDir):
 
436
    # must be in temp dir because config tests for the existence of the bazaar
 
437
    # subdirectory of $XDG_CONFIG_HOME
 
438
 
 
439
    def setUp(self):
 
440
        if sys.platform in ('darwin', 'win32'):
 
441
            raise tests.TestNotApplicable(
 
442
                'XDG config dir not used on this platform')
 
443
        super(TestXDGConfigDir, self).setUp()
 
444
        self.overrideEnv('HOME', self.test_home_dir)
 
445
        # BZR_HOME overrides everything we want to test so unset it.
 
446
        self.overrideEnv('BZR_HOME', None)
 
447
 
 
448
    def test_xdg_config_dir_exists(self):
 
449
        """When ~/.config/bazaar exists, use it as the config dir."""
 
450
        newdir = osutils.pathjoin(self.test_home_dir, '.config', 'bazaar')
 
451
        os.makedirs(newdir)
 
452
        self.assertEqual(config.config_dir(), newdir)
 
453
 
 
454
    def test_xdg_config_home(self):
 
455
        """When XDG_CONFIG_HOME is set, use it."""
 
456
        xdgconfigdir = osutils.pathjoin(self.test_home_dir, 'xdgconfig')
 
457
        self.overrideEnv('XDG_CONFIG_HOME', xdgconfigdir)
 
458
        newdir = osutils.pathjoin(xdgconfigdir, 'bazaar')
 
459
        os.makedirs(newdir)
 
460
        self.assertEqual(config.config_dir(), newdir)
 
461
 
 
462
 
371
463
class TestIniConfig(tests.TestCaseInTempDir):
372
464
 
373
465
    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
 
466
        conf = config.IniBasedConfig.from_string(s)
 
467
        return conf, conf._get_parser()
377
468
 
378
469
 
379
470
class TestIniConfigBuilding(TestIniConfig):
380
471
 
381
472
    def test_contructs(self):
382
 
        my_config = config.IniBasedConfig("nothing")
 
473
        my_config = config.IniBasedConfig()
383
474
 
384
475
    def test_from_fp(self):
385
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
386
 
        my_config = config.IniBasedConfig(None)
387
 
        self.failUnless(
388
 
            isinstance(my_config._get_parser(file=config_file),
389
 
                        configobj.ConfigObj))
 
476
        my_config = config.IniBasedConfig.from_string(sample_config_text)
 
477
        self.assertIsInstance(my_config._get_parser(), configobj.ConfigObj)
390
478
 
391
479
    def test_cached(self):
392
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
393
 
        my_config = config.IniBasedConfig(None)
394
 
        parser = my_config._get_parser(file=config_file)
 
480
        my_config = config.IniBasedConfig.from_string(sample_config_text)
 
481
        parser = my_config._get_parser()
395
482
        self.failUnless(my_config._get_parser() is parser)
396
483
 
397
484
    def _dummy_chown(self, path, uid, gid):
398
485
        self.path, self.uid, self.gid = path, uid, gid
399
486
 
400
487
    def test_ini_config_ownership(self):
401
 
        """Ensure that chown is happening during _write_config_file.
402
 
        """
 
488
        """Ensure that chown is happening during _write_config_file"""
403
489
        self.requireFeature(features.chown_feature)
404
490
        self.overrideAttr(os, 'chown', self._dummy_chown)
405
491
        self.path = self.uid = self.gid = None
406
 
        def get_filename():
407
 
            return 'foo.conf'
408
 
        conf = config.IniBasedConfig(get_filename)
 
492
        conf = config.IniBasedConfig(file_name='./foo.conf')
409
493
        conf._write_config_file()
410
 
        self.assertEquals(self.path, 'foo.conf')
 
494
        self.assertEquals(self.path, './foo.conf')
411
495
        self.assertTrue(isinstance(self.uid, int))
412
496
        self.assertTrue(isinstance(self.gid, int))
413
497
 
 
498
    def test_get_filename_parameter_is_deprecated_(self):
 
499
        conf = self.callDeprecated([
 
500
            'IniBasedConfig.__init__(get_filename) was deprecated in 2.3.'
 
501
            ' Use file_name instead.'],
 
502
            config.IniBasedConfig, lambda: 'ini.conf')
 
503
        self.assertEqual('ini.conf', conf.file_name)
 
504
 
 
505
    def test_get_parser_file_parameter_is_deprecated_(self):
 
506
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
507
        conf = config.IniBasedConfig.from_string(sample_config_text)
 
508
        conf = self.callDeprecated([
 
509
            'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
 
510
            ' Use IniBasedConfig(_content=xxx) instead.'],
 
511
            conf._get_parser, file=config_file)
 
512
 
 
513
class TestIniConfigSaving(tests.TestCaseInTempDir):
 
514
 
 
515
    def test_cant_save_without_a_file_name(self):
 
516
        conf = config.IniBasedConfig()
 
517
        self.assertRaises(AssertionError, conf._write_config_file)
 
518
 
 
519
    def test_saved_with_content(self):
 
520
        content = 'foo = bar\n'
 
521
        conf = config.IniBasedConfig.from_string(
 
522
            content, file_name='./test.conf', save=True)
 
523
        self.assertFileEqual(content, 'test.conf')
 
524
 
 
525
 
 
526
class TestIniBaseConfigOnDisk(tests.TestCaseInTempDir):
 
527
 
 
528
    def test_cannot_reload_without_name(self):
 
529
        conf = config.IniBasedConfig.from_string(sample_config_text)
 
530
        self.assertRaises(AssertionError, conf.reload)
 
531
 
 
532
    def test_reload_see_new_value(self):
 
533
        c1 = config.IniBasedConfig.from_string('editor=vim\n',
 
534
                                               file_name='./test/conf')
 
535
        c1._write_config_file()
 
536
        c2 = config.IniBasedConfig.from_string('editor=emacs\n',
 
537
                                               file_name='./test/conf')
 
538
        c2._write_config_file()
 
539
        self.assertEqual('vim', c1.get_user_option('editor'))
 
540
        self.assertEqual('emacs', c2.get_user_option('editor'))
 
541
        # Make sure we get the Right value
 
542
        c1.reload()
 
543
        self.assertEqual('emacs', c1.get_user_option('editor'))
 
544
 
 
545
 
 
546
class TestLockableConfig(tests.TestCaseInTempDir):
 
547
 
 
548
    scenarios = lockable_config_scenarios()
 
549
 
 
550
    # Set by load_tests
 
551
    config_class = None
 
552
    config_args = None
 
553
    config_section = None
 
554
 
 
555
    def setUp(self):
 
556
        super(TestLockableConfig, self).setUp()
 
557
        self._content = '[%s]\none=1\ntwo=2\n' % (self.config_section,)
 
558
        self.config = self.create_config(self._content)
 
559
 
 
560
    def get_existing_config(self):
 
561
        return self.config_class(*self.config_args)
 
562
 
 
563
    def create_config(self, content):
 
564
        kwargs = dict(save=True)
 
565
        c = self.config_class.from_string(content, *self.config_args, **kwargs)
 
566
        return c
 
567
 
 
568
    def test_simple_read_access(self):
 
569
        self.assertEquals('1', self.config.get_user_option('one'))
 
570
 
 
571
    def test_simple_write_access(self):
 
572
        self.config.set_user_option('one', 'one')
 
573
        self.assertEquals('one', self.config.get_user_option('one'))
 
574
 
 
575
    def test_listen_to_the_last_speaker(self):
 
576
        c1 = self.config
 
577
        c2 = self.get_existing_config()
 
578
        c1.set_user_option('one', 'ONE')
 
579
        c2.set_user_option('two', 'TWO')
 
580
        self.assertEquals('ONE', c1.get_user_option('one'))
 
581
        self.assertEquals('TWO', c2.get_user_option('two'))
 
582
        # The second update respect the first one
 
583
        self.assertEquals('ONE', c2.get_user_option('one'))
 
584
 
 
585
    def test_last_speaker_wins(self):
 
586
        # If the same config is not shared, the same variable modified twice
 
587
        # can only see a single result.
 
588
        c1 = self.config
 
589
        c2 = self.get_existing_config()
 
590
        c1.set_user_option('one', 'c1')
 
591
        c2.set_user_option('one', 'c2')
 
592
        self.assertEquals('c2', c2._get_user_option('one'))
 
593
        # The first modification is still available until another refresh
 
594
        # occur
 
595
        self.assertEquals('c1', c1._get_user_option('one'))
 
596
        c1.set_user_option('two', 'done')
 
597
        self.assertEquals('c2', c1._get_user_option('one'))
 
598
 
 
599
    def test_writes_are_serialized(self):
 
600
        c1 = self.config
 
601
        c2 = self.get_existing_config()
 
602
 
 
603
        # We spawn a thread that will pause *during* the write
 
604
        before_writing = threading.Event()
 
605
        after_writing = threading.Event()
 
606
        writing_done = threading.Event()
 
607
        c1_orig = c1._write_config_file
 
608
        def c1_write_config_file():
 
609
            before_writing.set()
 
610
            c1_orig()
 
611
            # The lock is held we wait for the main thread to decide when to
 
612
            # continue
 
613
            after_writing.wait()
 
614
        c1._write_config_file = c1_write_config_file
 
615
        def c1_set_option():
 
616
            c1.set_user_option('one', 'c1')
 
617
            writing_done.set()
 
618
        t1 = threading.Thread(target=c1_set_option)
 
619
        # Collect the thread after the test
 
620
        self.addCleanup(t1.join)
 
621
        # Be ready to unblock the thread if the test goes wrong
 
622
        self.addCleanup(after_writing.set)
 
623
        t1.start()
 
624
        before_writing.wait()
 
625
        self.assertTrue(c1._lock.is_held)
 
626
        self.assertRaises(errors.LockContention,
 
627
                          c2.set_user_option, 'one', 'c2')
 
628
        self.assertEquals('c1', c1.get_user_option('one'))
 
629
        # Let the lock be released
 
630
        after_writing.set()
 
631
        writing_done.wait()
 
632
        c2.set_user_option('one', 'c2')
 
633
        self.assertEquals('c2', c2.get_user_option('one'))
 
634
 
 
635
    def test_read_while_writing(self):
 
636
       c1 = self.config
 
637
       # We spawn a thread that will pause *during* the write
 
638
       ready_to_write = threading.Event()
 
639
       do_writing = threading.Event()
 
640
       writing_done = threading.Event()
 
641
       c1_orig = c1._write_config_file
 
642
       def c1_write_config_file():
 
643
           ready_to_write.set()
 
644
           # The lock is held we wait for the main thread to decide when to
 
645
           # continue
 
646
           do_writing.wait()
 
647
           c1_orig()
 
648
           writing_done.set()
 
649
       c1._write_config_file = c1_write_config_file
 
650
       def c1_set_option():
 
651
           c1.set_user_option('one', 'c1')
 
652
       t1 = threading.Thread(target=c1_set_option)
 
653
       # Collect the thread after the test
 
654
       self.addCleanup(t1.join)
 
655
       # Be ready to unblock the thread if the test goes wrong
 
656
       self.addCleanup(do_writing.set)
 
657
       t1.start()
 
658
       # Ensure the thread is ready to write
 
659
       ready_to_write.wait()
 
660
       self.assertTrue(c1._lock.is_held)
 
661
       self.assertEquals('c1', c1.get_user_option('one'))
 
662
       # If we read during the write, we get the old value
 
663
       c2 = self.get_existing_config()
 
664
       self.assertEquals('1', c2.get_user_option('one'))
 
665
       # Let the writing occur and ensure it occurred
 
666
       do_writing.set()
 
667
       writing_done.wait()
 
668
       # Now we get the updated value
 
669
       c3 = self.get_existing_config()
 
670
       self.assertEquals('c1', c3.get_user_option('one'))
 
671
 
 
672
 
414
673
class TestGetUserOptionAs(TestIniConfig):
415
674
 
416
675
    def test_get_user_option_as_bool(self):
522
781
        branch = self.make_branch('branch')
523
782
        self.assertEqual('branch', branch.nick)
524
783
 
525
 
        locations = config.locations_config_filename()
526
 
        config.ensure_config_dir_exists()
527
784
        local_url = urlutils.local_path_to_url('branch')
528
 
        open(locations, 'wb').write('[%s]\nnickname = foobar'
529
 
                                    % (local_url,))
 
785
        conf = config.LocationConfig.from_string(
 
786
            '[%s]\nnickname = foobar' % (local_url,),
 
787
            local_url, save=True)
530
788
        self.assertEqual('foobar', branch.nick)
531
789
 
532
790
    def test_config_local_path(self):
534
792
        branch = self.make_branch('branch')
535
793
        self.assertEqual('branch', branch.nick)
536
794
 
537
 
        locations = config.locations_config_filename()
538
 
        config.ensure_config_dir_exists()
539
 
        open(locations, 'wb').write('[%s/branch]\nnickname = barry'
540
 
                                    % (osutils.getcwd().encode('utf8'),))
 
795
        local_path = osutils.getcwd().encode('utf8')
 
796
        conf = config.LocationConfig.from_string(
 
797
            '[%s/branch]\nnickname = barry' % (local_path,),
 
798
            'branch',  save=True)
541
799
        self.assertEqual('barry', branch.nick)
542
800
 
543
801
    def test_config_creates_local(self):
544
802
        """Creating a new entry in config uses a local path."""
545
803
        branch = self.make_branch('branch', format='knit')
546
804
        branch.set_push_location('http://foobar')
547
 
        locations = config.locations_config_filename()
548
805
        local_path = osutils.getcwd().encode('utf8')
549
806
        # Surprisingly ConfigObj doesn't create a trailing newline
550
 
        self.check_file_contents(locations,
 
807
        self.check_file_contents(config.locations_config_filename(),
551
808
                                 '[%s/branch]\n'
552
809
                                 'push_location = http://foobar\n'
553
810
                                 'push_location:policy = norecurse\n'
558
815
        self.assertEqual('!repo', b.get_config().get_nickname())
559
816
 
560
817
    def test_warn_if_masked(self):
561
 
        _warning = trace.warning
562
818
        warnings = []
563
819
        def warning(*args):
564
820
            warnings.append(args[0] % args[1:])
 
821
        self.overrideAttr(trace, 'warning', warning)
565
822
 
566
823
        def set_option(store, warn_masked=True):
567
824
            warnings[:] = []
573
830
            else:
574
831
                self.assertEqual(1, len(warnings))
575
832
                self.assertEqual(warning, warnings[0])
576
 
        trace.warning = warning
577
 
        try:
578
 
            branch = self.make_branch('.')
579
 
            conf = branch.get_config()
580
 
            set_option(config.STORE_GLOBAL)
581
 
            assertWarning(None)
582
 
            set_option(config.STORE_BRANCH)
583
 
            assertWarning(None)
584
 
            set_option(config.STORE_GLOBAL)
585
 
            assertWarning('Value "4" is masked by "3" from branch.conf')
586
 
            set_option(config.STORE_GLOBAL, warn_masked=False)
587
 
            assertWarning(None)
588
 
            set_option(config.STORE_LOCATION)
589
 
            assertWarning(None)
590
 
            set_option(config.STORE_BRANCH)
591
 
            assertWarning('Value "3" is masked by "0" from locations.conf')
592
 
            set_option(config.STORE_BRANCH, warn_masked=False)
593
 
            assertWarning(None)
594
 
        finally:
595
 
            trace.warning = _warning
596
 
 
597
 
 
598
 
class TestGlobalConfigItems(tests.TestCase):
 
833
        branch = self.make_branch('.')
 
834
        conf = branch.get_config()
 
835
        set_option(config.STORE_GLOBAL)
 
836
        assertWarning(None)
 
837
        set_option(config.STORE_BRANCH)
 
838
        assertWarning(None)
 
839
        set_option(config.STORE_GLOBAL)
 
840
        assertWarning('Value "4" is masked by "3" from branch.conf')
 
841
        set_option(config.STORE_GLOBAL, warn_masked=False)
 
842
        assertWarning(None)
 
843
        set_option(config.STORE_LOCATION)
 
844
        assertWarning(None)
 
845
        set_option(config.STORE_BRANCH)
 
846
        assertWarning('Value "3" is masked by "0" from locations.conf')
 
847
        set_option(config.STORE_BRANCH, warn_masked=False)
 
848
        assertWarning(None)
 
849
 
 
850
 
 
851
class TestGlobalConfigItems(tests.TestCaseInTempDir):
599
852
 
600
853
    def test_user_id(self):
601
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
602
 
        my_config = config.GlobalConfig()
603
 
        my_config._parser = my_config._get_parser(file=config_file)
 
854
        my_config = config.GlobalConfig.from_string(sample_config_text)
604
855
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
605
856
                         my_config._get_user_id())
606
857
 
607
858
    def test_absent_user_id(self):
608
 
        config_file = StringIO("")
609
859
        my_config = config.GlobalConfig()
610
 
        my_config._parser = my_config._get_parser(file=config_file)
611
860
        self.assertEqual(None, my_config._get_user_id())
612
861
 
613
862
    def test_configured_editor(self):
614
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
615
 
        my_config = config.GlobalConfig()
616
 
        my_config._parser = my_config._get_parser(file=config_file)
 
863
        my_config = config.GlobalConfig.from_string(sample_config_text)
617
864
        self.assertEqual("vim", my_config.get_editor())
618
865
 
619
866
    def test_signatures_always(self):
620
 
        config_file = StringIO(sample_always_signatures)
621
 
        my_config = config.GlobalConfig()
622
 
        my_config._parser = my_config._get_parser(file=config_file)
 
867
        my_config = config.GlobalConfig.from_string(sample_always_signatures)
623
868
        self.assertEqual(config.CHECK_NEVER,
624
869
                         my_config.signature_checking())
625
870
        self.assertEqual(config.SIGN_ALWAYS,
627
872
        self.assertEqual(True, my_config.signature_needed())
628
873
 
629
874
    def test_signatures_if_possible(self):
630
 
        config_file = StringIO(sample_maybe_signatures)
631
 
        my_config = config.GlobalConfig()
632
 
        my_config._parser = my_config._get_parser(file=config_file)
 
875
        my_config = config.GlobalConfig.from_string(sample_maybe_signatures)
633
876
        self.assertEqual(config.CHECK_NEVER,
634
877
                         my_config.signature_checking())
635
878
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
637
880
        self.assertEqual(False, my_config.signature_needed())
638
881
 
639
882
    def test_signatures_ignore(self):
640
 
        config_file = StringIO(sample_ignore_signatures)
641
 
        my_config = config.GlobalConfig()
642
 
        my_config._parser = my_config._get_parser(file=config_file)
 
883
        my_config = config.GlobalConfig.from_string(sample_ignore_signatures)
643
884
        self.assertEqual(config.CHECK_ALWAYS,
644
885
                         my_config.signature_checking())
645
886
        self.assertEqual(config.SIGN_NEVER,
647
888
        self.assertEqual(False, my_config.signature_needed())
648
889
 
649
890
    def _get_sample_config(self):
650
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
651
 
        my_config = config.GlobalConfig()
652
 
        my_config._parser = my_config._get_parser(file=config_file)
 
891
        my_config = config.GlobalConfig.from_string(sample_config_text)
653
892
        return my_config
654
893
 
655
894
    def test_gpg_signing_command(self):
658
897
        self.assertEqual(False, my_config.signature_needed())
659
898
 
660
899
    def _get_empty_config(self):
661
 
        config_file = StringIO("")
662
900
        my_config = config.GlobalConfig()
663
 
        my_config._parser = my_config._get_parser(file=config_file)
664
901
        return my_config
665
902
 
666
903
    def test_gpg_signing_command_unset(self):
739
976
        self.assertIs(None, new_config.get_alias('commit'))
740
977
 
741
978
 
742
 
class TestLocationConfig(tests.TestCaseInTempDir):
 
979
class TestLocationConfig(tests.TestCaseInTempDir, TestOptionsMixin):
743
980
 
744
981
    def test_constructs(self):
745
982
        my_config = config.LocationConfig('http://example.com')
761
998
        self.assertEqual(parser._calls,
762
999
                         [('__init__', config.locations_config_filename(),
763
1000
                           'utf-8')])
764
 
        config.ensure_config_dir_exists()
765
 
        #os.mkdir(config.config_dir())
766
 
        f = file(config.branches_config_filename(), 'wb')
767
 
        f.write('')
768
 
        f.close()
769
 
        oldparserclass = config.ConfigObj
770
 
        config.ConfigObj = InstrumentedConfigObj
771
 
        try:
772
 
            my_config = config.LocationConfig('http://www.example.com')
773
 
            parser = my_config._get_parser()
774
 
        finally:
775
 
            config.ConfigObj = oldparserclass
776
1001
 
777
1002
    def test_get_global_config(self):
778
1003
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
865
1090
            'http://www.example.com', 'appendpath_option'),
866
1091
            config.POLICY_APPENDPATH)
867
1092
 
 
1093
    def test__get_options_with_policy(self):
 
1094
        self.get_branch_config('/dir/subdir',
 
1095
                               location_config="""\
 
1096
[/dir]
 
1097
other_url = /other-dir
 
1098
other_url:policy = appendpath
 
1099
[/dir/subdir]
 
1100
other_url = /other-subdir
 
1101
""")
 
1102
        self.assertOptions(
 
1103
            [(u'other_url', u'/other-subdir', u'/dir/subdir', 'locations'),
 
1104
             (u'other_url', u'/other-dir', u'/dir', 'locations'),
 
1105
             (u'other_url:policy', u'appendpath', u'/dir', 'locations')],
 
1106
            self.my_location_config)
 
1107
 
868
1108
    def test_location_without_username(self):
869
1109
        self.get_branch_config('http://www.example.com/ignoreparent')
870
1110
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
1006
1246
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1007
1247
                         self.my_config.post_commit())
1008
1248
 
1009
 
    def get_branch_config(self, location, global_config=None):
 
1249
    def get_branch_config(self, location, global_config=None,
 
1250
                          location_config=None):
 
1251
        my_branch = FakeBranch(location)
1010
1252
        if global_config is None:
1011
 
            global_file = StringIO(sample_config_text.encode('utf-8'))
1012
 
        else:
1013
 
            global_file = StringIO(global_config.encode('utf-8'))
1014
 
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
1015
 
        self.my_config = config.BranchConfig(FakeBranch(location))
1016
 
        # Force location config to use specified file
1017
 
        self.my_location_config = self.my_config._get_location_config()
1018
 
        self.my_location_config._get_parser(branches_file)
1019
 
        # Force global config to use specified file
1020
 
        self.my_config._get_global_config()._get_parser(global_file)
 
1253
            global_config = sample_config_text
 
1254
        if location_config is None:
 
1255
            location_config = sample_branches_text
 
1256
 
 
1257
        my_global_config = config.GlobalConfig.from_string(global_config,
 
1258
                                                           save=True)
 
1259
        my_location_config = config.LocationConfig.from_string(
 
1260
            location_config, my_branch.base, save=True)
 
1261
        my_config = config.BranchConfig(my_branch)
 
1262
        self.my_config = my_config
 
1263
        self.my_location_config = my_config._get_location_config()
1021
1264
 
1022
1265
    def test_set_user_setting_sets_and_saves(self):
1023
1266
        self.get_branch_config('/a/c')
1024
1267
        record = InstrumentedConfigObj("foo")
1025
1268
        self.my_location_config._parser = record
1026
1269
 
1027
 
        real_mkdir = os.mkdir
1028
 
        self.created = False
1029
 
        def checked_mkdir(path, mode=0777):
1030
 
            self.log('making directory: %s', path)
1031
 
            real_mkdir(path, mode)
1032
 
            self.created = True
1033
 
 
1034
 
        os.mkdir = checked_mkdir
1035
 
        try:
1036
 
            self.callDeprecated(['The recurse option is deprecated as of '
1037
 
                                 '0.14.  The section "/a/c" has been '
1038
 
                                 'converted to use policies.'],
1039
 
                                self.my_config.set_user_option,
1040
 
                                'foo', 'bar', store=config.STORE_LOCATION)
1041
 
        finally:
1042
 
            os.mkdir = real_mkdir
1043
 
 
1044
 
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
1045
 
        self.assertEqual([('__contains__', '/a/c'),
 
1270
        self.callDeprecated(['The recurse option is deprecated as of '
 
1271
                             '0.14.  The section "/a/c" has been '
 
1272
                             'converted to use policies.'],
 
1273
                            self.my_config.set_user_option,
 
1274
                            'foo', 'bar', store=config.STORE_LOCATION)
 
1275
        self.assertEqual([('reload',),
 
1276
                          ('__contains__', '/a/c'),
1046
1277
                          ('__contains__', '/a/c/'),
1047
1278
                          ('__setitem__', '/a/c', {}),
1048
1279
                          ('__getitem__', '/a/c'),
1077
1308
        self.assertEqual('bzr', my_config.get_bzr_remote_path())
1078
1309
        my_config.set_user_option('bzr_remote_path', '/path-bzr')
1079
1310
        self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
1080
 
        os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
 
1311
        self.overrideEnv('BZR_REMOTE_PATH', '/environ-bzr')
1081
1312
        self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
1082
1313
 
1083
1314
 
1091
1322
option = exact
1092
1323
"""
1093
1324
 
1094
 
 
1095
1325
class TestBranchConfigItems(tests.TestCaseInTempDir):
1096
1326
 
1097
1327
    def get_branch_config(self, global_config=None, location=None,
1098
1328
                          location_config=None, branch_data_config=None):
1099
 
        my_config = config.BranchConfig(FakeBranch(location))
 
1329
        my_branch = FakeBranch(location)
1100
1330
        if global_config is not None:
1101
 
            global_file = StringIO(global_config.encode('utf-8'))
1102
 
            my_config._get_global_config()._get_parser(global_file)
1103
 
        self.my_location_config = my_config._get_location_config()
 
1331
            my_global_config = config.GlobalConfig.from_string(global_config,
 
1332
                                                               save=True)
1104
1333
        if location_config is not None:
1105
 
            location_file = StringIO(location_config.encode('utf-8'))
1106
 
            self.my_location_config._get_parser(location_file)
 
1334
            my_location_config = config.LocationConfig.from_string(
 
1335
                location_config, my_branch.base, save=True)
 
1336
        my_config = config.BranchConfig(my_branch)
1107
1337
        if branch_data_config is not None:
1108
1338
            my_config.branch.control_files.files['branch.conf'] = \
1109
1339
                branch_data_config
1123
1353
                         my_config.username())
1124
1354
 
1125
1355
    def test_not_set_in_branch(self):
1126
 
        my_config = self.get_branch_config(sample_config_text)
 
1356
        my_config = self.get_branch_config(global_config=sample_config_text)
1127
1357
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
1128
1358
                         my_config._get_user_id())
1129
1359
        my_config.branch.control_files.files['email'] = "John"
1130
1360
        self.assertEqual("John", my_config._get_user_id())
1131
1361
 
1132
1362
    def test_BZR_EMAIL_OVERRIDES(self):
1133
 
        os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
 
1363
        self.overrideEnv('BZR_EMAIL', "Robert Collins <robertc@example.org>")
1134
1364
        branch = FakeBranch()
1135
1365
        my_config = config.BranchConfig(branch)
1136
1366
        self.assertEqual("Robert Collins <robertc@example.org>",
1153
1383
 
1154
1384
    def test_gpg_signing_command(self):
1155
1385
        my_config = self.get_branch_config(
 
1386
            global_config=sample_config_text,
1156
1387
            # branch data cannot set gpg_signing_command
1157
1388
            branch_data_config="gpg_signing_command=pgp")
1158
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
1159
 
        my_config._get_global_config()._get_parser(config_file)
1160
1389
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
1161
1390
 
1162
1391
    def test_get_user_option_global(self):
1163
 
        branch = FakeBranch()
1164
 
        my_config = config.BranchConfig(branch)
1165
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
1166
 
        (my_config._get_global_config()._get_parser(config_file))
 
1392
        my_config = self.get_branch_config(global_config=sample_config_text)
1167
1393
        self.assertEqual('something',
1168
1394
                         my_config.get_user_option('user_global_option'))
1169
1395
 
1170
1396
    def test_post_commit_default(self):
1171
 
        branch = FakeBranch()
1172
 
        my_config = self.get_branch_config(sample_config_text, '/a/c',
1173
 
                                           sample_branches_text)
 
1397
        my_config = self.get_branch_config(global_config=sample_config_text,
 
1398
                                      location='/a/c',
 
1399
                                      location_config=sample_branches_text)
1174
1400
        self.assertEqual(my_config.branch.base, '/a/c')
1175
1401
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1176
1402
                         my_config.post_commit())
1177
1403
        my_config.set_user_option('post_commit', 'rmtree_root')
1178
 
        # post-commit is ignored when bresent in branch data
 
1404
        # post-commit is ignored when present in branch data
1179
1405
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1180
1406
                         my_config.post_commit())
1181
1407
        my_config.set_user_option('post_commit', 'rmtree_root',
1183
1409
        self.assertEqual('rmtree_root', my_config.post_commit())
1184
1410
 
1185
1411
    def test_config_precedence(self):
 
1412
        # FIXME: eager test, luckily no persitent config file makes it fail
 
1413
        # -- vila 20100716
1186
1414
        my_config = self.get_branch_config(global_config=precedence_global)
1187
1415
        self.assertEqual(my_config.get_user_option('option'), 'global')
1188
1416
        my_config = self.get_branch_config(global_config=precedence_global,
1189
 
                                      branch_data_config=precedence_branch)
 
1417
                                           branch_data_config=precedence_branch)
1190
1418
        self.assertEqual(my_config.get_user_option('option'), 'branch')
1191
 
        my_config = self.get_branch_config(global_config=precedence_global,
1192
 
                                      branch_data_config=precedence_branch,
1193
 
                                      location_config=precedence_location)
 
1419
        my_config = self.get_branch_config(
 
1420
            global_config=precedence_global,
 
1421
            branch_data_config=precedence_branch,
 
1422
            location_config=precedence_location)
1194
1423
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
1195
 
        my_config = self.get_branch_config(global_config=precedence_global,
1196
 
                                      branch_data_config=precedence_branch,
1197
 
                                      location_config=precedence_location,
1198
 
                                      location='http://example.com/specific')
 
1424
        my_config = self.get_branch_config(
 
1425
            global_config=precedence_global,
 
1426
            branch_data_config=precedence_branch,
 
1427
            location_config=precedence_location,
 
1428
            location='http://example.com/specific')
1199
1429
        self.assertEqual(my_config.get_user_option('option'), 'exact')
1200
1430
 
1201
1431
    def test_get_mail_client(self):
1329
1559
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1330
1560
 
1331
1561
 
 
1562
class TestConfigGetOptions(tests.TestCaseWithTransport, TestOptionsMixin):
 
1563
 
 
1564
    def setUp(self):
 
1565
        super(TestConfigGetOptions, self).setUp()
 
1566
        create_configs(self)
 
1567
 
 
1568
    # One variable in none of the above
 
1569
    def test_no_variable(self):
 
1570
        # Using branch should query branch, locations and bazaar
 
1571
        self.assertOptions([], self.branch_config)
 
1572
 
 
1573
    def test_option_in_bazaar(self):
 
1574
        self.bazaar_config.set_user_option('file', 'bazaar')
 
1575
        self.assertOptions([('file', 'bazaar', 'DEFAULT', 'bazaar')],
 
1576
                           self.bazaar_config)
 
1577
 
 
1578
    def test_option_in_locations(self):
 
1579
        self.locations_config.set_user_option('file', 'locations')
 
1580
        self.assertOptions(
 
1581
            [('file', 'locations', self.tree.basedir, 'locations')],
 
1582
            self.locations_config)
 
1583
 
 
1584
    def test_option_in_branch(self):
 
1585
        self.branch_config.set_user_option('file', 'branch')
 
1586
        self.assertOptions([('file', 'branch', 'DEFAULT', 'branch')],
 
1587
                           self.branch_config)
 
1588
 
 
1589
    def test_option_in_bazaar_and_branch(self):
 
1590
        self.bazaar_config.set_user_option('file', 'bazaar')
 
1591
        self.branch_config.set_user_option('file', 'branch')
 
1592
        self.assertOptions([('file', 'branch', 'DEFAULT', 'branch'),
 
1593
                            ('file', 'bazaar', 'DEFAULT', 'bazaar'),],
 
1594
                           self.branch_config)
 
1595
 
 
1596
    def test_option_in_branch_and_locations(self):
 
1597
        # Hmm, locations override branch :-/
 
1598
        self.locations_config.set_user_option('file', 'locations')
 
1599
        self.branch_config.set_user_option('file', 'branch')
 
1600
        self.assertOptions(
 
1601
            [('file', 'locations', self.tree.basedir, 'locations'),
 
1602
             ('file', 'branch', 'DEFAULT', 'branch'),],
 
1603
            self.branch_config)
 
1604
 
 
1605
    def test_option_in_bazaar_locations_and_branch(self):
 
1606
        self.bazaar_config.set_user_option('file', 'bazaar')
 
1607
        self.locations_config.set_user_option('file', 'locations')
 
1608
        self.branch_config.set_user_option('file', 'branch')
 
1609
        self.assertOptions(
 
1610
            [('file', 'locations', self.tree.basedir, 'locations'),
 
1611
             ('file', 'branch', 'DEFAULT', 'branch'),
 
1612
             ('file', 'bazaar', 'DEFAULT', 'bazaar'),],
 
1613
            self.branch_config)
 
1614
 
 
1615
 
 
1616
class TestConfigRemoveOption(tests.TestCaseWithTransport, TestOptionsMixin):
 
1617
 
 
1618
    def setUp(self):
 
1619
        super(TestConfigRemoveOption, self).setUp()
 
1620
        create_configs_with_file_option(self)
 
1621
 
 
1622
    def test_remove_in_locations(self):
 
1623
        self.locations_config.remove_user_option('file', self.tree.basedir)
 
1624
        self.assertOptions(
 
1625
            [('file', 'branch', 'DEFAULT', 'branch'),
 
1626
             ('file', 'bazaar', 'DEFAULT', 'bazaar'),],
 
1627
            self.branch_config)
 
1628
 
 
1629
    def test_remove_in_branch(self):
 
1630
        self.branch_config.remove_user_option('file')
 
1631
        self.assertOptions(
 
1632
            [('file', 'locations', self.tree.basedir, 'locations'),
 
1633
             ('file', 'bazaar', 'DEFAULT', 'bazaar'),],
 
1634
            self.branch_config)
 
1635
 
 
1636
    def test_remove_in_bazaar(self):
 
1637
        self.bazaar_config.remove_user_option('file')
 
1638
        self.assertOptions(
 
1639
            [('file', 'locations', self.tree.basedir, 'locations'),
 
1640
             ('file', 'branch', 'DEFAULT', 'branch'),],
 
1641
            self.branch_config)
 
1642
 
 
1643
 
 
1644
class TestConfigGetSections(tests.TestCaseWithTransport):
 
1645
 
 
1646
    def setUp(self):
 
1647
        super(TestConfigGetSections, self).setUp()
 
1648
        create_configs(self)
 
1649
 
 
1650
    def assertSectionNames(self, expected, conf, name=None):
 
1651
        """Check which sections are returned for a given config.
 
1652
 
 
1653
        If fallback configurations exist their sections can be included.
 
1654
 
 
1655
        :param expected: A list of section names.
 
1656
 
 
1657
        :param conf: The configuration that will be queried.
 
1658
 
 
1659
        :param name: An optional section name that will be passed to
 
1660
            get_sections().
 
1661
        """
 
1662
        sections = list(conf._get_sections(name))
 
1663
        self.assertLength(len(expected), sections)
 
1664
        self.assertEqual(expected, [name for name, _, _ in sections])
 
1665
 
 
1666
    def test_bazaar_default_section(self):
 
1667
        self.assertSectionNames(['DEFAULT'], self.bazaar_config)
 
1668
 
 
1669
    def test_locations_default_section(self):
 
1670
        # No sections are defined in an empty file
 
1671
        self.assertSectionNames([], self.locations_config)
 
1672
 
 
1673
    def test_locations_named_section(self):
 
1674
        self.locations_config.set_user_option('file', 'locations')
 
1675
        self.assertSectionNames([self.tree.basedir], self.locations_config)
 
1676
 
 
1677
    def test_locations_matching_sections(self):
 
1678
        loc_config = self.locations_config
 
1679
        loc_config.set_user_option('file', 'locations')
 
1680
        # We need to cheat a bit here to create an option in sections above and
 
1681
        # below the 'location' one.
 
1682
        parser = loc_config._get_parser()
 
1683
        # locations.cong deals with '/' ignoring native os.sep
 
1684
        location_names = self.tree.basedir.split('/')
 
1685
        parent = '/'.join(location_names[:-1])
 
1686
        child = '/'.join(location_names + ['child'])
 
1687
        parser[parent] = {}
 
1688
        parser[parent]['file'] = 'parent'
 
1689
        parser[child] = {}
 
1690
        parser[child]['file'] = 'child'
 
1691
        self.assertSectionNames([self.tree.basedir, parent], loc_config)
 
1692
 
 
1693
    def test_branch_data_default_section(self):
 
1694
        self.assertSectionNames([None],
 
1695
                                self.branch_config._get_branch_data_config())
 
1696
 
 
1697
    def test_branch_default_sections(self):
 
1698
        # No sections are defined in an empty locations file
 
1699
        self.assertSectionNames([None, 'DEFAULT'],
 
1700
                                self.branch_config)
 
1701
        # Unless we define an option
 
1702
        self.branch_config._get_location_config().set_user_option(
 
1703
            'file', 'locations')
 
1704
        self.assertSectionNames([self.tree.basedir, None, 'DEFAULT'],
 
1705
                                self.branch_config)
 
1706
 
 
1707
    def test_bazaar_named_section(self):
 
1708
        # We need to cheat as the API doesn't give direct access to sections
 
1709
        # other than DEFAULT.
 
1710
        self.bazaar_config.set_alias('bazaar', 'bzr')
 
1711
        self.assertSectionNames(['ALIASES'], self.bazaar_config, 'ALIASES')
 
1712
 
 
1713
 
1332
1714
class TestAuthenticationConfigFile(tests.TestCase):
1333
1715
    """Test the authentication.conf file matching"""
1334
1716