test_line_ending.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # -*- coding: utf-8 -*-
  2. # test_line_ending.py -- Tests for the line ending functions
  3. # encoding: utf-8
  4. # Copyright (C) 2018-2019 Boris Feld <boris.feld@comet.ml>
  5. #
  6. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  7. # General Public License as public by the Free Software Foundation; version 2.0
  8. # or (at your option) any later version. You can redistribute it and/or
  9. # modify it under the terms of either of these two licenses.
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # You should have received a copy of the licenses; if not, see
  18. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  19. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  20. # License, Version 2.0.
  21. #
  22. """Tests for the line ending conversion."""
  23. from dulwich.line_ending import (
  24. normalize_blob,
  25. convert_crlf_to_lf,
  26. convert_lf_to_crlf,
  27. get_checkin_filter_autocrlf,
  28. get_checkout_filter_autocrlf,
  29. )
  30. from dulwich.objects import Blob
  31. from dulwich.tests import TestCase
  32. class LineEndingConversion(TestCase):
  33. """Test the line ending conversion functions in various cases"""
  34. def test_convert_crlf_to_lf_no_op(self):
  35. self.assertEqual(convert_crlf_to_lf(b"foobar"), b"foobar")
  36. def test_convert_crlf_to_lf(self):
  37. self.assertEqual(convert_crlf_to_lf(b"line1\r\nline2"), b"line1\nline2")
  38. def test_convert_crlf_to_lf_mixed(self):
  39. self.assertEqual(convert_crlf_to_lf(b"line1\r\n\nline2"), b"line1\n\nline2")
  40. def test_convert_lf_to_crlf_no_op(self):
  41. self.assertEqual(convert_lf_to_crlf(b"foobar"), b"foobar")
  42. def test_convert_lf_to_crlf(self):
  43. self.assertEqual(convert_lf_to_crlf(b"line1\nline2"), b"line1\r\nline2")
  44. def test_convert_lf_to_crlf_mixed(self):
  45. self.assertEqual(convert_lf_to_crlf(b"line1\r\n\nline2"), b"line1\r\n\r\nline2")
  46. class GetLineEndingAutocrlfFilters(TestCase):
  47. def test_get_checkin_filter_autocrlf_default(self):
  48. checkin_filter = get_checkin_filter_autocrlf(b"false")
  49. self.assertEqual(checkin_filter, None)
  50. def test_get_checkin_filter_autocrlf_true(self):
  51. checkin_filter = get_checkin_filter_autocrlf(b"true")
  52. self.assertEqual(checkin_filter, convert_crlf_to_lf)
  53. def test_get_checkin_filter_autocrlf_input(self):
  54. checkin_filter = get_checkin_filter_autocrlf(b"input")
  55. self.assertEqual(checkin_filter, convert_crlf_to_lf)
  56. def test_get_checkout_filter_autocrlf_default(self):
  57. checkout_filter = get_checkout_filter_autocrlf(b"false")
  58. self.assertEqual(checkout_filter, None)
  59. def test_get_checkout_filter_autocrlf_true(self):
  60. checkout_filter = get_checkout_filter_autocrlf(b"true")
  61. self.assertEqual(checkout_filter, convert_lf_to_crlf)
  62. def test_get_checkout_filter_autocrlf_input(self):
  63. checkout_filter = get_checkout_filter_autocrlf(b"input")
  64. self.assertEqual(checkout_filter, None)
  65. class NormalizeBlobTestCase(TestCase):
  66. def test_normalize_to_lf_no_op(self):
  67. base_content = b"line1\nline2"
  68. base_sha = "f8be7bb828880727816015d21abcbc37d033f233"
  69. base_blob = Blob()
  70. base_blob.set_raw_string(base_content)
  71. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  72. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  73. filtered_blob = normalize_blob(
  74. base_blob, convert_crlf_to_lf, binary_detection=False
  75. )
  76. self.assertEqual(filtered_blob.as_raw_chunks(), [base_content])
  77. self.assertEqual(filtered_blob.sha().hexdigest(), base_sha)
  78. def test_normalize_to_lf(self):
  79. base_content = b"line1\r\nline2"
  80. base_sha = "3a1bd7a52799fe5cf6411f1d35f4c10bacb1db96"
  81. base_blob = Blob()
  82. base_blob.set_raw_string(base_content)
  83. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  84. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  85. filtered_blob = normalize_blob(
  86. base_blob, convert_crlf_to_lf, binary_detection=False
  87. )
  88. normalized_content = b"line1\nline2"
  89. normalized_sha = "f8be7bb828880727816015d21abcbc37d033f233"
  90. self.assertEqual(filtered_blob.as_raw_chunks(), [normalized_content])
  91. self.assertEqual(filtered_blob.sha().hexdigest(), normalized_sha)
  92. def test_normalize_to_lf_binary(self):
  93. base_content = b"line1\r\nline2\0"
  94. base_sha = "b44504193b765f7cd79673812de8afb55b372ab2"
  95. base_blob = Blob()
  96. base_blob.set_raw_string(base_content)
  97. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  98. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  99. filtered_blob = normalize_blob(
  100. base_blob, convert_crlf_to_lf, binary_detection=True
  101. )
  102. self.assertEqual(filtered_blob.as_raw_chunks(), [base_content])
  103. self.assertEqual(filtered_blob.sha().hexdigest(), base_sha)
  104. def test_normalize_to_crlf_no_op(self):
  105. base_content = b"line1\r\nline2"
  106. base_sha = "3a1bd7a52799fe5cf6411f1d35f4c10bacb1db96"
  107. base_blob = Blob()
  108. base_blob.set_raw_string(base_content)
  109. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  110. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  111. filtered_blob = normalize_blob(
  112. base_blob, convert_lf_to_crlf, binary_detection=False
  113. )
  114. self.assertEqual(filtered_blob.as_raw_chunks(), [base_content])
  115. self.assertEqual(filtered_blob.sha().hexdigest(), base_sha)
  116. def test_normalize_to_crlf(self):
  117. base_content = b"line1\nline2"
  118. base_sha = "f8be7bb828880727816015d21abcbc37d033f233"
  119. base_blob = Blob()
  120. base_blob.set_raw_string(base_content)
  121. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  122. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  123. filtered_blob = normalize_blob(
  124. base_blob, convert_lf_to_crlf, binary_detection=False
  125. )
  126. normalized_content = b"line1\r\nline2"
  127. normalized_sha = "3a1bd7a52799fe5cf6411f1d35f4c10bacb1db96"
  128. self.assertEqual(filtered_blob.as_raw_chunks(), [normalized_content])
  129. self.assertEqual(filtered_blob.sha().hexdigest(), normalized_sha)
  130. def test_normalize_to_crlf_binary(self):
  131. base_content = b"line1\r\nline2\0"
  132. base_sha = "b44504193b765f7cd79673812de8afb55b372ab2"
  133. base_blob = Blob()
  134. base_blob.set_raw_string(base_content)
  135. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  136. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  137. filtered_blob = normalize_blob(
  138. base_blob, convert_lf_to_crlf, binary_detection=True
  139. )
  140. self.assertEqual(filtered_blob.as_raw_chunks(), [base_content])
  141. self.assertEqual(filtered_blob.sha().hexdigest(), base_sha)