~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

[merge] robertc's integration branch: add BzrDir, and checkouts

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
import codecs
24
25
from cStringIO import StringIO
25
26
import difflib
26
27
import errno
33
34
import tempfile
34
35
import unittest
35
36
import time
36
 
import codecs
 
37
 
37
38
 
38
39
import bzrlib.branch
 
40
import bzrlib.bzrdir as bzrdir
39
41
import bzrlib.commands
40
 
from bzrlib.errors import (BzrError,
41
 
                           FileExists,
42
 
                           UninitializableFormat,
43
 
                           )
 
42
import bzrlib.errors as errors
44
43
import bzrlib.inventory
45
44
import bzrlib.iterablefile
46
45
import bzrlib.merge3
56
55
from bzrlib.trace import mutter
57
56
from bzrlib.tests.TestUtil import TestLoader, TestSuite
58
57
from bzrlib.tests.treeshape import build_tree_contents
59
 
from bzrlib.workingtree import WorkingTree
 
58
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
60
59
 
61
60
default_transport = LocalRelpathServer
62
61
 
81
80
    import bzrlib.doc
82
81
    import bzrlib.tests.blackbox
83
82
    import bzrlib.tests.branch_implementations
 
83
    import bzrlib.tests.bzrdir_implementations
 
84
    import bzrlib.tests.repository_implementations
 
85
    import bzrlib.tests.workingtree_implementations
84
86
    return [
85
87
            bzrlib.doc,
86
88
            bzrlib.tests.blackbox,
87
89
            bzrlib.tests.branch_implementations,
 
90
            bzrlib.tests.bzrdir_implementations,
 
91
            bzrlib.tests.repository_implementations,
 
92
            bzrlib.tests.workingtree_implementations,
88
93
            ]
89
94
 
90
95
 
260
265
                                  charjunk=lambda x: False)
261
266
        return ''.join(difflines)
262
267
 
263
 
    def assertEqualDiff(self, a, b):
 
268
    def assertEqualDiff(self, a, b, message=None):
264
269
        """Assert two texts are equal, if not raise an exception.
265
270
        
266
271
        This is intended for use with multi-line strings where it can 
269
274
        # TODO: perhaps override assertEquals to call this for strings?
270
275
        if a == b:
271
276
            return
272
 
        raise AssertionError("texts not equal:\n" + 
 
277
        if message is None:
 
278
            message = "texts not equal:\n"
 
279
        raise AssertionError(message + 
273
280
                             self._ndiff_strings(a, b))      
274
281
        
 
282
    def assertEqualMode(self, mode, mode_test):
 
283
        self.assertEqual(mode, mode_test,
 
284
                         'mode mismatch %o != %o' % (mode, mode_test))
 
285
 
275
286
    def assertStartsWith(self, s, prefix):
276
287
        if not s.startswith(prefix):
277
288
            raise AssertionError('string %r does not start with %r' % (s, prefix))
614
625
                elif line_endings == 'native':
615
626
                    end = os.linesep
616
627
                else:
617
 
                    raise BzrError('Invalid line ending request %r' % (line_endings,))
 
628
                    raise errors.BzrError('Invalid line ending request %r' % (line_endings,))
618
629
                content = "contents of %s%s" % (name, end)
619
630
                transport.put(urlescape(name), StringIO(content))
620
631
 
664
675
        relpath provides for clients to get a path relative to the base url.
665
676
        These should only be downwards relative, not upwards.
666
677
        """
 
678
        base = self.get_readonly_server().get_url()
 
679
        if relpath is not None:
 
680
            if not base.endswith('/'):
 
681
                base = base + '/'
 
682
            base = base + relpath
 
683
        return base
 
684
 
 
685
    def get_readonly_server(self):
 
686
        """Get the server instance for the readonly transport
 
687
 
 
688
        This is useful for some tests with specific servers to do diagnostics.
 
689
        """
667
690
        if self.__readonly_server is None:
668
691
            if self.transport_readonly_server is None:
669
692
                # readonly decorator requested
675
698
                self.__readonly_server = self.transport_readonly_server()
676
699
                self.__readonly_server.setUp()
677
700
            self.addCleanup(self.__readonly_server.tearDown)
678
 
        base = self.__readonly_server.get_url()
679
 
        if relpath is not None:
680
 
            if not base.endswith('/'):
681
 
                base = base + '/'
682
 
            base = base + relpath
683
 
        return base
 
701
        return self.__readonly_server
 
702
 
 
703
    def get_server(self):
 
704
        """Get the read/write server instance.
 
705
 
 
706
        This is useful for some tests with specific servers that need
 
707
        diagnostics.
 
708
        """
 
709
        if self.__server is None:
 
710
            self.__server = self.transport_server()
 
