~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Gordon Tyler
  • Date: 2011-01-21 23:51:15 UTC
  • mto: This revision was merged to the branch mainline in revision 5632.
  • Revision ID: gordon@doxxx.net-20110121235115-9sdqamejot1h0481
Replace usage of format function from python 2.6 with our own very simple formatting function.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
    )
43
43
from bzrlib.tests import (
44
44
    features,
45
 
    TestSkipped,
46
45
    scenarios,
47
46
    )
48
47
from bzrlib.util.configobj import configobj
314
313
        """
315
314
        co = config.ConfigObj()
316
315
        co['test'] = 'foo#bar'
317
 
        outfile = StringIO()
318
 
        co.write(outfile=outfile)
319
 
        lines = outfile.getvalue().splitlines()
 
316
        lines = co.write()
320
317
        self.assertEqual(lines, ['test = "foo#bar"'])
321
318
        co2 = config.ConfigObj(lines)
322
319
        self.assertEqual(co2['test'], 'foo#bar')
323
320
 
324
 
    def test_triple_quotes(self):
325
 
        # Bug #710410: if the value string has triple quotes
326
 
        # then ConfigObj versions up to 4.7.2 will quote them wrong
327
 
        # and won't able to read them back
328
 
        triple_quotes_value = '''spam
329
 
""" that's my spam """
330
 
eggs'''
331
 
        co = config.ConfigObj()
332
 
        co['test'] = triple_quotes_value
333
 
        # While writing this test another bug in ConfigObj has been found:
334
 
        # method co.write() without arguments produces list of lines
335
 
        # one option per line, and multiline values are not split
336
 
        # across multiple lines,
337
 
        # and that breaks the parsing these lines back by ConfigObj.
338
 
        # This issue only affects test, but it's better to avoid
339
 
        # `co.write()` construct at all.
340
 
        # [bialix 20110222] bug report sent to ConfigObj's author
341
 
        outfile = StringIO()
342
 
        co.write(outfile=outfile)
343
 
        output = outfile.getvalue()
344
 
        # now we're trying to read it back
345
 
        co2 = config.ConfigObj(StringIO(output))
346
 
        self.assertEquals(triple_quotes_value, co2['test'])
347
 
 
348
321
 
349
322
erroneous_config = """[section] # line 1
350
323
good=good # line 2
510
483
    def test_cached(self):
511
484
        my_config = config.IniBasedConfig.from_string(sample_config_text)
512
485
        parser = my_config._get_parser()
513
 
        self.assertTrue(my_config._get_parser() is parser)
 
486
        self.failUnless(my_config._get_parser() is parser)
514
487
 
515
488
    def _dummy_chown(self, path, uid, gid):
516
489
        self.path, self.uid, self.gid = path, uid, gid
541
514
            ' Use IniBasedConfig(_content=xxx) instead.'],
542
515
            conf._get_parser, file=config_file)
543
516
 
544
 
 
545
517
class TestIniConfigSaving(tests.TestCaseInTempDir):
546
518
 
547
519
    def test_cant_save_without_a_file_name(self):
555
527
        self.assertFileEqual(content, 'test.conf')
556
528
 
557
529
 
558
 
class TestIniConfigOptionExpansionDefaultValue(tests.TestCaseInTempDir):
559
 
    """What is the default value of expand for config options.
560
 
 
561
 
    This is an opt-in beta feature used to evaluate whether or not option
562
 
    references can appear in dangerous place raising exceptions, disapearing
563
 
    (and as such corrupting data) or if it's safe to activate the option by
564
 
    default.
565
 
 
566
 
    Note that these tests relies on config._expand_default_value being already
567
 
    overwritten in the parent class setUp.
568
 
    """
569
 
 
570
 
    def setUp(self):
571
 
        super(TestIniConfigOptionExpansionDefaultValue, self).setUp()
572
 
        self.config = None
573
 
        self.warnings = []
574
 
        def warning(*args):
575
 
            self.warnings.append(args[0] % args[1:])
576
 
        self.overrideAttr(trace, 'warning', warning)
577
 
 
578
 
    def get_config(self, expand):
579
 
        c = config.GlobalConfig.from_string('bzr.config.expand=%s' % (expand,),
580
 
                                            save=True)
581
 
        return c
582
 
 
583
 
    def assertExpandIs(self, expected):
584
 
        actual = config._get_expand_default_value()
585
 
        #self.config.get_user_option_as_bool('bzr.config.expand')
586
 
        self.assertEquals(expected, actual)
587
 
 
588
 
    def test_default_is_None(self):
589
 
        self.assertEquals(None, config._expand_default_value)
590
 
 
591
 
    def test_default_is_False_even_if_None(self):
592
 
        self.config = self.get_config(None)
593
 
        self.assertExpandIs(False)
594
 
 
595
 
    def test_default_is_False_even_if_invalid(self):
596
 
        self.config = self.get_config('<your choice>')
597
 
        self.assertExpandIs(False)
598
 
        # ...
599
 
        # Huh ? My choice is False ? Thanks, always happy to hear that :D
600
 
        # Wait, you've been warned !
601
 
        self.assertLength(1, self.warnings)
602
 
        self.assertEquals(
603
 
            'Value "<your choice>" is not a boolean for "bzr.config.expand"',
604
 
            self.warnings[0])
605
 
 
606
 
    def test_default_is_True(self):
607
 
        self.config = self.get_config(True)
608
 
        self.assertExpandIs(True)
609
 
        
610
 
    def test_default_is_False(self):
611
 
        self.config = self.get_config(False)
612
 
        self.assertExpandIs(False)
613
 
        
614
 
 
615
 
class TestIniConfigOptionExpansion(tests.TestCase):
616
 
    """Test option expansion from the IniConfig level.
617
 
 
618
 
    What we really want here is to test the Config level, but the class being
619
 
    abstract as far as storing values is concerned, this can't be done
620
 
    properly (yet).
621
 
    """
622
 
    # FIXME: This should be rewritten when all configs share a storage
623
 
    # implementation -- vila 2011-02-18
624
 
 
625
 
    def get_config(self, string=None):
626
 
        if string is None:
627
 
            string = ''
628
 
        c = config.IniBasedConfig.from_string(string)
629
 
        return c
630
 
 
631
 
    def assertExpansion(self, expected, conf, string, env=None):
632
 
        self.assertEquals(expected, conf.expand_options(string, env))
633
 
 
634
 
    def test_no_expansion(self):
635
 
        c = self.get_config('')
636
 
        self.assertExpansion('foo', c, 'foo')
637
 
 
638
 
    def test_env_adding_options(self):
639
 
        c = self.get_config('')
640
 
        self.assertExpansion('bar', c, '{foo}', {'foo': 'bar'})
641
 
 
642
 
    def test_env_overriding_options(self):
643
 
        c = self.get_config('foo=baz')
644
 
        self.assertExpansion('bar', c, '{foo}', {'foo': 'bar'})
645
 
 
646
 
    def test_simple_ref(self):
647
 
        c = self.get_config('foo=xxx')
648
 
        self.assertExpansion('xxx', c, '{foo}')
649
 
 
650
 
    def test_unknown_ref(self):
651
 
        c = self.get_config('')
652
 
        self.assertRaises(errors.ExpandingUnknownOption,
653
 
                          c.expand_options, '{foo}')
654
 
 
655
 
    def test_indirect_ref(self):
656
 
        c = self.get_config('''
