2
0

test_utils.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # test_utils.py -- Tests for git test utilities.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for git test utilities."""
  22. from dulwich.object_store import MemoryObjectStore
  23. from dulwich.objects import Blob
  24. from dulwich.tests.utils import build_commit_graph, make_object
  25. from . import TestCase
  26. class BuildCommitGraphTest(TestCase):
  27. def setUp(self) -> None:
  28. super().setUp()
  29. self.store = MemoryObjectStore()
  30. def test_linear(self) -> None:
  31. c1, c2 = build_commit_graph(self.store, [[1], [2, 1]])
  32. for obj_id in [c1.id, c2.id, c1.tree, c2.tree]:
  33. self.assertIn(obj_id, self.store)
  34. self.assertEqual([], c1.parents)
  35. self.assertEqual([c1.id], c2.parents)
  36. self.assertEqual(c1.tree, c2.tree)
  37. self.assertEqual([], self.store[c1.tree].items())
  38. self.assertGreater(c2.commit_time, c1.commit_time)
  39. def test_merge(self) -> None:
  40. c1, c2, c3, c4 = build_commit_graph(
  41. self.store, [[1], [2, 1], [3, 1], [4, 2, 3]]
  42. )
  43. self.assertEqual([c2.id, c3.id], c4.parents)
  44. self.assertGreater(c4.commit_time, c2.commit_time)
  45. self.assertGreater(c4.commit_time, c3.commit_time)
  46. def test_missing_parent(self) -> None:
  47. self.assertRaises(
  48. ValueError, build_commit_graph, self.store, [[1], [3, 2], [2, 1]]
  49. )
  50. def test_trees(self) -> None:
  51. a1 = make_object(Blob, data=b"aaa1")
  52. a2 = make_object(Blob, data=b"aaa2")
  53. c1, c2 = build_commit_graph(
  54. self.store,
  55. [[1], [2, 1]],
  56. trees={1: [(b"a", a1)], 2: [(b"a", a2, 0o100644)]},
  57. )
  58. self.assertEqual((0o100644, a1.id), self.store[c1.tree][b"a"])
  59. self.assertEqual((0o100644, a2.id), self.store[c2.tree][b"a"])
  60. def test_attrs(self) -> None:
  61. c1, c2 = build_commit_graph(
  62. self.store, [[1], [2, 1]], attrs={1: {"message": b"Hooray!"}}
  63. )
  64. self.assertEqual(b"Hooray!", c1.message)
  65. self.assertEqual(b"Commit 2", c2.message)
  66. def test_commit_time(self) -> None:
  67. c1, c2, c3 = build_commit_graph(
  68. self.store,
  69. [[1], [2, 1], [3, 2]],
  70. attrs={1: {"commit_time": 124}, 2: {"commit_time": 123}},
  71. )
  72. self.assertEqual(124, c1.commit_time)
  73. self.assertEqual(123, c2.commit_time)
  74. self.assertTrue(c2.commit_time < c1.commit_time < c3.commit_time)