2
0

test_objects.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. import os
  19. import unittest
  20. from git.objects import (Blob,
  21. Tree,
  22. Commit,
  23. )
  24. a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  25. b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
  26. c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
  27. tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
  28. class BlobReadTests(unittest.TestCase):
  29. """Test decompression of blobs"""
  30. def get_sha_file(self, obj, base, sha):
  31. return obj.from_file(os.path.join(os.path.dirname(__file__),
  32. 'data', base, sha))
  33. def get_blob(self, sha):
  34. """Return the blob named sha from the test data dir"""
  35. return self.get_sha_file(Blob, 'blobs', sha)
  36. def get_tree(self, sha):
  37. return self.get_sha_file(Tree, 'trees', sha)
  38. def get_commit(self, sha):
  39. return self.get_sha_file(Commit, 'commits', sha)
  40. def test_decompress_simple_blob(self):
  41. b = self.get_blob(a_sha)
  42. self.assertEqual(b.text(), 'test 1\n')
  43. self.assertEqual(b.sha().hexdigest(), a_sha)
  44. def test_parse_empty_blob_object(self):
  45. sha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
  46. b = self.get_blob(sha)
  47. self.assertEqual(b.text(), '')
  48. self.assertEqual(b.sha().hexdigest(), sha)
  49. def test_create_blob_from_string(self):
  50. string = 'test 2\n'
  51. b = Blob.from_string(string)
  52. self.assertEqual(b.text(), string)
  53. self.assertEqual(b.sha().hexdigest(), b_sha)
  54. def test_parse_legacy_blob(self):
  55. string = 'test 3\n'
  56. b = self.get_blob(c_sha)
  57. self.assertEqual(b.text(), string)
  58. self.assertEqual(b.sha().hexdigest(), c_sha)
  59. def test_eq(self):
  60. blob1 = self.get_blob(a_sha)
  61. blob2 = self.get_blob(a_sha)
  62. self.assertEqual(blob1, blob2)
  63. def test_read_tree_from_file(self):
  64. t = self.get_tree(tree_sha)
  65. self.assertEqual(t.entries()[0], (33188, 'a', a_sha))
  66. self.assertEqual(t.entries()[1], (33188, 'b', b_sha))
  67. def test_read_commit_from_file(self):
  68. sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
  69. c = self.get_commit(sha)
  70. self.assertEqual(c.tree(), tree_sha)
  71. self.assertEqual(c.parents(), ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
  72. self.assertEqual(c.author(),
  73. 'James Westby <jw+debian@jameswestby.net>')
  74. self.assertEqual(c.committer(),
  75. 'James Westby <jw+debian@jameswestby.net>')
  76. self.assertEqual(c.commit_time(), 1174759230)
  77. self.assertEqual(c.message(), 'Test commit\n')
  78. def test_read_commit_no_parents(self):
  79. sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
  80. c = self.get_commit(sha)
  81. self.assertEqual(c.tree(), '90182552c4a85a45ec2a835cadc3451bebdfe870')
  82. self.assertEqual(c.parents(), [])
  83. self.assertEqual(c.author(),
  84. 'James Westby <jw+debian@jameswestby.net>')
  85. self.assertEqual(c.committer(),
  86. 'James Westby <jw+debian@jameswestby.net>')
  87. self.assertEqual(c.commit_time(), 1174758034)
  88. self.assertEqual(c.message(), 'Test commit\n')
  89. def test_read_commit_two_parents(self):
  90. sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
  91. c = self.get_commit(sha)
  92. self.assertEqual(c.tree(), 'd80c186a03f423a81b39df39dc87fd269736ca86')
  93. self.assertEqual(c.parents(), ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  94. '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
  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(), 1174773719)
  100. self.assertEqual(c.message(), 'Merge ../b\n')