test_repository.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # test_repository.py -- tests for repository.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 dulwich import errors
  21. from dulwich.repo import Repo
  22. missing_sha = 'b91fa4d900g17e99b433218e988c4eb4a3e9a097'
  23. class RepositoryTests(unittest.TestCase):
  24. def open_repo(self, name):
  25. return Repo(os.path.join(os.path.dirname(__file__),
  26. 'data/repos', name, '.git'))
  27. def test_simple_props(self):
  28. r = self.open_repo('a')
  29. basedir = os.path.join(os.path.dirname(__file__), 'data/repos/a/.git')
  30. self.assertEqual(r.basedir(), basedir)
  31. self.assertEqual(r.object_dir(), os.path.join(basedir, 'objects'))
  32. def test_ref(self):
  33. r = self.open_repo('a')
  34. self.assertEqual(r.ref('master'),
  35. 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  36. def test_head(self):
  37. r = self.open_repo('a')
  38. self.assertEqual(r.head(), 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  39. def test_get_object(self):
  40. r = self.open_repo('a')
  41. obj = r.get_object(r.head())
  42. self.assertEqual(obj._type, 'commit')
  43. def test_get_object_non_existant(self):
  44. r = self.open_repo('a')
  45. obj = r.get_object(missing_sha)
  46. self.assertEqual(obj, None)
  47. def test_commit(self):
  48. r = self.open_repo('a')
  49. obj = r.commit(r.head())
  50. self.assertEqual(obj._type, 'commit')
  51. def test_commit_not_commit(self):
  52. r = self.open_repo('a')
  53. self.assertRaises(errors.NotCommitError,
  54. r.commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
  55. def test_tree(self):
  56. r = self.open_repo('a')
  57. commit = r.commit(r.head())
  58. tree = r.tree(commit.tree)
  59. self.assertEqual(tree._type, 'tree')
  60. self.assertEqual(tree.sha().hexdigest(), commit.tree)
  61. def test_tree_not_tree(self):
  62. r = self.open_repo('a')
  63. self.assertRaises(errors.NotTreeError, r.tree, r.head())
  64. def test_get_blob(self):
  65. r = self.open_repo('a')
  66. commit = r.commit(r.head())
  67. tree = r.tree(commit.tree())
  68. blob_sha = tree.entries()[0][2]
  69. blob = r.get_blob(blob_sha)
  70. self.assertEqual(blob._type, 'blob')
  71. self.assertEqual(blob.sha().hexdigest(), blob_sha)
  72. def test_get_blob(self):
  73. r = self.open_repo('a')
  74. self.assertRaises(errors.NotBlobError, r.get_blob, r.head())
  75. def test_linear_history(self):
  76. r = self.open_repo('a')
  77. history = r.revision_history(r.head())
  78. shas = [c.sha().hexdigest() for c in history]
  79. self.assertEqual(shas, [r.head(),
  80. '2a72d929692c41d8554c07f6301757ba18a65d91'])
  81. def test_merge_history(self):
  82. r = self.open_repo('simple_merge')
  83. history = r.revision_history(r.head())
  84. shas = [c.sha().hexdigest() for c in history]
  85. self.assertEqual(shas, ['5dac377bdded4c9aeb8dff595f0faeebcc8498cc',
  86. 'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  87. '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6',
  88. '60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  89. '0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
  90. def test_revision_history_missing_commit(self):
  91. r = self.open_repo('simple_merge')
  92. self.assertRaises(errors.MissingCommitError, r.revision_history,
  93. missing_sha)
  94. def test_out_of_order_merge(self):
  95. """Test that revision history is ordered by date, not parent order."""
  96. r = self.open_repo('ooo_merge')
  97. history = r.revision_history(r.head())
  98. shas = [c.sha().hexdigest() for c in history]
  99. self.assertEqual(shas, ['7601d7f6231db6a57f7bbb79ee52e4d462fd44d1',
  100. 'f507291b64138b875c28e03469025b1ea20bc614',
  101. 'fb5b0425c7ce46959bec94d54b9a157645e114f5',
  102. 'f9e39b120c68182a4ba35349f832d0e4e61f485c'])