657
 
foo=xxx
658
 
bar={foo}
659
 
''')
660
 
        self.assertExpansion('xxx', c, '{bar}')
661
 
 
662
 
    def test_embedded_ref(self):
663
 
        c = self.get_config('''
664
 
foo=xxx
665
 
bar=foo
666
 
''')
667
 
        self.assertExpansion('xxx', c, '{{bar}}')
668
 
 
669
 
    def test_simple_loop(self):
670
 
        c = self.get_config('foo={foo}')
671
 
        self.assertRaises(errors.OptionExpansionLoop, c.expand_options, '{foo}')
672
 
 
673
 
    def test_indirect_loop(self):
674
 
        c = self.get_config('''
675
 
foo={bar}
676
 
bar={baz}
677
 
baz={foo}''')
678
 
        e = self.assertRaises(errors.OptionExpansionLoop,
679
 
                              c.expand_options, '{foo}')
680
 
        self.assertEquals('foo->bar->baz', e.refs)
681
 
        self.assertEquals('{foo}', e.string)
682
 
 
683
 
    def test_list(self):
684
 
        conf = self.get_config('''
685
 
foo=start
686
 
bar=middle
687
 
baz=end
688
 
list={foo},{bar},{baz}
689
 
''')
690
 
        self.assertEquals(['start', 'middle', 'end'],
691
 
                           conf.get_user_option('list', expand=True))
692
 
 
693
 
    def test_cascading_list(self):
694
 
        conf = self.get_config('''
695
 
foo=start,{bar}
696
 
bar=middle,{baz}
697
 
baz=end
698
 
list={foo}
699
 
''')
700
 
        self.assertEquals(['start', 'middle', 'end'],
701
 
                           conf.get_user_option('list', expand=True))
702
 
 
703
 
    def test_pathological_hidden_list(self):
704
 
        conf = self.get_config('''
705
 
foo=bin
706
 
