~bzr-pqm/bzr/bzr.dev

2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
1
# Copyright (C) 2005 Canonical Ltd
1551.6.20 by Aaron Bentley
Add copyright and whitespace
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1551.6.20 by Aaron Bentley
Add copyright and whitespace
16
17
1551.6.19 by Aaron Bentley
Fix pending merge status on empty trees
18
from StringIO import StringIO
19
5393.3.2 by Parth Malwankar
added StatusPostHookParams and test cases
20
from bzrlib import (
21
    config,
22
    status as _mod_status,
23
    )
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
24
from bzrlib.revisionspec import RevisionSpec
1852.2.1 by Robert Collins
General cleanups - status, add a status test with two revision trees, and commands.py importing of bzrlib.trace.
25
from bzrlib.status import show_pending_merges, show_tree_status
26
from bzrlib.tests import TestCaseWithTransport
27
28
29
class TestStatus(TestCaseWithTransport):
1551.6.20 by Aaron Bentley
Add copyright and whitespace
30
1551.6.19 by Aaron Bentley
Fix pending merge status on empty trees
31
    def test_pending_none(self):
1551.6.20 by Aaron Bentley
Add copyright and whitespace
32
        # Test whether show_pending_merges works in a tree with no commits
1852.2.1 by Robert Collins
General cleanups - status, add a status test with two revision trees, and commands.py importing of bzrlib.trace.
33
        tree = self.make_branch_and_tree('a')
1908.6.7 by Robert Collins
Remove all users of set_pending_merges and add_pending_merge except tests that they work correctly.
34
        tree.commit('empty commit')
1852.2.1 by Robert Collins
General cleanups - status, add a status test with two revision trees, and commands.py importing of bzrlib.trace.
35
        tree2 = self.make_branch_and_tree('b')
1979.2.1 by Robert Collins
(robertc) adds a convenience method "merge_from_branch" to WorkingTree.
36
        # set a left most parent that is not a present commit
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
37
        tree2.add_parent_tree_id('some-ghost', allow_leftmost_as_ghost=True)
1908.6.7 by Robert Collins
Remove all users of set_pending_merges and add_pending_merge except tests that they work correctly.
38
        # do a merge
1979.2.1 by Robert Collins
(robertc) adds a convenience method "merge_from_branch" to WorkingTree.
39
        tree2.merge_from_branch(tree.branch)
1551.6.19 by Aaron Bentley
Fix pending merge status on empty trees
40
        output = StringIO()
3074.3.1 by John Arbash Meinel
Fix bug #172657, use Graph.find_difference() rather than ancestry set operations.
41
        tree2.lock_read()
42
        try:
43
            show_pending_merges(tree2, output)
44
        finally:
45
            tree2.unlock()
1551.6.19 by Aaron Bentley
Fix pending merge status on empty trees
46
        self.assertContainsRe(output.getvalue(), 'empty commit')
1852.2.1 by Robert Collins
General cleanups - status, add a status test with two revision trees, and commands.py importing of bzrlib.trace.
47
3936.2.1 by Ian Clatworthy
verbose flag for status - code & tests
48
    def make_multiple_pending_tree(self):
3074.3.2 by John Arbash Meinel
update the ignore list in one-go.
49
        config.GlobalConfig().set_user_option('email', 'Joe Foo <joe@foo.com>')
50
        tree = self.make_branch_and_tree('a')
51
        tree.commit('commit 1', timestamp=1196796819, timezone=0)
52
        tree2 = tree.bzrdir.clone('b').open_workingtree()
53
        tree.commit('commit 2', timestamp=1196796819, timezone=0)
54
        tree2.commit('commit 2b', timestamp=1196796819, timezone=0)
3377.3.37 by John Arbash Meinel
Ian's first review comments.
55
        tree3 = tree2.bzrdir.clone('c').open_workingtree()
3074.3.2 by John Arbash Meinel
update the ignore list in one-go.
56
        tree2.commit('commit 3b', timestamp=1196796819, timezone=0)
57
        tree3.commit('commit 3c', timestamp=1196796819, timezone=0)
58
        tree.merge_from_branch(tree2.branch)
4721.3.2 by Vincent Ladeuil
Simplify mutable_tree.has_changes() and update call sites.
59
        tree.merge_from_branch(tree3.branch, force=True)
3936.2.1 by Ian Clatworthy
verbose flag for status - code & tests
60
        return tree
61
62
    def test_multiple_pending(self):
63
        tree = self.make_multiple_pending_tree()
3074.3.2 by John Arbash Meinel
update the ignore list in one-go.
64
        output = StringIO()
65
        tree.lock_read()
3377.3.42 by John Arbash Meinel
Get the tests to actually work, and get the code to go along with.
66
        self.addCleanup(tree.unlock)
