15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from cStringIO import StringIO
19
22
from bzrlib.branch import Branch
20
from bzrlib.selftest import TestCaseInTempDir
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
21
29
from bzrlib.trace import mutter
22
from bzrlib.workingtree import TreeEntry, TreeDirectory, TreeFile, TreeLink
30
from bzrlib.transport import get_transport
31
import bzrlib.workingtree as workingtree
32
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
24
class TestTreeDirectory(TestCaseInTempDir):
35
class TestTreeDirectory(TestCaseWithTransport):
26
37
def test_kind_character(self):
27
38
self.assertEqual(TreeDirectory().kind_character(), '/')
30
class TestTreeEntry(TestCaseInTempDir):
41
class TestTreeEntry(TestCaseWithTransport):
32
43
def test_kind_character(self):
33
44
self.assertEqual(TreeEntry().kind_character(), '???')
36
class TestTreeFile(TestCaseInTempDir):
47
class TestTreeFile(TestCaseWithTransport):
38
49
def test_kind_character(self):
39
50
self.assertEqual(TreeFile().kind_character(), '')
42
class TestTreeLink(TestCaseInTempDir):
53
class TestTreeLink(TestCaseWithTransport):
44
55
def test_kind_character(self):
45
56
self.assertEqual(TreeLink().kind_character(), '')
48
class TestWorkingTree(TestCaseInTempDir):
50
def test_listfiles(self):
51
branch = Branch.initialize('.')
53
print >> open('file', 'w'), "content"
54
os.symlink('target', 'symlink')
55
tree = branch.working_tree()
56
files = list(tree.list_files())
57
self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
58
self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
59
self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
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.