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
22
19
from bzrlib.branch import Branch
23
import bzrlib.bzrdir as bzrdir
24
from bzrlib.bzrdir import BzrDir
25
from bzrlib.conflicts import *
26
import bzrlib.errors as errors
27
from bzrlib.errors import NotBranchError, NotVersionedError
28
from bzrlib.lockdir import LockDir
29
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
30
from bzrlib.tests import TestCaseWithTransport, TestSkipped
20
from bzrlib.selftest import TestCaseInTempDir
31
21
from bzrlib.trace import mutter
32
from bzrlib.transport import get_transport
33
import bzrlib.workingtree as workingtree
34
22
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
37
class TestTreeDirectory(TestCaseWithTransport):
25
class TestTreeDirectory(TestCaseInTempDir):
39
27
def test_kind_character(self):
40
28
self.assertEqual(TreeDirectory().kind_character(), '/')
43
class TestTreeEntry(TestCaseWithTransport):
31
class TestTreeEntry(TestCaseInTempDir):
45
33
def test_kind_character(self):
46
34
self.assertEqual(TreeEntry().kind_character(), '???')
49
class TestTreeFile(TestCaseWithTransport):
37
class TestTreeFile(TestCaseInTempDir):
51
39
def test_kind_character(self):
52
40
self.assertEqual(TreeFile().kind_character(), '')
55
class TestTreeLink(TestCaseWithTransport):
43
class TestTreeLink(TestCaseInTempDir):
57
45
def test_kind_character(self):
58
46
self.assertEqual(TreeLink().kind_character(), '')
61
class TestDefaultFormat(TestCaseWithTransport):
63
def test_get_set_default_format(self):
64
old_format = workingtree.WorkingTreeFormat.get_default_format()
66
self.assertTrue(isinstance(old_format, workingtree.WorkingTreeFormat3))
67
workingtree.WorkingTreeFormat.set_default_format(SampleTreeFormat())
69
# the default branch format is used by the meta dir format
70
# which is not the default bzrdir format at this point
71
dir = bzrdir.BzrDirMetaFormat1().initialize('.')
72
dir.create_repository()
74
result = dir.create_workingtree()
75
self.assertEqual(result, 'A tree')
77
workingtree.WorkingTreeFormat.set_default_format(old_format)
78
self.assertEqual(old_format, workingtree.WorkingTreeFormat.get_default_format())
81
class SampleTreeFormat(workingtree.WorkingTreeFormat):
84
this format is initializable, unsupported to aid in testing the
85
open and open_downlevel routines.
88
def get_format_string(self):
89
"""See WorkingTreeFormat.get_format_string()."""
90
return "Sample tree format."
92
def initialize(self, a_bzrdir, revision_id=None):
93
"""Sample branches cannot be created."""
94
t = a_bzrdir.get_workingtree_transport(self)
95
t.put('format', StringIO(self.get_format_string()))
98
def is_supported(self):
101
def open(self, transport, _found=False):
102
return "opened tree."
105
class TestWorkingTreeFormat(TestCaseWithTransport):
106
"""Tests for the WorkingTreeFormat facility."""
108
def test_find_format(self):
109
# is the right format object found for a working tree?
110
# create a branch with a few known format objects.
111
self.build_tree(["foo/", "bar/"])
112
def check_format(format, url):
113
dir = format._matchingbzrdir.initialize(url)
114
dir.create_repository()
116
format.initialize(dir)
117
t = get_transport(url)
118
found_format = workingtree.WorkingTreeFormat.find_format(dir)
119
self.failUnless(isinstance(found_format, format.__class__))
120
check_format(workingtree.WorkingTreeFormat3(), "bar")
122
def test_find_format_no_tree(self):
123
dir = bzrdir.BzrDirMetaFormat1().initialize('.')
124
self.assertRaises(errors.NoWorkingTree,
125
workingtree.WorkingTreeFormat.find_format,
128
def test_find_format_unknown_format(self):
129
dir = bzrdir.BzrDirMetaFormat1().initialize('.')
130
dir.create_repository()
132
SampleTreeFormat().initialize(dir)
133
self.assertRaises(errors.UnknownFormatError,
134
workingtree.WorkingTreeFormat.find_format,
137
def test_register_unregister_format(self):
138
format = SampleTreeFormat()
140
dir = bzrdir.BzrDirMetaFormat1().initialize('.')
141
dir.create_repository()
144
format.initialize(dir)
145
# register a format for it.
146
workingtree.WorkingTreeFormat.register_format(format)
147
# which branch.Open will refuse (not supported)
148
self.assertRaises(errors.UnsupportedFormatError, workingtree.WorkingTree.open, '.')
149
# but open_downlevel will work
150
self.assertEqual(format.open(dir), workingtree.WorkingTree.open_downlevel('.'))
151
# unregister the format
152
workingtree.WorkingTreeFormat.unregister_format(format)
155
class TestWorkingTreeFormat3(TestCaseWithTransport):
156
"""Tests specific to WorkingTreeFormat3."""
158
def test_disk_layout(self):
159
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
160
control.create_repository()
161
control.create_branch()
162
tree = workingtree.WorkingTreeFormat3().initialize(control)
164
# format 'Bazaar-NG Working Tree format 3'
165
# inventory = blank inventory
166
# pending-merges = ''
168
# no inventory.basis yet
169
t = control.get_workingtree_transport(None)
170
self.assertEqualDiff('Bazaar-NG Working Tree format 3',
171
t.get('format').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.
183
def test_uses_lockdir(self):
184
"""WorkingTreeFormat3 uses its own LockDir:
186
- lock is a directory
187
- when the WorkingTree is locked, LockDir can see that
189
t = self.get_transport()
191
dir = bzrdir.BzrDirMetaFormat1().initialize(url)
192
repo = dir.create_repository()
193
branch = dir.create_branch()
195
tree = workingtree.WorkingTreeFormat3().initialize(dir)
196
except errors.NotLocalUrl:
197
raise TestSkipped('Not a local URL')
198
self.assertIsDirectory('.bzr', t)
199
self.assertIsDirectory('.bzr/checkout', t)
200
self.assertIsDirectory('.bzr/checkout/lock', t)
201
our_lock = LockDir(t, '.bzr/checkout/lock')
202
self.assertEquals(our_lock.peek(), None)
49
class TestWorkingTree(TestCaseInTempDir):
51
def test_listfiles(self):
52
branch = Branch.initialize('.')
54
print >> open('file', 'w'), "content"
55
os.symlink('target', 'symlink')
56
tree = branch.working_tree()
57
files = list(tree.list_files())
58
self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
59
self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
60
self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
62
def test_construct_with_branch(self):
63
branch = Branch.initialize('.')
64
tree = WorkingTree(branch.base, branch)
65
self.assertEqual(branch, tree.branch)
66
self.assertEqual(branch.inventory, tree._inventory)
67
self.assertEqual(branch.base, tree.basedir)
69
def test_construct_without_branch(self):
70
branch = Branch.initialize('.')
71
tree = WorkingTree(branch.base)
72
self.assertEqual(branch.base, tree.branch.base)
73
self.assertEqual(branch.inventory, tree._inventory)
74
self.assertEqual(branch.base, tree.basedir)
76
def test_basic_relpath(self):
77
# for comprehensive relpath tests, see whitebox.py.
78
branch = Branch.initialize('.')
79
tree = WorkingTree(branch.base)
80
self.assertEqual('child',
81
tree.relpath(os.path.join(os.getcwd(), 'child')))
83
def test_lock_locks_branch(self):
84
branch = Branch.initialize('.')
85
tree = WorkingTree(branch.base)
87
self.assertEqual(1, tree.branch._lock_count)
88
self.assertEqual('r', tree.branch._lock_mode)
90
self.assertEqual(None, tree.branch._lock_count)
204
self.assertTrue(our_lock.peek())
92
self.assertEqual(1, tree.branch._lock_count)
93
self.assertEqual('w', tree.branch._lock_mode)
206
self.assertEquals(our_lock.peek(), None)
209
class TestFormat2WorkingTree(TestCaseWithTransport):
210
"""Tests that are specific to format 2 trees."""
212
def create_format2_tree(self, url):
213
return self.make_branch_and_tree(
214
url, format=bzrlib.bzrdir.BzrDirFormat6())
216
def test_conflicts(self):
217
# test backwards compatability
218
tree = self.create_format2_tree('.')
219
self.assertRaises(errors.UnsupportedOperation, tree.set_conflicts,
221
file('lala.BASE', 'wb').write('labase')
222
expected = ContentsConflict('lala')
223
self.assertEqual(list(tree.conflicts()), [expected])
224
file('lala', 'wb').write('la')
225
tree.add('lala', 'lala-id')
226
expected = ContentsConflict('lala', file_id='lala-id')
227
self.assertEqual(list(tree.conflicts()), [expected])
228
file('lala.THIS', 'wb').write('lathis')
229
file('lala.OTHER', 'wb').write('laother')
230
# When "text conflict"s happen, stem, THIS and OTHER are text
231
expected = TextConflict('lala', file_id='lala-id')
232
self.assertEqual(list(tree.conflicts()), [expected])
233
os.unlink('lala.OTHER')
234
os.mkdir('lala.OTHER')
235
expected = ContentsConflict('lala', file_id='lala-id')
236
self.assertEqual(list(tree.conflicts()), [expected])
95
self.assertEqual(None, tree.branch._lock_count)