67
        show_pending_merges(tree, output)
3936.2.1 by Ian Clatworthy
verbose flag for status - code & tests
68
        # 2b doesn't appear because it's an ancestor of 3b
3936.2.3 by Ian Clatworthy
feedback from jameinel
69
        self.assertEqualDiff(
70
            'pending merge tips: (use -v to see all merge revisions)\n'
71
            '  Joe Foo 2007-12-04 commit 3b\n'
72
            '  Joe Foo 2007-12-04 commit 3c\n',
73
            output.getvalue())
3936.2.1 by Ian Clatworthy
verbose flag for status - code & tests
74
75
    def test_multiple_pending_verbose(self):
76
        tree = self.make_multiple_pending_tree()
77
        output = StringIO()
78
        tree.lock_read()
79
        self.addCleanup(tree.unlock)
80
        show_pending_merges(tree, output, verbose=True)
3377.3.37 by John Arbash Meinel
Ian's first review comments.
81
        # Even though 2b is in the ancestry of 3c, it should only be displayed
82
        # under the first merge parent.
3377.3.42 by John Arbash Meinel
Get the tests to actually work, and get the code to go along with.
83
        self.assertEqualDiff('pending merges:\n'
84
                             '  Joe Foo 2007-12-04 commit 3b\n'
85
                             '    Joe Foo 2007-12-04 commit 2b\n'
86
                             '  Joe Foo 2007-12-04 commit 3c\n',
87
                             output.getvalue())
3074.3.2 by John Arbash Meinel
update the ignore list in one-go.
88
3377.3.40 by John Arbash Meinel
a little bit more cleanup, change how ghosts are handled
89
    def test_with_pending_ghost(self):
90
        """Test when a pending merge is itself a ghost"""
91
        tree = self.make_branch_and_tree('a')
92
        tree.commit('first')
3377.3.42 by John Arbash Meinel
Get the tests to actually work, and get the code to go along with.
93
        tree.add_parent_tree_id('a-ghost-revision')
3377.3.40 by John Arbash Meinel
a little bit more cleanup, change how ghosts are handled
94
        tree.lock_read()
95
        self.addCleanup(tree.unlock)
96
        output = StringIO()
97
        show_pending_merges(tree, output)
3936.2.3 by Ian Clatworthy
feedback from jameinel
98
        self.assertEqualDiff(
99
            'pending merge tips: (use -v to see all merge revisions)\n'
100
            '  (ghost) a-ghost-revision\n',
101
            output.getvalue())
3377.3.40 by John Arbash Meinel
a little bit more cleanup, change how ghosts are handled
102
103
    def test_pending_with_ghosts(self):
104
        """Test when a pending merge's ancestry includes ghosts."""
105
        config.GlobalConfig().set_user_option('email', 'Joe Foo <joe@foo.com>')
106
        tree = self.make_branch_and_tree('a')
107
        tree.commit('empty commit')
108
        tree2 = tree.bzrdir.clone('b').open_workingtree()
3377.3.42 by John Arbash Meinel
Get the tests to actually work, and get the code to go along with.
109
        tree2.commit('a non-ghost', timestamp=1196796819, timezone=0)
3377.3.40 by John Arbash Meinel
a little bit more cleanup, change how ghosts are handled
110
        tree2.add_parent_tree_id('a-ghost-revision')
111
        tree2.commit('commit with ghost', timestamp=1196796819, timezone=0)
3377.3.42 by John Arbash Meinel
Get the tests to actually work, and get the code to go along with.
112
        tree2.commit('another non-ghost', timestamp=1196796819, timezone=0)
3377.3.40 by John Arbash Meinel
a little bit more cleanup, change how ghosts are handled
113
        tree.merge_from_branch(tree2.branch)
3377.3.42 by John Arbash Meinel
Get the tests to actually work, and get the code to go along with.
114
        tree.lock_read()
3377.3.40 by John Arbash Meinel
a little bit more cleanup, change how ghosts are handled
115
        self.addCleanup(tree.unlock)
116
        output = StringIO()
3936.2.1 by Ian Clatworthy
verbose flag for status - code & tests
117
        show_pending_merges(tree, output, verbose=True)
3377.3.42 by John Arbash Meinel
Get the tests to actually work, and get the code to go along with.
118
        self.assertEqualDiff('pending merges:\n'
119
                             '  Joe Foo 2007-12-04 another non-ghost\n'
3998.1.1 by Ian Clatworthy
Add merge indication to line format (Neil Martinsen-Burrell)
120
                             '    Joe Foo 2007-12-04 [merge] commit with ghost\n'
3377.3.42 by John Arbash Meinel
Get the tests to actually work, and get the code to go along with.
121
                             '    (ghost) a-ghost-revision\n'
122
                             '    Joe Foo 2007-12-04 a non-ghost\n',
123
                             output.getvalue())
