~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Martin Pool
  • Date: 2006-03-06 11:20:10 UTC
  • mfrom: (1593 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060306112010-17c0170dde5d1eea
[merge] large merge to sync with bzr.dev

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
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
 
45
import bzrlib.lockdir
46
46
import bzrlib.merge3
47
47
import bzrlib.osutils
48
48
import bzrlib.osutils as osutils
49
49
import bzrlib.plugin
50
50
import bzrlib.store
51
51
import bzrlib.trace
52
 
from bzrlib.transport import urlescape
 
52
from bzrlib.transport import urlescape, get_transport
53
53
import bzrlib.transport
54
54
from bzrlib.transport.local import LocalRelpathServer
55
55
from bzrlib.transport.readonly import ReadonlyServer
56
56
from bzrlib.trace import mutter
57
57
from bzrlib.tests.TestUtil import TestLoader, TestSuite
58
58
from bzrlib.tests.treeshape import build_tree_contents
59
 
from bzrlib.workingtree import WorkingTree
 
59
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
60
60
 
61
61
default_transport = LocalRelpathServer
62
62
 
67
67
                      bzrlib.errors,
68
68
                      bzrlib.inventory,
69
69
                      bzrlib.iterablefile,
 
70
                      bzrlib.lockdir,
70
71
                      bzrlib.merge3,
71
72
                      bzrlib.option,
72
73
                      bzrlib.osutils,
81
82
    import bzrlib.doc
82
83
    import bzrlib.tests.blackbox
83
84
    import bzrlib.tests.branch_implementations
 
85
    import bzrlib.tests.bzrdir_implementations
 
86
    import bzrlib.tests.interrepository_implementations
 
87
    import bzrlib.tests.repository_implementations
 
88
    import bzrlib.tests.workingtree_implementations
84
89
    return [
85
90
            bzrlib.doc,
 
91
            bzrlib.tests.blackbox,
86
92
            bzrlib.tests.branch_implementations,
 
93
            bzrlib.tests.bzrdir_implementations,
 
94
            bzrlib.tests.interrepository_implementations,
 
95
            bzrlib.tests.repository_implementations,
 
96
            bzrlib.tests.workingtree_implementations,
87
97
            ]
88
98
 
89
99
 
259
269
                                  charjunk=lambda x: False)
260
270
        return ''.join(difflines)
261
271
 
262
 
    def assertEqualDiff(self, a, b):
 
272
    def assertEqualDiff(self, a, b, message=None):
263
273
        """Assert two texts are equal, if not raise an exception.
264
274
        
265
275
        This is intended for use with multi-line strings where it can 
268
278
        # TODO: perhaps override assertEquals to call this for strings?
269
279
        if a == b:
270
280
            return
271
 
        raise AssertionError("texts not equal:\n" + 
 
281
        if message is None:
 
282
            message = "texts not equal:\n"
 
283
        raise AssertionError(message + 
272
284
                             self._ndiff_strings(a, b))      
273
285
        
 
286
    def assertEqualMode(self, mode, mode_test):
 
287
        self.assertEqual(mode, mode_test,
 
288
                         'mode mismatch %o != %o' % (mode, mode_test))
 
289
 
274
290
    def assertStartsWith(self, s, prefix):
275
291
        if not s.startswith(prefix):
276
292
            raise AssertionError('string %r does not start with %r' % (s, prefix))
567
583
            break
568
584
        # make a fake bzr directory there to prevent any tests propagating
569
585
        # up onto the source directory's real branch
570
 
        os.mkdir(osutils.pathjoin(TestCaseInTempDir.TEST_ROOT, '.bzr'))
 
586
        bzrdir.BzrDir.create_standalone_workingtree(TestCaseInTempDir.TEST_ROOT)
571
587
 
572
588
    def setUp(self):
573
589
        super(TestCaseInTempDir, self).setUp()
602
618
        """
603
619
        # XXX: It's OK to just create them using forward slashes on windows?
604
620
        if transport is None or transport.is_readonly():
605
 
            transport = bzrlib.transport.get_transport(".")
 
621
            transport = get_transport(".")
