~bzr-pqm/bzr/bzr.dev

1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1
# (C) 2005,2006 Canonical Ltd
1399.1.12 by Robert Collins
add new test script
2
# Authors:  Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
18
from cStringIO import StringIO
1399.1.12 by Robert Collins
add new test script
19
import os
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
20
21
import bzrlib
1399.1.12 by Robert Collins
add new test script
22
from bzrlib.branch import Branch
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
23
import bzrlib.bzrdir as bzrdir
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
24
from bzrlib.bzrdir import BzrDir
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
25
import bzrlib.errors as errors
1508.1.3 by Robert Collins
Do not consider urls to be relative paths within working trees.
26
from bzrlib.errors import NotBranchError, NotVersionedError
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
27
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
28
from bzrlib.tests import TestCaseWithTransport
1399.1.12 by Robert Collins
add new test script
29
from bzrlib.trace import mutter
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
30
from bzrlib.transport import get_transport
31
import bzrlib.workingtree as workingtree
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
32
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
33
                                WorkingTree)
1399.1.12 by Robert Collins
add new test script
34
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
35
class TestTreeDirectory(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
36
37
    def test_kind_character(self):
38
        self.assertEqual(TreeDirectory().kind_character(), '/')
39
40
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
41
class TestTreeEntry(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
42
43
    def test_kind_character(self):
44
        self.assertEqual(TreeEntry().kind_character(), '???')
45
46
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
47
class TestTreeFile(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
48
49
    def test_kind_character(self):
50
        self.assertEqual(TreeFile().kind_character(), '')
51
52
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
53
class TestTreeLink(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
54
55
    def test_kind_character(self):
56
        self.assertEqual(TreeLink().kind_character(), '')
57
58
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
59
class TestDefaultFormat(TestCaseWithTransport):
60
61
    def test_get_set_default_format(self):
62
        old_format = workingtree.WorkingTreeFormat.get_default_format()
63
        # default is 3
64
        self.assertTrue(isinstance(old_format, workingtree.WorkingTreeFormat3))
65
        workingtree.WorkingTreeFormat.set_default_format(SampleTreeFormat())
66
        try:
67
            # the default branch format is used by the meta dir format
68
            # which is not the default bzrdir format at this point
69
            dir = bzrdir.BzrDirMetaFormat1().initialize('.')
70
            dir.create_repository()
71
            dir.create_branch()
72
            result = dir.create_workingtree()
73
            self.assertEqual(result, 'A tree')
74
        finally:
75
            workingtree.WorkingTreeFormat.set_default_format(old_format)
76
        self.assertEqual(old_format, workingtree.WorkingTreeFormat.get_default_format())
77
78
79
class SampleTreeFormat(workingtree.WorkingTreeFormat):
80
    """A sample format
81
82
    this format is initializable, unsupported to aid in testing the 
83
    open and open_downlevel routines.
84
    """
85
86
    def get_format_string(self):
87
        """See WorkingTreeFormat.get_format_string()."""
88
        return "Sample tree format."
89
1508.1.24 by Robert Collins
Add update command for use with checkouts.
90
    def initialize(self, a_bzrdir, revision_id=None):
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
91
        """Sample branches cannot be created."""
92
        t = a_bzrdir.get_workingtree_transport(self)
93
        t.put('format', StringIO(self.get_format_string()))
94
        return 'A tree'
95
96
    def is_supported(self):
97
        return False
98
99
    def open(self, transport, _found=False):
100
        return "opened tree."
101
102
103
class TestWorkingTreeFormat(TestCaseWithTransport):
104
    """Tests for the WorkingTreeFormat facility."""
105
106
    def test_find_format(self):
107
        # is the right format object found for a working tree?
108
        # create a branch with a few known format objects.
109
        self.build_tree(["foo/", "bar/"])
110
        def check_format(format, url):
111
            dir = format._matchingbzrdir.initialize(url)
112
            dir.create_repository()
113
            dir.create_branch()
114
            format.initialize(dir)
115
            t = get_transport(url)
116
            found_format = workingtree.WorkingTreeFormat.find_format(dir)
117
            self.failUnless(isinstance(found_format, format.__class__))
118
        check_format(workingtree.WorkingTreeFormat3(), "bar")
119
        
120
    def test_find_format_no_tree(self):
121
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
122
        self.assertRaises(errors.NoWorkingTree,
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
123
                          workingtree.WorkingTreeFormat.find_format,
124
                          dir)
125
126
    def test_find_format_unknown_format(self):
127
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
128
        dir.create_repository()
129
        dir.create_branch()
130
        SampleTreeFormat().initialize(dir)
131
        self.assertRaises(errors.UnknownFormatError,
132
                          workingtree.WorkingTreeFormat.find_format,
133
                          dir)
134
135
    def test_register_unregister_format(self):
136
        format = SampleTreeFormat()
137
        # make a control dir
138
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
139
        dir.create_repository()
140
        dir.create_branch()
141
        # make a branch
142
        format.initialize(dir)
143
        # register a format for it.
144
        workingtree.WorkingTreeFormat.register_format(format)
145
        # which branch.Open will refuse (not supported)
146
        self.assertRaises(errors.UnsupportedFormatError, workingtree.WorkingTree.open, '.')
147
        # but open_downlevel will work
148
        self.assertEqual(format.open(dir), workingtree.WorkingTree.open_downlevel('.'))
149
        # unregister the format
150
        workingtree.WorkingTreeFormat.unregister_format(format)
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
151
152
153
class TestWorkingTreeFormat3(TestCaseWithTransport):
154
    """Tests specific to WorkingTreeFormat3."""
155
156
    def test_disk_layout(self):
157
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
158
        control.create_repository()
159
        control.create_branch()
160
        tree = workingtree.WorkingTreeFormat3().initialize(control)
161
        # we want:
162
        # format 'Bazaar-NG Working Tree format 3'
163
        # lock ''
164
        # inventory = blank inventory
165
        # pending-merges = ''
166
        # stat-cache = ??
167
        # no inventory.basis yet
168
        t = control.get_workingtree_transport(None)
169
        self.assertEqualDiff('Bazaar-NG Working Tree format 3',
170
                             t.get('format').read())
171
        self.assertEqualDiff('', t.get('lock').read())
172
        self.assertEqualDiff('<inventory format="5">\n'
173
                             '</inventory>\n',
174
                             t.get('inventory').read())
175
        self.assertEqualDiff('### bzr hashcache v5\n',
176
                             t.get('stat-cache').read())
177
        self.assertFalse(t.has('inventory.basis'))
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
178
        # no last-revision file means 'None' or 'NULLREVISION'
179
        self.assertFalse(t.has('last-revision'))
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
180
        # TODO RBC 20060210 do a commit, check the inventory.basis is created 
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
181
        # correctly and last-revision file becomes present.