bar=go
707
 
start={foo
708
 
middle=},{
709
 
end=bar}
710
 
hidden={start}{middle}{end}
711
 
''')
712
 
        # Nope, it's either a string or a list, and the list wins as soon as a
713
 
        # ',' appears, so the string concatenation never occur.
714
 
        self.assertEquals(['{foo', '}', '{', 'bar}'],
715
 
                          conf.get_user_option('hidden', expand=True))
716
 
 
717
 
class TestLocationConfigOptionExpansion(tests.TestCaseInTempDir):
718
 
 
719
 
    def get_config(self, location, string=None):
720
 
        if string is None:
721
 
            string = ''
722
 
        # Since we don't save the config we won't strictly require to inherit
723
 
        # from TestCaseInTempDir, but an error occurs so quickly...
724
 
        c = config.LocationConfig.from_string(string, location)
725
 
        return c
726
 
 
727
 
    def test_dont_cross_unrelated_section(self):
728
 
        c = self.get_config('/another/branch/path','''
729
 
[/one/branch/path]
730
 
foo = hello
731
 
bar = {foo}/2
732
 
 
733
 
[/another/branch/path]
734
 
bar = {foo}/2
735
 
''')
736
 
        self.assertRaises(errors.ExpandingUnknownOption,
737
 
                          c.get_user_option, 'bar', expand=True)
738
 
 
739
 
    def test_cross_related_sections(self):
740
 
        c = self.get_config('/project/branch/path','''
741
 
[/project]
742
 
foo = qu
743
 
 
744
 
[/project/branch/path]
745
 
bar = {foo}ux
746
 