606
622
        for name in shape:
607
623
            self.assert_(isinstance(name, basestring))
608
624
            if name[-1] == '/':
613
629
                elif line_endings == 'native':
614
630
                    end = os.linesep
615
631
                else:
616
 
                    raise BzrError('Invalid line ending request %r' % (line_endings,))
 
632
                    raise errors.BzrError('Invalid line ending request %r' % (line_endings,))
617
633
                content = "contents of %s%s" % (name, end)
618
634
                transport.put(urlescape(name), StringIO(content))
619
635
 
663
679
        relpath provides for clients to get a path relative to the base url.
664
680
        These should only be downwards relative, not upwards.
665
681
        """
 
682
        base = self.get_readonly_server().get_url()
 
683
        if relpath is not None:
 
684
            if not base.endswith('/'):
 
685
                base = base + '/'
 
686
            base = base + relpath
 
687
        return base
 
688
 
 
689
    def get_readonly_server(self):
 
690
        """Get the server instance for the readonly transport
 
691
 
 
692
        This is useful for some tests with specific servers to do diagnostics.
 
693
        """
666
694
        if self.__readonly_server is None:
667
695
            if self.transport_readonly_server is None:
668
696
                # readonly decorator requested
674
702
                self.__readonly_server = self.transport_readonly_server()
675
703
                self.__readonly_server.setUp()
676
704
            self.addCleanup(self.__readonly_server.tearDown)
677
 
        base = self.__readonly_server.get_url()
678
 
        if relpath is not None:
679
 
            if not base.endswith('/'):
680
 
                base = base + '/'
681
 
            base = base + relpath
682
 
        return base
 
705
        return self.__readonly_server
 
706
 
 
707
    def get_server(self):
 
708
        """Get the read/write server instance.
 
709
 
 
710
        This is useful for some tests with specific servers that need
 
711
        diagnostics.
 
712
        """
 
713
        if self.__server is None:
 
714
            self.__server = self.transport_server()
 
715
            self.__server.setUp()
 
716
            self.addCleanup(self.__server.tearDown)
 
717
        return self.__server
683
718
 
684
719
    def get_url(self, relpath=None):
685
720
        """Get a URL for the readwrite transport.
689
724
        relpath provides for clients to get a path relative to the base url.
690
725
        These should only be downwards relative, not upwards.
691
726
        """
692
 
        if self.__server is None:
693
 
            self.__server = self.transport_server()
694
 
            self.__server.setUp()
695
 
            self.addCleanup(self.__server.tearDown)
696
 
        base = self.__server.get_url()
 
727
        base = self.get_server().get_url()
697
728
        if relpath is not None and relpath != '.':
698
729
            if not base.endswith('/'):
699
730
                base = base + '/'
700
731
            base = base + relpath
701
732
        return base
702
733
 
 
734
    def get_transport(self):
 
735
        """Return a writeable transport for the test scratch space"""
 
736
        t = get_transport(self.get_url())
 
737
        self.assertFalse(t.is_readonly())
 
738
        return t
 
739
 
 
740
    def get_readonly_transport(self):
 
741
        """Return a readonly transport for the test scratch space
 
742
        
 
743
        This can be used to test that operations which should only need
 
744
        readonly access in fact do not try to write.
 
745
        """
 
746
        t = get_transport(self.get_readonly_url())
 
747
        self.assertTrue(t.is_readonly())
 
748
        return t
 
749
 
703
750
    def make_branch(self, relpath):
704
751
        """Create a branch on the transport at relpath."""
 
752
        repo = self.make_repository(relpath)
 
753
        return repo.bzrdir.create_branch()
 
754
 
 
755
    def make_bzrdir(self, relpath):
705
756
        try:
706
757
            url = self.get_url(relpath)
707
758
            segments = relpath.split('/')
708
759
            if segments and segments[-1] not in ('', '.'):
709
760
                parent = self.get_url('/'.join(segments[:-1]))
710
 
                t = bzrlib.transport.get_transport(parent)
 
761
                t = get_transport(parent)
711
762
                try:
712
763
                    t.mkdir(segments[-1])
713
 
                except FileExists:
 
764
                except errors.FileExists:
714
765
                    pass
715
 
            return bzrlib.branch.Branch.create(url)
716
 
        except UninitializableFormat:
 
766
            return bzrlib.bzrdir.BzrDir.create(url)
 
767
        except errors.UninitializableFormat:
717
768
            raise TestSkipped("Format %s is not initializable.")
718
769
 
 
770
    def make_repository(self, relpath, shared=False):
 
771
        """Create a repository on our default transport at relpath."""
 
772
        made_control = self.make_bzrdir(relpath)
 
773
        return made_control.create_repository(shared=shared)
 
774
 
719
775
    def make_branch_and_tree(self, relpath):
720
776
        """Create a branch on the transport and a tree locally.
