~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
 
39
39
import bzrlib.branch
 
40
import bzrlib.bzrdir as bzrdir
40
41
import bzrlib.commands
41
 
from bzrlib.errors import (BzrError,
42
 
                           FileExists,
43
 
                           UninitializableFormat,
44
 
                           )
 
42
import bzrlib.errors as errors
45
43
import bzrlib.inventory
46
44
import bzrlib.iterablefile
47
45
import bzrlib.merge3
57
55
from bzrlib.trace import mutter
58
56
from bzrlib.tests.TestUtil import TestLoader, TestSuite
59
57
from bzrlib.tests.treeshape import build_tree_contents
60
 
from bzrlib.workingtree import WorkingTree
 
58
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
61
59
 
62
60
default_transport = LocalRelpathServer
63
61
 
267
265
                                  charjunk=lambda x: False)
268
266
        return ''.join(difflines)
269
267
 
270
 
    def assertEqualDiff(self, a, b):
 
268
    def assertEqualDiff(self, a, b, message=None):
271
269
        """Assert two texts are equal, if not raise an exception.
272
270
        
273
271
        This is intended for use with multi-line strings where it can 
276
274
        # TODO: perhaps override assertEquals to call this for strings?
277
275
        if a == b:
278
276
            return
279
 
        raise AssertionError("texts not equal:\n" + 
 
277
        if message is None:
 
278
            message = "texts not equal:\n"
 
279
        raise AssertionError(message + 
280
280
                             self._ndiff_strings(a, b))      
281
281
        
 
282
    def assertEqualMode(self, mode, mode_test):
 
283
        self.assertEqual(mode, mode_test,
 
284
                         'mode mismatch %o != %o' % (mode, mode_test))
 
285
 
282
286
    def assertStartsWith(self, s, prefix):
283
287
        if not s.startswith(prefix):
284
288
            raise AssertionError('string %r does not start with %r' % (s, prefix))
621
625
                elif line_endings == 'native':
622
626
                    end = os.linesep
623
627
                else:
624
 
                    raise BzrError('Invalid line ending request %r' % (line_endings,))
 
628
                    raise errors.BzrError('Invalid line ending request %r' % (line_endings,))
625
629
                content = "contents of %s%s" % (name, end)
626
630
                transport.put(urlescape(name), StringIO(content))
627
631
 
671
675
        relpath provides for clients to get a path relative to the base url.
672
676
        These should only be downwards relative, not upwards.
673
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
        """
674
690
        if self.__readonly_server is None:
675
691
            if self.transport_readonly_server is None:
676
692
                # readonly decorator requested
682
698
                self.__readonly_server = self.transport_readonly_server()
683
699
                self.__readonly_server.setUp()
684
700
            self.addCleanup(self.__readonly_server.tearDown)
685
 
        base = self.__readonly_server.get_url()
686
 
        if relpath is not None:
687
 
            if not base.endswith('/'):
688
 
                base = base + '/'
689
 
            base = base + relpath
690
 
        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
691
714
 
692
715
    def get_url(self, relpath=None):
693
716
        """Get a URL for the readwrite transport.
697
720
        relpath provides for clients to get a path relative to the base url.
698
721
        These should only be downwards relative, not upwards.
699
722
        """
700
 
        if self.__server is None:
701
 
            self.__server = self.transport_server()
702
 
            self.__server.setUp()
703
 
            self.addCleanup(self.__server.tearDown)
704
 
        base = self.__server.get_url()
 
723
        base = self.get_server().get_url()
705
724
        if relpath is not None and relpath != '.':
706
725
            if not base.endswith('/'):
707
726
                base = base + '/'
722
741
                t = bzrlib.transport.get_transport(parent)
723
742
                try:
724
743
                    t.mkdir(segments[-1])
725
 
                except FileExists:
 
744
                except errors.FileExists:
726
745
                    pass
727
746
            return bzrlib.bzrdir.BzrDir.create(url)
728
 
        except UninitializableFormat:
 
747
        except errors.UninitializableFormat:
729
748
            raise TestSkipped("Format %s is not initializable.")
730
749
 
731
750
    def make_repository(self, relpath):
738
757
 
739
758
        Returns the tree.
740
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
741
764
        b = self.make_branch(relpath)
742
 
        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))
743
773
 
744
774
 
745
775
class ChrootedTestCase(TestCaseWithTransport):