1
# Copyright (C) 2006 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Tests for the TreeBuilder helper class."""
20
from breezy import tests
21
from breezy.memorytree import MemoryTree
22
from breezy.tests import TestCaseWithTransport
23
from breezy.treebuilder import (
30
class FakeTree(object):
31
"""A pretend tree to test the calls made by TreeBuilder."""
36
def lock_tree_write(self):
37
self._calls.append("lock_tree_write")
40
self._calls.append("unlock")
43
class TestFakeTree(TestCaseWithTransport):
45
def testFakeTree(self):
46
"""Check that FakeTree works as required for the TreeBuilder tests."""
48
self.assertEqual([], tree._calls)
49
tree.lock_tree_write()
50
self.assertEqual(["lock_tree_write"], tree._calls)
52
self.assertEqual(["lock_tree_write", "unlock"], tree._calls)
55
class TestTreeBuilderMemoryTree(tests.TestCaseWithMemoryTransport):
57
def test_create(self):
60
def test_start_tree_locks_write(self):
61
builder = TreeBuilder()
63
builder.start_tree(tree)
64
self.assertEqual(["lock_tree_write"], tree._calls)
66
def test_start_tree_when_started_fails(self):
67
builder = TreeBuilder()
69
builder.start_tree(tree)
70
self.assertRaises(AlreadyBuilding, builder.start_tree, tree)
72
def test_finish_tree_not_started_errors(self):
73
builder = TreeBuilder()
74
self.assertRaises(NotBuilding, builder.finish_tree)
76
def test_finish_tree_unlocks(self):
77
builder = TreeBuilder()
79
builder.start_tree(tree)
81
self.assertEqual(["lock_tree_write", "unlock"], tree._calls)
83
def test_build_tree_not_started_errors(self):
84
builder = TreeBuilder()
85
self.assertRaises(NotBuilding, builder.build, "foo")
87
def test_build_tree(self):
88
"""Test building works using a MemoryTree."""
89
branch = self.make_branch('branch')
90
tree = MemoryTree.create_on_branch(branch)
91
builder = TreeBuilder()
92
builder.start_tree(tree)
93
builder.build(['foo', "bar/", "bar/file"])
96
tree.get_file('foo').read())
98
b'contents of bar/file\n',
99
tree.get_file('bar/file').read())
100
builder.finish_tree()