~bzr-pqm/bzr/bzr.dev

1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1
# Copyright (C) 2005, 2006 Canonical Ltd
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
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
from cStringIO import StringIO
19
import os
20
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
21
from bzrlib import branch, bzrdir, errors, ui, workingtree
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
22
from bzrlib.errors import (NotBranchError, NotVersionedError, 
23
                           UnsupportedOperation)
24
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
25
from bzrlib.tests import TestSkipped, TestCase
26
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
27
from bzrlib.trace import mutter
28
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
29
                                WorkingTree)
30
31
32
class CapturingUIFactory(ui.UIFactory):
33
    """A UI Factory for testing - capture the updates made through it."""
34
35
    def __init__(self):
36
        super(CapturingUIFactory, self).__init__()
37
        self._calls = []
38
        self.depth = 0
39
40
    def clear(self):
41
        """See progress.ProgressBar.clear()."""
42
43
    def clear_term(self):
44
        """See progress.ProgressBar.clear_term()."""
45
46
    def finished(self):
47
        """See progress.ProgressBar.finished()."""
48
        self.depth -= 1
49
50
    def note(self, fmt_string, *args, **kwargs):
51
        """See progress.ProgressBar.note()."""
52
53
    def progress_bar(self):
54
        return self
55
    
56
    def nested_progress_bar(self):
57
        self.depth += 1
58
        return self
59
60
    def update(self, message, count=None, total=None):
61
        """See progress.ProgressBar.update()."""
62
        if self.depth == 1:
63
            self._calls.append(("update", count, total))
64
65
66
class TestCapturingUI(TestCase):
67
68
    def test_nested_ignore_depth_beyond_one(self):
69
        # we only want to capture the first level out progress, not
70
        # want sub-components might do. So we have nested bars ignored.
71
        factory = CapturingUIFactory()
72
        pb1 = factory.nested_progress_bar()
73
        pb1.update('foo', 0, 1)
74
        pb2 = factory.nested_progress_bar()
75
        pb2.update('foo', 0, 1)
76
        pb2.finished()
77
        pb1.finished()
78
        self.assertEqual([("update", 0, 1)], factory._calls)
79
80
81
class TestCommit(TestCaseWithWorkingTree):
82
83
    def test_commit_sets_last_revision(self):
84
        tree = self.make_branch_and_tree('tree')
1773.1.1 by Robert Collins
Teach WorkingTree.commit to return the committed revision id.
85
        committed_id = tree.commit('foo', rev_id='foo', allow_pointless=True)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
86
        self.assertEqual(['foo'], tree.get_parent_ids())
1773.1.1 by Robert Collins
Teach WorkingTree.commit to return the committed revision id.
87
        # the commit should have returned the same id we asked for.
88
        self.assertEqual('foo', committed_id)
89
90
    def test_commit_returns_revision_id(self):
91
        tree = self.make_branch_and_tree('.')
92
        committed_id = tree.commit('message', allow_pointless=True)
93
        self.assertTrue(tree.branch.repository.has_revision(committed_id))
94
        self.assertNotEqual(None, committed_id)
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
95
96
    def test_commit_local_unbound(self):
97
        # using the library api to do a local commit on unbound branches is 
98
        # also an error
99
        tree = self.make_branch_and_tree('tree')
100
        self.assertRaises(errors.LocalRequiresBoundBranch,
101
                          tree.commit,
102
                          'foo',
103
                          local=True)
104
 
105
    def test_local_commit_ignores_master(self):
106
        # a --local commit does not require access to the master branch
107
        # at all, or even for it to exist.
108
        # we test this by setting up a bound branch and then corrupting
109
        # the master.
110
        master = self.make_branch('master')
111
        tree = self.make_branch_and_tree('tree')
112
        try:
113
            tree.branch.bind(master)
114
        except errors.UpgradeRequired:
115
            # older format.
116
            return
1955.3.14 by John Arbash Meinel
Correctly fix the workingtree put() test fixes
117
        master.bzrdir.transport.put_bytes('branch-format', 'garbage')
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
118
        del master
119
        # check its corrupted.
120
        self.assertRaises(errors.UnknownFormatError,
121
                          bzrdir.BzrDir.open,
122
                          'master')
123
        tree.commit('foo', rev_id='foo', local=True)
124
 
125
    def test_local_commit_does_not_push_to_master(self):
126
        # a --local commit does not require access to the master branch
127
        # at all, or even for it to exist.
128
        # we test that even when its available it does not push to it.
129
        master = self.make_branch('master')
130
        tree = self.make_branch_and_tree('tree')
131
        try:
132
            tree.branch.bind(master)
133
        except errors.UpgradeRequired:
134
            # older format.
135
            return
136
        tree.commit('foo', rev_id='foo', local=True)
137
        self.failIf(master.repository.has_revision('foo'))
138
        self.assertEqual(None, master.last_revision())
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
139
140
    def test_record_initial_ghost(self):
141
        """The working tree needs to record ghosts during commit."""
142
        wt = self.make_branch_and_tree('.')
1908.6.7 by Robert Collins
Remove all users of set_pending_merges and add_pending_merge except tests that they work correctly.
143
        wt.set_parent_ids(['non:existent@rev--ision--0--2'],
144
            allow_leftmost_as_ghost=True)
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
145
        rev_id = wt.commit('commit against a ghost first parent.')
146
        rev = wt.branch.repository.get_revision(rev_id)
147
        self.assertEqual(rev.parent_ids, ['non:existent@rev--ision--0--2'])
