~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

  • Committer: Martin Pool
  • Date: 2006-02-22 04:29:54 UTC
  • mfrom: (1566 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1569.
  • Revision ID: mbp@sourcefrog.net-20060222042954-60333f08dd56a646
[merge] from bzr.dev before integration
Fix undefined ordering in sign_my_revisions breaking tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# (C) 2005 Canonical Ltd
 
1
# (C) 2005,2006 Canonical Ltd
2
2
# Authors:  Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
20
20
 
21
21
import bzrlib
22
22
from bzrlib.branch import Branch
 
23
import bzrlib.bzrdir as bzrdir
 
24
from bzrlib.bzrdir import BzrDir
 
25
import bzrlib.errors as errors
23
26
from bzrlib.errors import NotBranchError, NotVersionedError
 
27
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
24
28
from bzrlib.tests import TestCaseWithTransport
25
29
from bzrlib.trace import mutter
26
 
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
 
30
from bzrlib.transport import get_transport
 
31
import bzrlib.workingtree as workingtree
27
32
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
28
33
                                WorkingTree)
29
34
 
51
56
        self.assertEqual(TreeLink().kind_character(), '')
52
57
 
53
58
 
54
 
class TestWorkingTree(TestCaseWithTransport):
55
 
 
56
 
    def test_listfiles(self):
57
 
        tree = WorkingTree.create_standalone('.')
58
 
        os.mkdir('dir')
59
 
        print >> open('file', 'w'), "content"
60
 
        if has_symlinks():
61
 
            os.symlink('target', 'symlink')
62
 
        files = list(tree.list_files())
63
 
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
64
 
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
65
 
        if has_symlinks():
66
 
            self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
67
 
 
68
 
    def test_open_containing(self):
69
 
        branch = WorkingTree.create_standalone('.').branch
70
 
        wt, relpath = WorkingTree.open_containing()
71
 
        self.assertEqual('', relpath)
72
 
        self.assertEqual(wt.basedir + '/', branch.base)
73
 
        wt, relpath = WorkingTree.open_containing(u'.')
74
 
        self.assertEqual('', relpath)
75
 
        self.assertEqual(wt.basedir + '/', branch.base)
76
 
        wt, relpath = WorkingTree.open_containing('./foo')
77
 
        self.assertEqual('foo', relpath)
78
 
        self.assertEqual(wt.basedir + '/', branch.base)
79
 
        # paths that are urls are just plain wrong for working trees.
80
 
        self.assertRaises(NotBranchError,
81
 
                          WorkingTree.open_containing, 
82
 
                          'file:///' + getcwd())
83
 
 
84
 
    def test_construct_with_branch(self):
85
 
        branch = WorkingTree.create_standalone('.').branch
86
 
        tree = WorkingTree(branch.base, branch)
87
 
        self.assertEqual(branch, tree.branch)
88
 
        self.assertEqual(branch.base, tree.basedir + '/')
89
 
    
90
 
    def test_construct_without_branch(self):
91
 
        branch = WorkingTree.create_standalone('.').branch
92
 
        tree = WorkingTree(branch.base)
93
 
        self.assertEqual(branch.base, tree.branch.base)
94
 
        self.assertEqual(branch.base, tree.basedir + '/')
95
 
 
96
 
    def test_basic_relpath(self):
97
 
        # for comprehensive relpath tests, see whitebox.py.
98
 
        tree = WorkingTree.create_standalone('.')
99
 
        self.assertEqual('child',
100
 
                         tree.relpath(pathjoin(getcwd(), 'child')))
101
 
 
102
 
    def test_lock_locks_branch(self):
103
 
        tree = WorkingTree.create_standalone('.')
104
 
        tree.lock_read()
105
 
        self.assertEqual('r', tree.branch.peek_lock_mode())
106
 
        tree.unlock()
107
 
        self.assertEqual(None, tree.branch.peek_lock_mode())
108
 
        tree.lock_write()
