2
0

test_lfs.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # test_lfs.py -- tests for LFS
  2. # Copyright (C) 2020 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 LFS support."""
  22. import shutil
  23. import tempfile
  24. from dulwich.lfs import LFSFilterDriver, LFSPointer, LFSStore
  25. from . import TestCase
  26. class LFSTests(TestCase):
  27. def setUp(self) -> None:
  28. super().setUp()
  29. self.test_dir = tempfile.mkdtemp()
  30. self.addCleanup(shutil.rmtree, self.test_dir)
  31. self.lfs = LFSStore.create(self.test_dir)
  32. def test_create(self) -> None:
  33. sha = self.lfs.write_object([b"a", b"b"])
  34. with self.lfs.open_object(sha) as f:
  35. self.assertEqual(b"ab", f.read())
  36. def test_missing(self) -> None:
  37. self.assertRaises(KeyError, self.lfs.open_object, "abcdeabcdeabcdeabcde")
  38. def test_write_object_empty(self) -> None:
  39. """Test writing an empty object."""
  40. sha = self.lfs.write_object([])
  41. with self.lfs.open_object(sha) as f:
  42. self.assertEqual(b"", f.read())
  43. def test_write_object_multiple_chunks(self) -> None:
  44. """Test writing an object with multiple chunks."""
  45. chunks = [b"chunk1", b"chunk2", b"chunk3"]
  46. sha = self.lfs.write_object(chunks)
  47. with self.lfs.open_object(sha) as f:
  48. self.assertEqual(b"".join(chunks), f.read())
  49. def test_sha_path_calculation(self) -> None:
  50. """Test the internal sha path calculation."""
  51. # The implementation splits the sha into parts for directory structure
  52. # Write and verify we can read it back
  53. sha = self.lfs.write_object([b"test data"])
  54. self.assertEqual(len(sha), 64) # SHA-256 is 64 hex chars
  55. # Open should succeed, which verifies the path calculation works
  56. with self.lfs.open_object(sha) as f:
  57. self.assertEqual(b"test data", f.read())
  58. def test_create_lfs_dir(self) -> None:
  59. """Test creating an LFS directory when it doesn't exist."""
  60. import os
  61. # Create a temporary directory for the test
  62. lfs_parent_dir = tempfile.mkdtemp()
  63. self.addCleanup(shutil.rmtree, lfs_parent_dir)
  64. # Create a path for the LFS directory
  65. lfs_dir = os.path.join(lfs_parent_dir, "lfs")
  66. # Create the LFS store
  67. LFSStore.create(lfs_dir)
  68. # Verify the directories were created
  69. self.assertTrue(os.path.isdir(lfs_dir))
  70. self.assertTrue(os.path.isdir(os.path.join(lfs_dir, "tmp")))
  71. self.assertTrue(os.path.isdir(os.path.join(lfs_dir, "objects")))
  72. class LFSPointerTests(TestCase):
  73. def test_from_bytes_valid(self) -> None:
  74. """Test parsing a valid LFS pointer."""
  75. pointer_data = (
  76. b"version https://git-lfs.github.com/spec/v1\n"
  77. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  78. b"size 0\n"
  79. )
  80. pointer = LFSPointer.from_bytes(pointer_data)
  81. self.assertIsNotNone(pointer)
  82. self.assertEqual(
  83. pointer.oid,
  84. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
  85. )
  86. self.assertEqual(pointer.size, 0)
  87. def test_from_bytes_with_extra_fields(self) -> None:
  88. """Test parsing LFS pointer with extra fields (should still work)."""
  89. pointer_data = (
  90. b"version https://git-lfs.github.com/spec/v1\n"
  91. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  92. b"size 1234\n"
  93. b"x-custom-field value\n"
  94. )
  95. pointer = LFSPointer.from_bytes(pointer_data)
  96. self.assertIsNotNone(pointer)
  97. self.assertEqual(pointer.size, 1234)
  98. def test_from_bytes_invalid_version(self) -> None:
  99. """Test parsing with invalid version line."""
  100. pointer_data = (
  101. b"version https://invalid.com/spec/v1\n"
  102. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  103. b"size 0\n"
  104. )
  105. pointer = LFSPointer.from_bytes(pointer_data)
  106. self.assertIsNone(pointer)
  107. def test_from_bytes_missing_oid(self) -> None:
  108. """Test parsing with missing OID."""
  109. pointer_data = b"version https://git-lfs.github.com/spec/v1\nsize 0\n"
  110. pointer = LFSPointer.from_bytes(pointer_data)
  111. self.assertIsNone(pointer)
  112. def test_from_bytes_missing_size(self) -> None:
  113. """Test parsing with missing size."""
  114. pointer_data = (
  115. b"version https://git-lfs.github.com/spec/v1\n"
  116. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  117. )
  118. pointer = LFSPointer.from_bytes(pointer_data)
  119. self.assertIsNone(pointer)
  120. def test_from_bytes_invalid_size(self) -> None:
  121. """Test parsing with invalid size."""
  122. pointer_data = (
  123. b"version https://git-lfs.github.com/spec/v1\n"
  124. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  125. b"size not_a_number\n"
  126. )
  127. pointer = LFSPointer.from_bytes(pointer_data)
  128. self.assertIsNone(pointer)
  129. def test_from_bytes_binary_data(self) -> None:
  130. """Test parsing binary data (not an LFS pointer)."""
  131. binary_data = b"\x00\x01\x02\x03\x04"
  132. pointer = LFSPointer.from_bytes(binary_data)
  133. self.assertIsNone(pointer)
  134. def test_to_bytes(self) -> None:
  135. """Test converting LFS pointer to bytes."""
  136. pointer = LFSPointer(
  137. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 1234
  138. )
  139. data = pointer.to_bytes()
  140. expected = (
  141. b"version https://git-lfs.github.com/spec/v1\n"
  142. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  143. b"size 1234\n"
  144. )
  145. self.assertEqual(data, expected)
  146. def test_round_trip(self) -> None:
  147. """Test converting to bytes and back."""
  148. original = LFSPointer(
  149. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 9876
  150. )
  151. data = original.to_bytes()
  152. parsed = LFSPointer.from_bytes(data)
  153. self.assertIsNotNone(parsed)
  154. self.assertEqual(parsed.oid, original.oid)
  155. self.assertEqual(parsed.size, original.size)
  156. def test_is_valid_oid(self) -> None:
  157. """Test OID validation."""
  158. # Valid SHA256
  159. valid_pointer = LFSPointer(
  160. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 0
  161. )
  162. self.assertTrue(valid_pointer.is_valid_oid())
  163. # Too short
  164. short_pointer = LFSPointer("e3b0c44298fc1c14", 0)
  165. self.assertFalse(short_pointer.is_valid_oid())
  166. # Invalid hex characters
  167. invalid_pointer = LFSPointer(
  168. "g3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 0
  169. )
  170. self.assertFalse(invalid_pointer.is_valid_oid())
  171. class LFSFilterDriverTests(TestCase):
  172. def setUp(self) -> None:
  173. super().setUp()
  174. self.test_dir = tempfile.mkdtemp()
  175. self.addCleanup(shutil.rmtree, self.test_dir)
  176. self.lfs_store = LFSStore.create(self.test_dir)
  177. self.filter_driver = LFSFilterDriver(self.lfs_store)
  178. def test_clean_new_file(self) -> None:
  179. """Test clean filter on new file content."""
  180. content = b"This is a test file content"
  181. result = self.filter_driver.clean(content)
  182. # Result should be an LFS pointer
  183. pointer = LFSPointer.from_bytes(result)
  184. self.assertIsNotNone(pointer)
  185. self.assertEqual(pointer.size, len(content))
  186. # Content should be stored in LFS
  187. with self.lfs_store.open_object(pointer.oid) as f:
  188. self.assertEqual(f.read(), content)
  189. def test_clean_existing_pointer(self) -> None:
  190. """Test clean filter on already-pointer content."""
  191. # Create a pointer
  192. pointer = LFSPointer(
  193. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 1234
  194. )
  195. pointer_data = pointer.to_bytes()
  196. # Clean should return the pointer unchanged
  197. result = self.filter_driver.clean(pointer_data)
  198. self.assertEqual(result, pointer_data)
  199. def test_smudge_valid_pointer(self) -> None:
  200. """Test smudge filter with valid pointer."""
  201. # Store some content
  202. content = b"This is the actual file content"
  203. sha = self.lfs_store.write_object([content])
  204. # Create pointer
  205. pointer = LFSPointer(sha, len(content))
  206. pointer_data = pointer.to_bytes()
  207. # Smudge should return the actual content
  208. result = self.filter_driver.smudge(pointer_data)
  209. self.assertEqual(result, content)
  210. def test_smudge_missing_object(self) -> None:
  211. """Test smudge filter with missing LFS object."""
  212. # Create pointer to non-existent object
  213. pointer = LFSPointer(
  214. "0000000000000000000000000000000000000000000000000000000000000000", 1234
  215. )
  216. pointer_data = pointer.to_bytes()
  217. # Smudge should return the pointer as-is when object is missing
  218. result = self.filter_driver.smudge(pointer_data)
  219. self.assertEqual(result, pointer_data)
  220. def test_smudge_non_pointer(self) -> None:
  221. """Test smudge filter on non-pointer content."""
  222. content = b"This is not an LFS pointer"
  223. # Smudge should return content unchanged
  224. result = self.filter_driver.smudge(content)
  225. self.assertEqual(result, content)
  226. def test_round_trip(self) -> None:
  227. """Test clean followed by smudge."""
  228. original_content = b"Round trip test content"
  229. # Clean (working tree -> repo)
  230. pointer_data = self.filter_driver.clean(original_content)
  231. # Verify it's a pointer
  232. pointer = LFSPointer.from_bytes(pointer_data)
  233. self.assertIsNotNone(pointer)
  234. # Smudge (repo -> working tree)
  235. restored_content = self.filter_driver.smudge(pointer_data)
  236. # Should get back the original content
  237. self.assertEqual(restored_content, original_content)