148
        # parent_sha1s is not populated now, WTF. rbc 20051003
149
        self.assertEqual(len(rev.parent_sha1s), 0)
150
151
    def test_record_two_ghosts(self):
152
        """The working tree should preserve all the parents during commit."""
153
        wt = self.make_branch_and_tree('.')
1908.6.7 by Robert Collins
Remove all users of set_pending_merges and add_pending_merge except tests that they work correctly.
154
        wt.set_parent_ids([
155
                'foo@azkhazan-123123-abcabc',
156
                'wibble@fofof--20050401--1928390812',
157
            ],
158
            allow_leftmost_as_ghost=True)
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
159
        rev_id = wt.commit("commit from ghost base with one merge")
160
        # the revision should have been committed with two parents
161
        rev = wt.branch.repository.get_revision(rev_id)
162
        self.assertEqual(['foo@azkhazan-123123-abcabc',
163
            'wibble@fofof--20050401--1928390812'],
164
            rev.parent_ids)
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
165
1988.3.1 by Robert Collins
Add test case to ensure that the working tree inventory and disk state is correctly update when commit is removing directory entries.
166
    def test_commit_deleted_subtree_and_files_updates_workingtree(self):
167
        """The working trees inventory may be adjusted by commit."""
168
        wt = self.make_branch_and_tree('.')
169
        wt.lock_write()
170
        self.build_tree(['a', 'b/', 'b/c', 'd'])
171
        wt.add(['a', 'b', 'b/c', 'd'], ['a-id', 'b-id', 'c-id', 'd-id'])
172
        this_dir = self.get_transport()
173
        this_dir.delete_tree('b')
174
        this_dir.delete('d')
175
        # now we have a tree with a through d in the inventory, but only
176
        # a present on disk. After commit b-id, c-id and d-id should be
177
        # missing from the inventory, within the same tree transaction.
178
        wt.commit('commit stuff')
179
        self.assertTrue(wt.has_id('a-id'))
180
        self.assertFalse(wt.has_or_had_id('b-id'))
181
        self.assertFalse(wt.has_or_had_id('c-id'))
182
        self.assertFalse(wt.has_or_had_id('d-id'))
183
        self.assertTrue(wt.has_filename('a'))
184
        self.assertFalse(wt.has_filename('b'))
185
        self.assertFalse(wt.has_filename('b/c'))
186
        self.assertFalse(wt.has_filename('d'))
187
        wt.unlock()
188
        # the changes should have persisted to disk - reopen the workingtree
189
        # to be sure.
190
        wt = wt.bzrdir.open_workingtree()
191
        wt.lock_read()
192
        self.assertTrue(wt.has_id('a-id'))
193
        self.assertFalse(wt.has_or_had_id('b-id'))
194
        self.assertFalse(wt.has_or_had_id('c-id'))
195
        self.assertFalse(wt.has_or_had_id('d-id'))
196
        self.assertTrue(wt.has_filename('a'))
197
        self.assertFalse(wt.has_filename('b'))
198
        self.assertFalse(wt.has_filename('b/c'))
199
        self.assertFalse(wt.has_filename('d'))
200
        wt.unlock()
201
        
202
1740.3.10 by Jelmer Vernooij
Fix some minor issues pointed out by j-a-m.
203
class TestCommitProgress(TestCaseWithWorkingTree):
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
204
    
205
    def restoreDefaults(self):
206
        ui.ui_factory = self.old_ui_factory
207
208
    def test_commit_progress_steps(self):
209
        # during commit we one progress update for every entry in the 
210
        # inventory, and then one for the inventory, and one for the
211
        # inventory, and one for the revision insertions.
212
        # first we need a test commit to do. Lets setup a branch with 
213
        # 3 files, and alter one in a selected-file commit. This exercises
214
        # a number of cases quickly. We should also test things like 
215
        # selective commits which excludes newly added files.
216
        tree = self.make_branch_and_tree('.')
217
        self.build_tree(['a', 'b', 'c'])
218
        tree.add(['a', 'b', 'c'])
219
        tree.commit('first post')
220
        f = file('b', 'wt')
221
        f.write('new content')
222
        f.close()
223
        # set a progress bar that captures the calls so we can see what is 
224
        # emitted
225
        self.old_ui_factory = ui.ui_factory
226
        self.addCleanup(self.restoreDefaults)
227
        factory = CapturingUIFactory()
228
        ui.ui_factory = factory
229
        # TODO RBC 20060421 it would be nice to merge the reporter output
230
        # into the factory for this test - just make the test ui factory
231
        # pun as a reporter. Then we can check the ordering is right.
232
        tree.commit('second post', specific_files=['b'])
1740.3.10 by Jelmer Vernooij
Fix some minor issues pointed out by j-a-m.
233
        # 9 steps: 1 for rev, 2 for inventory, 1 for finishing. 2 for root
234
        # and 6 for inventory files.
235
        # 2 steps don't trigger an update, as 'a' and 'c' are not 
236
        # committed.
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
237
        self.assertEqual(
1740.3.10 by Jelmer Vernooij
Fix some minor issues pointed out by j-a-m.
238
            [("update", 0, 9),
239
             ("update", 1, 9),
240
             ("update", 2, 9),
241
             ("update", 3, 9),
242
             ("update", 4, 9),
243
             ("update", 5, 9),
244
             ("update", 6, 9),
245
             ("update", 7, 9)],
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
246
            factory._calls
247
           )