test_objects.py 24 KB

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