3377.3.40 by John Arbash Meinel
a little bit more cleanup, change how ghosts are handled
124
1852.2.1 by Robert Collins
General cleanups - status, add a status test with two revision trees, and commands.py importing of bzrlib.trace.
125
    def tests_revision_to_revision(self):
126
        """doing a status between two revision trees should work."""
127
        tree = self.make_branch_and_tree('.')
128
        r1_id = tree.commit('one', allow_pointless=True)
129
        r2_id = tree.commit('two', allow_pointless=True)
130
        r2_tree = tree.branch.repository.revision_tree(r2_id)
131
        output = StringIO()
1948.4.29 by John Arbash Meinel
Remove a few more direct references to RevisionSpec
132
        show_tree_status(tree, to_file=output,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
133
                     revision=[RevisionSpec.from_string("revid:%s" % r1_id),
134
                               RevisionSpec.from_string("revid:%s" % r2_id)])
1852.2.1 by Robert Collins
General cleanups - status, add a status test with two revision trees, and commands.py importing of bzrlib.trace.
135
        # return does not matter as long as it did not raise.
5393.3.2 by Parth Malwankar
added StatusPostHookParams and test cases
136
137
138
class TestHooks(TestCaseWithTransport):
139
140
    def test_constructor(self):
141
        """Check that creating a StatusHooks instance has the right defaults.
142
        """
143
        hooks = _mod_status.StatusHooks()
144
        self.assertTrue("post_status" in hooks, "post_status not in %s" % hooks)
5393.3.7 by Parth Malwankar
added pre_status hook
145
        self.assertTrue("pre_status" in hooks, "pre_status not in %s" % hooks)
5393.3.2 by Parth Malwankar
added StatusPostHookParams and test cases
146
147
    def test_installed_hooks_are_StatusHooks(self):
148
        """The installed hooks object should be a StatusHooks.
149
        """
150
        # the installed hooks are saved in self._preserved_hooks.
151
        self.assertIsInstance(self._preserved_hooks[_mod_status][1],
152
            _mod_status.StatusHooks)
153
154
    def test_post_status_hook(self):
155
        """Ensure that post_status hook is invoked with the right args.
156
        """
157
        calls = []
158
        _mod_status.hooks.install_named_hook('post_status', calls.append, None)
159
        self.assertLength(0, calls)
160
        tree = self.make_branch_and_tree('.')
161
        r1_id = tree.commit('one', allow_pointless=True)
162
        r2_id = tree.commit('two', allow_pointless=True)
163
        r2_tree = tree.branch.repository.revision_tree(r2_id)
164
        output = StringIO()
165
        show_tree_status(tree, to_file=output,
166
            revision=[RevisionSpec.from_string("revid:%s" % r1_id),
167
                RevisionSpec.from_string("revid:%s" % r2_id)])
168
        self.assertLength(1, calls)
169
        params = calls[0]
5393.3.7 by Parth Malwankar
added pre_status hook
170
        self.assertIsInstance(params, _mod_status.StatusHookParams)
171
        attrs = ['old_tree', 'new_tree', 'to_file', 'versioned',
172
            'show_ids', 'short', 'verbose']
173
        for a in attrs:
174
            self.assertTrue(hasattr(params, a),
175
                'Attribute "%s" not found in StatusHookParam' % a)
176
177
    def test_pre_status_hook(self):
178
        """Ensure that pre_status hook is invoked with the right args.
179
        """
180
        calls = []
181
        _mod_status.hooks.install_named_hook('pre_status', calls.append, None)
182
        self.assertLength(0, calls)
183
        tree = self.make_branch_and_tree('.')
184
        r1_id = tree.commit('one', allow_pointless=True)
185
        r2_id = tree.commit('two', allow_pointless=True)
186
        r2_tree = tree.branch.repository.revision_tree(r2_id)
187
        output = StringIO()
188
        show_tree_status(tree, to_file=output,
189
            revision=[RevisionSpec.from_string("revid:%s" % r1_id),
190
                RevisionSpec.from_string("revid:%s" % r2_id)])
191
        self.assertLength(1, calls)
192
        params = calls[0]
193
        self.assertIsInstance(params, _mod_status.StatusHookParams)
194
        attrs = ['old_tree', 'new_tree', 'to_file', 'versioned',
195
            'show_ids', 'short', 'verbose']
196
        for a in attrs:
197
            self.assertTrue(hasattr(params, a),
198
                'Attribute "%s" not found in StatusHookParam' % a)
5393.3.2 by Parth Malwankar
added StatusPostHookParams and test cases
199