~bzr-pqm/bzr/bzr.dev

1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
1
# Copyright (C) 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
"""Tests for the MemoryTree class."""
19
20
from bzrlib import errors
21
from bzrlib.memorytree import MemoryTree
22
from bzrlib.tests import TestCaseWithTransport
23
from bzrlib.treebuilder import TreeBuilder
24
25
26
class TestMemoryTree(TestCaseWithTransport):
27
    
28
    def test_create_on_branch(self):
29
        """Creating a mutable tree on a trivial branch works."""
30
        branch = self.make_branch('branch')
31
        tree = MemoryTree.create_on_branch(branch)
32
        self.assertEqual(branch.bzrdir, tree.bzrdir)
33
        self.assertEqual(branch, tree.branch)
34
        self.assertEqual([], tree.get_parent_ids())
35
    
36
    def test_create_on_branch_with_content(self):
37
        """Creating a mutable tree on a non-trivial branch works."""
38
        branch = self.make_branch('branch')
39
        tree = MemoryTree.create_on_branch(branch)
40
        # build some content
41
        tree.lock_write()
42
        builder = TreeBuilder()
43
        builder.start_tree(tree)
44
        builder.build(['foo'])
45
        builder.finish_tree()
46
        rev_id = tree.commit('first post')
47
        tree.unlock()
48
        tree = MemoryTree.create_on_branch(branch)
49
        tree.lock_read()
50
        self.assertEqual([rev_id], tree.get_parent_ids())
51
        self.assertEqual('contents of foo\n',
52
            tree.get_file(tree.path2id('foo')).read())
53
        tree.unlock()
54
2946.3.5 by John Arbash Meinel
MemoryTree is not tested under the tree_implementations tests.
55
    def test_get_root_id(self):
56
        branch = self.make_branch('branch')
57
        tree = MemoryTree.create_on_branch(branch)
58
        tree.lock_write()
59
        try:
60
            tree.add([''])
61
            self.assertIsNot(None, tree.get_root_id())
62
        finally:
63
            tree.unlock()
64
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
65
    def test_lock_tree_write(self):
66
        """Check we can lock_tree_write and unlock MemoryTrees."""
67
        branch = self.make_branch('branch')
68
        tree = MemoryTree.create_on_branch(branch)
69
        tree.lock_tree_write()
70
        tree.unlock()
71
72
    def test_lock_tree_write_after_read_fails(self):
73
        """Check that we error when trying to upgrade a read lock to write."""
74
        branch = self.make_branch('branch')
75
        tree = MemoryTree.create_on_branch(branch)
76
        tree.lock_read()
77
        self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
78
        tree.unlock()
79
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
80
    def test_lock_write(self):
81
        """Check we can lock_write and unlock MemoryTrees."""
82
        branch = self.make_branch('branch')
83
        tree = MemoryTree.create_on_branch(branch)
84
        tree.lock_write()
85
        tree.unlock()
86
87
    def test_lock_write_after_read_fails(self):
88
        """Check that we error when trying to upgrade a read lock to write."""
89
        branch = self.make_branch('branch')
90
        tree = MemoryTree.create_on_branch(branch)
91
        tree.lock_read()
92
        self.assertRaises(errors.ReadOnlyError, tree.lock_write)
93
        tree.unlock()
94
95
    def test_add_with_kind(self):
96
        branch = self.make_branch('branch')
97
        tree = MemoryTree.create_on_branch(branch)
98
        tree.lock_write()
1731.1.50 by Aaron Bentley
Merge bzr.dev
99
        tree.add(['', 'afile', 'adir'], None, 
100
                 ['directory', 'file', 'directory'])
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
101
        self.assertEqual('afile', tree.id2path(tree.path2id('afile')))
102
        self.assertEqual('adir', tree.id2path(tree.path2id('adir')))
103
        self.assertFalse(tree.has_filename('afile'))
104
        self.assertFalse(tree.has_filename('adir'))
105
        tree.unlock()
106
107
    def test_put_new_file(self):
108
        branch = self.make_branch('branch')
109
        tree = MemoryTree.create_on_branch(branch)
110
        tree.lock_write()
1731.1.50 by Aaron Bentley
Merge bzr.dev
111
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'], 
112
                  kinds=['directory', 'file'])
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
113
        tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
114
        self.assertEqual('barshoom', tree.get_file('foo-id').read())
115
        tree.unlock()
116
117
    def test_put_existing_file(self):
118
        branch = self.make_branch('branch')
119
        tree = MemoryTree.create_on_branch(branch)
120
        tree.lock_write()
1731.1.50 by Aaron Bentley
Merge bzr.dev
121
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'], 
122
                 kinds=['directory', 'file'])
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
123
        tree.put_file_bytes_non_atomic('foo-id', 'first-content')
124
        tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
125
        self.assertEqual('barshoom', tree.get_file('foo-id').read())
126
        tree.unlock()
127
128
    def test_commit_trivial(self):
129
        """Smoke test for commit on a MemoryTree.
130
131
        Becamse of commits design and layering, if this works, all commit
132
        logic should work quite reliably.
133
        """
134
        branch = self.make_branch('branch')
135
        tree = MemoryTree.create_on_branch(branch)
136
        tree.lock_write()
1731.1.50 by Aaron Bentley
Merge bzr.dev
137
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'], 
138
                 kinds=['directory', 'file'])
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
139
        tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
140
        revision_id = tree.commit('message baby')
141
        # the parents list for the tree should have changed.
142
        self.assertEqual([revision_id], tree.get_parent_ids())
143
        tree.unlock()
144
        # and we should have a revision that is accessible outside the tree lock
145
        revtree = tree.branch.repository.revision_tree(revision_id)
3010.1.7 by Robert Collins
Locking in test_memorytree.
146
        revtree.lock_read()
147
        self.addCleanup(revtree.unlock)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
148
        self.assertEqual('barshoom', revtree.get_file('foo-id').read())
1986.1.3 by Robert Collins
Merge bzr.dev.
149
150
    def test_unversion(self):
151
        """Some test for unversion of a memory tree."""
152
        branch = self.make_branch('branch')
153
        tree = MemoryTree.create_on_branch(branch)
154
        tree.lock_write()
1731.1.50 by Aaron Bentley
Merge bzr.dev
155
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'], 
156
                 kinds=['directory', 'file'])
1986.1.3 by Robert Collins
Merge bzr.dev.
157
        tree.unversion(['foo-id'])
158
        self.assertFalse(tree.has_id('foo-id'))
159
        tree.unlock()
1986.4.1 by Robert Collins
Merge MemoryTree branch.
160
161
    def test_last_revision(self):
162
        """There should be a last revision method we can call."""
163
        tree = self.make_branch_and_memory_tree('branch')
1731.1.64 by Aaron Bentley
Fix up more memorytree tests
164
        tree.lock_write()
165
        tree.add('')
1986.4.1 by Robert Collins
Merge MemoryTree branch.
166
        rev_id = tree.commit('first post')
1731.1.64 by Aaron Bentley
Fix up more memorytree tests
167
        tree.unlock()
1986.4.1 by Robert Collins
Merge MemoryTree branch.
168
        self.assertEqual(rev_id, tree.last_revision())