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.

109 lines
4.0KB

  1. # test_tree.py
  2. # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
  3. #
  4. # This module is part of GitPython and is released under
  5. # the BSD License: http://www.opensource.org/licenses/bsd-license.php
  6. from io import BytesIO
  7. import sys
  8. from unittest import skipIf
  9. from git import (
  10. Tree,
  11. Blob
  12. )
  13. from git.test.lib import TestBase
  14. from git.util import HIDE_WINDOWS_KNOWN_ERRORS
  15. import os.path as osp
  16. class TestTree(TestBase):
  17. @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and sys.version_info[:2] == (3, 5), """
  18. File "C:\\projects\\gitpython\\git\\cmd.py", line 559, in execute
  19. raise GitCommandNotFound(command, err)
  20. git.exc.GitCommandNotFound: Cmd('git') not found due to: OSError('[WinError 6] The handle is invalid')
  21. cmdline: git cat-file --batch-check""")
  22. def test_serializable(self):
  23. # tree at the given commit contains a submodule as well
  24. roottree = self.rorepo.tree('6c1faef799095f3990e9970bc2cb10aa0221cf9c')
  25. for item in roottree.traverse(ignore_self=False):
  26. if item.type != Tree.type:
  27. continue
  28. # END skip non-trees
  29. tree = item
  30. # trees have no dict
  31. self.failUnlessRaises(AttributeError, setattr, tree, 'someattr', 1)
  32. orig_data = tree.data_stream.read()
  33. orig_cache = tree._cache
  34. stream = BytesIO()
  35. tree._serialize(stream)
  36. assert stream.getvalue() == orig_data
  37. stream.seek(0)
  38. testtree = Tree(self.rorepo, Tree.NULL_BIN_SHA, 0, '')
  39. testtree._deserialize(stream)
  40. assert testtree._cache == orig_cache
  41. # replaces cache, but we make sure of it
  42. del(testtree._cache)
  43. testtree._deserialize(stream)
  44. # END for each item in tree
  45. @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and sys.version_info[:2] == (3, 5), """
  46. File "C:\\projects\\gitpython\\git\\cmd.py", line 559, in execute
  47. raise GitCommandNotFound(command, err)
  48. git.exc.GitCommandNotFound: Cmd('git') not found due to: OSError('[WinError 6] The handle is invalid')
  49. cmdline: git cat-file --batch-check""")
  50. def test_traverse(self):
  51. root = self.rorepo.tree('0.1.6')
  52. num_recursive = 0
  53. all_items = []
  54. for obj in root.traverse():
  55. if "/" in obj.path:
  56. num_recursive += 1
  57. assert isinstance(obj, (Blob, Tree))
  58. all_items.append(obj)
  59. # END for each object
  60. assert all_items == root.list_traverse()
  61. # limit recursion level to 0 - should be same as default iteration
  62. assert all_items
  63. assert 'CHANGES' in root
  64. assert len(list(root)) == len(list(root.traverse(depth=1)))
  65. # only choose trees
  66. trees_only = lambda i, d: i.type == "tree"
  67. trees = list(root.traverse(predicate=trees_only))
  68. assert len(trees) == len([i for i in root.traverse() if trees_only(i, 0)])
  69. # test prune
  70. lib_folder = lambda t, d: t.path == "lib"
  71. pruned_trees = list(root.traverse(predicate=trees_only, prune=lib_folder))
  72. assert len(pruned_trees) < len(trees)
  73. # trees and blobs
  74. assert len(set(trees) | set(root.trees)) == len(trees)
  75. assert len({b for b in root if isinstance(b, Blob)} | set(root.blobs)) == len(root.blobs)
  76. subitem = trees[0][0]
  77. assert "/" in subitem.path
  78. assert subitem.name == osp.basename(subitem.path)
  79. # assure that at some point the traversed paths have a slash in them
  80. found_slash = False
  81. for item in root.traverse():
  82. assert osp.isabs(item.abspath)
  83. if '/' in item.path:
  84. found_slash = True
  85. # END check for slash
  86. # slashes in paths are supported as well
  87. # NOTE: on py3, / doesn't work with strings anymore ...
  88. assert root[item.path] == item == root / item.path
  89. # END for each item
  90. assert found_slash