~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

  • Committer: Robert Collins
  • Date: 2006-02-13 21:52:05 UTC
  • mto: (1534.1.22 integration)
  • mto: This revision was merged to the branch mainline in revision 1554.
  • Revision ID: robertc@robertcollins.net-20060213215205-cb3bacc87092a639
Update per review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (C) 2005,2006 Canonical Ltd
 
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
 
 
18
from cStringIO import StringIO
 
19
import os
 
20
 
 
21
import bzrlib
 
22
from bzrlib.branch import Branch
 
23
import bzrlib.bzrdir as bzrdir
 
24
from bzrlib.bzrdir import BzrDir
 
25
import bzrlib.errors as errors
 
26
from bzrlib.errors import NotBranchError, NotVersionedError
 
27
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
 
28
from bzrlib.tests import TestCaseWithTransport
 
29
from bzrlib.trace import mutter
 
30
from bzrlib.transport import get_transport
 
31
import bzrlib.workingtree as workingtree
 
32
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
 
33
                                WorkingTree)
 
34
 
 
35
class TestTreeDirectory(TestCaseWithTransport):
 
36
 
 
37
    def test_kind_character(self):
 
38
        self.assertEqual(TreeDirectory().kind_character(), '/')
 
39
 
 
40
 
 
41
class TestTreeEntry(TestCaseWithTransport):
 
42
 
 
43
    def test_kind_character(self):
 
44
        self.assertEqual(TreeEntry().kind_character(), '???')
 
45
 
 
46
 
 
47
class TestTreeFile(TestCaseWithTransport):
 
48
 
 
49
    def test_kind_character(self):
 
50
        self.assertEqual(TreeFile().kind_character(), '')
 
51
 
 
52
 
 
53
class TestTreeLink(TestCaseWithTransport):
 
54
 
 
55
    def test_kind_character(self):
 
56
        self.assertEqual(TreeLink().kind_character(), '')
 
57
 
 
58
 
 
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
 
 
90
    def initialize(self, a_bzrdir, revision_id=None):
 
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('.')
 
122
        self.assertRaises(errors.NoWorkingTree,
 
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)
 
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'))
 
178
        # no last-revision file means 'None' or 'NULLREVISION'
 
179
        self.assertFalse(t.has('last-revision'))
 
180
        # TODO RBC 20060210 do a commit, check the inventory.basis is created 
 
181
        # correctly and last-revision file becomes present.