''')
747
 
        self.assertEquals('quux', c.get_user_option('bar', expand=True))
748
 
 
749
 
 
750
530
class TestIniBaseConfigOnDisk(tests.TestCaseInTempDir):
751
531
 
752
532
    def test_cannot_reload_without_name(self):
962
742
            parser = my_config._get_parser()
963
743
        finally:
964
744
            config.ConfigObj = oldparserclass
965
 
        self.assertIsInstance(parser, InstrumentedConfigObj)
 
745
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
966
746
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
967
747
                                          'utf-8')])
968
748
 
979
759
        my_config = config.BranchConfig(branch)
980
760
        location_config = my_config._get_location_config()
981
761
        self.assertEqual(branch.base, location_config.location)
982
 
        self.assertIs(location_config, my_config._get_location_config())
 
762
        self.failUnless(location_config is my_config._get_location_config())
983
763
 
984
764
    def test_get_config(self):
985
765
        """The Branch.get_config method works properly"""
1205
985
        conf = self._get_empty_config()
1206
986
        cmdline = conf.find_merge_tool('kdiff3')
1207
987
        self.assertEquals('kdiff3 {base} {this} {other} -o {result}', cmdline)
1208
 
 
 
988
        
1209
989
    def test_find_merge_tool_override_known(self):
1210
990
        conf = self._get_empty_config()
1211
991
        conf.set_user_option('bzr.mergetool.kdiff3', 'kdiff3 blah')
1253
1033
            parser = my_config._get_parser()
1254
1034
        finally:
1255
1035
            config.ConfigObj = oldparserclass
1256
 
        self.assertIsInstance(parser, InstrumentedConfigObj)
 
1036
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
1257
1037
        self.assertEqual(parser._calls,
1258
1038
                         [('__init__', config.locations_config_filename(),
1259
1039
                           'utf-8')])
1261
1041
    def test_get_global_config(self):
1262
1042
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
1263
1043
        global_config = my_config._get_global_config()
1264
 
        self.assertIsInstance(global_config, config.GlobalConfig)
1265
 
        self.assertIs(global_config, my_config._get_global_config())
1266
 
 
1267
 
    def assertLocationMatching(self, expected):
1268
 
        self.assertEqual(expected,
1269
 
                         list(self.my_location_config._get_matching_sections()))
 
1044
        self.failUnless(isinstance(global_config, config.GlobalConfig))
 
1045
        self.failUnless(global_config is my_config._get_global_config())
1270
1046
 
1271
1047
    def test__get_matching_sections_no_match(self):
1272
1048
        self.get_branch_config('/')
1273
 
        self.assertLocationMatching([])
 
1049
        self.assertEqual([], self.my_location_config._get_matching_sections())
1274
1050
 
1275
1051
    def test__get_matching_sections_exact(self):
1276
1052
        self.get_branch_config('http://www.example.com')
1277
 
        self.assertLocationMatching([('http://www.example.com', '')])
 
1053
        self.assertEqual([('http://www.example.com', '')],
 
1054
                         self.my_location_config._get_matching_sections())
1278
1055
 
1279
1056
    def test__get_matching_sections_suffix_does_not(self):
1280
1057
        self.get_branch_config('http://www.example.com-com')
1281
 
        self.assertLocationMatching([])
 
1058
        self.assertEqual([], self.my_location_config._get_matching_sections())
1282
1059
 
1283
1060
    def test__get_matching_sections_subdir_recursive(self):
1284
1061
        self.get_branch_config('http://www.example.com/com')
1285
 
        self.assertLocationMatching([('http://www.example.com', 'com')])
 
1062
        self.assertEqual([('http://www.example.com', 'com')],
 
1063
                         self.my_location_config._get_matching_sections())
1286
1064
 
1287
1065
    def test__get_matching_sections_ignoreparent(self):
1288
1066
        self.get_branch_config('http://www.example.com/ignoreparent')
1289
 
        self.assertLocationMatching([('http://www.example.com/ignoreparent',
1290
 
                                      '')])
 
1067
        self.assertEqual([('http://www.example.com/ignoreparent', '')],
 
1068
                         self.my_location_config._get_matching_sections())
1291
1069
 
1292
1070
    def test__get_matching_sections_ignoreparent_subdir(self):
1293
1071
        self.get_branch_config(
1294
1072
            'http://www.example.com/ignoreparent/childbranch')
1295
 
        self.assertLocationMatching([('http://www.example.com/ignoreparent',
1296
 
                                      'childbranch')])
 
1073
        self.assertEqual([('http://www.example.com/ignoreparent',
 
1074
                           'childbranch')],
 
1075
                         self.my_location_config._get_matching_sections())
1297
1076
 
1298
1077
    def test__get_matching_sections_subdir_trailing_slash(self):
1299
1078
        self.get_branch_config('/b')
1300
 
        self.assertLocationMatching([('/b/', '')])
 
1079
        self.assertEqual([('/b/', '')],
 
1080
                         self.my_location_config._get_matching_sections())
1301
1081
 
1302
1082
    def test__get_matching_sections_subdir_child(self):
1303
1083
        self.get_branch_config('/a/foo')
1304
 
        self.assertLocationMatching([('/a/*', ''), ('/a/', 'foo')])
 
1084
        self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
 
1085
                         self.my_location_config._get_matching_sections())
1305
1086
 
1306
1087
    def test__get_matching_sections_subdir_child_child(self):
1307
1088
        self.get_branch_config('/a/foo/bar')
1308
 
        self.assertLocationMatching([('/a/*', 'bar'), ('/a/', 'foo/bar')])
 
1089
        self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
 
1090
                         self.my_location_config._get_matching_sections())
1309
1091
 
1310
1092
    def test__get_matching_sections_trailing_slash_with_children(self):
1311
1093
        self.get_branch_config('/a/')
1312
 
        self.assertLocationMatching([('/a/', '')])
 
1094
        self.assertEqual([('/a/', '')],
 
1095
                         self.my_location_config._get_matching_sections())
1313
1096
 
1314
1097
    def test__get_matching_sections_explicit_over_glob(self):
1315
1098
        # XXX: 2006-09-08 jamesh
1317
1100
        # was a config section for '/a/?', it would get precedence
1318
1101
        # over '/a/c'.
1319
1102
        self.get_branch_config('/a/c')
1320
 
        self.assertLocationMatching([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')])
 
1103
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
 
1104
                         self.my_location_config._get_matching_sections())
1321
1105
 
1322
1106
    def test__get_option_policy_normal(self):
1323
1107
        self.get_branch_config('http://www.example.com')
2466
2250
# test_user_prompted ?
2467
2251
class TestAuthenticationRing(tests.TestCaseWithTransport):
2468
2252
    pass
2469
 
 
2470
 
 
2471
 
class TestAutoUserId(tests.TestCase):
2472
 
    """Test inferring an automatic user name."""
2473
 
 
2474
 
    def test_auto_user_id(self):
2475
 
        """Automatic inference of user name.
2476
 
        
2477
 
        This is a bit hard to test in an isolated way, because it depends on
2478
 
        system functions that go direct to /etc or perhaps somewhere else.
2479
 
        But it's reasonable to say that on Unix, with an /etc/mailname, we ought
2480
 
        to be able to choose a user name with no configuration.
2481
 
        """
2482
 
        if sys.platform == 'win32':
2483
 
            raise TestSkipped("User name inference not implemented on win32")
2484
 
        realname, address = config._auto_user_id()
2485
 
        if os.path.exists('/etc/mailname'):
2486
 
            self.assertIsNot(None, realname)
2487
 
            self.assertIsNot(None, address)
2488
 
        else:
2489
 
            self.assertEquals((None, None), (realname, address))
2490