Debian Codegraph @

lib/tests/test_commit_data.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2009 Alexander Belchenko
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 """Tests for commit data object and operations."""
21
22 from breezy.tests import TestCase, TestCaseWithTransport
23 from breezy.plugins.qbrz.lib.commit_data import (
24 CommitData,
25 QBzrCommitData,
26 )
27
28
29 class TestCommitDataBase(TestCase):
30
31 def test_empty(self):
32 d = CommitData()
33 # CommitData instance has bool value False if there is no data inside
34 self.assertFalse(bool(d))
35 self.assertEqual(None, d['message'])
36 self.assertEqual({}, d.as_dict())
37 self.assertEqual([], list(d.keys()))
38
39 def test_set_data_dict(self):
40 d = CommitData()
41 d.set_data({'message': 'foo bar'})
42 # CommitData instance has bool value True if there is some data inside
43 self.assertTrue(bool(d))
44 self.assertEqual('foo bar', d['message'])
45 self.assertEqual({'message': 'foo bar'}, d.as_dict())
46 self.assertEqual(['message'], list(d.keys()))
47
48 def test_set_data_kw(self):
49 d = CommitData()
50 d.set_data(message='foo bar')
51 # CommitData instance has bool value True if there is some data inside
52 self.assertTrue(bool(d))
53 self.assertEqual('foo bar', d['message'])
54 self.assertEqual({'message': 'foo bar'}, d.as_dict())
55 self.assertEqual(['message'], list(d.keys()))
56
57 def test_set_data_dict_and_kw(self):
58 d = CommitData()
59 d.set_data({'fixes': 'lp:123456'}, message='foo bar')
60 # CommitData instance has bool value True if there is some data inside
61 self.assertTrue(bool(d))
62 self.assertEqual('foo bar', d['message'])
63 self.assertEqual({'message': 'foo bar',
64 'fixes': 'lp:123456',
65 }, d.as_dict())
66 self.assertEqual(set(['message', 'fixes']), set(d.keys()))
67
68 def test_init_with_data(self):
69 d = CommitData(data={'fixes': 'lp:123456', 'message': 'foo bar'})
70 # CommitData instance has bool value True if there is some data inside
71 self.assertTrue(bool(d))
72 self.assertEqual('foo bar', d['message'])
73 self.assertEqual({'message': 'foo bar',
74 'fixes': 'lp:123456',
75 }, d.as_dict())
76 self.assertEqual(set(['message', 'fixes']), set(d.keys()))
77
78 def test_compare_data(self):
79 this = CommitData(data={'fixes': 'lp:123456', 'message': 'foo bar'})
80 self.assertTrue(this.compare_data(
81 CommitData(data={'fixes': 'lp:123456',
82 'message': 'foo bar'})))
83 self.assertTrue(this.compare_data({'fixes': 'lp:123456',
84 'message': 'foo bar'}))
85 other = CommitData(data={'fixes': 'lp:123456',
86 'message': 'foo bar',
87 'old_revid': 'xxx',
88 'new_revid': 'yyy',
89 })
90 self.assertFalse(this.compare_data(other))
91 self.assertTrue(this.compare_data(other, all_keys=False))
92
93
94 class TestCommitDataWithTree(TestCaseWithTransport):
95
96 def test_set_data_on_uncommit(self):
97 wt = self.make_branch_and_tree('.')
98 revid1 = wt.commit(message='1')
99 # imitate uncommit in branch with only one revision
100 d = CommitData(branch=wt.branch)
101 d.set_data_on_uncommit(revid1, None)
102
103 self.assertEqual({'message': '1',
104 'old_revid': revid1,
105 'new_revid': b'null:',
106 }, d.as_dict())
107 #
108 revid2 = wt.commit(message='2')
109 # imitate uncommit in branch with several revisions
110 d = CommitData(branch=wt.branch)
111 d.set_data_on_uncommit(revid2, revid1)
112 self.assertEqual({'message': '2',
113 'old_revid': revid2,
114 'new_revid': revid1,
115 }, d.as_dict())
116
117 # RJL: unfortunately, this test is flawed - not in its own right but because
118 # the test version of run_bzr is more forgiving than the 'real' version:
119 # it will accept strings whereas the actual version needs an array.
120 # The test version is in breezy/tests/__init__.py
121 def test_set_data_on_uncommit_bugids(self):
122 wt = self.make_branch_and_tree('.')
123 self.run_bzr('commit --unchanged -m foo --fixes lp:12345 --fixes lp:67890')
124 revid1 = wt.last_revision()
125 d = CommitData(branch=wt.branch)
126 d.set_data_on_uncommit(revid1, None)
127 self.assertEqual({'message': 'foo',
128 'bugs': 'lp:12345 lp:67890',
129 'old_revid': revid1,
130 'new_revid': b'null:',
131 }, d.as_dict())
132
133 def test_set_data_on_uncommit_bugids_with_message(self):
134 wt = self.make_branch_and_tree('.')
135 self.run_bzr('commit --unchanged -m "foo with spaces" --fixes lp:12345 --fixes lp:67890')
136 revid1 = wt.last_revision()
137 d = CommitData(branch=wt.branch)
138 d.set_data_on_uncommit(revid1, None)
139 self.assertEqual({'message': 'foo with spaces',
140 'bugs': 'lp:12345 lp:67890',
141 'old_revid': revid1,
142 'new_revid': b'null:',
143 }, d.as_dict())
144
145 def test_load_nothing(self):
146 wt = self.make_branch_and_tree('.')
147 d = CommitData(tree=wt)
148 d.load()
149 self.assertEqual({}, d.as_dict())
150
151 def test_save_data(self):
152 wt = self.make_branch_and_tree('.')
153 d = CommitData(tree=wt)
154 d.set_data(message='spam', old_revid='foo', new_revid='bar')
155 d.save()
156 # check branch.conf
157 cfg = wt.branch.get_config()
158 self.assertEqual({'message': 'spam',
159 'old_revid': 'foo',
160 'new_revid': 'bar',
161 }, cfg.get_user_option('commit_data'))
162
163 def test_save_message_with_spaces(self):
164 wt = self.make_branch_and_tree('.')
165 d = CommitData(tree=wt)
166 d.set_data(message='spam with spaces', old_revid='foo', new_revid='bar')
167 d.save()
168 # check branch.conf
169 cfg = wt.branch.get_config()
170 self.assertEqual({'message': 'spam with spaces',
171 'old_revid': 'foo',
172 'new_revid': 'bar',
173 }, cfg.get_user_option('commit_data'))
174
175 def test_save_filter_out_empty_data(self):
176 wt = self.make_branch_and_tree('.')
177 d = CommitData(tree=wt)
178 d.set_data({'message': '', 'foo': 'bar'})
179 d.save()
180 # check branch.conf
181 cfg = wt.branch.get_config()
182 self.assertEqual({'foo': 'bar'}, cfg.get_user_option('commit_data'))
183
184 def test_load_saved_data(self):
185 wt = self.make_branch_and_tree('.')
186 cfg = wt.branch.get_config()
187 cfg.set_user_option('commit_data',
188 {'message': 'spam',
189 'old_revid': 'foo',
190 'new_revid': 'bar',
191 })
192 d = CommitData(tree=wt)
193 d.load()
194 self.assertEqual({'message': 'spam',
195 'old_revid': 'foo',
196 'new_revid': 'bar',
197 }, d.as_dict())
198
199 def test_wipe_saved_data(self):
200 wt = self.make_branch_and_tree('.')
201 cfg = wt.branch.get_config()
202 cfg.set_user_option('commit_data',
203 {'message': 'spam',
204 'old_revid': 'foo',
205 'new_revid': 'bar',
206 })
207 d = CommitData(tree=wt)
208 d.wipe()
209 # check branch.conf
210 cfg = wt.branch.get_config()
211 self.assertEqual({}, cfg.get_user_option('commit_data'))
212
213
214
215 class TestQBzrCommitData(TestCaseWithTransport):
216
217 def test_io_old_data_transition(self):
218 # we should handle old data (i.e. qbrz_commit_message) gracefully
219 wt = self.make_branch_and_tree('.')
220 cfg = wt.branch.get_config()
221 cfg.set_user_option('qbrz_commit_message', 'spam')
222 # load
223 d = QBzrCommitData(tree=wt)
224 d.load()
225 self.assertEqual({'message': 'spam',}, d.as_dict())
226 #
227 # if here both old and new then prefer new
228 cfg.set_user_option('commit_data', {'foo': 'bar'})
229 d = QBzrCommitData(tree=wt)
230 d.load()
231 self.assertEqual({'foo': 'bar',}, d.as_dict())
232 #
233 # on save we should clear old data
234 d = QBzrCommitData(tree=wt)
235 d.set_data(message='eggs', old_revid='foo', new_revid='bar')
236 d.save()
237 # check branch.conf
238 cfg = wt.branch.get_config()
239 self.assertEqual('', cfg.get_user_option('qbrz_commit_message'))
240 self.assertEqual({'message': 'eggs','old_revid': 'foo','new_revid': 'bar',}, cfg.get_user_option('commit_data'))
241 #
242 # on wipe we should clear old data too
243 cfg = wt.branch.get_config()
244 cfg.set_user_option('qbrz_commit_message', 'spam')
245 d = QBzrCommitData(tree=wt)
246 d.wipe()
247 # check branch.conf
248 cfg = wt.branch.get_config()
249 self.assertEqual('', cfg.get_user_option('qbrz_commit_message'))
250 self.assertEqual({}, cfg.get_user_option('commit_data'))