1
# (C) 2005,2006 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
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.
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.
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
18
from cStringIO import StringIO
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,
35
class TestTreeDirectory(TestCaseWithTransport):
37
def test_kind_character(self):
38
self.assertEqual(TreeDirectory().kind_character(), '/')
41
class TestTreeEntry(TestCaseWithTransport):
43
def test_kind_character(self):
44
self.assertEqual(TreeEntry().kind_character(), '???')
47
class TestTreeFile(TestCaseWithTransport):
49
def test_kind_character(self):
50
self.assertEqual(TreeFile().kind_character(), '')
53
class TestTreeLink(TestCaseWithTransport):
55
def test_kind_character(self):
56
self.assertEqual(TreeLink().kind_character(), '')
59
class TestDefaultFormat(TestCaseWithTransport):
61
def test_get_set_default_format(self):
62
old_format = workingtree.WorkingTreeFormat.get_default_format()
64
self.assertTrue(isinstance(old_format, workingtree.WorkingTreeFormat3))
65
workingtree.WorkingTreeFormat.set_default_format(SampleTreeFormat())
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()
72
result = dir.create_workingtree()
73
self.assertEqual(result, 'A tree')
75
workingtree.WorkingTreeFormat.set_default_format(old_format)
76
self.assertEqual(old_format, workingtree.WorkingTreeFormat.get_default_format())
79
class SampleTreeFormat(workingtree.WorkingTreeFormat):
82
this format is initializable, unsupported to aid in testing the
83
open and open_downlevel routines.
86
def get_format_string(self):
87
"""See WorkingTreeFormat.get_format_string()."""
88
return "Sample tree format."
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()))
96
def is_supported(self):
99
def open(self, transport, _found=False):
100
return "opened tree."
103
class TestWorkingTreeFormat(TestCaseWithTransport):
104
"""Tests for the WorkingTreeFormat facility."""
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()
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")
120
def test_find_format_no_tree(self):
121
dir = bzrdir.BzrDirMetaFormat1().initialize('.')
122
self.assertRaises(errors.NoWorkingTree,
123
workingtree.WorkingTreeFormat.find_format,
126
def test_find_format_unknown_format(self):
127
dir = bzrdir.BzrDirMetaFormat1().initialize('.')
128
dir.create_repository()
130
SampleTreeFormat().initialize(dir)
131
self.assertRaises(errors.UnknownFormatError,
132
workingtree.WorkingTreeFormat.find_format,
135
def test_register_unregister_format(self):
136
format = SampleTreeFormat()
138
dir = bzrdir.BzrDirMetaFormat1().initialize('.')
139
dir.create_repository()
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)
153
class TestWorkingTreeFormat3(TestCaseWithTransport):
154
"""Tests specific to WorkingTreeFormat3."""
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)
162
# format 'Bazaar-NG Working Tree format 3'
164
# inventory = blank inventory
165
# pending-merges = ''
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'
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.