109
 
        self.assertEqual('w', tree.branch.peek_lock_mode())
110
 
        tree.unlock()
111
 
        self.assertEqual(None, tree.branch.peek_lock_mode())
112
 
 
113
 
    def get_pullable_trees(self):
114
 
        self.build_tree(['from/', 'from/file', 'to/'])
115
 
        tree = WorkingTree.create_standalone('from')
116
 
        tree.add('file')
117
 
        tree.commit('foo', rev_id='A')
118
 
        tree_b = WorkingTree.create_standalone('to')
119
 
        return tree, tree_b
120
 
 
121
 
    def test_pull(self):
122
 
        tree_a, tree_b = self.get_pullable_trees()
123
 
        tree_b.pull(tree_a.branch)
124
 
        self.failUnless(tree_b.branch.repository.has_revision('A'))
125
 
        self.assertEqual(['A'], tree_b.branch.revision_history())
126
 
 
127
 
    def test_pull_overwrites(self):
128
 
        tree_a, tree_b = self.get_pullable_trees()
129
 
        tree_b.commit('foo', rev_id='B')
130
 
        self.assertEqual(['B'], tree_b.branch.revision_history())
131
 
        tree_b.pull(tree_a.branch, overwrite=True)
132
 
        self.failUnless(tree_b.branch.repository.has_revision('A'))
133
 
        self.failUnless(tree_b.branch.repository.has_revision('B'))
134
 
        self.assertEqual(['A'], tree_b.branch.revision_history())
135
 
 
136
 
    def test_revert(self):
137
 
        """Test selected-file revert"""
138
 
        tree = WorkingTree.create_standalone('.')
139
 
 
140
 
        self.build_tree(['hello.txt'])
141
 
        file('hello.txt', 'w').write('initial hello')
142
 
 
143
 
        self.assertRaises(NotVersionedError,
144
 
                          tree.revert, ['hello.txt'])
145
 
        tree.add(['hello.txt'])
146
 
        tree.commit('create initial hello.txt')
147
 
 
148
 
        self.check_file_contents('hello.txt', 'initial hello')
149
 
        file('hello.txt', 'w').write('new hello')
150
 
        self.check_file_contents('hello.txt', 'new hello')
151
 
 
152
 
        # revert file modified since last revision
153
 
        tree.revert(['hello.txt'])
154
 
        self.check_file_contents('hello.txt', 'initial hello')
155
 
        self.check_file_contents('hello.txt~', 'new hello')
156
 
 
157
 
        # reverting again does not clobber the backup
158
 
        tree.revert(['hello.txt'])
159
 
        self.check_file_contents('hello.txt', 'initial hello')
160
 
        self.check_file_contents('hello.txt~', 'new hello')
161
 
 
162
 
    def test_unknowns(self):
163
 
        tree = WorkingTree.create_standalone('.')
164
 
        self.build_tree(['hello.txt',
165
 
                         'hello.txt~'])
166
 
        self.assertEquals(list(tree.unknowns()),
167
 
                          ['hello.txt'])
168
 
 
169
 
    def test_hashcache(self):
170
 
        from bzrlib.tests.test_hashcache import pause
171
 
        tree = WorkingTree.create_standalone('.')
172
 
        self.build_tree(['hello.txt',
173
 
                         'hello.txt~'])
174
 
        tree.add('hello.txt')
175
 
        pause()
176
 
        sha = tree.get_file_sha1(tree.path2id('hello.txt'))
177
 
        self.assertEqual(1, tree._hashcache.miss_count)
178
 
        tree2 = WorkingTree('.', tree.branch)
179
 
        sha2 = tree2.get_file_sha1(tree2.path2id('hello.txt'))
180
 
        self.assertEqual(0, tree2._hashcache.miss_count)
181
 
        self.assertEqual(1, tree2._hashcache.hit_count)
182
 
 
183
 
    def test_checkout(self):
184
 
        # at this point as we dont have checkout versions, checkout simply
