/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_treebuilder.py

  • Committer: Jelmer Vernooij
  • Date: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
"""Tests for the TreeBuilder helper class."""
 
19
 
 
20
from breezy import tests
 
21
from breezy.memorytree import MemoryTree
 
22
from breezy.tests import TestCaseWithTransport
 
23
from breezy.treebuilder import (
 
24
    AlreadyBuilding,
 
25
    NotBuilding,
 
26
    TreeBuilder,
 
27
    )
 
28
 
 
29
 
 
30
class FakeTree(object):
 
31
    """A pretend tree to test the calls made by TreeBuilder."""
 
32
 
 
33
    def __init__(self):
 
34
        self._calls = []
 
35
 
 
36
    def lock_tree_write(self):
 
37
        self._calls.append("lock_tree_write")
 
38
 
 
39
    def unlock(self):
 
40
        self._calls.append("unlock")
 
41
 
 
42
 
 
43
class TestFakeTree(TestCaseWithTransport):
 
44
 
 
45
    def testFakeTree(self):
 
46
        """Check that FakeTree works as required for the TreeBuilder tests."""
 
47
        tree = FakeTree()
 
48
        self.assertEqual([], tree._calls)
 
49
        tree.lock_tree_write()
 
50
        self.assertEqual(["lock_tree_write"], tree._calls)
 
51
        tree.unlock()
 
52
        self.assertEqual(["lock_tree_write", "unlock"], tree._calls)
 
53
 
 
54
 
 
55
class TestTreeBuilderMemoryTree(tests.TestCaseWithMemoryTransport):
 
56
 
 
57
    def test_create(self):
 
58
        TreeBuilder()
 
59
 
 
60
    def test_start_tree_locks_write(self):
 
61
        builder = TreeBuilder()
 
62
        tree = FakeTree()
 
63
        builder.start_tree(tree)
 
64
        self.assertEqual(["lock_tree_write"], tree._calls)
 
65
 
 
66
    def test_start_tree_when_started_fails(self):
 
67
        builder = TreeBuilder()
 
68
        tree = FakeTree()
 
69
        builder.start_tree(tree)
 
70
        self.assertRaises(AlreadyBuilding, builder.start_tree, tree)
 
71
 
 
72
    def test_finish_tree_not_started_errors(self):
 
73
        builder = TreeBuilder()
 
74
        self.assertRaises(NotBuilding, builder.finish_tree)
 
75
 
 
76
    def test_finish_tree_unlocks(self):
 
77
        builder = TreeBuilder()
 
78
        tree = FakeTree()
 
79
        builder.start_tree(tree)
 
80
        builder.finish_tree()
 
81
        self.assertEqual(["lock_tree_write", "unlock"], tree._calls)
 
82
 
 
83
    def test_build_tree_not_started_errors(self):
 
84
        builder = TreeBuilder()
 
85
        self.assertRaises(NotBuilding, builder.build, "foo")
 
86
 
 
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"])
 
94
        self.assertEqual(
 
95
            b'contents of foo\n',
 
96
            tree.get_file('foo').read())
 
97
        self.assertEqual(
 
98
            b'contents of bar/file\n',
 
99
            tree.get_file('bar/file').read())
 
100
        builder.finish_tree()