~bzr-pqm/bzr/bzr.dev

2466.7.3 by Robert Collins
Create bzrlib.branchbuilder.
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for the BranchBuilder class."""
18
2466.7.4 by Robert Collins
Add BranchBuilder.get_branch().
19
from bzrlib import (
20
    branch as _mod_branch,
21
    revision as _mod_revision,
22
    tests,
23
    )
2466.7.3 by Robert Collins
Create bzrlib.branchbuilder.
24
from bzrlib.branchbuilder import BranchBuilder
25
26
27
class TestBranchBuilder(tests.TestCaseWithMemoryTransport):
28
    
29
    def test_create(self):
30
        """Test the constructor api."""
31
        builder = BranchBuilder(self.get_transport().clone('foo'))
32
        # we dont care if the branch has been built or not at this point.
2466.7.4 by Robert Collins
Add BranchBuilder.get_branch().
33
34
    def test_get_branch(self):
35
        """get_branch returns the created branch."""
36
        builder = BranchBuilder(self.get_transport().clone('foo'))
37
        branch = builder.get_branch()
38
        self.assertIsInstance(branch, _mod_branch.Branch)
39
        self.assertEqual(self.get_transport().clone('foo').base,
40
            branch.base)
41
        self.assertEqual(
42
            (0, _mod_revision.NULL_REVISION),
43
            branch.last_revision_info())
2466.7.6 by Robert Collins
Add BranchBuilder.build_commit.
44
2466.7.10 by Robert Collins
Add a format parameter to BranchBuilder.
45
    def test_format(self):
46
        """Making a BranchBuilder with a format option sets the branch type."""
47
        builder = BranchBuilder(self.get_transport(), format='dirstate-tags')
48
        branch = builder.get_branch()
49
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
50
2466.7.6 by Robert Collins
Add BranchBuilder.build_commit.
51
    def test_build_one_commit(self):
52
        """doing build_commit causes a commit to happen."""
53
        builder = BranchBuilder(self.get_transport().clone('foo'))
2466.7.9 by Robert Collins
Return the commited revision id from BranchBuilder.build_commit to save later instrospection.
54
        rev_id = builder.build_commit()
2466.7.6 by Robert Collins
Add BranchBuilder.build_commit.
55
        branch = builder.get_branch()
2466.7.9 by Robert Collins
Return the commited revision id from BranchBuilder.build_commit to save later instrospection.
56
        self.assertEqual((1, rev_id), branch.last_revision_info())
2466.7.6 by Robert Collins
Add BranchBuilder.build_commit.
57
        self.assertEqual(
58
            'commit 1',
59
            branch.repository.get_revision(branch.last_revision()).message)
60
61
    def test_build_two_commits(self):
62
        """The second commit has the right parents and message."""
63
        builder = BranchBuilder(self.get_transport().clone('foo'))
2466.7.9 by Robert Collins
Return the commited revision id from BranchBuilder.build_commit to save later instrospection.
64
        rev_id1 = builder.build_commit()
65
        rev_id2 = builder.build_commit()
2466.7.6 by Robert Collins
Add BranchBuilder.build_commit.
66
        branch = builder.get_branch()
2466.7.9 by Robert Collins
Return the commited revision id from BranchBuilder.build_commit to save later instrospection.
67
        self.assertEqual((2, rev_id2), branch.last_revision_info())
2466.7.6 by Robert Collins
Add BranchBuilder.build_commit.
68
        self.assertEqual(
69
            'commit 2',
70
            branch.repository.get_revision(branch.last_revision()).message)
71
        self.assertEqual(
2466.7.9 by Robert Collins
Return the commited revision id from BranchBuilder.build_commit to save later instrospection.
72
            [rev_id1],
2466.7.6 by Robert Collins
Add BranchBuilder.build_commit.
73
            branch.repository.get_revision(branch.last_revision()).parent_ids)
3567.4.1 by John Arbash Meinel
Initial work to have BranchBuilder allow us to do tree-shape work.
74
3567.4.3 by John Arbash Meinel
Add an action for modifying an existing file
75
76
class TestBranchBuilderBuildSnapshot(tests.TestCaseWithMemoryTransport):
77
78
    def assertTreeShape(self, expected_shape, tree):
79
        """Check that the tree shape matches expectations."""
80
        tree.lock_read()
81
        try:
82
            entries = [(path, ie.file_id, ie.kind)
83
                       for path, ie in tree.iter_entries_by_dir()]
84
        finally:
85
            tree.unlock()
86
        self.assertEqual(expected_shape, entries)
87
88
    def build_a_rev(self):
3567.4.1 by John Arbash Meinel
Initial work to have BranchBuilder allow us to do tree-shape work.
89
        builder = BranchBuilder(self.get_transport().clone('foo'))
90
        rev_id1 = builder.build_snapshot(None, 'A-id',
91
            [('add', ('', 'a-root-id', 'directory', None)),
92
             ('add', ('a', 'a-id', 'file', 'contents'))])
93
        self.assertEqual('A-id', rev_id1)
3567.4.3 by John Arbash Meinel
Add an action for modifying an existing file
94
        return builder
95
96
    def test_add_one_file(self):
97
        builder = self.build_a_rev()
3567.4.1 by John Arbash Meinel
Initial work to have BranchBuilder allow us to do tree-shape work.
98
        branch = builder.get_branch()
3567.4.3 by John Arbash Meinel
Add an action for modifying an existing file
99
        self.assertEqual((1, 'A-id'), branch.last_revision_info())
100
        rev_tree = branch.repository.revision_tree('A-id')
3567.4.1 by John Arbash Meinel
Initial work to have BranchBuilder allow us to do tree-shape work.
101
        rev_tree.lock_read()
102
        self.addCleanup(rev_tree.unlock)
3567.4.2 by John Arbash Meinel
test that we can add more files into an existing build
103
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
104
                              (u'a', 'a-id', 'file')], rev_tree)
3567.4.1 by John Arbash Meinel
Initial work to have BranchBuilder allow us to do tree-shape work.
105
        self.assertEqual('contents', rev_tree.get_file_text('a-id'))
3567.4.2 by John Arbash Meinel
test that we can add more files into an existing build
106
3567.4.3 by John Arbash Meinel
Add an action for modifying an existing file
107
    def test_add_second_file(self):
108
        builder = self.build_a_rev()
3567.4.2 by John Arbash Meinel
test that we can add more files into an existing build
109
        rev_id2 = builder.build_snapshot(None, 'B-id',
110
            [('add', ('b', 'b-id', 'file', 'content_b'))])
111
        self.assertEqual('B-id', rev_id2)
112
        branch = builder.get_branch()
113
        self.assertEqual((2, rev_id2), branch.last_revision_info())
114
        rev_tree = branch.repository.revision_tree(rev_id2)
115
        rev_tree.lock_read()
116
        self.addCleanup(rev_tree.unlock)
117
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
118
                              (u'a', 'a-id', 'file'),
119
                              (u'b', 'b-id', 'file')], rev_tree)
120
        self.assertEqual('content_b', rev_tree.get_file_text('b-id'))
3567.4.3 by John Arbash Meinel
Add an action for modifying an existing file
121
122
    def test_modify_file(self):
123
        builder = self.build_a_rev()
124
        rev_id2 = builder.build_snapshot(None, 'B-id',
125
            [('modify', ('a-id', 'new\ncontent\n'))])
126
        self.assertEqual('B-id', rev_id2)
127
        branch = builder.get_branch()
128
        rev_tree = branch.repository.revision_tree(rev_id2)
129
        rev_tree.lock_read()
130
        self.addCleanup(rev_tree.unlock)
131
        self.assertEqual('new\ncontent\n', rev_tree.get_file_text('a-id'))