2
0

test_line_ending.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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(
  38. convert_crlf_to_lf(b"line1\r\nline2"), b"line1\nline2"
  39. )
  40. def test_convert_crlf_to_lf_mixed(self):
  41. self.assertEqual(
  42. convert_crlf_to_lf(b"line1\r\n\nline2"), b"line1\n\nline2"
  43. )
  44. def test_convert_lf_to_crlf_no_op(self):
  45. self.assertEqual(convert_lf_to_crlf(b"foobar"), b"foobar")
  46. def test_convert_lf_to_crlf(self):
  47. self.assertEqual(
  48. convert_lf_to_crlf(b"line1\nline2"), b"line1\r\nline2"
  49. )
  50. def test_convert_lf_to_crlf_mixed(self):
  51. self.assertEqual(
  52. convert_lf_to_crlf(b"line1\r\n\nline2"), b"line1\r\n\r\nline2"
  53. )
  54. class GetLineEndingAutocrlfFilters(TestCase):
  55. def test_get_checkin_filter_autocrlf_default(self):
  56. checkin_filter = get_checkin_filter_autocrlf(b"false")
  57. self.assertEqual(checkin_filter, None)
  58. def test_get_checkin_filter_autocrlf_true(self):
  59. checkin_filter = get_checkin_filter_autocrlf(b"true")
  60. self.assertEqual(checkin_filter, convert_crlf_to_lf)
  61. def test_get_checkin_filter_autocrlf_input(self):
  62. checkin_filter = get_checkin_filter_autocrlf(b"input")
  63. self.assertEqual(checkin_filter, convert_crlf_to_lf)
  64. def test_get_checkout_filter_autocrlf_default(self):
  65. checkout_filter = get_checkout_filter_autocrlf(b"false")
  66. self.assertEqual(checkout_filter, None)
  67. def test_get_checkout_filter_autocrlf_true(self):
  68. checkout_filter = get_checkout_filter_autocrlf(b"true")
  69. self.assertEqual(checkout_filter, convert_lf_to_crlf)
  70. def test_get_checkout_filter_autocrlf_input(self):
  71. checkout_filter = get_checkout_filter_autocrlf(b"input")
  72. self.assertEqual(checkout_filter, None)
  73. class NormalizeBlobTestCase(TestCase):
  74. def test_normalize_to_lf_no_op(self):
  75. base_content = b"line1\nline2"
  76. base_sha = "f8be7bb828880727816015d21abcbc37d033f233"
  77. base_blob = Blob()
  78. base_blob.set_raw_string(base_content)
  79. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  80. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  81. filtered_blob = normalize_blob(
  82. base_blob, convert_crlf_to_lf, binary_detection=False
  83. )
  84. self.assertEqual(filtered_blob.as_raw_chunks(), [base_content])
  85. self.assertEqual(filtered_blob.sha().hexdigest(), base_sha)
  86. def test_normalize_to_lf(self):
  87. base_content = b"line1\r\nline2"
  88. base_sha = "3a1bd7a52799fe5cf6411f1d35f4c10bacb1db96"
  89. base_blob = Blob()
  90. base_blob.set_raw_string(base_content)
  91. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  92. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  93. filtered_blob = normalize_blob(
  94. base_blob, convert_crlf_to_lf, binary_detection=False
  95. )
  96. normalized_content = b"line1\nline2"
  97. normalized_sha = "f8be7bb828880727816015d21abcbc37d033f233"
  98. self.assertEqual(filtered_blob.as_raw_chunks(), [normalized_content])
  99. self.assertEqual(filtered_blob.sha().hexdigest(), normalized_sha)
  100. def test_normalize_to_lf_binary(self):
  101. base_content = b"line1\r\nline2\0"
  102. base_sha = "b44504193b765f7cd79673812de8afb55b372ab2"
  103. base_blob = Blob()
  104. base_blob.set_raw_string(base_content)
  105. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  106. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  107. filtered_blob = normalize_blob(
  108. base_blob, convert_crlf_to_lf, binary_detection=True
  109. )
  110. self.assertEqual(filtered_blob.as_raw_chunks(), [base_content])
  111. self.assertEqual(filtered_blob.sha().hexdigest(), base_sha)
  112. def test_normalize_to_crlf_no_op(self):
  113. base_content = b"line1\r\nline2"
  114. base_sha = "3a1bd7a52799fe5cf6411f1d35f4c10bacb1db96"
  115. base_blob = Blob()
  116. base_blob.set_raw_string(base_content)
  117. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  118. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  119. filtered_blob = normalize_blob(
  120. base_blob, convert_lf_to_crlf, binary_detection=False
  121. )
  122. self.assertEqual(filtered_blob.as_raw_chunks(), [base_content])
  123. self.assertEqual(filtered_blob.sha().hexdigest(), base_sha)
  124. def test_normalize_to_crlf(self):
  125. base_content = b"line1\nline2"
  126. base_sha = "f8be7bb828880727816015d21abcbc37d033f233"
  127. base_blob = Blob()
  128. base_blob.set_raw_string(base_content)
  129. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  130. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  131. filtered_blob = normalize_blob(
  132. base_blob, convert_lf_to_crlf, binary_detection=False
  133. )
  134. normalized_content = b"line1\r\nline2"
  135. normalized_sha = "3a1bd7a52799fe5cf6411f1d35f4c10bacb1db96"
  136. self.assertEqual(filtered_blob.as_raw_chunks(), [normalized_content])
  137. self.assertEqual(filtered_blob.sha().hexdigest(), normalized_sha)
  138. def test_normalize_to_crlf_binary(self):
  139. base_content = b"line1\r\nline2\0"
  140. base_sha = "b44504193b765f7cd79673812de8afb55b372ab2"
  141. base_blob = Blob()
  142. base_blob.set_raw_string(base_content)
  143. self.assertEqual(base_blob.as_raw_chunks(), [base_content])
  144. self.assertEqual(base_blob.sha().hexdigest(), base_sha)
  145. filtered_blob = normalize_blob(
  146. base_blob, convert_lf_to_crlf, binary_detection=True
  147. )
  148. self.assertEqual(filtered_blob.as_raw_chunks(), [base_content])
  149. self.assertEqual(filtered_blob.sha().hexdigest(), base_sha)