~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_memorytree.py

Various changes to allow non-workingtree specific tests to run entirely
from MemoryTransports:
 * Create MemoryTree and pull up common code for it from WorkingTree to
   a new common base class MutableTree.
 * Add MutableTree.mkdir().
 * Add MutableTree.put_file_bytes_nonatomic().
 * New test helper make_branch_and_memory_tree().
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
55
    def test_lock_write(self):
 
56
        """Check we can lock_write and unlock MemoryTrees."""
 
57
        branch = self.make_branch('branch')
 
58
        tree = MemoryTree.create_on_branch(branch)
 
59
        tree.lock_write()
 
60
        tree.unlock()
 
61
 
 
62
    def test_lock_write_after_read_fails(self):
 
63
        """Check that we error when trying to upgrade a read lock to write."""
 
64
        branch = self.make_branch('branch')
 
65
        tree = MemoryTree.create_on_branch(branch)
 
66
        tree.lock_read()
 
67
        self.assertRaises(errors.ReadOnlyError, tree.lock_write)
 
68
        tree.unlock()
 
69
 
 
70
    def test_add_with_kind(self):
 
71
        branch = self.make_branch('branch')
 
72
        tree = MemoryTree.create_on_branch(branch)
 
73
        tree.lock_write()
 
74
        tree.add(['afile', 'adir'], None, ['file', 'directory'])
 
75
        self.assertEqual('afile', tree.id2path(tree.path2id('afile')))
 
76
        self.assertEqual('adir', tree.id2path(tree.path2id('adir')))
 
77
        self.assertFalse(tree.has_filename('afile'))
 
78
        self.assertFalse(tree.has_filename('adir'))
 
79
        tree.unlock()
 
80
 
 
81
    def test_put_new_file(self):
 
82
        branch = self.make_branch('branch')
 
83
        tree = MemoryTree.create_on_branch(branch)
 
84
        tree.lock_write()
 
85
        tree.add(['foo'], ids=['foo-id'], kinds=['file'])
 
86
        tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
 
87
        self.assertEqual('barshoom', tree.get_file('foo-id').read())
 
88
        tree.unlock()
 
89
 
 
90
    def test_put_existing_file(self):
 
91
        branch = self.make_branch('branch')
 
92
        tree = MemoryTree.create_on_branch(branch)
 
93
        tree.lock_write()
 
94
        tree.add(['foo'], ids=['foo-id'], kinds=['file'])
 
95
        tree.put_file_bytes_non_atomic('foo-id', 'first-content')
 
96
        tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
 
97
        self.assertEqual('barshoom', tree.get_file('foo-id').read())
 
98
        tree.unlock()
 
99
 
 
100
    def test_commit_trivial(self):
 
101
        """Smoke test for commit on a MemoryTree.
 
102
 
 
103
        Becamse of commits design and layering, if this works, all commit
 
104
        logic should work quite reliably.
 
105
        """
 
106
        branch = self.make_branch('branch')
 
107
        tree = MemoryTree.create_on_branch(branch)
 
108
        tree.lock_write()
 
109
        tree.add(['foo'], ids=['foo-id'], kinds=['file'])
 
110
        tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
 
111
        revision_id = tree.commit('message baby')
 
112
        # the parents list for the tree should have changed.
 
113
        self.assertEqual([revision_id], tree.get_parent_ids())
 
114
        tree.unlock()
 
115
        # and we should have a revision that is accessible outside the tree lock
 
116
        revtree = tree.branch.repository.revision_tree(revision_id)
 
117
        self.assertEqual('barshoom', revtree.get_file('foo-id').read())