/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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')
85
        tree.commit('foo', rev_id='foo', allow_pointless=True)
86
        self.assertEqual('foo', tree.last_revision())
87
88
    def test_commit_local_unbound(self):
89
        # using the library api to do a local commit on unbound branches is 
90
        # also an error
91
        tree = self.make_branch_and_tree('tree')
92
        self.assertRaises(errors.LocalRequiresBoundBranch,
93
                          tree.commit,
94
                          'foo',
95
                          local=True)
96
 
97
    def test_local_commit_ignores_master(self):
98
        # a --local commit does not require access to the master branch
99
        # at all, or even for it to exist.
100
        # we test this by setting up a bound branch and then corrupting
101
        # the master.
102
        master = self.make_branch('master')
103
        tree = self.make_branch_and_tree('tree')
104
        try:
105
            tree.branch.bind(master)
106
        except errors.UpgradeRequired:
107
            # older format.
108
            return
109
        master.bzrdir.transport.put('branch-format', StringIO('garbage'))
110
        del master
111
        # check its corrupted.
112
        self.assertRaises(errors.UnknownFormatError,
113
                          bzrdir.BzrDir.open,
114
                          'master')
115
        tree.commit('foo', rev_id='foo', local=True)
116
 
117
    def test_local_commit_does_not_push_to_master(self):
118
        # a --local commit does not require access to the master branch
119
        # at all, or even for it to exist.
120
        # we test that even when its available it does not push to it.
121
        master = self.make_branch('master')
122
        tree = self.make_branch_and_tree('tree')
123
        try:
124
            tree.branch.bind(master)
125
        except errors.UpgradeRequired:
126
            # older format.
127
            return
128
        tree.commit('foo', rev_id='foo', local=True)
129
        self.failIf(master.repository.has_revision('foo'))
130
        self.assertEqual(None, master.last_revision())
131
        
132
1740.3.10 by Jelmer Vernooij
Fix some minor issues pointed out by j-a-m.
133
class TestCommitProgress(TestCaseWithWorkingTree):
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
134
    
135
    def restoreDefaults(self):
136
        ui.ui_factory = self.old_ui_factory
137
138
    def test_commit_progress_steps(self):
139
        # during commit we one progress update for every entry in the 
140
        # inventory, and then one for the inventory, and one for the
141
        # inventory, and one for the revision insertions.
142
        # first we need a test commit to do. Lets setup a branch with 
143
        # 3 files, and alter one in a selected-file commit. This exercises
144
        # a number of cases quickly. We should also test things like 
145
        # selective commits which excludes newly added files.
146
        tree = self.make_branch_and_tree('.')
147
        self.build_tree(['a', 'b', 'c'])
148
        tree.add(['a', 'b', 'c'])
149
        tree.commit('first post')
150
        f = file('b', 'wt')
151
        f.write('new content')
152
        f.close()
153
        # set a progress bar that captures the calls so we can see what is 
154
        # emitted
155
        self.old_ui_factory = ui.ui_factory
156
        self.addCleanup(self.restoreDefaults)
157
        factory = CapturingUIFactory()
158
        ui.ui_factory = factory
159
        # TODO RBC 20060421 it would be nice to merge the reporter output
160
        # into the factory for this test - just make the test ui factory
161
        # pun as a reporter. Then we can check the ordering is right.
162
        tree.commit('second post', specific_files=['b'])
1740.3.10 by Jelmer Vernooij
Fix some minor issues pointed out by j-a-m.
163
        # 9 steps: 1 for rev, 2 for inventory, 1 for finishing. 2 for root
164
        # and 6 for inventory files.
165
        # 2 steps don't trigger an update, as 'a' and 'c' are not 
166
        # committed.
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
167
        self.assertEqual(
1740.3.10 by Jelmer Vernooij
Fix some minor issues pointed out by j-a-m.
168
            [("update", 0, 9),
169
             ("update", 1, 9),
170
             ("update", 2, 9),
171
             ("update", 3, 9),
172
             ("update", 4, 9),
173
             ("update", 5, 9),
174
             ("update", 6, 9),
175
             ("update", 7, 9)],
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
176
            factory._calls
177
           )