~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: John Arbash Meinel
  • Date: 2011-01-11 18:10:55 UTC
  • mto: This revision was merged to the branch mainline in revision 5596.
  • Revision ID: john@arbash-meinel.com-20110111181055-d79p02kioxcg4f7l
Change tuned_gzip.GzipFile to be deprecated

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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
52
52
    tests,
53
53
    transport,
54
54
    workingtree,
55
 
    workingtree_3,
56
 
    workingtree_4,
57
55
    )
58
56
from bzrlib.repofmt import (
59
57
    groupcompress_repo,
 
58
    weaverepo,
60
59
    )
61
60
from bzrlib.symbol_versioning import (
62
61
    deprecated_function,
108
107
 
109
108
        filename = u'hell\u00d8'
110
109
        self.build_tree_contents([(filename, 'contents of hello')])
111
 
        self.assertPathExists(filename)
 
110
        self.failUnlessExists(filename)
112
111
 
113
112
 
114
113
class TestClassesAvailable(tests.TestCase):
340
339
        from bzrlib.tests.per_workingtree import make_scenarios
341
340
        server1 = "a"
342
341
        server2 = "b"
343
 
        formats = [workingtree_4.WorkingTreeFormat4(),
344
 
                   workingtree_3.WorkingTreeFormat3(),]
 
342
        formats = [workingtree.WorkingTreeFormat2(),
 
343
                   workingtree.WorkingTreeFormat3(),]
345
344
        scenarios = make_scenarios(server1, server2, formats)
346
345
        self.assertEqual([
347
 
            ('WorkingTreeFormat4',
 
346
            ('WorkingTreeFormat2',
348
347
             {'bzrdir_format': formats[0]._matchingbzrdir,
349
348
              'transport_readonly_server': 'b',
350
349
              'transport_server': 'a',
377
376
            )
378
377
        server1 = "a"
379
378
        server2 = "b"
380
 
        formats = [workingtree_4.WorkingTreeFormat4(),
381
 
                   workingtree_3.WorkingTreeFormat3(),]
 
379
        formats = [workingtree.WorkingTreeFormat2(),
 
380
                   workingtree.WorkingTreeFormat3(),]
382
381
        scenarios = make_scenarios(server1, server2, formats)
383
382
        self.assertEqual(7, len(scenarios))
384
 
        default_wt_format = workingtree.format_registry.get_default()
385
 
        wt4_format = workingtree_4.WorkingTreeFormat4()
386
 
        wt5_format = workingtree_4.WorkingTreeFormat5()
 
383
        default_wt_format = workingtree.WorkingTreeFormat4._default_format
 
384
        wt4_format = workingtree.WorkingTreeFormat4()
 
385
        wt5_format = workingtree.WorkingTreeFormat5()
387
386
        expected_scenarios = [
388
 
            ('WorkingTreeFormat4',
 
387
            ('WorkingTreeFormat2',
389
388
             {'bzrdir_format': formats[0]._matchingbzrdir,
390
389
              'transport_readonly_server': 'b',
391
390
              'transport_server': 'a',
451
450
        # ones to add.
452
451
        from bzrlib.tests.per_tree import (
453
452
            return_parameter,
 
453
            revision_tree_from_workingtree
454
454
            )
455
455
        from bzrlib.tests.per_intertree import (
456
456
            make_scenarios,
457
457
            )
458
 
        from bzrlib.workingtree_3 import WorkingTreeFormat3
459
 
        from bzrlib.workingtree_4 import WorkingTreeFormat4
 
458
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
460
459
        input_test = TestInterTreeScenarios(
461
460
            "test_scenarios")
462
461
        server1 = "a"
463
462
        server2 = "b"
464
 
        format1 = WorkingTreeFormat4()
 
463
        format1 = WorkingTreeFormat2()
465
464
        format2 = WorkingTreeFormat3()
466
465
        formats = [("1", str, format1, format2, "converter1"),
467
466
            ("2", int, format2, format1, "converter2")]
513
512
        self.assertRaises(AssertionError, self.assertEqualStat,
514
513
            os.lstat("foo"), os.lstat("longname"))
515
514
 
516
 
    def test_failUnlessExists(self):
517
 
        """Deprecated failUnlessExists and failIfExists"""
518
 
        self.applyDeprecated(
519
 
            deprecated_in((2, 4)),
520
 
            self.failUnlessExists, '.')
521
 
        self.build_tree(['foo/', 'foo/bar'])
522
 
        self.applyDeprecated(
523
 
            deprecated_in((2, 4)),
524
 
            self.failUnlessExists, 'foo/bar')
525
 
        self.applyDeprecated(
526
 
            deprecated_in((2, 4)),
527
 
            self.failIfExists, 'foo/foo')
528
 
 
529
 
    def test_assertPathExists(self):
530
 
        self.assertPathExists('.')
531
 
        self.build_tree(['foo/', 'foo/bar'])
532
 
        self.assertPathExists('foo/bar')
533
 
        self.assertPathDoesNotExist('foo/foo')
534
 
 
535
515
 
536
516
class TestTestCaseWithMemoryTransport(tests.TestCaseWithMemoryTransport):
537
517
 
571
551
        tree = self.make_branch_and_memory_tree('dir')
572
552
        # Guard against regression into MemoryTransport leaking
573
553
        # files to disk instead of keeping them in memory.
574
 
        self.assertFalse(osutils.lexists('dir'))
 
554
        self.failIf(osutils.lexists('dir'))
575
555
        self.assertIsInstance(tree, memorytree.MemoryTree)
576
556
 
577
557
    def test_make_branch_and_memory_tree_with_format(self):
578
558
        """make_branch_and_memory_tree should accept a format option."""
579
559
        format = bzrdir.BzrDirMetaFormat1()
580
 
        format.repository_format = repository.format_registry.get_default()
 
560
        format.repository_format = weaverepo.RepositoryFormat7()
581
561
        tree = self.make_branch_and_memory_tree('dir', format=format)
582
562
        # Guard against regression into MemoryTransport leaking
583
563
        # files to disk instead of keeping them in memory.
584
 
        self.assertFalse(osutils.lexists('dir'))
 
564
        self.failIf(osutils.lexists('dir'))
585
565
        self.assertIsInstance(tree, memorytree.MemoryTree)
586
566
        self.assertEqual(format.repository_format.__class__,
587
567
            tree.branch.repository._format.__class__)
591
571
        self.assertIsInstance(builder, branchbuilder.BranchBuilder)
592
572
        # Guard against regression into MemoryTransport leaking
593
573
        # files to disk instead of keeping them in memory.
594
 
        self.assertFalse(osutils.lexists('dir'))
 
574
        self.failIf(osutils.lexists('dir'))
595
575
 
596
576
    def test_make_branch_builder_with_format(self):
597
577
        # Use a repo layout that doesn't conform to a 'named' layout, to ensure
598
578
        # that the format objects are used.
599
579
        format = bzrdir.BzrDirMetaFormat1()
600
 
        repo_format = repository.format_registry.get_default()
 
580
        repo_format = weaverepo.RepositoryFormat7()
601
581
        format.repository_format = repo_format
602
582
        builder = self.make_branch_builder('dir', format=format)
603
583
        the_branch = builder.get_branch()
604
584
        # Guard against regression into MemoryTransport leaking
605
585
        # files to disk instead of keeping them in memory.
606
 
        self.assertFalse(osutils.lexists('dir'))
 
586
        self.failIf(osutils.lexists('dir'))
607
587
        self.assertEqual(format.repository_format.__class__,
608
588
                         the_branch.repository._format.__class__)
609
589
        self.assertEqual(repo_format.get_format_string(),
615
595
        the_branch = builder.get_branch()
616
596
        # Guard against regression into MemoryTransport leaking
617
597
        # files to disk instead of keeping them in memory.
618
 
        self.assertFalse(osutils.lexists('dir'))
 
598
        self.failIf(osutils.lexists('dir'))
619
599
        dir_format = bzrdir.format_registry.make_bzrdir('knit')
620
600
        self.assertEqual(dir_format.repository_format.__class__,
621
601
                         the_branch.repository._format.__class__)
654
634
        url2 = self.get_readonly_url('foo/bar')
655
635
        t = transport.get_transport(url)
656
636
        t2 = transport.get_transport(url2)
657
 
        self.assertIsInstance(t, ReadonlyTransportDecorator)
658
 
        self.assertIsInstance(t2, ReadonlyTransportDecorator)
 
637
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
 
638
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
659
639
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
660
640
 
661
641
    def test_get_readonly_url_http(self):
669
649
        # the transport returned may be any HttpTransportBase subclass
670
650
        t = transport.get_transport(url)
671
651
        t2 = transport.get_transport(url2)
672
 
        self.assertIsInstance(t, HttpTransportBase)
673
 
        self.assertIsInstance(t2, HttpTransportBase)
 
652
        self.failUnless(isinstance(t, HttpTransportBase))
 
653
        self.failUnless(isinstance(t2, HttpTransportBase))
674
654
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
675
655
 
676
656
    def test_is_directory(self):
684
664
    def test_make_branch_builder(self):
685
665
        builder = self.make_branch_builder('dir')
686
666
        rev_id = builder.build_commit()
687
 
        self.assertPathExists('dir')
 
667
        self.failUnlessExists('dir')
688
668
        a_dir = bzrdir.BzrDir.open('dir')
689
669
        self.assertRaises(errors.NoWorkingTree, a_dir.open_workingtree)
690
670
        a_branch = a_dir.open_branch()
706
686
        self.assertIsInstance(result_bzrdir.transport,
707
687
                              memory.MemoryTransport)
708
688
        # should not be on disk, should only be in memory
709
 
        self.assertPathDoesNotExist('subdir')
 
689
        self.failIfExists('subdir')
710
690
 
711
691
 
712
692
class TestChrootedTest(tests.ChrootedTestCase):
771
751
        self.check_timing(ShortDelayTestCase('test_short_delay'),
772
752
                          r"^ +[0-9]+ms$")
773
753
 
 
754
    def _patch_get_bzr_source_tree(self):
 
755
        # Reading from the actual source tree breaks isolation, but we don't
 
756
        # want to assume that thats *all* that would happen.
 
757
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
 
758
 
 
759
    def test_assigned_benchmark_file_stores_date(self):
 
760
        self._patch_get_bzr_source_tree()
 
761
        output = StringIO()
 
762
        result = bzrlib.tests.TextTestResult(self._log_file,
 
763
                                        descriptions=0,
 
764
                                        verbosity=1,
 
765
                                        bench_history=output
 
766
                                        )
 
767
        output_string = output.getvalue()
 
768
        # if you are wondering about the regexp please read the comment in
 
769
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
 
770
        # XXX: what comment?  -- Andrew Bennetts
 
771
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
772
 
 
773
    def test_benchhistory_records_test_times(self):
 
774
        self._patch_get_bzr_source_tree()
 
775
        result_stream = StringIO()
 
776
        result = bzrlib.tests.TextTestResult(
 
777
            self._log_file,
 
778
            descriptions=0,
 
779
            verbosity=1,
 
780
            bench_history=result_stream
 
781
            )
 
782
 
 
783
        # we want profile a call and check that its test duration is recorded
 
784
        # make a new test instance that when run will generate a benchmark
 
785
        example_test_case = TestTestResult("_time_hello_world_encoding")
 
786
        # execute the test, which should succeed and record times
 
787
        example_test_case.run(result)
 
788
        lines = result_stream.getvalue().splitlines()
 
789
        self.assertEqual(2, len(lines))
 
790
        self.assertContainsRe(lines[1],
 
791
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
 
792
            "._time_hello_world_encoding")
 
793
 
774
794
    def _time_hello_world_encoding(self):
775
795
        """Profile two sleep calls
776
796
 
1195
1215
            ],
1196
1216
            lines[-3:])
1197
1217
 
 
1218
    def _patch_get_bzr_source_tree(self):
 
1219
        # Reading from the actual source tree breaks isolation, but we don't
 
1220
        # want to assume that thats *all* that would happen.
 
1221
        self._get_source_tree_calls = []
 
1222
        def new_get():
 
1223
            self._get_source_tree_calls.append("called")
 
1224
            return None
 
1225
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree',  new_get)
 
1226
 
 
1227
    def test_bench_history(self):
 
1228
        # tests that the running the benchmark passes bench_history into
 
1229
        # the test result object. We can tell that happens if
 
1230
        # _get_bzr_source_tree is called.
 
1231
        self._patch_get_bzr_source_tree()
 
1232
        test = TestRunner('dummy_test')
 
1233
        output = StringIO()
 
1234
        runner = tests.TextTestRunner(stream=self._log_file,
 
1235
                                      bench_history=output)
 
1236
        result = self.run_test_runner(runner, test)
 
1237
        output_string = output.getvalue()
 
1238
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
1239
        self.assertLength(1, self._get_source_tree_calls)
 
1240
 
1198
1241
    def test_verbose_test_count(self):
1199
1242
        """A verbose test run reports the right test count at the start"""
1200
1243
        suite = TestUtil.TestSuite([
1445
1488
        # Note this test won't fail with hooks that the core library doesn't
1446
1489
        # use - but it trigger with a plugin that adds hooks, so its still a
1447
1490
        # useful warning in that case.
1448
 
        self.assertEqual(bzrlib.branch.BranchHooks(), bzrlib.branch.Branch.hooks)
1449
 
        self.assertEqual(
1450
 
            bzrlib.smart.server.SmartServerHooks(),
 
1491
        self.assertEqual(bzrlib.branch.BranchHooks(),
 
1492
            bzrlib.branch.Branch.hooks)
 
1493
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1451
1494
            bzrlib.smart.server.SmartTCPServer.hooks)
1452
 
        self.assertEqual(
1453
 
            bzrlib.commands.CommandHooks(), bzrlib.commands.Command.hooks)
 
1495
        self.assertEqual(bzrlib.commands.CommandHooks(),
 
1496
            bzrlib.commands.Command.hooks)
1454
1497
 
1455
1498
    def test__gather_lsprof_in_benchmarks(self):
1456
1499
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1934
1977
    def test_make_branch_and_tree_with_format(self):
1935
1978
        # we should be able to supply a format to make_branch_and_tree
1936
1979
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
 
1980
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1937
1981
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1938
1982
                              bzrlib.bzrdir.BzrDirMetaFormat1)
 
1983
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
 
1984
                              bzrlib.bzrdir.BzrDirFormat6)
1939
1985
 
1940
1986
    def test_make_branch_and_memory_tree(self):
1941
1987
        # we should be able to get a new branch and a mutable tree from
2320
2366
        # stdout and stderr of the invoked run_bzr
2321
2367
        current_factory = bzrlib.ui.ui_factory
2322
2368
        self.run_bzr(['foo'])
2323
 
        self.assertFalse(current_factory is self.factory)
 
2369
        self.failIf(current_factory is self.factory)
2324
2370
        self.assertNotEqual(sys.stdout, self.factory.stdout)
2325
2371
        self.assertNotEqual(sys.stderr, self.factory.stderr)
2326
2372
        self.assertEqual('foo\n', self.factory.stdout.getvalue())
2508
2554
        self.assertEqual([], command[2:])
2509
2555
 
2510
2556
    def test_set_env(self):
2511
 
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
 
2557
        self.failIf('EXISTANT_ENV_VAR' in os.environ)
2512
2558
        # set in the child
2513
2559
        def check_environment():
2514
2560
            self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2520
2566
 
2521
2567
    def test_run_bzr_subprocess_env_del(self):
2522
2568
        """run_bzr_subprocess can remove environment variables too."""
2523
 
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
 
2569
        self.failIf('EXISTANT_ENV_VAR' in os.environ)
2524
2570
        def check_environment():
2525
2571
            self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2526
2572
        os.environ['EXISTANT_ENV_VAR'] = 'set variable'
2532
2578
        del os.environ['EXISTANT_ENV_VAR']
2533
2579
 
2534
2580
    def test_env_del_missing(self):
2535
 
        self.assertFalse('NON_EXISTANT_ENV_VAR' in os.environ)
 
2581
        self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
2536
2582
        def check_environment():
2537
2583
            self.assertFalse('NON_EXISTANT_ENV_VAR' in os.environ)
2538
2584
        self.check_popen_state = check_environment
2813
2859
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
2814
2860
 
2815
2861
 
2816
 
class TestCheckTreeShape(tests.TestCaseWithTransport):
 
2862
class TestCheckInventoryShape(tests.TestCaseWithTransport):
2817
2863
 
2818
 
    def test_check_tree_shape(self):
 
2864
    def test_check_inventory_shape(self):
2819
2865
        files = ['a', 'b/', 'b/c']
2820
2866
        tree = self.make_branch_and_tree('.')
2821
2867
        self.build_tree(files)
2822
2868
        tree.add(files)
2823
2869
        tree.lock_read()
2824
2870
        try:
2825
 
            self.check_tree_shape(tree, files)
 
2871
            self.check_inventory_shape(tree.inventory, files)
2826
2872
        finally:
2827
2873
            tree.unlock()
2828
2874
 
3364
3410
class TestEnvironHandling(tests.TestCase):
3365
3411
 
3366
3412
    def test_overrideEnv_None_called_twice_doesnt_leak(self):
3367
 
        self.assertFalse('MYVAR' in os.environ)
 
3413
        self.failIf('MYVAR' in os.environ)
3368
3414
        self.overrideEnv('MYVAR', '42')
3369
3415
        # We use an embedded test to make sure we fix the _captureVar bug
3370
3416
        class Test(tests.TestCase):