2
0

test_repository.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 git.errors import (NotCommitError,
  21. NotTreeError,
  22. NotBlobError,
  23. )
  24. from git.repository import Repository
  25. class RepositoryTests(unittest.TestCase):
  26. def open_repo(self, name):
  27. return Repository(os.path.join(os.path.dirname(__file__),
  28. 'data/repos', name, '.git'))
  29. def test_simple_props(self):
  30. r = self.open_repo('a')
  31. basedir = os.path.join(os.path.dirname(__file__), 'data/repos/a/.git')
  32. self.assertEqual(r.basedir(), basedir)
  33. self.assertEqual(r.object_dir(), os.path.join(basedir, 'objects'))
  34. def test_ref(self):
  35. r = self.open_repo('a')
  36. self.assertEqual(r.ref('master'),
  37. 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  38. def test_head(self):
  39. r = self.open_repo('a')
  40. self.assertEqual(r.head(), 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  41. def test_get_object(self):
  42. r = self.open_repo('a')
  43. obj = r.get_object(r.head())
  44. self.assertEqual(obj._type, 'commit')
  45. def test_get_object_non_existant(self):
  46. r = self.open_repo('a')
  47. obj = r.get_object('b91fa4d900g17e99b433218e988c4eb4a3e9a097')
  48. self.assertEqual(obj, None)
  49. def test_get_commit(self):
  50. r = self.open_repo('a')
  51. obj = r.get_commit(r.head())
  52. self.assertEqual(obj._type, 'commit')
  53. def test_get_commit_not_commit(self):
  54. r = self.open_repo('a')
  55. self.assertRaises(NotCommitError,
  56. r.get_commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
  57. def test_get_tree(self):
  58. r = self.open_repo('a')
  59. commit = r.get_commit(r.head())
  60. tree = r.get_tree(commit.tree())
  61. self.assertEqual(tree._type, 'tree')
  62. self.assertEqual(tree.sha().hexdigest(), commit.tree())
  63. def test_get_tree_not_tree(self):
  64. r = self.open_repo('a')
  65. self.assertRaises(NotTreeError, r.get_tree, r.head())
  66. def test_get_blob(self):
  67. r = self.open_repo('a')
  68. commit = r.get_commit(r.head())
  69. tree = r.get_tree(commit.tree())
  70. blob_sha = tree.entries()[0][2]
  71. blob = r.get_blob(blob_sha)
  72. self.assertEqual(blob._type, 'blob')
  73. self.assertEqual(blob.sha().hexdigest(), blob_sha)
  74. def test_get_blob(self):
  75. r = self.open_repo('a')
  76. self.assertRaises(NotBlobError, r.get_blob, r.head())