/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Robert Collins
  • Date: 2006-04-28 11:01:38 UTC
  • mfrom: (1687 +trunk)
  • mto: (1704.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1706.
  • Revision ID: robertc@robertcollins.net-20060428110138-0e69ecb765434f9d
MergeĀ fromĀ mainline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 by 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
21
21
# little as possible, so this should be used rarely if it's added at all.
22
22
# (Suggestion from j-a-meinel, 2005-11-24)
23
23
 
 
24
# NOTE: Some classes in here use camelCaseNaming() rather than
 
25
# underscore_naming().  That's for consistency with unittest; it's not the
 
26
# general style of bzrlib.  Please continue that consistency when adding e.g.
 
27
# new assertFoo() methods.
 
28
 
24
29
import codecs
25
30
from cStringIO import StringIO
26
31
import difflib
42
47
import bzrlib.errors as errors
43
48
import bzrlib.inventory
44
49
import bzrlib.iterablefile
 
50
import bzrlib.lockdir
 
51
from bzrlib.merge import merge_inner
45
52
import bzrlib.merge3
46
53
import bzrlib.osutils
47
54
import bzrlib.osutils as osutils
48
55
import bzrlib.plugin
49
56
import bzrlib.progress as progress
 
57
from bzrlib.revision import common_ancestor
50
58
import bzrlib.store
51
59
import bzrlib.trace
52
 
from bzrlib.transport import urlescape
 
60
from bzrlib.transport import urlescape, get_transport
53
61
import bzrlib.transport
54
62
from bzrlib.transport.local import LocalRelpathServer
55
63
from bzrlib.transport.readonly import ReadonlyServer
67
75
                      bzrlib.errors,
68
76
                      bzrlib.inventory,
69
77
                      bzrlib.iterablefile,
 
78
                      bzrlib.lockdir,
70
79
                      bzrlib.merge3,
71
80
                      bzrlib.option,
72
81
                      bzrlib.osutils,
82
91
    import bzrlib.tests.blackbox
83
92
    import bzrlib.tests.branch_implementations
84
93
    import bzrlib.tests.bzrdir_implementations
 
94
    import bzrlib.tests.interrepository_implementations
 
95
    import bzrlib.tests.interversionedfile_implementations
85
96
    import bzrlib.tests.repository_implementations
 
97
    import bzrlib.tests.revisionstore_implementations
86
98
    import bzrlib.tests.workingtree_implementations
87
99
    return [
88
100
            bzrlib.doc,
89
101
            bzrlib.tests.blackbox,
90
102
            bzrlib.tests.branch_implementations,
91
103
            bzrlib.tests.bzrdir_implementations,
 
104
            bzrlib.tests.interrepository_implementations,
 
105
            bzrlib.tests.interversionedfile_implementations,
92
106
            bzrlib.tests.repository_implementations,
 
107
            bzrlib.tests.revisionstore_implementations,
93
108
            bzrlib.tests.workingtree_implementations,
94
109
            ]
95
110
 
274
289
        # This is still a little bogus, 
275
290
        # but only a little. Folk not using our testrunner will
276
291
        # have to delete their temp directories themselves.
 
292
        test_root = TestCaseInTempDir.TEST_ROOT
277
293
        if result.wasSuccessful() and not self.keep_output:
278
 
            if TestCaseInTempDir.TEST_ROOT is not None:
279
 
                shutil.rmtree(TestCaseInTempDir.TEST_ROOT) 
 
294
            if test_root is not None:
 
295
                print 'Deleting test root %s...' % test_root
 
296
                try:
 
297
                    shutil.rmtree(test_root)
 
298
                finally:
 
299
                    print
280
300
        else:
281
301
            self.stream.write("Failed tests working directories are in '%s'\n"
282
302
                              % TestCaseInTempDir.TEST_ROOT)
388
408
            raise AssertionError('pattern "%s" not found in "%s"'
389
409
                    % (needle_re, haystack))
390
410
 
391
 
    def AssertSubset(self, sublist, superlist):
 
411
    def assertSubset(self, sublist, superlist):
392
412
        """Assert that every entry in sublist is present in superlist."""
393
413
        missing = []
394
414
        for entry in sublist:
405
425
    def assertTransportMode(self, transport, path, mode):
406
426
        """Fail if a path does not have mode mode.
407
427
        
408
 
        If modes are not supported on this platform, the test is skipped.
 
428
        If modes are not supported on this transport, the assertion is ignored.
409
429
        """
410
 
        if sys.platform == 'win32':
 
430
        if not transport._can_roundtrip_unix_modebits():
411
431
            return
412
432
        path_stat = transport.stat(path)
413
433
        actual_mode = stat.S_IMODE(path_stat.st_mode)
414
434
        self.assertEqual(mode, actual_mode,
415
435
            'mode of %r incorrect (%o != %o)' % (path, mode, actual_mode))
416
436
 
 
437
    def assertIsInstance(self, obj, kls):
 
438
        """Fail if obj is not an instance of kls"""
 
439
        if not isinstance(obj, kls):
 
440
            self.fail("%r is an instance of %s rather than %s" % (
 
441
                obj, obj.__class__, kls))
 
442
 
417
443
    def _startLogFile(self):
418
444
        """Send bzr and test log messages to a temporary file.
419
445
 
622
648
            sys.stderr = real_stderr
623
649
            sys.stdin = real_stdin
624
650
 
 
651
    def merge(self, branch_from, wt_to):
 
652
        """A helper for tests to do a ui-less merge.
 
653
 
 
654
        This should move to the main library when someone has time to integrate
 
655
        it in.
 
656
        """
 
657
        # minimal ui-less merge.
 
658
        wt_to.branch.fetch(branch_from)
 
659
        base_rev = common_ancestor(branch_from.last_revision(),
 
660
                                   wt_to.branch.last_revision(),
 
661
                                   wt_to.branch.repository)
 
662
        merge_inner(wt_to.branch, branch_from.basis_tree(), 
 
663
                    wt_to.branch.repository.revision_tree(base_rev),
 
664
                    this_tree=wt_to)
 
665
        wt_to.add_pending_merge(branch_from.last_revision())
 
666
 
625
667
 
626
668
BzrTestBase = TestCase
627
669
 
670
712
            break
671
713
        # make a fake bzr directory there to prevent any tests propagating
672
714
        # up onto the source directory's real branch
673
 
        os.mkdir(osutils.pathjoin(TestCaseInTempDir.TEST_ROOT, '.bzr'))
 
715
        bzrdir.BzrDir.create_standalone_workingtree(TestCaseInTempDir.TEST_ROOT)
674
716
 
675
717
    def setUp(self):
676
718
        super(TestCaseInTempDir, self).setUp()
677
719
        self._make_test_root()
678
720
        _currentdir = os.getcwdu()
 
721
        # shorten the name, to avoid test failures due to path length
679
722
        short_id = self.id().replace('bzrlib.tests.', '') \
680
 
                   .replace('__main__.', '')
681
 
        self.test_dir = osutils.pathjoin(self.TEST_ROOT, short_id)
682
 
        os.mkdir(self.test_dir)
683
 
        os.chdir(self.test_dir)
 
723
                   .replace('__main__.', '')[-100:]
 
724
        # it's possible the same test class is run several times for
 
725
        # parameterized tests, so make sure the names don't collide.  
 
726
        i = 0
 
727
        while True:
 
728
            if i > 0:
 
729
                candidate_dir = '%s/%s.%d' % (self.TEST_ROOT, short_id, i)
 
730
            else:
 
731
                candidate_dir = '%s/%s' % (self.TEST_ROOT, short_id)
 
732
            if os.path.exists(candidate_dir):
 
733
                i = i + 1
 
734
                continue
 
735
            else:
 
736
                self.test_dir = candidate_dir
 
737
                os.mkdir(self.test_dir)
 
738
                os.chdir(self.test_dir)
 
739
                break
684
740
        os.environ['HOME'] = self.test_dir
685
741
        os.environ['APPDATA'] = self.test_dir
686
742
        def _leaveDirectory():
705
761
        """
706
762
        # XXX: It's OK to just create them using forward slashes on windows?
707
763
        if transport is None or transport.is_readonly():
708
 
            transport = bzrlib.transport.get_transport(".")
 
764
            transport = get_transport(".")
709
765
        for name in shape:
710
766
            self.assert_(isinstance(name, basestring))
711
767
            if name[-1] == '/':
818
874
            base = base + relpath
819
875
        return base
820
876
 
821
 
    def make_branch(self, relpath):
 
877
    def get_transport(self):
 
878
        """Return a writeable transport for the test scratch space"""
 
879
        t = get_transport(self.get_url())
 
880
        self.assertFalse(t.is_readonly())
 
881
        return t
 
882
 
 
883
    def get_readonly_transport(self):
 
884
        """Return a readonly transport for the test scratch space
 
885
        
 
886
        This can be used to test that operations which should only need
 
887
        readonly access in fact do not try to write.
 
888
        """
 
889
        t = get_transport(self.get_readonly_url())
 
890
        self.assertTrue(t.is_readonly())
 
891
        return t
 
892
 
 
893
    def make_branch(self, relpath, format=None):
822
894
        """Create a branch on the transport at relpath."""
823
 
        repo = self.make_repository(relpath)
 
895
        repo = self.make_repository(relpath, format=format)
824
896
        return repo.bzrdir.create_branch()
825
897
 
826
 
    def make_bzrdir(self, relpath):
 
898
    def make_bzrdir(self, relpath, format=None):
827
899
        try:
828
900
            url = self.get_url(relpath)
829
 
            segments = url.split('/')
 
901
            segments = relpath.split('/')
830
902
            if segments and segments[-1] not in ('', '.'):
831
 
                parent = '/'.join(segments[:-1])
832
 
                t = bzrlib.transport.get_transport(parent)
 
903
                parent = self.get_url('/'.join(segments[:-1]))
 
904
                t = get_transport(parent)
833
905
                try:
834
906
                    t.mkdir(segments[-1])
835
907
                except errors.FileExists:
836
908
                    pass
837
 
            return bzrlib.bzrdir.BzrDir.create(url)
 
909
            if format is None:
 
910
                format=bzrlib.bzrdir.BzrDirFormat.get_default_format()
 
911
            # FIXME: make this use a single transport someday. RBC 20060418
 
912
            return format.initialize_on_transport(get_transport(relpath))
838
913
        except errors.UninitializableFormat:
839
914
            raise TestSkipped("Format %s is not initializable.")
840
915
 
841
 
    def make_repository(self, relpath):
 
916
    def make_repository(self, relpath, shared=False, format=None):
842
917
        """Create a repository on our default transport at relpath."""
843
 
        made_control = self.make_bzrdir(relpath)
844
 
        return made_control.create_repository()
 
918
        made_control = self.make_bzrdir(relpath, format=format)
 
919
        return made_control.create_repository(shared=shared)
845
920
 
846
 
    def make_branch_and_tree(self, relpath):
 
921
    def make_branch_and_tree(self, relpath, format=None):
847
922
        """Create a branch on the transport and a tree locally.
848
923
 
849
924
        Returns the tree.
852
927
        # this obviously requires a format that supports branch references
853
928
        # so check for that by checking bzrdir.BzrDirFormat.get_default_format()
854
929
        # RBC 20060208
855
 
        b = self.make_branch(relpath)
 
930
        b = self.make_branch(relpath, format=format)
856
931
        try:
857
932
            return b.bzrdir.create_workingtree()
858
933
        except errors.NotLocalUrl:
862
937
            # TODO: rbc 20060208
863
938
            return WorkingTreeFormat2().initialize(bzrdir.BzrDir.open(relpath))
864
939
 
 
940
    def assertIsDirectory(self, relpath, transport):
 
941
        """Assert that relpath within transport is a directory.
 
942
 
 
943
        This may not be possible on all transports; in that case it propagates
 
944
        a TransportNotPossible.
 
945
        """
 
946
        try:
 
947
            mode = transport.stat(relpath).st_mode
 
948
        except errors.NoSuchFile:
 
949
            self.fail("path %s is not a directory; no such file"
 
950
                      % (relpath))
 
951
        if not stat.S_ISDIR(mode):
 
952
            self.fail("path %s is not a directory; has mode %#o"
 
953
                      % (relpath, mode))
 
954
 
865
955
 
866
956
class ChrootedTestCase(TestCaseWithTransport):
867
957
    """A support class that provides readonly urls outside the local namespace.
942
1032
                   'bzrlib.tests.test_annotate',
943
1033
                   'bzrlib.tests.test_api',
944
1034
                   'bzrlib.tests.test_bad_files',
945
 
                   'bzrlib.tests.test_basis_inventory',
946
1035
                   'bzrlib.tests.test_branch',
947
1036
                   'bzrlib.tests.test_bzrdir',
948
1037
                   'bzrlib.tests.test_command',
954
1043
                   'bzrlib.tests.test_diff',
955
1044
                   'bzrlib.tests.test_doc_generate',
956
1045
                   'bzrlib.tests.test_errors',
 
1046
                   'bzrlib.tests.test_escaped_store',
957
1047
                   'bzrlib.tests.test_fetch',
958
1048
                   'bzrlib.tests.test_gpg',
959
1049
                   'bzrlib.tests.test_graph',
961
1051
                   'bzrlib.tests.test_http',
962
1052
                   'bzrlib.tests.test_identitymap',
963
1053
                   'bzrlib.tests.test_inv',
 
1054
                   'bzrlib.tests.test_knit',
 
1055
                   'bzrlib.tests.test_lockdir',
964
1056
                   'bzrlib.tests.test_lockable_files',
965
1057
                   'bzrlib.tests.test_log',
966
1058
                   'bzrlib.tests.test_merge',
971
1063
                   'bzrlib.tests.test_nonascii',
972
1064
                   'bzrlib.tests.test_options',
973
1065
                   'bzrlib.tests.test_osutils',
 
1066
                   'bzrlib.tests.test_patch',
974
1067
                   'bzrlib.tests.test_permissions',
975
1068
                   'bzrlib.tests.test_plugins',
 
1069
                   'bzrlib.tests.test_progress',
 
1070
                   'bzrlib.tests.test_reconcile',
976
1071
                   'bzrlib.tests.test_repository',
977
1072
                   'bzrlib.tests.test_revision',
978
1073
                   'bzrlib.tests.test_revisionnamespaces',
979
1074
                   'bzrlib.tests.test_revprops',
980
 
                   'bzrlib.tests.test_reweave',
981
1075
                   'bzrlib.tests.test_rio',
982
1076
                   'bzrlib.tests.test_sampler',
983
1077
                   'bzrlib.tests.test_selftest',
988
1082
                   'bzrlib.tests.test_store',
989
1083
                   'bzrlib.tests.test_symbol_versioning',
990
1084
                   'bzrlib.tests.test_testament',
 
1085
                   'bzrlib.tests.test_textfile',
 
1086
                   'bzrlib.tests.test_textmerge',
991
1087
                   'bzrlib.tests.test_trace',
992
1088
                   'bzrlib.tests.test_transactions',
 
1089
                   'bzrlib.tests.test_transform',
993
1090
                   'bzrlib.tests.test_transport',
994
1091
                   'bzrlib.tests.test_tsort',
 
1092
                   'bzrlib.tests.test_tuned_gzip',
995
1093
                   'bzrlib.tests.test_ui',
996
 
                   'bzrlib.tests.test_uncommit',
997
1094
                   'bzrlib.tests.test_upgrade',
 
1095
                   'bzrlib.tests.test_versionedfile',
998
1096
                   'bzrlib.tests.test_weave',
999
1097
                   'bzrlib.tests.test_whitebox',
1000
1098
                   'bzrlib.tests.test_workingtree',