test_objects.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. # test_objects.py -- tests for objects.py
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) any later version of
  8. # the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Tests for git base objects."""
  20. # TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.
  21. import datetime
  22. import os
  23. import stat
  24. import unittest
  25. from dulwich.errors import (
  26. ChecksumMismatch,
  27. ObjectFormatException,
  28. )
  29. from dulwich.objects import (
  30. Blob,
  31. Tree,
  32. Commit,
  33. Tag,
  34. format_timezone,
  35. hex_to_sha,
  36. sha_to_hex,
  37. hex_to_filename,
  38. check_hexsha,
  39. check_identity,
  40. parse_timezone,
  41. parse_tree,
  42. _parse_tree_py,
  43. )
  44. from dulwich.tests import (
  45. TestSkipped,
  46. )
  47. a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  48. b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
  49. c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
  50. tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
  51. tag_sha = '71033db03a03c6a36721efcf1968dd8f8e0cf023'
  52. try:
  53. from itertools import permutations
  54. except ImportError:
  55. # Implementation of permutations from Python 2.6 documentation:
  56. # http://docs.python.org/2.6/library/itertools.html#itertools.permutations
  57. # Copyright (c) 2001-2010 Python Software Foundation; All Rights Reserved
  58. def permutations(iterable, r=None):
  59. # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
  60. # permutations(range(3)) --> 012 021 102 120 201 210
  61. pool = tuple(iterable)
  62. n = len(pool)
  63. r = n if r is None else r
  64. if r > n:
  65. return
  66. indices = range(n)
  67. cycles = range(n, n-r, -1)
  68. yield tuple(pool[i] for i in indices[:r])
  69. while n:
  70. for i in reversed(range(r)):
  71. cycles[i] -= 1
  72. if cycles[i] == 0:
  73. indices[i:] = indices[i+1:] + indices[i:i+1]
  74. cycles[i] = n - i
  75. else:
  76. j = cycles[i]
  77. indices[i], indices[-j] = indices[-j], indices[i]
  78. yield tuple(pool[i] for i in indices[:r])
  79. break
  80. else:
  81. return
  82. class TestHexToSha(unittest.TestCase):
  83. def test_simple(self):
  84. self.assertEquals("\xab\xcd" * 10, hex_to_sha("abcd" * 10))
  85. def test_reverse(self):
  86. self.assertEquals("abcd" * 10, sha_to_hex("\xab\xcd" * 10))
  87. class BlobReadTests(unittest.TestCase):
  88. """Test decompression of blobs"""
  89. def get_sha_file(self, cls, base, sha):
  90. dir = os.path.join(os.path.dirname(__file__), 'data', base)
  91. return cls.from_file(hex_to_filename(dir, sha))
  92. def get_blob(self, sha):
  93. """Return the blob named sha from the test data dir"""
  94. return self.get_sha_file(Blob, 'blobs', sha)
  95. def get_tree(self, sha):
  96. return self.get_sha_file(Tree, 'trees', sha)
  97. def get_tag(self, sha):
  98. return self.get_sha_file(Tag, 'tags', sha)
  99. def commit(self, sha):
  100. return self.get_sha_file(Commit, 'commits', sha)
  101. def test_decompress_simple_blob(self):
  102. b = self.get_blob(a_sha)
  103. self.assertEqual(b.data, 'test 1\n')
  104. self.assertEqual(b.sha().hexdigest(), a_sha)
  105. def test_hash(self):
  106. b = self.get_blob(a_sha)
  107. self.assertEqual(hash(b.id), hash(b))
  108. def test_parse_empty_blob_object(self):
  109. sha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
  110. b = self.get_blob(sha)
  111. self.assertEqual(b.data, '')
  112. self.assertEqual(b.id, sha)
  113. self.assertEqual(b.sha().hexdigest(), sha)
  114. def test_create_blob_from_string(self):
  115. string = 'test 2\n'
  116. b = Blob.from_string(string)
  117. self.assertEqual(b.data, string)
  118. self.assertEqual(b.sha().hexdigest(), b_sha)
  119. def test_chunks(self):
  120. string = 'test 5\n'
  121. b = Blob.from_string(string)
  122. self.assertEqual([string], b.chunked)
  123. def test_set_chunks(self):
  124. b = Blob()
  125. b.chunked = ['te', 'st', ' 5\n']
  126. self.assertEqual('test 5\n', b.data)
  127. b.chunked = ['te', 'st', ' 6\n']
  128. self.assertEqual('test 6\n', b.as_raw_string())
  129. def test_parse_legacy_blob(self):
  130. string = 'test 3\n'
  131. b = self.get_blob(c_sha)
  132. self.assertEqual(b.data, string)
  133. self.assertEqual(b.sha().hexdigest(), c_sha)
  134. def test_eq(self):
  135. blob1 = self.get_blob(a_sha)
  136. blob2 = self.get_blob(a_sha)
  137. self.assertEqual(blob1, blob2)
  138. def test_read_tree_from_file(self):
  139. t = self.get_tree(tree_sha)
  140. self.assertEqual(t.entries()[0], (33188, 'a', a_sha))
  141. self.assertEqual(t.entries()[1], (33188, 'b', b_sha))
  142. def test_read_tag_from_file(self):
  143. t = self.get_tag(tag_sha)
  144. self.assertEqual(t.object, (Commit, '51b668fd5bf7061b7d6fa525f88803e6cfadaa51'))
  145. self.assertEqual(t.name,'signed')
  146. self.assertEqual(t.tagger,'Ali Sabil <ali.sabil@gmail.com>')
  147. self.assertEqual(t.tag_time, 1231203091)
  148. self.assertEqual(t.message, 'This is a signed tag\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n\niEYEABECAAYFAkliqx8ACgkQqSMmLy9u/kcx5ACfakZ9NnPl02tOyYP6pkBoEkU1\n5EcAn0UFgokaSvS371Ym/4W9iJj6vh3h\n=ql7y\n-----END PGP SIGNATURE-----\n')
  149. def test_read_commit_from_file(self):
  150. sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
  151. c = self.commit(sha)
  152. self.assertEqual(c.tree, tree_sha)
  153. self.assertEqual(c.parents, ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
  154. self.assertEqual(c.author,
  155. 'James Westby <jw+debian@jameswestby.net>')
  156. self.assertEqual(c.committer,
  157. 'James Westby <jw+debian@jameswestby.net>')
  158. self.assertEqual(c.commit_time, 1174759230)
  159. self.assertEqual(c.commit_timezone, 0)
  160. self.assertEqual(c.author_timezone, 0)
  161. self.assertEqual(c.message, 'Test commit\n')
  162. def test_read_commit_no_parents(self):
  163. sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
  164. c = self.commit(sha)
  165. self.assertEqual(c.tree, '90182552c4a85a45ec2a835cadc3451bebdfe870')
  166. self.assertEqual(c.parents, [])
  167. self.assertEqual(c.author,
  168. 'James Westby <jw+debian@jameswestby.net>')
  169. self.assertEqual(c.committer,
  170. 'James Westby <jw+debian@jameswestby.net>')
  171. self.assertEqual(c.commit_time, 1174758034)
  172. self.assertEqual(c.commit_timezone, 0)
  173. self.assertEqual(c.author_timezone, 0)
  174. self.assertEqual(c.message, 'Test commit\n')
  175. def test_read_commit_two_parents(self):
  176. sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
  177. c = self.commit(sha)
  178. self.assertEqual(c.tree, 'd80c186a03f423a81b39df39dc87fd269736ca86')
  179. self.assertEqual(c.parents, ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  180. '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
  181. self.assertEqual(c.author,
  182. 'James Westby <jw+debian@jameswestby.net>')
  183. self.assertEqual(c.committer,
  184. 'James Westby <jw+debian@jameswestby.net>')
  185. self.assertEqual(c.commit_time, 1174773719)
  186. self.assertEqual(c.commit_timezone, 0)
  187. self.assertEqual(c.author_timezone, 0)
  188. self.assertEqual(c.message, 'Merge ../b\n')
  189. def test_check_id(self):
  190. wrong_sha = '1' * 40
  191. b = self.get_blob(wrong_sha)
  192. self.assertEqual(wrong_sha, b.id)
  193. self.assertRaises(ChecksumMismatch, b.check)
  194. self.assertEqual('742b386350576589175e374a5706505cbd17680c', b.id)
  195. class ShaFileCheckTests(unittest.TestCase):
  196. def assertCheckFails(self, cls, data):
  197. obj = cls()
  198. def do_check():
  199. obj.set_raw_string(data)
  200. obj.check()
  201. self.assertRaises(ObjectFormatException, do_check)
  202. def assertCheckSucceeds(self, cls, data):
  203. obj = cls()
  204. obj.set_raw_string(data)
  205. self.assertEqual(None, obj.check())
  206. class CommitSerializationTests(unittest.TestCase):
  207. def make_base(self):
  208. c = Commit()
  209. c.tree = 'd80c186a03f423a81b39df39dc87fd269736ca86'
  210. c.parents = ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd', '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6']
  211. c.author = 'James Westby <jw+debian@jameswestby.net>'
  212. c.committer = 'James Westby <jw+debian@jameswestby.net>'
  213. c.commit_time = 1174773719
  214. c.author_time = 1174773719
  215. c.commit_timezone = 0
  216. c.author_timezone = 0
  217. c.message = 'Merge ../b\n'
  218. return c
  219. def test_encoding(self):
  220. c = self.make_base()
  221. c.encoding = "iso8859-1"
  222. self.assertTrue("encoding iso8859-1\n" in c.as_raw_string())
  223. def test_short_timestamp(self):
  224. c = self.make_base()
  225. c.commit_time = 30
  226. c1 = Commit()
  227. c1.set_raw_string(c.as_raw_string())
  228. self.assertEquals(30, c1.commit_time)
  229. def test_raw_length(self):
  230. c = self.make_base()
  231. self.assertEquals(len(c.as_raw_string()), c.raw_length())
  232. def test_simple(self):
  233. c = self.make_base()
  234. self.assertEquals(c.id, '5dac377bdded4c9aeb8dff595f0faeebcc8498cc')
  235. self.assertEquals(
  236. 'tree d80c186a03f423a81b39df39dc87fd269736ca86\n'
  237. 'parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n'
  238. 'parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n'
  239. 'author James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
  240. 'committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
  241. '\n'
  242. 'Merge ../b\n', c.as_raw_string())
  243. def test_timezone(self):
  244. c = self.make_base()
  245. c.commit_timezone = 5 * 60
  246. self.assertTrue(" +0005\n" in c.as_raw_string())
  247. def test_neg_timezone(self):
  248. c = self.make_base()
  249. c.commit_timezone = -1 * 3600
  250. self.assertTrue(" -0100\n" in c.as_raw_string())
  251. default_committer = 'James Westby <jw+debian@jameswestby.net> 1174773719 +0000'
  252. class CommitParseTests(ShaFileCheckTests):
  253. def make_commit_lines(self,
  254. tree='d80c186a03f423a81b39df39dc87fd269736ca86',
  255. parents=['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  256. '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
  257. author=default_committer,
  258. committer=default_committer,
  259. encoding=None,
  260. message='Merge ../b\n',
  261. extra=None):
  262. lines = []
  263. if tree is not None:
  264. lines.append('tree %s' % tree)
  265. if parents is not None:
  266. lines.extend('parent %s' % p for p in parents)
  267. if author is not None:
  268. lines.append('author %s' % author)
  269. if committer is not None:
  270. lines.append('committer %s' % committer)
  271. if encoding is not None:
  272. lines.append('encoding %s' % encoding)
  273. if extra is not None:
  274. for name, value in sorted(extra.iteritems()):
  275. lines.append('%s %s' % (name, value))
  276. lines.append('')
  277. if message is not None:
  278. lines.append(message)
  279. return lines
  280. def make_commit_text(self, **kwargs):
  281. return '\n'.join(self.make_commit_lines(**kwargs))
  282. def test_simple(self):
  283. c = Commit.from_string(self.make_commit_text())
  284. self.assertEquals('Merge ../b\n', c.message)
  285. self.assertEquals('James Westby <jw+debian@jameswestby.net>', c.author)
  286. self.assertEquals('James Westby <jw+debian@jameswestby.net>',
  287. c.committer)
  288. self.assertEquals('d80c186a03f423a81b39df39dc87fd269736ca86', c.tree)
  289. self.assertEquals(['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  290. '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
  291. c.parents)
  292. expected_time = datetime.datetime(2007, 3, 24, 22, 1, 59)
  293. self.assertEquals(expected_time,
  294. datetime.datetime.utcfromtimestamp(c.commit_time))
  295. self.assertEquals(0, c.commit_timezone)
  296. self.assertEquals(expected_time,
  297. datetime.datetime.utcfromtimestamp(c.author_time))
  298. self.assertEquals(0, c.author_timezone)
  299. self.assertEquals(None, c.encoding)
  300. def test_custom(self):
  301. c = Commit.from_string(self.make_commit_text(
  302. extra={'extra-field': 'data'}))
  303. self.assertEquals([('extra-field', 'data')], c.extra)
  304. def test_encoding(self):
  305. c = Commit.from_string(self.make_commit_text(encoding='UTF-8'))
  306. self.assertEquals('UTF-8', c.encoding)
  307. def test_check(self):
  308. self.assertCheckSucceeds(Commit, self.make_commit_text())
  309. self.assertCheckSucceeds(Commit, self.make_commit_text(parents=None))
  310. self.assertCheckSucceeds(Commit,
  311. self.make_commit_text(encoding='UTF-8'))
  312. self.assertCheckFails(Commit, self.make_commit_text(tree='xxx'))
  313. self.assertCheckFails(Commit, self.make_commit_text(
  314. parents=[a_sha, 'xxx']))
  315. bad_committer = "some guy without an email address 1174773719 +0000"
  316. self.assertCheckFails(Commit,
  317. self.make_commit_text(committer=bad_committer))
  318. self.assertCheckFails(Commit,
  319. self.make_commit_text(author=bad_committer))
  320. self.assertCheckFails(Commit, self.make_commit_text(author=None))
  321. self.assertCheckFails(Commit, self.make_commit_text(committer=None))
  322. self.assertCheckFails(Commit, self.make_commit_text(
  323. author=None, committer=None))
  324. def test_check_duplicates(self):
  325. # duplicate each of the header fields
  326. for i in xrange(5):
  327. lines = self.make_commit_lines(parents=[a_sha], encoding='UTF-8')
  328. lines.insert(i, lines[i])
  329. text = '\n'.join(lines)
  330. if lines[i].startswith('parent'):
  331. # duplicate parents are ok for now
  332. self.assertCheckSucceeds(Commit, text)
  333. else:
  334. self.assertCheckFails(Commit, text)
  335. def test_check_order(self):
  336. lines = self.make_commit_lines(parents=[a_sha], encoding='UTF-8')
  337. headers = lines[:5]
  338. rest = lines[5:]
  339. # of all possible permutations, ensure only the original succeeds
  340. for perm in permutations(headers):
  341. perm = list(perm)
  342. text = '\n'.join(perm + rest)
  343. if perm == headers:
  344. self.assertCheckSucceeds(Commit, text)
  345. else:
  346. self.assertCheckFails(Commit, text)
  347. class TreeTests(ShaFileCheckTests):
  348. def test_simple(self):
  349. myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86"
  350. x = Tree()
  351. x["myname"] = (0100755, myhexsha)
  352. self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha),
  353. x.as_raw_string())
  354. def test_tree_dir_sort(self):
  355. x = Tree()
  356. x["a.c"] = (0100755, "d80c186a03f423a81b39df39dc87fd269736ca86")
  357. x["a"] = (stat.S_IFDIR, "d80c186a03f423a81b39df39dc87fd269736ca86")
  358. x["a/c"] = (stat.S_IFDIR, "d80c186a03f423a81b39df39dc87fd269736ca86")
  359. self.assertEquals(["a.c", "a", "a/c"], [p[0] for p in x.iteritems()])
  360. def _do_test_parse_tree(self, parse_tree):
  361. dir = os.path.join(os.path.dirname(__file__), 'data', 'trees')
  362. o = Tree.from_file(hex_to_filename(dir, tree_sha))
  363. o._parse_file()
  364. self.assertEquals([('a', 0100644, a_sha), ('b', 0100644, b_sha)],
  365. list(parse_tree(o.as_raw_string())))
  366. def test_parse_tree(self):
  367. self._do_test_parse_tree(_parse_tree_py)
  368. def test_parse_tree_extension(self):
  369. if parse_tree is _parse_tree_py:
  370. raise TestSkipped('parse_tree extension not found')
  371. self._do_test_parse_tree(parse_tree)
  372. def test_check(self):
  373. t = Tree
  374. sha = hex_to_sha(a_sha)
  375. # filenames
  376. self.assertCheckSucceeds(t, '100644 .a\0%s' % sha)
  377. self.assertCheckFails(t, '100644 \0%s' % sha)
  378. self.assertCheckFails(t, '100644 .\0%s' % sha)
  379. self.assertCheckFails(t, '100644 a/a\0%s' % sha)
  380. self.assertCheckFails(t, '100644 ..\0%s' % sha)
  381. # modes
  382. self.assertCheckSucceeds(t, '100644 a\0%s' % sha)
  383. self.assertCheckSucceeds(t, '100755 a\0%s' % sha)
  384. self.assertCheckSucceeds(t, '160000 a\0%s' % sha)
  385. # TODO more whitelisted modes
  386. self.assertCheckFails(t, '123456 a\0%s' % sha)
  387. self.assertCheckFails(t, '123abc a\0%s' % sha)
  388. # shas
  389. self.assertCheckFails(t, '100644 a\0%s' % ('x' * 5))
  390. self.assertCheckFails(t, '100644 a\0%s' % ('x' * 18 + '\0'))
  391. self.assertCheckFails(t, '100644 a\0%s\n100644 b\0%s' % ('x' * 21, sha))
  392. # ordering
  393. sha2 = hex_to_sha(b_sha)
  394. self.assertCheckSucceeds(t, '100644 a\0%s\n100644 b\0%s' % (sha, sha))
  395. self.assertCheckSucceeds(t, '100644 a\0%s\n100644 b\0%s' % (sha, sha2))
  396. self.assertCheckFails(t, '100644 a\0%s\n100755 a\0%s' % (sha, sha2))
  397. self.assertCheckFails(t, '100644 b\0%s\n100644 a\0%s' % (sha2, sha))
  398. def test_iter(self):
  399. t = Tree()
  400. t["foo"] = (0100644, a_sha)
  401. self.assertEquals(set(["foo"]), set(t))
  402. class TagSerializeTests(unittest.TestCase):
  403. def test_serialize_simple(self):
  404. x = Tag()
  405. x.tagger = "Jelmer Vernooij <jelmer@samba.org>"
  406. x.name = "0.1"
  407. x.message = "Tag 0.1"
  408. x.object = (Blob, "d80c186a03f423a81b39df39dc87fd269736ca86")
  409. x.tag_time = 423423423
  410. x.tag_timezone = 0
  411. self.assertEquals("""object d80c186a03f423a81b39df39dc87fd269736ca86
  412. type blob
  413. tag 0.1
  414. tagger Jelmer Vernooij <jelmer@samba.org> 423423423 +0000
  415. Tag 0.1""", x.as_raw_string())
  416. default_tagger = ('Linus Torvalds <torvalds@woody.linux-foundation.org> '
  417. '1183319674 -0700')
  418. default_message = """Linux 2.6.22-rc7
  419. -----BEGIN PGP SIGNATURE-----
  420. Version: GnuPG v1.4.7 (GNU/Linux)
  421. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  422. OK2XeQOiEeXtT76rV4t2WR4=
  423. =ivrA
  424. -----END PGP SIGNATURE-----
  425. """
  426. class TagParseTests(ShaFileCheckTests):
  427. def make_tag_lines(self,
  428. object_sha="a38d6181ff27824c79fc7df825164a212eff6a3f",
  429. object_type_name="commit",
  430. name="v2.6.22-rc7",
  431. tagger=default_tagger,
  432. message=default_message):
  433. lines = []
  434. if object_sha is not None:
  435. lines.append("object %s" % object_sha)
  436. if object_type_name is not None:
  437. lines.append("type %s" % object_type_name)
  438. if name is not None:
  439. lines.append("tag %s" % name)
  440. if tagger is not None:
  441. lines.append("tagger %s" % tagger)
  442. lines.append("")
  443. if message is not None:
  444. lines.append(message)
  445. return lines
  446. def make_tag_text(self, **kwargs):
  447. return "\n".join(self.make_tag_lines(**kwargs))
  448. def test_parse(self):
  449. x = Tag()
  450. x.set_raw_string(self.make_tag_text())
  451. self.assertEquals(
  452. "Linus Torvalds <torvalds@woody.linux-foundation.org>", x.tagger)
  453. self.assertEquals("v2.6.22-rc7", x.name)
  454. object_type, object_sha = x.object
  455. self.assertEquals("a38d6181ff27824c79fc7df825164a212eff6a3f",
  456. object_sha)
  457. self.assertEquals(Commit, object_type)
  458. self.assertEquals(datetime.datetime.utcfromtimestamp(x.tag_time),
  459. datetime.datetime(2007, 7, 1, 19, 54, 34))
  460. self.assertEquals(-25200, x.tag_timezone)
  461. def test_parse_no_tagger(self):
  462. x = Tag()
  463. x.set_raw_string(self.make_tag_text(tagger=None))
  464. self.assertEquals(None, x.tagger)
  465. self.assertEquals("v2.6.22-rc7", x.name)
  466. def test_check(self):
  467. self.assertCheckSucceeds(Tag, self.make_tag_text())
  468. self.assertCheckFails(Tag, self.make_tag_text(object_sha=None))
  469. self.assertCheckFails(Tag, self.make_tag_text(object_type_name=None))
  470. self.assertCheckFails(Tag, self.make_tag_text(name=None))
  471. self.assertCheckFails(Tag, self.make_tag_text(name=''))
  472. self.assertCheckFails(Tag, self.make_tag_text(
  473. object_type_name="foobar"))
  474. self.assertCheckFails(Tag, self.make_tag_text(
  475. tagger="some guy without an email address 1183319674 -0700"))
  476. self.assertCheckFails(Tag, self.make_tag_text(
  477. tagger=("Linus Torvalds <torvalds@woody.linux-foundation.org> "
  478. "Sun 7 Jul 2007 12:54:34 +0700")))
  479. self.assertCheckFails(Tag, self.make_tag_text(object_sha="xxx"))
  480. def test_check_duplicates(self):
  481. # duplicate each of the header fields
  482. for i in xrange(4):
  483. lines = self.make_tag_lines()
  484. lines.insert(i, lines[i])
  485. self.assertCheckFails(Tag, '\n'.join(lines))
  486. def test_check_order(self):
  487. lines = self.make_tag_lines()
  488. headers = lines[:4]
  489. rest = lines[4:]
  490. # of all possible permutations, ensure only the original succeeds
  491. for perm in permutations(headers):
  492. perm = list(perm)
  493. text = '\n'.join(perm + rest)
  494. if perm == headers:
  495. self.assertCheckSucceeds(Tag, text)
  496. else:
  497. self.assertCheckFails(Tag, text)
  498. class CheckTests(unittest.TestCase):
  499. def test_check_hexsha(self):
  500. check_hexsha(a_sha, "failed to check good sha")
  501. self.assertRaises(ObjectFormatException, check_hexsha, '1' * 39,
  502. 'sha too short')
  503. self.assertRaises(ObjectFormatException, check_hexsha, '1' * 41,
  504. 'sha too long')
  505. self.assertRaises(ObjectFormatException, check_hexsha, 'x' * 40,
  506. 'invalid characters')
  507. def test_check_identity(self):
  508. check_identity("Dave Borowitz <dborowitz@google.com>",
  509. "failed to check good identity")
  510. check_identity("<dborowitz@google.com>",
  511. "failed to check good identity")
  512. self.assertRaises(ObjectFormatException, check_identity,
  513. "Dave Borowitz", "no email")
  514. self.assertRaises(ObjectFormatException, check_identity,
  515. "Dave Borowitz <dborowitz", "incomplete email")
  516. self.assertRaises(ObjectFormatException, check_identity,
  517. "dborowitz@google.com>", "incomplete email")
  518. self.assertRaises(ObjectFormatException, check_identity,
  519. "Dave Borowitz <<dborowitz@google.com>", "typo")
  520. self.assertRaises(ObjectFormatException, check_identity,
  521. "Dave Borowitz <dborowitz@google.com>>", "typo")
  522. self.assertRaises(ObjectFormatException, check_identity,
  523. "Dave Borowitz <dborowitz@google.com>xxx",
  524. "trailing characters")
  525. class TimezoneTests(unittest.TestCase):
  526. def test_parse_timezone_utc(self):
  527. self.assertEquals((0, False), parse_timezone("+0000"))
  528. def test_parse_timezone_utc_negative(self):
  529. self.assertEquals((0, True), parse_timezone("-0000"))
  530. def test_generate_timezone_utc(self):
  531. self.assertEquals("+0000", format_timezone(0))
  532. def test_generate_timezone_utc_negative(self):
  533. self.assertEquals("-0000", format_timezone(0, True))
  534. def test_parse_timezone_cet(self):
  535. self.assertEquals((60 * 60, False), parse_timezone("+0100"))
  536. def test_format_timezone_cet(self):
  537. self.assertEquals("+0100", format_timezone(60 * 60))
  538. def test_format_timezone_pdt(self):
  539. self.assertEquals("-0400", format_timezone(-4 * 60 * 60))
  540. def test_parse_timezone_pdt(self):
  541. self.assertEquals((-4 * 60 * 60, False), parse_timezone("-0400"))
  542. def test_format_timezone_pdt_half(self):
  543. self.assertEquals("-0440",
  544. format_timezone(int(((-4 * 60) - 40) * 60)))
  545. def test_parse_timezone_pdt_half(self):
  546. self.assertEquals((((-4 * 60) - 40) * 60, False),
  547. parse_timezone("-0440"))