test_reflog.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # test_reflog.py -- tests for reflog.py
  2. # Copyright (C) 2015 Jelmer Vernooij <jelmer@jelmer.uk>
  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 dulwich.reflog."""
  22. from io import BytesIO
  23. from dulwich.objects import ZERO_SHA
  24. from dulwich.reflog import (
  25. drop_reflog_entry,
  26. format_reflog_line,
  27. parse_reflog_line,
  28. read_reflog,
  29. )
  30. from . import TestCase
  31. class ReflogLineTests(TestCase):
  32. def test_format(self) -> None:
  33. self.assertEqual(
  34. b"0000000000000000000000000000000000000000 "
  35. b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
  36. b"<jelmer@jelmer.uk> 1446552482 +0000 "
  37. b"clone: from git://jelmer.uk/samba",
  38. format_reflog_line(
  39. b"0000000000000000000000000000000000000000",
  40. b"49030649db3dfec5a9bc03e5dde4255a14499f16",
  41. b"Jelmer Vernooij <jelmer@jelmer.uk>",
  42. 1446552482,
  43. 0,
  44. b"clone: from git://jelmer.uk/samba",
  45. ),
  46. )
  47. self.assertEqual(
  48. b"0000000000000000000000000000000000000000 "
  49. b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
  50. b"<jelmer@jelmer.uk> 1446552482 +0000 "
  51. b"clone: from git://jelmer.uk/samba",
  52. format_reflog_line(
  53. None,
  54. b"49030649db3dfec5a9bc03e5dde4255a14499f16",
  55. b"Jelmer Vernooij <jelmer@jelmer.uk>",
  56. 1446552482,
  57. 0,
  58. b"clone: from git://jelmer.uk/samba",
  59. ),
  60. )
  61. def test_parse(self) -> None:
  62. reflog_line = (
  63. b"0000000000000000000000000000000000000000 "
  64. b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
  65. b"<jelmer@jelmer.uk> 1446552482 +0000 "
  66. b"clone: from git://jelmer.uk/samba"
  67. )
  68. self.assertEqual(
  69. (
  70. b"0000000000000000000000000000000000000000",
  71. b"49030649db3dfec5a9bc03e5dde4255a14499f16",
  72. b"Jelmer Vernooij <jelmer@jelmer.uk>",
  73. 1446552482,
  74. 0,
  75. b"clone: from git://jelmer.uk/samba",
  76. ),
  77. parse_reflog_line(reflog_line),
  78. )
  79. _TEST_REFLOG = (
  80. b"0000000000000000000000000000000000000000 "
  81. b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
  82. b"<jelmer@jelmer.uk> 1446552482 +0000 "
  83. b"clone: from git://jelmer.uk/samba\n"
  84. b"49030649db3dfec5a9bc03e5dde4255a14499f16 "
  85. b"42d06bd4b77fed026b154d16493e5deab78f02ec Jelmer Vernooij "
  86. b"<jelmer@jelmer.uk> 1446552483 +0000 "
  87. b"clone: from git://jelmer.uk/samba\n"
  88. b"42d06bd4b77fed026b154d16493e5deab78f02ec "
  89. b"df6800012397fb85c56e7418dd4eb9405dee075c Jelmer Vernooij "
  90. b"<jelmer@jelmer.uk> 1446552484 +0000 "
  91. b"clone: from git://jelmer.uk/samba\n"
  92. )
  93. class ReflogDropTests(TestCase):
  94. def setUp(self) -> None:
  95. TestCase.setUp(self)
  96. self.f = BytesIO(_TEST_REFLOG)
  97. self.original_log = list(read_reflog(self.f))
  98. self.f.seek(0)
  99. def _read_log(self):
  100. self.f.seek(0)
  101. return list(read_reflog(self.f))
  102. def test_invalid(self) -> None:
  103. self.assertRaises(ValueError, drop_reflog_entry, self.f, -1)
  104. def test_drop_entry(self) -> None:
  105. drop_reflog_entry(self.f, 0)
  106. log = self._read_log()
  107. self.assertEqual(len(log), 2)
  108. self.assertEqual(self.original_log[0:2], log)
  109. self.f.seek(0)
  110. drop_reflog_entry(self.f, 1)
  111. log = self._read_log()
  112. self.assertEqual(len(log), 1)
  113. self.assertEqual(self.original_log[1], log[0])
  114. def test_drop_entry_with_rewrite(self) -> None:
  115. drop_reflog_entry(self.f, 1, True)
  116. log = self._read_log()
  117. self.assertEqual(len(log), 2)
  118. self.assertEqual(self.original_log[0], log[0])
  119. self.assertEqual(self.original_log[0].new_sha, log[1].old_sha)
  120. self.assertEqual(self.original_log[2].new_sha, log[1].new_sha)
  121. self.f.seek(0)
  122. drop_reflog_entry(self.f, 1, True)
  123. log = self._read_log()
  124. self.assertEqual(len(log), 1)
  125. self.assertEqual(ZERO_SHA, log[0].old_sha)
  126. self.assertEqual(self.original_log[2].new_sha, log[0].new_sha)