You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.5KB

  1. # -*- coding: utf-8 -*-
  2. # test_base.py
  3. # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
  4. #
  5. # This module is part of GitPython and is released under
  6. # the BSD License: http://www.opensource.org/licenses/bsd-license.php
  7. import os
  8. import sys
  9. import tempfile
  10. from unittest import SkipTest, skipIf
  11. from git import (
  12. Blob,
  13. Tree,
  14. Commit,
  15. TagObject
  16. )
  17. from git.compat import is_win
  18. from git.objects.util import get_object_type_by_name
  19. from git.test.lib import (
  20. TestBase,
  21. assert_raises,
  22. with_rw_repo,
  23. with_rw_and_rw_remote_repo
  24. )
  25. from git.util import hex_to_bin
  26. import git.objects.base as base
  27. import os.path as osp
  28. class TestBase(TestBase):
  29. def tearDown(self):
  30. import gc
  31. gc.collect()
  32. type_tuples = (("blob", "8741fc1d09d61f02ffd8cded15ff603eff1ec070", "blob.py"),
  33. ("tree", "3a6a5e3eeed3723c09f1ef0399f81ed6b8d82e79", "directory"),
  34. ("commit", "4251bd59fb8e11e40c40548cba38180a9536118c", None),
  35. ("tag", "e56a60e8e9cd333cfba0140a77cd12b0d9398f10", None))
  36. def test_base_object(self):
  37. # test interface of base object classes
  38. types = (Blob, Tree, Commit, TagObject)
  39. self.assertEqual(len(types), len(self.type_tuples))
  40. s = set()
  41. num_objs = 0
  42. num_index_objs = 0
  43. for obj_type, (typename, hexsha, path) in zip(types, self.type_tuples):
  44. binsha = hex_to_bin(hexsha)
  45. item = None
  46. if path is None:
  47. item = obj_type(self.rorepo, binsha)
  48. else:
  49. item = obj_type(self.rorepo, binsha, 0, path)
  50. # END handle index objects
  51. num_objs += 1
  52. self.assertEqual(item.hexsha, hexsha)
  53. self.assertEqual(item.type, typename)
  54. assert item.size
  55. self.assertEqual(item, item)
  56. self.assertNotEqual(not item, item)
  57. self.assertEqual(str(item), item.hexsha)
  58. assert repr(item)
  59. s.add(item)
  60. if isinstance(item, base.IndexObject):
  61. num_index_objs += 1
  62. if hasattr(item, 'path'): # never runs here
  63. assert not item.path.startswith("/") # must be relative
  64. assert isinstance(item.mode, int)
  65. # END index object check
  66. # read from stream
  67. data_stream = item.data_stream
  68. data = data_stream.read()
  69. assert data
  70. tmpfilename = tempfile.mktemp(suffix='test-stream')
  71. with open(tmpfilename, 'wb+') as tmpfile:
  72. self.assertEqual(item, item.stream_data(tmpfile))
  73. tmpfile.seek(0)
  74. self.assertEqual(tmpfile.read(), data)
  75. os.remove(tmpfilename)
  76. # END for each object type to create
  77. # each has a unique sha
  78. self.assertEqual(len(s), num_objs)
  79. self.assertEqual(len(s | s), num_objs)
  80. self.assertEqual(num_index_objs, 2)
  81. def test_get_object_type_by_name(self):
  82. for tname in base.Object.TYPES:
  83. assert base.Object in get_object_type_by_name(tname).mro()
  84. # END for each known type
  85. assert_raises(ValueError, get_object_type_by_name, b"doesntexist")
  86. def test_object_resolution(self):
  87. # objects must be resolved to shas so they compare equal
  88. self.assertEqual(self.rorepo.head.reference.object, self.rorepo.active_branch.object)
  89. @with_rw_repo('HEAD', bare=True)
  90. def test_with_bare_rw_repo(self, bare_rw_repo):
  91. assert bare_rw_repo.config_reader("repository").getboolean("core", "bare")
  92. assert osp.isfile(osp.join(bare_rw_repo.git_dir, 'HEAD'))
  93. @with_rw_repo('0.1.6')
  94. def test_with_rw_repo(self, rw_repo):
  95. assert not rw_repo.config_reader("repository").getboolean("core", "bare")
  96. assert osp.isdir(osp.join(rw_repo.working_tree_dir, 'lib'))
  97. #@skipIf(HIDE_WINDOWS_FREEZE_ERRORS, "FIXME: Freezes! sometimes...")
  98. @with_rw_and_rw_remote_repo('0.1.6')
  99. def test_with_rw_remote_and_rw_repo(self, rw_repo, rw_remote_repo):
  100. assert not rw_repo.config_reader("repository").getboolean("core", "bare")
  101. assert rw_remote_repo.config_reader("repository").getboolean("core", "bare")
  102. assert osp.isdir(osp.join(rw_repo.working_tree_dir, 'lib'))
  103. @skipIf(sys.version_info < (3,) and is_win,
  104. "Unicode woes, see https://github.com/gitpython-developers/GitPython/pull/519")
  105. @with_rw_repo('0.1.6')
  106. def test_add_unicode(self, rw_repo):
  107. filename = u"שלום.txt"
  108. file_path = osp.join(rw_repo.working_dir, filename)
  109. # verify first that we could encode file name in this environment
  110. try:
  111. file_path.encode(sys.getfilesystemencoding())
  112. except UnicodeEncodeError:
  113. raise SkipTest("Environment doesn't support unicode filenames")
  114. with open(file_path, "wb") as fp:
  115. fp.write(b'something')
  116. if is_win:
  117. # on windows, there is no way this works, see images on
  118. # https://github.com/gitpython-developers/GitPython/issues/147#issuecomment-68881897
  119. # Therefore, it must be added using the python implementation
  120. rw_repo.index.add([file_path])
  121. # However, when the test winds down, rmtree fails to delete this file, which is recognized
  122. # as ??? only.
  123. else:
  124. # on posix, we can just add unicode files without problems
  125. rw_repo.git.add(rw_repo.working_dir)
  126. # end
  127. rw_repo.index.commit('message')