test_objects.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. import os
  20. import stat
  21. import unittest
  22. from dulwich.objects import (
  23. Blob,
  24. Tree,
  25. Commit,
  26. Tag,
  27. hex_to_sha,
  28. )
  29. a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  30. b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
  31. c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
  32. tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
  33. tag_sha = '71033db03a03c6a36721efcf1968dd8f8e0cf023'
  34. class BlobReadTests(unittest.TestCase):
  35. """Test decompression of blobs"""
  36. def get_sha_file(self, obj, base, sha):
  37. return obj.from_file(os.path.join(os.path.dirname(__file__),
  38. 'data', base, sha))
  39. def get_blob(self, sha):
  40. """Return the blob named sha from the test data dir"""
  41. return self.get_sha_file(Blob, 'blobs', sha)
  42. def get_tree(self, sha):
  43. return self.get_sha_file(Tree, 'trees', sha)
  44. def get_tag(self, sha):
  45. return self.get_sha_file(Tag, 'tags', sha)
  46. def commit(self, sha):
  47. return self.get_sha_file(Commit, 'commits', sha)
  48. def test_decompress_simple_blob(self):
  49. b = self.get_blob(a_sha)
  50. self.assertEqual(b.data, 'test 1\n')
  51. self.assertEqual(b.sha().hexdigest(), a_sha)
  52. def test_parse_empty_blob_object(self):
  53. sha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
  54. b = self.get_blob(sha)
  55. self.assertEqual(b.data, '')
  56. self.assertEqual(b.id, sha)
  57. self.assertEqual(b.sha().hexdigest(), sha)
  58. def test_create_blob_from_string(self):
  59. string = 'test 2\n'
  60. b = Blob.from_string(string)
  61. self.assertEqual(b.data, string)
  62. self.assertEqual(b.sha().hexdigest(), b_sha)
  63. def test_parse_legacy_blob(self):
  64. string = 'test 3\n'
  65. b = self.get_blob(c_sha)
  66. self.assertEqual(b.data, string)
  67. self.assertEqual(b.sha().hexdigest(), c_sha)
  68. def test_eq(self):
  69. blob1 = self.get_blob(a_sha)
  70. blob2 = self.get_blob(a_sha)
  71. self.assertEqual(blob1, blob2)
  72. def test_read_tree_from_file(self):
  73. t = self.get_tree(tree_sha)
  74. self.assertEqual(t.entries()[0], (33188, 'a', a_sha))
  75. self.assertEqual(t.entries()[1], (33188, 'b', b_sha))
  76. def test_read_tag_from_file(self):
  77. t = self.get_tag(tag_sha)
  78. self.assertEqual(t.object, (Commit, '51b668fd5bf7061b7d6fa525f88803e6cfadaa51'))
  79. self.assertEqual(t.name,'signed')
  80. self.assertEqual(t.tagger,'Ali Sabil <ali.sabil@gmail.com>')
  81. self.assertEqual(t.tag_time, 1231203091)
  82. 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')
  83. def test_read_commit_from_file(self):
  84. sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
  85. c = self.commit(sha)
  86. self.assertEqual(c.tree, tree_sha)
  87. self.assertEqual(c.parents, ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
  88. self.assertEqual(c.author,
  89. 'James Westby <jw+debian@jameswestby.net>')
  90. self.assertEqual(c.committer,
  91. 'James Westby <jw+debian@jameswestby.net>')
  92. self.assertEqual(c.commit_time, 1174759230)
  93. self.assertEqual(c.commit_timezone, 0)
  94. self.assertEqual(c.author_timezone, 0)
  95. self.assertEqual(c.message, 'Test commit\n')
  96. def test_read_commit_no_parents(self):
  97. sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
  98. c = self.commit(sha)
  99. self.assertEqual(c.tree, '90182552c4a85a45ec2a835cadc3451bebdfe870')
  100. self.assertEqual(c.parents, [])
  101. self.assertEqual(c.author,
  102. 'James Westby <jw+debian@jameswestby.net>')
  103. self.assertEqual(c.committer,
  104. 'James Westby <jw+debian@jameswestby.net>')
  105. self.assertEqual(c.commit_time, 1174758034)
  106. self.assertEqual(c.commit_timezone, 0)
  107. self.assertEqual(c.author_timezone, 0)
  108. self.assertEqual(c.message, 'Test commit\n')
  109. def test_read_commit_two_parents(self):
  110. sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
  111. c = self.commit(sha)
  112. self.assertEqual(c.tree, 'd80c186a03f423a81b39df39dc87fd269736ca86')
  113. self.assertEqual(c.parents, ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  114. '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
  115. self.assertEqual(c.author,
  116. 'James Westby <jw+debian@jameswestby.net>')
  117. self.assertEqual(c.committer,
  118. 'James Westby <jw+debian@jameswestby.net>')
  119. self.assertEqual(c.commit_time, 1174773719)
  120. self.assertEqual(c.commit_timezone, 0)
  121. self.assertEqual(c.author_timezone, 0)
  122. self.assertEqual(c.message, 'Merge ../b\n')
  123. class CommitSerializationTests(unittest.TestCase):
  124. def make_base(self):
  125. c = Commit()
  126. c.tree = 'd80c186a03f423a81b39df39dc87fd269736ca86'
  127. c.parents = ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd', '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6']
  128. c.author = 'James Westby <jw+debian@jameswestby.net>'
  129. c.committer = 'James Westby <jw+debian@jameswestby.net>'
  130. c.commit_time = 1174773719
  131. c.author_time = 1174773719
  132. c.commit_timezone = 0
  133. c.author_timezone = 0
  134. c.message = 'Merge ../b\n'
  135. return c
  136. def test_short_timestamp(self):
  137. c = self.make_base()
  138. c.commit_time = 30
  139. c1 = Commit()
  140. c1.set_raw_string(c.as_raw_string())
  141. self.assertEquals(30, c1.commit_time)
  142. def test_simple(self):
  143. c = self.make_base()
  144. self.assertEquals(c.id, '5dac377bdded4c9aeb8dff595f0faeebcc8498cc')
  145. self.assertEquals(
  146. 'tree d80c186a03f423a81b39df39dc87fd269736ca86\n'
  147. 'parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n'
  148. 'parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n'
  149. 'author James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
  150. 'committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
  151. '\n'
  152. 'Merge ../b\n', c.as_raw_string())
  153. def test_timezone(self):
  154. c = self.make_base()
  155. c.commit_timezone = 5 * 60
  156. self.assertTrue(" +0005\n" in c.as_raw_string())
  157. def test_neg_timezone(self):
  158. c = self.make_base()
  159. c.commit_timezone = -1 * 3600
  160. self.assertTrue(" -0100\n" in c.as_raw_string())
  161. class TreeSerializationTests(unittest.TestCase):
  162. def test_simple(self):
  163. myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86"
  164. x = Tree()
  165. x["myname"] = (0100755, myhexsha)
  166. self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha),
  167. x.as_raw_string())
  168. def test_tree_dir_sort(self):
  169. x = Tree()
  170. x["a.c"] = (0100755, "d80c186a03f423a81b39df39dc87fd269736ca86")
  171. x["a"] = (stat.S_IFDIR, "d80c186a03f423a81b39df39dc87fd269736ca86")
  172. x["a/c"] = (stat.S_IFDIR, "d80c186a03f423a81b39df39dc87fd269736ca86")
  173. self.assertEquals(["a.c", "a", "a/c"], [p[0] for p in x.iteritems()])