test_lfs.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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 published 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 json
  23. import os
  24. import shutil
  25. import tempfile
  26. from pathlib import Path
  27. from dulwich import porcelain
  28. from dulwich.lfs import LFSFilterDriver, LFSPointer, LFSStore
  29. from dulwich.repo import Repo
  30. from . import TestCase
  31. class LFSTests(TestCase):
  32. def setUp(self) -> None:
  33. super().setUp()
  34. # Suppress LFS warnings during these tests
  35. import logging
  36. self._old_level = logging.getLogger("dulwich.lfs").level
  37. logging.getLogger("dulwich.lfs").setLevel(logging.ERROR)
  38. self.test_dir = tempfile.mkdtemp()
  39. self.addCleanup(shutil.rmtree, self.test_dir)
  40. self.lfs = LFSStore.create(self.test_dir)
  41. def tearDown(self) -> None:
  42. # Restore original logging level
  43. import logging
  44. logging.getLogger("dulwich.lfs").setLevel(self._old_level)
  45. super().tearDown()
  46. def test_create(self) -> None:
  47. sha = self.lfs.write_object([b"a", b"b"])
  48. with self.lfs.open_object(sha) as f:
  49. self.assertEqual(b"ab", f.read())
  50. def test_missing(self) -> None:
  51. self.assertRaises(KeyError, self.lfs.open_object, "abcdeabcdeabcdeabcde")
  52. def test_write_object_empty(self) -> None:
  53. """Test writing an empty object."""
  54. sha = self.lfs.write_object([])
  55. with self.lfs.open_object(sha) as f:
  56. self.assertEqual(b"", f.read())
  57. def test_write_object_multiple_chunks(self) -> None:
  58. """Test writing an object with multiple chunks."""
  59. chunks = [b"chunk1", b"chunk2", b"chunk3"]
  60. sha = self.lfs.write_object(chunks)
  61. with self.lfs.open_object(sha) as f:
  62. self.assertEqual(b"".join(chunks), f.read())
  63. def test_sha_path_calculation(self) -> None:
  64. """Test the internal sha path calculation."""
  65. # The implementation splits the sha into parts for directory structure
  66. # Write and verify we can read it back
  67. sha = self.lfs.write_object([b"test data"])
  68. self.assertEqual(len(sha), 64) # SHA-256 is 64 hex chars
  69. # Open should succeed, which verifies the path calculation works
  70. with self.lfs.open_object(sha) as f:
  71. self.assertEqual(b"test data", f.read())
  72. def test_create_lfs_dir(self) -> None:
  73. """Test creating an LFS directory when it doesn't exist."""
  74. import os
  75. # Create a temporary directory for the test
  76. lfs_parent_dir = tempfile.mkdtemp()
  77. self.addCleanup(shutil.rmtree, lfs_parent_dir)
  78. # Create a path for the LFS directory
  79. lfs_dir = os.path.join(lfs_parent_dir, "lfs")
  80. # Create the LFS store
  81. LFSStore.create(lfs_dir)
  82. # Verify the directories were created
  83. self.assertTrue(os.path.isdir(lfs_dir))
  84. self.assertTrue(os.path.isdir(os.path.join(lfs_dir, "tmp")))
  85. self.assertTrue(os.path.isdir(os.path.join(lfs_dir, "objects")))
  86. class LFSPointerTests(TestCase):
  87. def test_from_bytes_valid(self) -> None:
  88. """Test parsing a valid LFS pointer."""
  89. pointer_data = (
  90. b"version https://git-lfs.github.com/spec/v1\n"
  91. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  92. b"size 0\n"
  93. )
  94. pointer = LFSPointer.from_bytes(pointer_data)
  95. self.assertIsNotNone(pointer)
  96. self.assertEqual(
  97. pointer.oid,
  98. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
  99. )
  100. self.assertEqual(pointer.size, 0)
  101. def test_from_bytes_with_extra_fields(self) -> None:
  102. """Test parsing LFS pointer with extra fields (should still work)."""
  103. pointer_data = (
  104. b"version https://git-lfs.github.com/spec/v1\n"
  105. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  106. b"size 1234\n"
  107. b"x-custom-field value\n"
  108. )
  109. pointer = LFSPointer.from_bytes(pointer_data)
  110. self.assertIsNotNone(pointer)
  111. self.assertEqual(pointer.size, 1234)
  112. def test_from_bytes_invalid_version(self) -> None:
  113. """Test parsing with invalid version line."""
  114. pointer_data = (
  115. b"version https://invalid.com/spec/v1\n"
  116. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  117. b"size 0\n"
  118. )
  119. pointer = LFSPointer.from_bytes(pointer_data)
  120. self.assertIsNone(pointer)
  121. def test_from_bytes_missing_oid(self) -> None:
  122. """Test parsing with missing OID."""
  123. pointer_data = b"version https://git-lfs.github.com/spec/v1\nsize 0\n"
  124. pointer = LFSPointer.from_bytes(pointer_data)
  125. self.assertIsNone(pointer)
  126. def test_from_bytes_missing_size(self) -> None:
  127. """Test parsing with missing size."""
  128. pointer_data = (
  129. b"version https://git-lfs.github.com/spec/v1\n"
  130. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  131. )
  132. pointer = LFSPointer.from_bytes(pointer_data)
  133. self.assertIsNone(pointer)
  134. def test_from_bytes_invalid_size(self) -> None:
  135. """Test parsing with invalid size."""
  136. pointer_data = (
  137. b"version https://git-lfs.github.com/spec/v1\n"
  138. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  139. b"size not_a_number\n"
  140. )
  141. pointer = LFSPointer.from_bytes(pointer_data)
  142. self.assertIsNone(pointer)
  143. def test_from_bytes_binary_data(self) -> None:
  144. """Test parsing binary data (not an LFS pointer)."""
  145. binary_data = b"\x00\x01\x02\x03\x04"
  146. pointer = LFSPointer.from_bytes(binary_data)
  147. self.assertIsNone(pointer)
  148. def test_to_bytes(self) -> None:
  149. """Test converting LFS pointer to bytes."""
  150. pointer = LFSPointer(
  151. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 1234
  152. )
  153. data = pointer.to_bytes()
  154. expected = (
  155. b"version https://git-lfs.github.com/spec/v1\n"
  156. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  157. b"size 1234\n"
  158. )
  159. self.assertEqual(data, expected)
  160. def test_round_trip(self) -> None:
  161. """Test converting to bytes and back."""
  162. original = LFSPointer(
  163. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 9876
  164. )
  165. data = original.to_bytes()
  166. parsed = LFSPointer.from_bytes(data)
  167. self.assertIsNotNone(parsed)
  168. self.assertEqual(parsed.oid, original.oid)
  169. self.assertEqual(parsed.size, original.size)
  170. def test_is_valid_oid(self) -> None:
  171. """Test OID validation."""
  172. # Valid SHA256
  173. valid_pointer = LFSPointer(
  174. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 0
  175. )
  176. self.assertTrue(valid_pointer.is_valid_oid())
  177. # Too short
  178. short_pointer = LFSPointer("e3b0c44298fc1c14", 0)
  179. self.assertFalse(short_pointer.is_valid_oid())
  180. # Invalid hex characters
  181. invalid_pointer = LFSPointer(
  182. "g3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 0
  183. )
  184. self.assertFalse(invalid_pointer.is_valid_oid())
  185. class LFSIntegrationTests(TestCase):
  186. """Integration tests for LFS with Git operations."""
  187. def setUp(self) -> None:
  188. super().setUp()
  189. # Suppress LFS warnings during these integration tests
  190. import logging
  191. self._old_level = logging.getLogger("dulwich.lfs").level
  192. logging.getLogger("dulwich.lfs").setLevel(logging.ERROR)
  193. # Create temporary directory for test repo
  194. self.test_dir = tempfile.mkdtemp()
  195. self.addCleanup(shutil.rmtree, self.test_dir)
  196. # Initialize repo
  197. from dulwich.repo import Repo
  198. self.repo = Repo.init(self.test_dir)
  199. self.lfs_dir = os.path.join(self.test_dir, ".git", "lfs")
  200. self.lfs_store = LFSStore.create(self.lfs_dir)
  201. def tearDown(self) -> None:
  202. # Restore original logging level
  203. import logging
  204. logging.getLogger("dulwich.lfs").setLevel(self._old_level)
  205. super().tearDown()
  206. def test_lfs_with_gitattributes(self) -> None:
  207. """Test LFS integration with .gitattributes."""
  208. import os
  209. # Create .gitattributes file
  210. gitattributes_path = os.path.join(self.test_dir, ".gitattributes")
  211. with open(gitattributes_path, "wb") as f:
  212. f.write(b"*.bin filter=lfs diff=lfs merge=lfs -text\n")
  213. # Create a binary file
  214. bin_path = os.path.join(self.test_dir, "large.bin")
  215. large_content = b"Large binary content" * 1000
  216. with open(bin_path, "wb") as f:
  217. f.write(large_content)
  218. # Add files to repo
  219. self.repo.get_worktree().stage([".gitattributes", "large.bin"])
  220. # Get the blob for large.bin from the index
  221. index = self.repo.open_index()
  222. entry = index[b"large.bin"]
  223. blob = self.repo.object_store[entry.sha]
  224. # With LFS configured, the blob should contain an LFS pointer
  225. # (Note: This would require actual LFS filter integration in dulwich)
  226. # For now, we just verify the structure
  227. self.assertIsNotNone(blob)
  228. def test_lfs_checkout_missing_object(self) -> None:
  229. """Test checkout behavior when LFS object is missing."""
  230. from dulwich.objects import Blob, Commit, Tree
  231. # Create an LFS pointer blob
  232. pointer = LFSPointer(
  233. "0000000000000000000000000000000000000000000000000000000000000000", 1234
  234. )
  235. blob = Blob()
  236. blob.data = pointer.to_bytes()
  237. self.repo.object_store.add_object(blob)
  238. # Create tree with the blob
  239. tree = Tree()
  240. tree.add(b"missing.bin", 0o100644, blob.id)
  241. self.repo.object_store.add_object(tree)
  242. # Create commit
  243. commit = Commit()
  244. commit.tree = tree.id
  245. commit.message = b"Add missing LFS file"
  246. commit.author = commit.committer = b"Test User <test@example.com>"
  247. commit.commit_time = commit.author_time = 1234567890
  248. commit.commit_timezone = commit.author_timezone = 0
  249. self.repo.object_store.add_object(commit)
  250. # Update HEAD
  251. self.repo.refs[b"HEAD"] = commit.id
  252. # Checkout should leave pointer file when object is missing
  253. # (actual checkout would require more integration)
  254. def test_lfs_pointer_detection(self) -> None:
  255. """Test detection of LFS pointer files."""
  256. # Test various file contents
  257. test_cases = [
  258. # Valid LFS pointer
  259. (
  260. b"version https://git-lfs.github.com/spec/v1\n"
  261. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  262. b"size 1234\n",
  263. True,
  264. ),
  265. # Regular text file
  266. (b"This is a regular text file\n", False),
  267. # Binary file
  268. (b"\x00\x01\x02\x03\x04", False),
  269. # File that starts like pointer but isn't
  270. (b"version 1.0\nThis is not an LFS pointer\n", False),
  271. ]
  272. for content, expected_is_pointer in test_cases:
  273. pointer = LFSPointer.from_bytes(content)
  274. self.assertEqual(
  275. pointer is not None,
  276. expected_is_pointer,
  277. f"Failed for content: {content!r}",
  278. )
  279. def test_builtin_lfs_clone_no_config(self) -> None:
  280. """Test cloning with LFS when no git-lfs commands are configured."""
  281. # Create source repository
  282. source_dir = os.path.join(self.test_dir, "source")
  283. os.makedirs(source_dir)
  284. source_repo = Repo.init(source_dir)
  285. # Create empty config (no LFS commands)
  286. config = source_repo.get_config()
  287. config.write_to_path()
  288. # Create .gitattributes with LFS filter
  289. gitattributes_path = os.path.join(source_dir, ".gitattributes")
  290. with open(gitattributes_path, "wb") as f:
  291. f.write(b"*.bin filter=lfs\n")
  292. # Create test content and store in LFS
  293. test_content = b"Test binary content"
  294. test_oid = LFSStore.from_repo(source_repo, create=True).write_object(
  295. [test_content]
  296. )
  297. # Create LFS pointer file
  298. pointer = LFSPointer(test_oid, len(test_content))
  299. pointer_file = os.path.join(source_dir, "test.bin")
  300. with open(pointer_file, "wb") as f:
  301. f.write(pointer.to_bytes())
  302. # Commit files
  303. porcelain.add(source_repo, paths=[".gitattributes", "test.bin"])
  304. porcelain.commit(source_repo, message=b"Add LFS tracked file")
  305. source_repo.close()
  306. # Clone the repository
  307. target_dir = os.path.join(self.test_dir, "target")
  308. target_repo = porcelain.clone(source_dir, target_dir)
  309. # Verify no LFS commands in config
  310. target_config = target_repo.get_config_stack()
  311. with self.assertRaises(KeyError):
  312. target_config.get((b"filter", b"lfs"), b"smudge")
  313. # Check the cloned file
  314. cloned_file = os.path.join(target_dir, "test.bin")
  315. with open(cloned_file, "rb") as f:
  316. content = f.read()
  317. # Should still be a pointer (LFS object not in target's store)
  318. self.assertTrue(
  319. content.startswith(b"version https://git-lfs.github.com/spec/v1")
  320. )
  321. self.assertIn(test_oid.encode(), content)
  322. target_repo.close()
  323. def test_builtin_lfs_with_local_objects(self) -> None:
  324. """Test built-in LFS filter when objects are available locally."""
  325. # No LFS config
  326. config = self.repo.get_config()
  327. config.write_to_path()
  328. # Create .gitattributes
  329. gitattributes_path = os.path.join(self.test_dir, ".gitattributes")
  330. with open(gitattributes_path, "wb") as f:
  331. f.write(b"*.dat filter=lfs\n")
  332. # Create LFS store and add object
  333. test_content = b"Hello from LFS!"
  334. lfs_store = LFSStore.from_repo(self.repo, create=True)
  335. test_oid = lfs_store.write_object([test_content])
  336. # Create pointer file
  337. pointer = LFSPointer(test_oid, len(test_content))
  338. pointer_file = os.path.join(self.test_dir, "data.dat")
  339. with open(pointer_file, "wb") as f:
  340. f.write(pointer.to_bytes())
  341. # Commit
  342. porcelain.add(self.repo, paths=[".gitattributes", "data.dat"])
  343. porcelain.commit(self.repo, message=b"Add LFS file")
  344. # Reset index to trigger checkout with filter
  345. self.repo.get_worktree().reset_index()
  346. # Check file content
  347. with open(pointer_file, "rb") as f:
  348. content = f.read()
  349. # Built-in filter should have converted pointer to actual content
  350. self.assertEqual(content, test_content)
  351. def test_builtin_lfs_filter_used(self) -> None:
  352. """Verify that built-in LFS filter is used when no config exists."""
  353. # Get filter registry
  354. normalizer = self.repo.get_blob_normalizer()
  355. filter_registry = normalizer.filter_registry
  356. lfs_driver = filter_registry.get_driver("lfs")
  357. # Should be built-in LFS filter
  358. self.assertIsInstance(lfs_driver, LFSFilterDriver)
  359. self.assertEqual(type(lfs_driver).__module__, "dulwich.lfs")
  360. class LFSFilterDriverTests(TestCase):
  361. def setUp(self) -> None:
  362. super().setUp()
  363. self.test_dir = tempfile.mkdtemp()
  364. self.addCleanup(shutil.rmtree, self.test_dir)
  365. self.lfs_store = LFSStore.create(self.test_dir)
  366. self.filter_driver = LFSFilterDriver(self.lfs_store)
  367. def test_clean_new_file(self) -> None:
  368. """Test clean filter on new file content."""
  369. content = b"This is a test file content"
  370. result = self.filter_driver.clean(content)
  371. # Result should be an LFS pointer
  372. pointer = LFSPointer.from_bytes(result)
  373. self.assertIsNotNone(pointer)
  374. self.assertEqual(pointer.size, len(content))
  375. # Content should be stored in LFS
  376. with self.lfs_store.open_object(pointer.oid) as f:
  377. self.assertEqual(f.read(), content)
  378. def test_clean_existing_pointer(self) -> None:
  379. """Test clean filter on already-pointer content."""
  380. # Create a pointer
  381. pointer = LFSPointer(
  382. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 1234
  383. )
  384. pointer_data = pointer.to_bytes()
  385. # Clean should return the pointer unchanged
  386. result = self.filter_driver.clean(pointer_data)
  387. self.assertEqual(result, pointer_data)
  388. def test_smudge_valid_pointer(self) -> None:
  389. """Test smudge filter with valid pointer."""
  390. # Store some content
  391. content = b"This is the actual file content"
  392. sha = self.lfs_store.write_object([content])
  393. # Create pointer
  394. pointer = LFSPointer(sha, len(content))
  395. pointer_data = pointer.to_bytes()
  396. # Smudge should return the actual content
  397. result = self.filter_driver.smudge(pointer_data)
  398. self.assertEqual(result, content)
  399. def test_smudge_missing_object(self) -> None:
  400. """Test smudge filter with missing LFS object."""
  401. # Create pointer to non-existent object
  402. pointer = LFSPointer(
  403. "0000000000000000000000000000000000000000000000000000000000000000", 1234
  404. )
  405. pointer_data = pointer.to_bytes()
  406. # Smudge should return the pointer as-is when object is missing
  407. result = self.filter_driver.smudge(pointer_data)
  408. self.assertEqual(result, pointer_data)
  409. def test_smudge_non_pointer(self) -> None:
  410. """Test smudge filter on non-pointer content."""
  411. content = b"This is not an LFS pointer"
  412. # Smudge should return content unchanged
  413. result = self.filter_driver.smudge(content)
  414. self.assertEqual(result, content)
  415. def test_round_trip(self) -> None:
  416. """Test clean followed by smudge."""
  417. original_content = b"Round trip test content"
  418. # Clean (working tree -> repo)
  419. pointer_data = self.filter_driver.clean(original_content)
  420. # Verify it's a pointer
  421. pointer = LFSPointer.from_bytes(pointer_data)
  422. self.assertIsNotNone(pointer)
  423. # Smudge (repo -> working tree)
  424. restored_content = self.filter_driver.smudge(pointer_data)
  425. # Should get back the original content
  426. self.assertEqual(restored_content, original_content)
  427. def test_clean_empty_file(self) -> None:
  428. """Test clean filter on empty file."""
  429. content = b""
  430. result = self.filter_driver.clean(content)
  431. # Result should be an LFS pointer
  432. pointer = LFSPointer.from_bytes(result)
  433. self.assertIsNotNone(pointer)
  434. self.assertEqual(pointer.size, 0)
  435. # Empty content should be stored in LFS
  436. with self.lfs_store.open_object(pointer.oid) as f:
  437. self.assertEqual(f.read(), content)
  438. def test_clean_large_file(self) -> None:
  439. """Test clean filter on large file."""
  440. # Create a large file (1MB)
  441. content = b"x" * (1024 * 1024)
  442. result = self.filter_driver.clean(content)
  443. # Result should be an LFS pointer
  444. pointer = LFSPointer.from_bytes(result)
  445. self.assertIsNotNone(pointer)
  446. self.assertEqual(pointer.size, len(content))
  447. # Content should be stored in LFS
  448. with self.lfs_store.open_object(pointer.oid) as f:
  449. self.assertEqual(f.read(), content)
  450. def test_smudge_corrupt_pointer(self) -> None:
  451. """Test smudge filter with corrupt pointer data."""
  452. # Create corrupt pointer data
  453. corrupt_data = (
  454. b"version https://git-lfs.github.com/spec/v1\noid sha256:invalid\n"
  455. )
  456. # Smudge should return the data as-is
  457. result = self.filter_driver.smudge(corrupt_data)
  458. self.assertEqual(result, corrupt_data)
  459. def test_clean_unicode_content(self) -> None:
  460. """Test clean filter with unicode content."""
  461. # UTF-8 encoded unicode content
  462. content = "Hello 世界 🌍".encode()
  463. result = self.filter_driver.clean(content)
  464. # Result should be an LFS pointer
  465. pointer = LFSPointer.from_bytes(result)
  466. self.assertIsNotNone(pointer)
  467. # Content should be preserved exactly
  468. with self.lfs_store.open_object(pointer.oid) as f:
  469. self.assertEqual(f.read(), content)
  470. class LFSStoreEdgeCaseTests(TestCase):
  471. """Edge case tests for LFS store."""
  472. def setUp(self) -> None:
  473. super().setUp()
  474. self.test_dir = tempfile.mkdtemp()
  475. self.addCleanup(shutil.rmtree, self.test_dir)
  476. self.lfs = LFSStore.create(self.test_dir)
  477. def test_concurrent_writes(self) -> None:
  478. """Test that concurrent writes to same content work correctly."""
  479. content = b"duplicate content"
  480. # Write the same content multiple times
  481. sha1 = self.lfs.write_object([content])
  482. sha2 = self.lfs.write_object([content])
  483. # Should get the same SHA
  484. self.assertEqual(sha1, sha2)
  485. # Content should be stored only once
  486. with self.lfs.open_object(sha1) as f:
  487. self.assertEqual(f.read(), content)
  488. def test_write_with_generator(self) -> None:
  489. """Test writing object with generator chunks."""
  490. def chunk_generator():
  491. yield b"chunk1"
  492. yield b"chunk2"
  493. yield b"chunk3"
  494. sha = self.lfs.write_object(chunk_generator())
  495. # Verify content
  496. with self.lfs.open_object(sha) as f:
  497. self.assertEqual(f.read(), b"chunk1chunk2chunk3")
  498. def test_partial_write_rollback(self) -> None:
  499. """Test that partial writes don't leave artifacts."""
  500. import os
  501. # Count initial objects
  502. objects_dir = os.path.join(self.test_dir, "objects")
  503. initial_count = sum(len(files) for _, _, files in os.walk(objects_dir))
  504. # Try to write with a failing generator
  505. def failing_generator():
  506. yield b"chunk1"
  507. raise RuntimeError("Simulated error")
  508. # This should fail
  509. with self.assertRaises(RuntimeError):
  510. self.lfs.write_object(failing_generator())
  511. # No new objects should have been created
  512. final_count = sum(len(files) for _, _, files in os.walk(objects_dir))
  513. self.assertEqual(initial_count, final_count)
  514. class LFSPointerEdgeCaseTests(TestCase):
  515. """Edge case tests for LFS pointer parsing."""
  516. def test_pointer_with_windows_line_endings(self) -> None:
  517. """Test parsing pointer with Windows line endings."""
  518. pointer_data = (
  519. b"version https://git-lfs.github.com/spec/v1\r\n"
  520. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\r\n"
  521. b"size 1234\r\n"
  522. )
  523. pointer = LFSPointer.from_bytes(pointer_data)
  524. self.assertIsNotNone(pointer)
  525. self.assertEqual(pointer.size, 1234)
  526. def test_pointer_with_extra_whitespace(self) -> None:
  527. """Test parsing pointer with extra whitespace."""
  528. pointer_data = (
  529. b"version https://git-lfs.github.com/spec/v1 \n"
  530. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  531. b"size 1234 \n"
  532. )
  533. pointer = LFSPointer.from_bytes(pointer_data)
  534. self.assertIsNotNone(pointer)
  535. self.assertEqual(pointer.size, 1234)
  536. def test_pointer_case_sensitivity(self) -> None:
  537. """Test that pointer parsing is case sensitive."""
  538. # Version line must be exact
  539. pointer_data = (
  540. b"Version https://git-lfs.github.com/spec/v1\n" # Capital V
  541. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  542. b"size 1234\n"
  543. )
  544. pointer = LFSPointer.from_bytes(pointer_data)
  545. self.assertIsNone(pointer) # Should fail due to case
  546. def test_pointer_oid_formats(self) -> None:
  547. """Test different OID formats."""
  548. # SHA256 is currently the only supported format
  549. # Test SHA1 format (should fail)
  550. pointer_data = (
  551. b"version https://git-lfs.github.com/spec/v1\n"
  552. b"oid sha1:356a192b7913b04c54574d18c28d46e6395428ab\n" # SHA1
  553. b"size 1234\n"
  554. )
  555. pointer = LFSPointer.from_bytes(pointer_data)
  556. # This might be accepted but marked as invalid OID
  557. if pointer:
  558. self.assertFalse(pointer.is_valid_oid())
  559. def test_pointer_size_limits(self) -> None:
  560. """Test size value limits."""
  561. # Test with very large size
  562. pointer_data = (
  563. b"version https://git-lfs.github.com/spec/v1\n"
  564. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  565. b"size 999999999999999999\n" # Very large number
  566. )
  567. pointer = LFSPointer.from_bytes(pointer_data)
  568. self.assertIsNotNone(pointer)
  569. self.assertEqual(pointer.size, 999999999999999999)
  570. # Test with negative size (should fail)
  571. pointer_data = (
  572. b"version https://git-lfs.github.com/spec/v1\n"
  573. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  574. b"size -1\n"
  575. )
  576. pointer = LFSPointer.from_bytes(pointer_data)
  577. self.assertIsNone(pointer) # Should fail with negative size
  578. class LFSServerTests(TestCase):
  579. """Tests for the LFS server implementation."""
  580. def setUp(self) -> None:
  581. super().setUp()
  582. import threading
  583. from dulwich.lfs_server import run_lfs_server
  584. # Create temporary directory for LFS storage
  585. self.test_dir = tempfile.mkdtemp()
  586. self.addCleanup(shutil.rmtree, self.test_dir)
  587. # Start LFS server
  588. self.server, self.server_url = run_lfs_server(port=0, lfs_dir=self.test_dir)
  589. self.server_thread = threading.Thread(target=self.server.serve_forever)
  590. self.server_thread.daemon = True
  591. self.server_thread.start()
  592. def cleanup_server():
  593. self.server.shutdown()
  594. self.server.server_close()
  595. self.server_thread.join(timeout=1.0)
  596. self.addCleanup(cleanup_server)
  597. def test_server_batch_endpoint(self) -> None:
  598. """Test the batch endpoint directly."""
  599. from urllib.request import Request, urlopen
  600. # Create batch request
  601. batch_data = {
  602. "operation": "download",
  603. "transfers": ["basic"],
  604. "objects": [{"oid": "abc123", "size": 100}],
  605. }
  606. req = Request(
  607. f"{self.server_url}/objects/batch",
  608. data=json.dumps(batch_data).encode("utf-8"),
  609. headers={
  610. "Content-Type": "application/vnd.git-lfs+json",
  611. "Accept": "application/vnd.git-lfs+json",
  612. },
  613. method="POST",
  614. )
  615. with urlopen(req) as response:
  616. result = json.loads(response.read())
  617. self.assertIn("objects", result)
  618. self.assertEqual(len(result["objects"]), 1)
  619. self.assertEqual(result["objects"][0]["oid"], "abc123")
  620. self.assertIn("error", result["objects"][0]) # Object doesn't exist
  621. def test_server_upload_download(self) -> None:
  622. """Test uploading and downloading an object."""
  623. import hashlib
  624. from urllib.request import Request, urlopen
  625. test_content = b"test server content"
  626. test_oid = hashlib.sha256(test_content).hexdigest()
  627. # Get upload URL via batch
  628. batch_data = {
  629. "operation": "upload",
  630. "transfers": ["basic"],
  631. "objects": [{"oid": test_oid, "size": len(test_content)}],
  632. }
  633. req = Request(
  634. f"{self.server_url}/objects/batch",
  635. data=json.dumps(batch_data).encode("utf-8"),
  636. headers={
  637. "Content-Type": "application/vnd.git-lfs+json",
  638. "Accept": "application/vnd.git-lfs+json",
  639. },
  640. method="POST",
  641. )
  642. with urlopen(req) as response:
  643. batch_result = json.loads(response.read())
  644. upload_url = batch_result["objects"][0]["actions"]["upload"]["href"]
  645. # Upload the object
  646. upload_req = Request(
  647. upload_url,
  648. data=test_content,
  649. headers={"Content-Type": "application/octet-stream"},
  650. method="PUT",
  651. )
  652. with urlopen(upload_req) as response:
  653. self.assertEqual(response.status, 200)
  654. # Download the object
  655. download_batch_data = {
  656. "operation": "download",
  657. "transfers": ["basic"],
  658. "objects": [{"oid": test_oid, "size": len(test_content)}],
  659. }
  660. req = Request(
  661. f"{self.server_url}/objects/batch",
  662. data=json.dumps(download_batch_data).encode("utf-8"),
  663. headers={
  664. "Content-Type": "application/vnd.git-lfs+json",
  665. "Accept": "application/vnd.git-lfs+json",
  666. },
  667. method="POST",
  668. )
  669. with urlopen(req) as response:
  670. download_batch_result = json.loads(response.read())
  671. download_url = download_batch_result["objects"][0]["actions"]["download"][
  672. "href"
  673. ]
  674. # Download the object
  675. download_req = Request(download_url)
  676. with urlopen(download_req) as response:
  677. downloaded_content = response.read()
  678. self.assertEqual(downloaded_content, test_content)
  679. def test_server_verify_endpoint(self) -> None:
  680. """Test the verify endpoint."""
  681. import hashlib
  682. from urllib.error import HTTPError
  683. from urllib.request import Request, urlopen
  684. test_content = b"verify test"
  685. test_oid = hashlib.sha256(test_content).hexdigest()
  686. # First upload the object
  687. self.server.lfs_store.write_object([test_content])
  688. # Test verify for existing object
  689. verify_req = Request(
  690. f"{self.server_url}/objects/{test_oid}/verify",
  691. data=json.dumps({"oid": test_oid, "size": len(test_content)}).encode(
  692. "utf-8"
  693. ),
  694. headers={"Content-Type": "application/vnd.git-lfs+json"},
  695. method="POST",
  696. )
  697. with urlopen(verify_req) as response:
  698. self.assertEqual(response.status, 200)
  699. # Test verify for non-existent object
  700. fake_oid = "0" * 64
  701. verify_req = Request(
  702. f"{self.server_url}/objects/{fake_oid}/verify",
  703. data=json.dumps({"oid": fake_oid, "size": 100}).encode("utf-8"),
  704. headers={"Content-Type": "application/vnd.git-lfs+json"},
  705. method="POST",
  706. )
  707. with self.assertRaises(HTTPError) as cm:
  708. with urlopen(verify_req):
  709. pass
  710. self.assertEqual(cm.exception.code, 404)
  711. def test_server_invalid_endpoints(self) -> None:
  712. """Test invalid endpoints return 404."""
  713. from urllib.error import HTTPError
  714. from urllib.request import Request, urlopen
  715. # Test invalid GET endpoint
  716. with self.assertRaises(HTTPError) as cm:
  717. with urlopen(f"{self.server_url}/invalid"):
  718. pass
  719. self.assertEqual(cm.exception.code, 404)
  720. # Test invalid POST endpoint
  721. req = Request(f"{self.server_url}/invalid", data=b"test", method="POST")
  722. with self.assertRaises(HTTPError) as cm:
  723. with urlopen(req):
  724. pass
  725. self.assertEqual(cm.exception.code, 404)
  726. def test_server_batch_invalid_operation(self) -> None:
  727. """Test batch endpoint with invalid operation."""
  728. from urllib.error import HTTPError
  729. from urllib.request import Request, urlopen
  730. batch_data = {"operation": "invalid", "transfers": ["basic"], "objects": []}
  731. req = Request(
  732. f"{self.server_url}/objects/batch",
  733. data=json.dumps(batch_data).encode("utf-8"),
  734. headers={"Content-Type": "application/vnd.git-lfs+json"},
  735. method="POST",
  736. )
  737. with self.assertRaises(HTTPError) as cm:
  738. with urlopen(req):
  739. pass
  740. self.assertEqual(cm.exception.code, 400)
  741. def test_server_batch_missing_fields(self) -> None:
  742. """Test batch endpoint with missing required fields."""
  743. from urllib.request import Request, urlopen
  744. # Missing oid
  745. batch_data = {
  746. "operation": "download",
  747. "transfers": ["basic"],
  748. "objects": [{"size": 100}], # Missing oid
  749. }
  750. req = Request(
  751. f"{self.server_url}/objects/batch",
  752. data=json.dumps(batch_data).encode("utf-8"),
  753. headers={"Content-Type": "application/vnd.git-lfs+json"},
  754. method="POST",
  755. )
  756. with urlopen(req) as response:
  757. result = json.loads(response.read())
  758. self.assertIn("error", result["objects"][0])
  759. self.assertIn("Missing oid", result["objects"][0]["error"]["message"])
  760. def test_server_upload_oid_mismatch(self) -> None:
  761. """Test upload with OID mismatch."""
  762. from urllib.error import HTTPError
  763. from urllib.request import Request, urlopen
  764. # Upload with wrong OID
  765. upload_req = Request(
  766. f"{self.server_url}/objects/wrongoid123",
  767. data=b"test content",
  768. headers={"Content-Type": "application/octet-stream"},
  769. method="PUT",
  770. )
  771. with self.assertRaises(HTTPError) as cm:
  772. with urlopen(upload_req):
  773. pass
  774. self.assertEqual(cm.exception.code, 400)
  775. self.assertIn("OID mismatch", cm.exception.read().decode())
  776. def test_server_download_non_existent(self) -> None:
  777. """Test downloading non-existent object."""
  778. from urllib.error import HTTPError
  779. from urllib.request import urlopen
  780. fake_oid = "0" * 64
  781. with self.assertRaises(HTTPError) as cm:
  782. with urlopen(f"{self.server_url}/objects/{fake_oid}"):
  783. pass
  784. self.assertEqual(cm.exception.code, 404)
  785. def test_server_invalid_json(self) -> None:
  786. """Test batch endpoint with invalid JSON."""
  787. from urllib.error import HTTPError
  788. from urllib.request import Request, urlopen
  789. req = Request(
  790. f"{self.server_url}/objects/batch",
  791. data=b"not json",
  792. headers={"Content-Type": "application/vnd.git-lfs+json"},
  793. method="POST",
  794. )
  795. with self.assertRaises(HTTPError) as cm:
  796. with urlopen(req):
  797. pass
  798. self.assertEqual(cm.exception.code, 400)
  799. class LFSClientTests(TestCase):
  800. """Tests for LFS client network operations."""
  801. def setUp(self) -> None:
  802. super().setUp()
  803. import threading
  804. from dulwich.lfs import HTTPLFSClient
  805. from dulwich.lfs_server import run_lfs_server
  806. # Create temporary directory for LFS storage
  807. self.test_dir = tempfile.mkdtemp()
  808. self.addCleanup(shutil.rmtree, self.test_dir)
  809. # Start LFS server in a thread
  810. self.server, self.server_url = run_lfs_server(port=0, lfs_dir=self.test_dir)
  811. self.server_thread = threading.Thread(target=self.server.serve_forever)
  812. self.server_thread.daemon = True
  813. self.server_thread.start()
  814. def cleanup_server():
  815. self.server.shutdown()
  816. self.server.server_close()
  817. self.server_thread.join(timeout=1.0)
  818. self.addCleanup(cleanup_server)
  819. # Create HTTP LFS client pointing to our test server
  820. self.client = HTTPLFSClient(self.server_url)
  821. def test_client_url_normalization(self) -> None:
  822. """Test that client URL is normalized correctly."""
  823. from dulwich.lfs import LFSClient
  824. # Test with trailing slash
  825. client = LFSClient("https://example.com/repo.git/info/lfs/")
  826. self.assertEqual(client.url, "https://example.com/repo.git/info/lfs")
  827. # Test without trailing slash
  828. client = LFSClient("https://example.com/repo.git/info/lfs")
  829. self.assertEqual(client.url, "https://example.com/repo.git/info/lfs")
  830. def test_batch_request_format(self) -> None:
  831. """Test batch request formatting."""
  832. # Create an object in the store
  833. test_content = b"test content for batch"
  834. sha = self.server.lfs_store.write_object([test_content])
  835. # Request download batch
  836. result = self.client.batch(
  837. "download", [{"oid": sha, "size": len(test_content)}]
  838. )
  839. self.assertIsNotNone(result.objects)
  840. self.assertEqual(len(result.objects), 1)
  841. self.assertEqual(result.objects[0].oid, sha)
  842. self.assertIsNotNone(result.objects[0].actions)
  843. self.assertIn("download", result.objects[0].actions)
  844. def test_download_with_verification(self) -> None:
  845. """Test download with size and hash verification."""
  846. import hashlib
  847. from dulwich.lfs import LFSError
  848. test_content = b"test content for download"
  849. test_oid = hashlib.sha256(test_content).hexdigest()
  850. # Store the object
  851. sha = self.server.lfs_store.write_object([test_content])
  852. self.assertEqual(sha, test_oid) # Verify SHA calculation
  853. # Download the object
  854. content = self.client.download(test_oid, len(test_content))
  855. self.assertEqual(content, test_content)
  856. # Test size mismatch
  857. with self.assertRaises(LFSError) as cm:
  858. self.client.download(test_oid, 999) # Wrong size
  859. self.assertIn("size", str(cm.exception))
  860. def test_upload_with_verify(self) -> None:
  861. """Test upload with verification step."""
  862. import hashlib
  863. test_content = b"upload test content"
  864. test_oid = hashlib.sha256(test_content).hexdigest()
  865. test_size = len(test_content)
  866. # Upload the object
  867. self.client.upload(test_oid, test_size, test_content)
  868. # Verify it was stored
  869. with self.server.lfs_store.open_object(test_oid) as f:
  870. stored_content = f.read()
  871. self.assertEqual(stored_content, test_content)
  872. def test_upload_already_exists(self) -> None:
  873. """Test upload when object already exists on server."""
  874. import hashlib
  875. test_content = b"existing content"
  876. test_oid = hashlib.sha256(test_content).hexdigest()
  877. # Pre-store the object
  878. self.server.lfs_store.write_object([test_content])
  879. # Upload again - should not raise an error
  880. self.client.upload(test_oid, len(test_content), test_content)
  881. # Verify it's still there
  882. with self.server.lfs_store.open_object(test_oid) as f:
  883. self.assertEqual(f.read(), test_content)
  884. def test_error_handling(self) -> None:
  885. """Test error handling for various scenarios."""
  886. from urllib.error import HTTPError
  887. from dulwich.lfs import LFSError
  888. # Test downloading non-existent object
  889. with self.assertRaises(LFSError) as cm:
  890. self.client.download(
  891. "0000000000000000000000000000000000000000000000000000000000000000", 100
  892. )
  893. self.assertIn("Object not found", str(cm.exception))
  894. # Test uploading with wrong OID
  895. with self.assertRaises(HTTPError) as cm:
  896. self.client.upload("wrong_oid", 5, b"hello")
  897. # Server should reject due to OID mismatch
  898. self.assertIn("OID mismatch", str(cm.exception))
  899. def test_from_config_validates_lfs_url(self) -> None:
  900. """Test that from_config validates lfs.url and raises error for invalid URLs."""
  901. from dulwich.config import ConfigFile
  902. from dulwich.lfs import LFSClient
  903. # Test with invalid lfs.url - no scheme/host
  904. config = ConfigFile()
  905. config.set((b"lfs",), b"url", b"objects")
  906. with self.assertRaises(ValueError) as cm:
  907. LFSClient.from_config(config)
  908. self.assertIn("Invalid lfs.url", str(cm.exception))
  909. self.assertIn("objects", str(cm.exception))
  910. # Test with another malformed URL - no scheme
  911. config.set((b"lfs",), b"url", b"//example.com/path")
  912. with self.assertRaises(ValueError) as cm:
  913. LFSClient.from_config(config)
  914. self.assertIn("Invalid lfs.url", str(cm.exception))
  915. # Test with relative path - should be rejected (not supported by git-lfs)
  916. config.set((b"lfs",), b"url", b"../lfs")
  917. with self.assertRaises(ValueError) as cm:
  918. LFSClient.from_config(config)
  919. self.assertIn("Invalid lfs.url", str(cm.exception))
  920. # Test with relative path starting with ./
  921. config.set((b"lfs",), b"url", b"./lfs")
  922. with self.assertRaises(ValueError) as cm:
  923. LFSClient.from_config(config)
  924. self.assertIn("Invalid lfs.url", str(cm.exception))
  925. # Test with unsupported scheme - git://
  926. config.set((b"lfs",), b"url", b"git://example.com/repo.git")
  927. with self.assertRaises(ValueError) as cm:
  928. LFSClient.from_config(config)
  929. self.assertIn("Invalid lfs.url", str(cm.exception))
  930. # Test with unsupported scheme - ssh://
  931. config.set((b"lfs",), b"url", b"ssh://git@example.com/repo.git")
  932. with self.assertRaises(ValueError) as cm:
  933. LFSClient.from_config(config)
  934. self.assertIn("Invalid lfs.url", str(cm.exception))
  935. # Test with http:// but no hostname
  936. config.set((b"lfs",), b"url", b"http://")
  937. with self.assertRaises(ValueError) as cm:
  938. LFSClient.from_config(config)
  939. self.assertIn("Invalid lfs.url", str(cm.exception))
  940. # Test with valid https URL - should succeed
  941. config.set((b"lfs",), b"url", b"https://example.com/repo.git/info/lfs")
  942. client = LFSClient.from_config(config)
  943. self.assertIsNotNone(client)
  944. assert client is not None # for mypy
  945. self.assertEqual(client.url, "https://example.com/repo.git/info/lfs")
  946. # Test with valid http URL - should succeed
  947. config.set((b"lfs",), b"url", b"http://localhost:8080/lfs")
  948. client = LFSClient.from_config(config)
  949. self.assertIsNotNone(client)
  950. assert client is not None # for mypy
  951. self.assertEqual(client.url, "http://localhost:8080/lfs")
  952. # Test with valid file:// URL - should succeed
  953. config.set((b"lfs",), b"url", b"file:///path/to/lfs")
  954. client = LFSClient.from_config(config)
  955. self.assertIsNotNone(client)
  956. assert client is not None # for mypy
  957. self.assertEqual(client.url, "file:///path/to/lfs")
  958. # Test with no lfs.url but valid remote - should derive URL
  959. config2 = ConfigFile()
  960. config2.set(
  961. (b"remote", b"origin"), b"url", b"https://example.com/user/repo.git"
  962. )
  963. client2 = LFSClient.from_config(config2)
  964. self.assertIsNotNone(client2)
  965. assert client2 is not None # for mypy
  966. self.assertEqual(client2.url, "https://example.com/user/repo.git/info/lfs")
  967. class FileLFSClientTests(TestCase):
  968. """Tests for FileLFSClient with file:// URLs."""
  969. def setUp(self) -> None:
  970. super().setUp()
  971. # Create temporary directory for LFS storage
  972. self.test_dir = tempfile.mkdtemp()
  973. self.addCleanup(shutil.rmtree, self.test_dir)
  974. # Create LFS store and populate with test data
  975. from dulwich.lfs import FileLFSClient, LFSStore
  976. self.lfs_store = LFSStore.create(self.test_dir)
  977. self.test_content = b"Test file content for FileLFSClient"
  978. self.test_oid = self.lfs_store.write_object([self.test_content])
  979. # Create FileLFSClient pointing to the test directory
  980. # Use Path.as_uri() to create proper file:// URLs on all platforms
  981. file_url = Path(self.test_dir).as_uri()
  982. self.client = FileLFSClient(file_url)
  983. def test_download_existing_object(self) -> None:
  984. """Test downloading an existing object from file:// URL."""
  985. content = self.client.download(self.test_oid, len(self.test_content))
  986. self.assertEqual(content, self.test_content)
  987. def test_download_missing_object(self) -> None:
  988. """Test downloading a non-existent object raises LFSError."""
  989. from dulwich.lfs import LFSError
  990. fake_oid = "0" * 64
  991. with self.assertRaises(LFSError) as cm:
  992. self.client.download(fake_oid, 100)
  993. self.assertIn("Object not found", str(cm.exception))
  994. def test_download_size_mismatch(self) -> None:
  995. """Test download with wrong size raises LFSError."""
  996. from dulwich.lfs import LFSError
  997. with self.assertRaises(LFSError) as cm:
  998. self.client.download(self.test_oid, 999) # Wrong size
  999. self.assertIn("Size mismatch", str(cm.exception))
  1000. def test_upload_new_object(self) -> None:
  1001. """Test uploading a new object to file:// URL."""
  1002. import hashlib
  1003. new_content = b"New content to upload"
  1004. new_oid = hashlib.sha256(new_content).hexdigest()
  1005. # Upload
  1006. self.client.upload(new_oid, len(new_content), new_content)
  1007. # Verify it was stored
  1008. with self.lfs_store.open_object(new_oid) as f:
  1009. stored_content = f.read()
  1010. self.assertEqual(stored_content, new_content)
  1011. def test_upload_size_mismatch(self) -> None:
  1012. """Test upload with mismatched size raises LFSError."""
  1013. from dulwich.lfs import LFSError
  1014. content = b"test"
  1015. oid = "0" * 64
  1016. with self.assertRaises(LFSError) as cm:
  1017. self.client.upload(oid, 999, content) # Wrong size
  1018. self.assertIn("Size mismatch", str(cm.exception))
  1019. def test_upload_oid_mismatch(self) -> None:
  1020. """Test upload with mismatched OID raises LFSError."""
  1021. from dulwich.lfs import LFSError
  1022. content = b"test"
  1023. wrong_oid = "0" * 64 # Won't match actual SHA256
  1024. with self.assertRaises(LFSError) as cm:
  1025. self.client.upload(wrong_oid, len(content), content)
  1026. self.assertIn("OID mismatch", str(cm.exception))
  1027. def test_from_config_creates_file_client(self) -> None:
  1028. """Test that from_config creates FileLFSClient for file:// URLs."""
  1029. from dulwich.config import ConfigFile
  1030. from dulwich.lfs import FileLFSClient, LFSClient
  1031. config = ConfigFile()
  1032. file_url = Path(self.test_dir).as_uri()
  1033. config.set((b"lfs",), b"url", file_url.encode())
  1034. client = LFSClient.from_config(config)
  1035. self.assertIsInstance(client, FileLFSClient)
  1036. assert client is not None # for mypy
  1037. self.assertEqual(client.url, file_url)
  1038. def test_round_trip(self) -> None:
  1039. """Test uploading and then downloading an object."""
  1040. import hashlib
  1041. content = b"Round trip test content"
  1042. oid = hashlib.sha256(content).hexdigest()
  1043. # Upload
  1044. self.client.upload(oid, len(content), content)
  1045. # Download
  1046. downloaded = self.client.download(oid, len(content))
  1047. self.assertEqual(downloaded, content)