test_objects.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 unittest
  21. from dulwich.objects import (
  22. Blob,
  23. Tree,
  24. Commit,
  25. Tag,
  26. )
  27. a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  28. b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
  29. c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
  30. tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
  31. tag_sha = '71033db03a03c6a36721efcf1968dd8f8e0cf023'
  32. class BlobReadTests(unittest.TestCase):
  33. """Test decompression of blobs"""
  34. def get_sha_file(self, obj, base, sha):
  35. return obj.from_file(os.path.join(os.path.dirname(__file__),
  36. 'data', base, sha))
  37. def get_blob(self, sha):
  38. """Return the blob named sha from the test data dir"""
  39. return self.get_sha_file(Blob, 'blobs', sha)
  40. def get_tree(self, sha):
  41. return self.get_sha_file(Tree, 'trees', sha)
  42. def get_tag(self, sha):
  43. return self.get_sha_file(Tag, 'tags', sha)
  44. def commit(self, sha):
  45. return self.get_sha_file(Commit, 'commits', sha)
  46. def test_decompress_simple_blob(self):
  47. b = self.get_blob(a_sha)
  48. self.assertEqual(b.data, 'test 1\n')
  49. self.assertEqual(b.sha().hexdigest(), a_sha)
  50. def test_parse_empty_blob_object(self):
  51. sha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
  52. b = self.get_blob(sha)
  53. self.assertEqual(b.data, '')
  54. self.assertEqual(b.sha().hexdigest(), sha)
  55. def test_create_blob_from_string(self):
  56. string = 'test 2\n'
  57. b = Blob.from_string(string)
  58. self.assertEqual(b.data, string)
  59. self.assertEqual(b.sha().hexdigest(), b_sha)
  60. def test_parse_legacy_blob(self):
  61. string = 'test 3\n'
  62. b = self.get_blob(c_sha)
  63. self.assertEqual(b.data, string)
  64. self.assertEqual(b.sha().hexdigest(), c_sha)
  65. def test_eq(self):
  66. blob1 = self.get_blob(a_sha)
  67. blob2 = self.get_blob(a_sha)
  68. self.assertEqual(blob1, blob2)
  69. def test_read_tree_from_file(self):
  70. t = self.get_tree(tree_sha)
  71. self.assertEqual(t.entries()[0], (33188, 'a', a_sha))
  72. self.assertEqual(t.entries()[1], (33188, 'b', b_sha))
  73. def test_read_tag_from_file(self):
  74. t = self.get_tag(tag_sha)
  75. self.assertEqual(t.object, (Commit, '51b668fd5bf7061b7d6fa525f88803e6cfadaa51'))
  76. self.assertEqual(t.name,'signed')
  77. self.assertEqual(t.tagger,'Ali Sabil <ali.sabil@gmail.com>')
  78. self.assertEqual(t.tag_time, 1231203091)
  79. 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')
  80. def test_read_commit_from_file(self):
  81. sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
  82. c = self.commit(sha)
  83. self.assertEqual(c.tree, tree_sha)
  84. self.assertEqual(c.parents, ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
  85. self.assertEqual(c.author,
  86. 'James Westby <jw+debian@jameswestby.net>')
  87. self.assertEqual(c.committer,
  88. 'James Westby <jw+debian@jameswestby.net>')
  89. self.assertEqual(c.commit_time, 1174759230)
  90. self.assertEqual(c.commit_timezone, 0)
  91. self.assertEqual(c.author_timezone, 0)
  92. self.assertEqual(c.message, 'Test commit\n')
  93. def test_read_commit_no_parents(self):
  94. sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
  95. c = self.commit(sha)
  96. self.assertEqual(c.tree, '90182552c4a85a45ec2a835cadc3451bebdfe870')
  97. self.assertEqual(c.parents, [])
  98. self.assertEqual(c.author,
  99. 'James Westby <jw+debian@jameswestby.net>')
  100. self.assertEqual(c.committer,
  101. 'James Westby <jw+debian@jameswestby.net>')
  102. self.assertEqual(c.commit_time, 1174758034)
  103. self.assertEqual(c.commit_timezone, 0)
  104. self.assertEqual(c.author_timezone, 0)
  105. self.assertEqual(c.message, 'Test commit\n')
  106. def test_read_commit_two_parents(self):
  107. sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
  108. c = self.commit(sha)
  109. self.assertEqual(c.tree, 'd80c186a03f423a81b39df39dc87fd269736ca86')
  110. self.assertEqual(c.parents, ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  111. '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
  112. self.assertEqual(c.author,
  113. 'James Westby <jw+debian@jameswestby.net>')
  114. self.assertEqual(c.committer,
  115. 'James Westby <jw+debian@jameswestby.net>')
  116. self.assertEqual(c.commit_time, 1174773719)
  117. self.assertEqual(c.commit_timezone, 0)
  118. self.assertEqual(c.author_timezone, 0)
  119. self.assertEqual(c.message, 'Merge ../b\n')