2
0

test_reflog.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # test_reflog.py -- tests for reflog.py
  2. # Copyright (C) 2015 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for dulwich.reflog."""
  21. from io import BytesIO
  22. from dulwich.objects import ZERO_SHA
  23. from dulwich.reflog import (
  24. drop_reflog_entry,
  25. format_reflog_line,
  26. parse_reflog_line,
  27. read_reflog,
  28. )
  29. from . import TestCase
  30. class ReflogLineTests(TestCase):
  31. def test_format(self):
  32. self.assertEqual(
  33. b"0000000000000000000000000000000000000000 "
  34. b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
  35. b"<jelmer@jelmer.uk> 1446552482 +0000 "
  36. b"clone: from git://jelmer.uk/samba",
  37. format_reflog_line(
  38. b"0000000000000000000000000000000000000000",
  39. b"49030649db3dfec5a9bc03e5dde4255a14499f16",
  40. b"Jelmer Vernooij <jelmer@jelmer.uk>",
  41. 1446552482,
  42. 0,
  43. b"clone: from git://jelmer.uk/samba",
  44. ),
  45. )
  46. self.assertEqual(
  47. b"0000000000000000000000000000000000000000 "
  48. b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
  49. b"<jelmer@jelmer.uk> 1446552482 +0000 "
  50. b"clone: from git://jelmer.uk/samba",
  51. format_reflog_line(
  52. None,
  53. b"49030649db3dfec5a9bc03e5dde4255a14499f16",
  54. b"Jelmer Vernooij <jelmer@jelmer.uk>",
  55. 1446552482,
  56. 0,
  57. b"clone: from git://jelmer.uk/samba",
  58. ),
  59. )
  60. def test_parse(self):
  61. reflog_line = (
  62. b"0000000000000000000000000000000000000000 "
  63. b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
  64. b"<jelmer@jelmer.uk> 1446552482 +0000 "
  65. b"clone: from git://jelmer.uk/samba"
  66. )
  67. self.assertEqual(
  68. (
  69. b"0000000000000000000000000000000000000000",
  70. b"49030649db3dfec5a9bc03e5dde4255a14499f16",
  71. b"Jelmer Vernooij <jelmer@jelmer.uk>",
  72. 1446552482,
  73. 0,
  74. b"clone: from git://jelmer.uk/samba",
  75. ),
  76. parse_reflog_line(reflog_line),
  77. )
  78. _TEST_REFLOG = (
  79. b"0000000000000000000000000000000000000000 "
  80. b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
  81. b"<jelmer@jelmer.uk> 1446552482 +0000 "
  82. b"clone: from git://jelmer.uk/samba\n"
  83. b"49030649db3dfec5a9bc03e5dde4255a14499f16 "
  84. b"42d06bd4b77fed026b154d16493e5deab78f02ec Jelmer Vernooij "
  85. b"<jelmer@jelmer.uk> 1446552483 +0000 "
  86. b"clone: from git://jelmer.uk/samba\n"
  87. b"42d06bd4b77fed026b154d16493e5deab78f02ec "
  88. b"df6800012397fb85c56e7418dd4eb9405dee075c Jelmer Vernooij "
  89. b"<jelmer@jelmer.uk> 1446552484 +0000 "
  90. b"clone: from git://jelmer.uk/samba\n"
  91. )
  92. class ReflogDropTests(TestCase):
  93. def setUp(self):
  94. TestCase.setUp(self)
  95. self.f = BytesIO(_TEST_REFLOG)
  96. self.original_log = list(read_reflog(self.f))
  97. self.f.seek(0)
  98. def _read_log(self):
  99. self.f.seek(0)
  100. return list(read_reflog(self.f))
  101. def test_invalid(self):
  102. self.assertRaises(ValueError, drop_reflog_entry, self.f, -1)
  103. def test_drop_entry(self):
  104. drop_reflog_entry(self.f, 0)
  105. log = self._read_log()
  106. self.assertEqual(len(log), 2)
  107. self.assertEqual(self.original_log[0:2], log)
  108. self.f.seek(0)
  109. drop_reflog_entry(self.f, 1)
  110. log = self._read_log()
  111. self.assertEqual(len(log), 1)
  112. self.assertEqual(self.original_log[1], log[0])
  113. def test_drop_entry_with_rewrite(self):
  114. drop_reflog_entry(self.f, 1, True)
  115. log = self._read_log()
  116. self.assertEqual(len(log), 2)
  117. self.assertEqual(self.original_log[0], log[0])
  118. self.assertEqual(self.original_log[0].new_sha, log[1].old_sha)
  119. self.assertEqual(self.original_log[2].new_sha, log[1].new_sha)
  120. self.f.seek(0)
  121. drop_reflog_entry(self.f, 1, True)
  122. log = self._read_log()
  123. self.assertEqual(len(log), 1)
  124. self.assertEqual(ZERO_SHA, log[0].old_sha)
  125. self.assertEqual(self.original_log[2].new_sha, log[0].new_sha)