test_objects.py 5.3 KB

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