711
            self.__server.setUp()
 
712
            self.addCleanup(self.__server.tearDown)
 
713
        return self.__server
684
714
 
685
715
    def get_url(self, relpath=None):
686
716
        """Get a URL for the readwrite transport.
690
720
        relpath provides for clients to get a path relative to the base url.
691
721
        These should only be downwards relative, not upwards.
692
722
        """
693
 
        if self.__server is None:
694
 
            self.__server = self.transport_server()
695
 
            self.__server.setUp()
696
 
            self.addCleanup(self.__server.tearDown)
697
 
        base = self.__server.get_url()
 
723
        base = self.get_server().get_url()
698
724
        if relpath is not None and relpath != '.':
699
725
            if not base.endswith('/'):
700
726
                base = base + '/'
703
729
 
704
730
    def make_branch(self, relpath):
705
731
        """Create a branch on the transport at relpath."""
 
732
        repo = self.make_repository(relpath)
 
733
        return repo.bzrdir.create_branch()
 
734
 
 
735
    def make_bzrdir(self, relpath):
706
736
        try:
707
737
            url = self.get_url(relpath)
708
 
            segments = relpath.split('/')
 
738
            segments = url.split('/')
709
739
            if segments and segments[-1] not in ('', '.'):
710
 
                parent = self.get_url('/'.join(segments[:-1]))
 
740
                parent = '/'.join(segments[:-1])
711
741
                t = bzrlib.transport.get_transport(parent)
712
742
                try:
713
743
                    t.mkdir(segments[-1])
714
 
                except FileExists:
 
744
                except errors.FileExists:
715
745
                    pass
716
 
            return bzrlib.branch.Branch.create(url)
717
 
        except UninitializableFormat:
 
746
            return bzrlib.bzrdir.BzrDir.create(url)
 
747
        except errors.UninitializableFormat:
718
748
            raise TestSkipped("Format %s is not initializable.")
719
749
 
 
750
    def make_repository(self, relpath):
 
751
        """Create a repository on our default transport at relpath."""
 
752
        made_control = self.make_bzrdir(relpath)
 
753
        return made_control.create_repository()
 
754
 
720
755
    def make_branch_and_tree(self, relpath):
721
756
        """Create a branch on the transport and a tree locally.
722
757
 
723
758
        Returns the tree.
724
759
        """
 
760
        # TODO: always use the local disk path for the working tree,
 
761
        # this obviously requires a format that supports branch references
 
762
        # so check for that by checking bzrdir.BzrDirFormat.get_default_format()
 
763
        # RBC 20060208
725
764
        b = self.make_branch(relpath)
726
 
        return WorkingTree.create(b, relpath)
 
765
        try:
 
766
            return b.bzrdir.create_workingtree()
 
767
        except errors.NotLocalUrl:
 
768
            # new formats - catch No tree error and create
 
769
            # a branch reference and a checkout.
 
770
            # old formats at that point - raise TestSkipped.
 
771
            # TODO: rbc 20060208
 
772
            return WorkingTreeFormat2().initialize(bzrdir.BzrDir.open(relpath))
727
773
 
728
774
 
729
775
class ChrootedTestCase(TestCaseWithTransport):
811
857
                   'bzrlib.tests.test_bad_files',
812
858
                   'bzrlib.tests.test_basis_inventory',
813
859
                   'bzrlib.tests.test_branch',
 
860
                   'bzrlib.tests.test_bzrdir',
814
861
                   'bzrlib.tests.test_command',
815
862
                   'bzrlib.tests.test_commit',
816
863
                   'bzrlib.tests.test_commit_merge',
819
866
                   'bzrlib.tests.test_decorators',
820
867
                   'bzrlib.tests.test_diff',
821
868
                   'bzrlib.tests.test_doc_generate',
 
869
                   'bzrlib.tests.test_errors',
822
870
                   'bzrlib.tests.test_fetch',
823
 
                   'bzrlib.tests.test_fileid_involved',
824
871
                   'bzrlib.tests.test_gpg',
825
872
                   'bzrlib.tests.test_graph',
826
873
                   'bzrlib.tests.test_hashcache',
837
884
                   'bzrlib.tests.test_nonascii',
838
885
                   'bzrlib.tests.test_options',
839
886
                   'bzrlib.tests.test_osutils',
840
 
                   'bzrlib.tests.test_parent',
841
887
                   'bzrlib.tests.test_permissions',
842
888
                   'bzrlib.tests.test_plugins',
 
889
                   'bzrlib.tests.test_repository',
843
890
                   'bzrlib.tests.test_revision',
844
891
                   'bzrlib.tests.test_revisionnamespaces',
845
892
                   'bzrlib.tests.test_revprops',