OLD | NEW |
| (Empty) |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import collections | |
6 import textwrap | |
7 import unittest | |
8 | |
9 from infra.services.gnumbd.support import data | |
10 | |
11 class TestCommitTimestamp(unittest.TestCase): | |
12 def testBasic(self): | |
13 TS = '1399330903 -0700' | |
14 t = data.CommitTimestamp.from_raw(TS) | |
15 self.assertEqual(1399330903, t.secs) | |
16 self.assertEqual(7, t.hours) | |
17 self.assertEqual(0, t.mins) | |
18 self.assertEqual('-', t.sign) | |
19 self.assertEqual('-0700', t.tz_str) | |
20 self.assertEqual(TS, str(t)) | |
21 self.assertEqual("CommitTimestamp(1399330903, '-', 7, 0)", repr(t)) | |
22 | |
23 def testError(self): | |
24 with self.assertRaises(Exception): | |
25 data.CommitTimestamp.from_raw(' 1000 +0700') | |
26 with self.assertRaises(Exception): | |
27 data.CommitTimestamp.from_raw('1000 +0700 ') | |
28 with self.assertRaises(Exception): | |
29 data.CommitTimestamp.from_raw('1000 +0700') | |
30 with self.assertRaises(Exception): | |
31 data.CommitTimestamp.from_raw('cat -0700') | |
32 with self.assertRaises(Exception): | |
33 data.CommitTimestamp.from_raw('1000 =0700') | |
34 with self.assertRaises(Exception): | |
35 data.CommitTimestamp.from_raw('1000 +x100') | |
36 with self.assertRaises(Exception): | |
37 data.CommitTimestamp.from_raw('1000 +010x') | |
38 | |
39 def testEquality(self): | |
40 TS = '1399330903 -0700' | |
41 t = data.CommitTimestamp.from_raw(TS) | |
42 self.assertEqual(t, t) | |
43 self.assertIs(t, t) | |
44 | |
45 t2 = data.CommitTimestamp.from_raw(TS) | |
46 self.assertEqual(t, t2) | |
47 self.assertIsNot(t, t2) | |
48 | |
49 self.assertNotEqual(t, t.alter(sign='+')) | |
50 | |
51 def testToDict(self): | |
52 TS = '1399330903 -0700' | |
53 t = data.CommitTimestamp.from_raw(TS) | |
54 self.assertEqual(t.to_dict(), { | |
55 'secs': 1399330903, 'sign': '-', 'hours': 7, 'mins': 0}) | |
56 | |
57 def testAlter(self): | |
58 TS = '1399330903 -0700' | |
59 t = data.CommitTimestamp.from_raw(TS) | |
60 self.assertEqual(str(t.alter(hours=20, sign='+')), '1399330903 +2000') | |
61 | |
62 | |
63 class TestCommitUser(unittest.TestCase): | |
64 def testBasic(self): | |
65 USER = 'Bob Boberton <bob@chromium.org> 1399330903 -0700' | |
66 u = data.CommitUser.from_raw(USER) | |
67 self.assertEqual(u.email, 'bob@chromium.org') | |
68 self.assertEqual(u.user, 'Bob Boberton') | |
69 self.assertEqual(u.timestamp, data.CommitTimestamp(1399330903, '-', 7, 0)) | |
70 self.assertEqual(str(u), USER) | |
71 self.assertEqual( | |
72 repr(u), ( | |
73 "CommitUser('Bob Boberton', 'bob@chromium.org', " | |
74 "CommitTimestamp(1399330903, '-', 7, 0))" | |
75 ) | |
76 ) | |
77 | |
78 USER = 'Bob Boberton < bob@chromium.org> 1399330903 -0700' | |
79 u = data.CommitUser.from_raw(USER) | |
80 self.assertEqual(u.email, ' bob@chromium.org') | |
81 self.assertEqual(u.user, 'Bob Boberton ') | |
82 self.assertEqual(str(u), USER) | |
83 | |
84 def testError(self): | |
85 with self.assertRaises(Exception): | |
86 USER = 'Bob Boberton <bob@chromium.org> 1399330903 -0700' | |
87 data.CommitUser.from_raw(USER) | |
88 with self.assertRaises(Exception): | |
89 USER = 'Bob Boberton <bob@chromium.org 1399330903 -0700' | |
90 data.CommitUser.from_raw(USER) | |
91 with self.assertRaises(Exception): | |
92 USER = 'Bob Boberton bob@chromium.org> 1399330903 -0700' | |
93 data.CommitUser.from_raw(USER) | |
94 with self.assertRaises(Exception): | |
95 USER = ' <bob@chromium.org> 1399330903 -0700' | |
96 data.CommitUser.from_raw(USER) | |
97 with self.assertRaises(Exception): | |
98 USER = '<bob@chromium.org> 1399330903 -0700' | |
99 data.CommitUser.from_raw(USER) | |
100 with self.assertRaises(Exception): | |
101 USER = 'Bob <> 1399330903 -0700' | |
102 data.CommitUser.from_raw(USER) | |
103 | |
104 def testToDict(self): | |
105 USER = 'Bob Boberton <bob@chromium.org> 1399330903 -0700' | |
106 u = data.CommitUser.from_raw(USER) | |
107 self.assertEqual(u.to_dict(), { | |
108 'user': 'Bob Boberton', 'email': 'bob@chromium.org', | |
109 'timestamp': data.CommitTimestamp(1399330903, '-', 7, 0)}) | |
110 | |
111 def testEquality(self): | |
112 USER = 'Bob Boberton <bob@chromium.org> 1399330903 -0700' | |
113 u = data.CommitUser.from_raw(USER) | |
114 self.assertEqual(u, u) | |
115 self.assertIs(u, u) | |
116 | |
117 u2 = data.CommitUser.from_raw(USER) | |
118 self.assertEqual(u, u2) | |
119 self.assertIsNot(u, u2) | |
120 | |
121 self.assertNotEqual(u, u.alter(user='Catty Catterson')) | |
122 | |
123 def testAlter(self): | |
124 USER = 'Bob Boberton <bob@chromium.org> 1399330903 -0700' | |
125 u = data.CommitUser.from_raw(USER) | |
126 self.assertEqual(u, u.alter(user='Bob Boberton')) | |
127 | |
128 USER = 'Bob Boberton <roberton@chromium.org> 1399330903 -0700' | |
129 u2 = data.CommitUser.from_raw(USER) | |
130 nu = u2.alter(email='bob@chromium.org') | |
131 self.assertEqual(u, nu) | |
132 | |
133 self.assertEqual(u2.alter(timestamp=u2.timestamp.alter(secs=100)).timestamp, | |
134 data.CommitTimestamp.from_raw('100 -0700')) | |
135 | |
136 | |
137 class TestCommitData(unittest.TestCase): | |
138 def testBasic(self): | |
139 COMMIT = textwrap.dedent("""\ | |
140 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
141 parent 1b346cb5145e1fe4c074611e335d8ac96e18c686 | |
142 parent ab8d80f57839fb03674c0fc69ff5ccf2145fa6e2 | |
143 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
144 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
145 | |
146 Cool commit message | |
147 """) | |
148 self.maxDiff = 10000 | |
149 d = data.CommitData.from_raw(COMMIT) | |
150 self.assertEqual(d.to_dict(), { | |
151 'tree': 'b966f77e58b8a3cf7c02dd0271a4d636c0857af4', | |
152 'parents': ('1b346cb5145e1fe4c074611e335d8ac96e18c686', | |
153 'ab8d80f57839fb03674c0fc69ff5ccf2145fa6e2'), | |
154 'author': data.CommitUser( | |
155 'Bob Boberton', 'bob@chromium.org', | |
156 data.CommitTimestamp(1399330903, '-', 7, 0)), | |
157 'committer': data.CommitUser( | |
158 'Jane January', 'jane@chromium.org', | |
159 data.CommitTimestamp(1399330903, '-', 7, 0)), | |
160 'message_lines': ('Cool commit message',), | |
161 'footer_lines': (), | |
162 'other_header_lines': () | |
163 }) | |
164 self.assertEqual( | |
165 repr(d), | |
166 "CommitData('b966f77e58b8a3cf7c02dd0271a4d636c0857af4', " | |
167 "('1b346cb5145e1fe4c074611e335d8ac96e18c686', " | |
168 "'ab8d80f57839fb03674c0fc69ff5ccf2145fa6e2'), " | |
169 "CommitUser('Bob Boberton', 'bob@chromium.org', " | |
170 "CommitTimestamp(1399330903, '-', 7, 0)), " | |
171 "CommitUser('Jane January', 'jane@chromium.org', " | |
172 "CommitTimestamp(1399330903, '-', 7, 0)), (), " | |
173 "('Cool commit message',), ())" | |
174 ) | |
175 self.assertEqual(str(d), COMMIT) | |
176 | |
177 def testFooters(self): | |
178 COMMIT = textwrap.dedent("""\ | |
179 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
180 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
181 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
182 | |
183 Cool commit message | |
184 with: misleading footers | |
185 | |
186 Xenophobe: False | |
187 Cool-Footer: 100 | |
188 Double-Awesome: 100 | |
189 Cool-Footer: Redux | |
190 """) | |
191 d = data.CommitData.from_raw(COMMIT) | |
192 self.assertEqual(d, d) | |
193 self.assertNotEqual(d, {}) | |
194 self.assertDictContainsSubset({ | |
195 'tree': 'b966f77e58b8a3cf7c02dd0271a4d636c0857af4', | |
196 'author': data.CommitUser( | |
197 'Bob Boberton', 'bob@chromium.org', | |
198 data.CommitTimestamp(1399330903, '-', 7, 0)), | |
199 'committer': data.CommitUser( | |
200 'Jane January', 'jane@chromium.org', | |
201 data.CommitTimestamp(1399330903, '-', 7, 0)) | |
202 }, d.to_dict()) | |
203 self.assertSequenceEqual( | |
204 d.message_lines, ['Cool commit message', 'with: misleading footers']) | |
205 self.assertSequenceEqual( | |
206 d.footer_lines, | |
207 [('Xenophobe', 'False'), ('Cool-Footer', '100'), | |
208 ('Double-Awesome', '100'), ('Cool-Footer', 'Redux')]) | |
209 self.assertEqual(d.footers, { | |
210 'Cool-Footer': ('100', 'Redux'), | |
211 'Double-Awesome': ('100',), | |
212 'Xenophobe': ('False',), | |
213 }) | |
214 self.assertSequenceEqual(['Xenophobe', 'Cool-Footer', 'Double-Awesome'], | |
215 d.footers.keys()) | |
216 self.assertEqual(str(d), COMMIT) | |
217 | |
218 def testMisleadingFooters(self): | |
219 COMMIT = textwrap.dedent("""\ | |
220 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
221 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
222 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
223 | |
224 Cool commit message | |
225 with: misleading footers | |
226 """) | |
227 d = data.CommitData.from_raw(COMMIT) | |
228 self.assertDictContainsSubset({ | |
229 'tree': 'b966f77e58b8a3cf7c02dd0271a4d636c0857af4', | |
230 'author': data.CommitUser( | |
231 'Bob Boberton', 'bob@chromium.org', | |
232 data.CommitTimestamp(1399330903, '-', 7, 0)), | |
233 'committer': data.CommitUser( | |
234 'Jane January', 'jane@chromium.org', | |
235 data.CommitTimestamp(1399330903, '-', 7, 0)), | |
236 }, d.to_dict()) | |
237 self.assertSequenceEqual( | |
238 d.message_lines, ['Cool commit message', 'with: misleading footers']) | |
239 self.assertSequenceEqual( | |
240 d.footer_lines, []) | |
241 self.assertEqual(d.footers, {}) | |
242 | |
243 def testAlter(self): | |
244 TREE1 = "tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4\n" | |
245 | |
246 TREE2 = "tree deadbeefdeadbeefdeadbeefdeadbeefdeadbeef\n" | |
247 | |
248 POSTFIX = textwrap.dedent("""\ | |
249 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
250 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
251 | |
252 Cool commit message | |
253 with: misleading footers | |
254 """) | |
255 | |
256 d = data.CommitData.from_raw(TREE1 + POSTFIX) | |
257 d2 = data.CommitData.from_raw(TREE2 + POSTFIX) | |
258 | |
259 self.assertEqual( | |
260 d.alter(tree='deadbeefdeadbeefdeadbeefdeadbeefdeadbeef'), d2) | |
261 | |
262 HEADERS = textwrap.dedent("""\ | |
263 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
264 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
265 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
266 meep happytimes | |
267 | |
268 Cool commit message | |
269 with: misleading footers | |
270 """) | |
271 self.assertEqual(str(d.alter(other_headers={'meep': ['happytimes']})), | |
272 HEADERS) | |
273 | |
274 def testAlterReorder(self): | |
275 PREFIX = textwrap.dedent("""\ | |
276 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
277 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
278 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
279 | |
280 Cool commit message | |
281 with: misleading footers | |
282 | |
283 """) | |
284 COMMIT = PREFIX + textwrap.dedent("""\ | |
285 Xenophobe: False | |
286 Cool-Footer: 100 | |
287 Double-Awesome: 100 | |
288 Cool-Footer: Redux | |
289 """) | |
290 NEW_COMMIT = PREFIX + textwrap.dedent("""\ | |
291 Xenophobe: False | |
292 Cool-Footer: 100 | |
293 Cool-Footer: Redux | |
294 Double-Awesome: 100 | |
295 """) | |
296 d = data.CommitData.from_raw(COMMIT) | |
297 nd = data.CommitData.from_raw(NEW_COMMIT) | |
298 self.assertEqual(d, d) | |
299 self.assertNotEqual(d, nd) | |
300 | |
301 def testAlterAdd(self): | |
302 PREFIX = textwrap.dedent("""\ | |
303 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
304 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
305 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
306 | |
307 Cool commit message | |
308 with: misleading footers | |
309 | |
310 """) | |
311 COMMIT = PREFIX + textwrap.dedent("""\ | |
312 Xenophobe: False | |
313 Cool-Footer: 100 | |
314 Double-Awesome: 100 | |
315 Cool-Footer: Redux | |
316 """) | |
317 NEW_COMMIT = PREFIX + textwrap.dedent("""\ | |
318 Cool-Footer: 100 | |
319 Cool-Footer: Redux | |
320 Double-Awesome: 100 | |
321 Fungus: neat! | |
322 """) | |
323 d = data.CommitData.from_raw(COMMIT) | |
324 nd = data.CommitData.from_raw(NEW_COMMIT) | |
325 | |
326 f = {'Fungus': ('neat!',)} | |
327 f['Xenophobe'] = None | |
328 self.assertEqual(d.alter(footers=f), nd) | |
329 | |
330 def testAlterDel(self): | |
331 PREFIX = textwrap.dedent("""\ | |
332 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
333 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
334 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
335 | |
336 Cool commit message | |
337 with: misleading footers | |
338 | |
339 """) | |
340 COMMIT = PREFIX + textwrap.dedent("""\ | |
341 Xenophobe: False | |
342 Cool-Footer: 100 | |
343 Double-Awesome: 100 | |
344 Cool-Footer: Redux | |
345 """) | |
346 NEW_COMMIT = PREFIX[:-1] | |
347 d = data.CommitData.from_raw(COMMIT) | |
348 nd = data.CommitData.from_raw(NEW_COMMIT) | |
349 to_nuke = {k: None for k in d.footers} | |
350 to_nuke['Non-exist'] = None | |
351 self.assertEqual(d.alter(footers=to_nuke), nd) | |
352 | |
353 def testAlterAppend(self): | |
354 PREFIX = textwrap.dedent("""\ | |
355 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
356 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
357 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
358 | |
359 Cool commit message | |
360 with: misleading footers | |
361 | |
362 """) | |
363 COMMIT = PREFIX + textwrap.dedent("""\ | |
364 Xenophobe: False | |
365 Cool-Footer: 100 | |
366 Double-Awesome: 100 | |
367 Cool-Footer: Redux | |
368 """) | |
369 NEW_COMMIT = PREFIX + textwrap.dedent("""\ | |
370 Xenophobe: False | |
371 Cool-Footer: 100 | |
372 Cool-Footer: Redux | |
373 Cool-Footer: Sweet | |
374 Double-Awesome: 100 | |
375 """) | |
376 d = data.CommitData.from_raw(COMMIT) | |
377 nd = data.CommitData.from_raw(NEW_COMMIT) | |
378 self.assertEqual( | |
379 d.alter( | |
380 footers={'Cool-Footer': d.footers['Cool-Footer'] + ('Sweet',)} | |
381 ), | |
382 nd) | |
383 | |
384 def testOtherHeaders(self): | |
385 COMMIT = textwrap.dedent("""\ | |
386 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
387 parent 1b346cb5145e1fe4c074611e335d8ac96e18c686 | |
388 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
389 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
390 generation 300 | |
391 apple-sauce cat food | |
392 generation 100 | |
393 | |
394 Cool commit message | |
395 with: misleading footers | |
396 """) | |
397 c = data.CommitData.from_raw(COMMIT) | |
398 expect = collections.OrderedDict() | |
399 expect['generation'] = ('300', '100') | |
400 expect['apple-sauce'] = ('cat food',) | |
401 self.assertEqual(c.other_headers, expect) | |
402 self.assertEqual(str(c), COMMIT) | |
403 | |
404 # Error cases | |
405 def testDupTree(self): | |
406 COMMIT = textwrap.dedent("""\ | |
407 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
408 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
409 parent 1b346cb5145e1fe4c074611e335d8ac96e18c686 | |
410 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
411 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
412 | |
413 Cool commit message | |
414 with: misleading footers | |
415 """) | |
416 with self.assertRaises(data.UnexpectedHeader): | |
417 data.CommitData.from_raw(COMMIT) | |
418 | |
419 def testDupUser(self): | |
420 COMMIT = textwrap.dedent("""\ | |
421 tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 | |
422 parent 1b346cb5145e1fe4c074611e335d8ac96e18c686 | |
423 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
424 author Bob Boberton <bob@chromium.org> 1399330903 -0700 | |
425 committer Jane January <jane@chromium.org> 1399330903 -0700 | |
426 | |
427 Cool commit message | |
428 with: misleading footers | |
429 """) | |
430 with self.assertRaises(data.UnexpectedHeader): | |
431 data.CommitData.from_raw(COMMIT) | |
432 | |
433 def testPartial(self): | |
434 COMMIT = textwrap.dedent("""\ | |
435 tree deadbeefdeadbeefdeadbeefdeadbeefdeadbeef | |
436 """) | |
437 with self.assertRaises(data.PartialCommit): | |
438 data.CommitData.from_raw(COMMIT) | |
439 | |
440 def testEmpty(self): | |
441 with self.assertRaises(data.PartialCommit): | |
442 data.CommitData.from_raw('') | |
OLD | NEW |