185
 
        # populates the required files for a working tree at the dir.
186
 
        self.build_tree(['branch/'])
187
 
        b = Branch.create('branch')
188
 
        t = WorkingTree.create(b, 'tree')
189
 
        # as we are moving the ownership to working tree, we will check here
190
 
        # that its split out correctly
191
 
        self.failIfExists('branch/.bzr/inventory')
192
 
        self.failIfExists('branch/.bzr/pending-merges')
193
 
        sio = StringIO()
194
 
        bzrlib.xml5.serializer_v5.write_inventory(bzrlib.inventory.Inventory(),
195
 
                                                  sio)
196
 
        self.assertFileEqual(sio.getvalue(), 'tree/.bzr/inventory')
197
 
        self.assertFileEqual('', 'tree/.bzr/pending-merges')
198
 
 
199
 
    def test_initialize(self):
200
 
        # initialize should create a working tree and branch in an existing dir
201
 
        t = WorkingTree.create_standalone('.')
202
 
        b = Branch.open('.')
203
 
        self.assertEqual(t.branch.base, b.base)
204
 
        t2 = WorkingTree('.')
205
 
        self.assertEqual(t.basedir, t2.basedir)
206
 
        self.assertEqual(b.base, t2.branch.base)
207
 
        # TODO maybe we should check the branch format? not sure if its
208
 
        # appropriate here.
209
 
 
210
 
    def test_rename_dirs(self):
211
 
        """Test renaming directories and the files within them."""
212
 
        wt = self.make_branch_and_tree('.')
213
 
        b = wt.branch
214
 
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
215
 
        wt.add(['dir', 'dir/sub', 'dir/sub/file'])
216
 
 
217
 
        wt.commit('create initial state')
218
 
 
219
 
        revid = b.revision_history()[0]
220
 
        self.log('first revision_id is {%s}' % revid)
 
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")
221
119
        
222
 
        inv = b.repository.get_revision_inventory(revid)
223
 
        self.log('contents of inventory: %r' % inv.entries())
224
 
 
225
 
        self.check_inventory_shape(inv,
226
 
                                   ['dir', 'dir/sub', 'dir/sub/file'])
227
 
 
228
 
        wt.rename_one('dir', 'newdir')
229
 
 
230
 
        self.check_inventory_shape(wt.read_working_inventory(),
231
 
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
232
 
 
233
 
        wt.rename_one('newdir/sub', 'newdir/newsub')
234
 
        self.check_inventory_shape(wt.read_working_inventory(),
235
 
                                   ['newdir', 'newdir/newsub',
236
 
                                    'newdir/newsub/file'])
237
 
 
238
 
    def test_add_in_unversioned(self):
239
 
        """Try to add a file in an unversioned directory.
240
 
 
241
 
        "bzr add" adds the parent as necessary, but simple working tree add
242
 
        doesn't do that.
243
 
        """
244
 
        from bzrlib.errors import NotVersionedError
245
 
        wt = self.make_branch_and_tree('.')
246
 
        self.build_tree(['foo/',
247
 
                         'foo/hello'])
248
 
        self.assertRaises(NotVersionedError,
249
 
                          wt.add,
250
 
                          'foo/hello')
251
 
 
252
 
    def test_remove_verbose(self):
253
 
        #FIXME the remove api should not print or otherwise depend on the
254
 
        # text UI - RBC 20060124
255
 
        wt = self.make_branch_and_tree('.')
256
 
        self.build_tree(['hello'])
257
 
        wt.add(['hello'])
258
 
        wt.commit(message='add hello')
259
 
        stdout = StringIO()
260
 
        stderr = StringIO()
261
 
        self.assertEqual(None, self.apply_redirected(None, stdout, stderr,
262
 
                                                     wt.remove,
263
 
                                                     ['hello'],
264
 
                                                     verbose=True))
265
 
        self.assertEqual('?       hello\n', stdout.getvalue())
266
 
        self.assertEqual('', stderr.getvalue())
 
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.