721
777
 
722
778
        Returns the tree.
723
779
        """
 
780
        # TODO: always use the local disk path for the working tree,
 
781
        # this obviously requires a format that supports branch references
 
782
        # so check for that by checking bzrdir.BzrDirFormat.get_default_format()
 
783
        # RBC 20060208
724
784
        b = self.make_branch(relpath)
725
 
        return WorkingTree.create(b, relpath)
 
785
        try:
 
786
            return b.bzrdir.create_workingtree()
 
787
        except errors.NotLocalUrl:
 
788
            # new formats - catch No tree error and create
 
789
            # a branch reference and a checkout.
 
790
            # old formats at that point - raise TestSkipped.
 
791
            # TODO: rbc 20060208
 
792
            return WorkingTreeFormat2().initialize(bzrdir.BzrDir.open(relpath))
726
793
 
727
794
 
728
795
class ChrootedTestCase(TestCaseWithTransport):
810
877
                   'bzrlib.tests.test_bad_files',
811
878
                   'bzrlib.tests.test_basis_inventory',
812
879
                   'bzrlib.tests.test_branch',
 
880
                   'bzrlib.tests.test_bzrdir',
813
881
                   'bzrlib.tests.test_command',
814
882
                   'bzrlib.tests.test_commit',
815
883
                   'bzrlib.tests.test_commit_merge',
818
886
                   'bzrlib.tests.test_decorators',
819
887
                   'bzrlib.tests.test_diff',
820
888
                   'bzrlib.tests.test_doc_generate',
 
889
                   'bzrlib.tests.test_errors',
821
890
                   'bzrlib.tests.test_fetch',
822
 
                   'bzrlib.tests.test_fileid_involved',
823
891
                   'bzrlib.tests.test_gpg',
824
892
                   'bzrlib.tests.test_graph',
825
893
                   'bzrlib.tests.test_hashcache',
826
894
                   'bzrlib.tests.test_http',
827
895
                   'bzrlib.tests.test_identitymap',
828
896
                   'bzrlib.tests.test_inv',
 
897
                   'bzrlib.tests.test_lockdir',
829
898
                   'bzrlib.tests.test_lockable_files',
830
899
                   'bzrlib.tests.test_log',
831
900
                   'bzrlib.tests.test_merge',
836
905
                   'bzrlib.tests.test_nonascii',
837
906
                   'bzrlib.tests.test_options',
838
907
                   'bzrlib.tests.test_osutils',
839
 
                   'bzrlib.tests.test_parent',
840
908
                   'bzrlib.tests.test_permissions',
841
909
                   'bzrlib.tests.test_plugins',
 
910
                   'bzrlib.tests.test_reconcile',
 
911
                   'bzrlib.tests.test_repository',
842
912
                   'bzrlib.tests.test_revision',
843
913
                   'bzrlib.tests.test_revisionnamespaces',
844
914
                   'bzrlib.tests.test_revprops',
855
925
                   'bzrlib.tests.test_testament',
856
926
                   'bzrlib.tests.test_trace',
857
927
                   'bzrlib.tests.test_transactions',
 
928
                   'bzrlib.tests.test_transform',
858
929
                   'bzrlib.tests.test_transport',
859
930
                   'bzrlib.tests.test_tsort',
860
931
                   'bzrlib.tests.test_ui',