2
0

test_lfs.py 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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.client import LocalGitClient
  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. source_worktree = source_repo.get_worktree()
  304. source_worktree.stage([b".gitattributes", b"test.bin"])
  305. source_worktree.commit(
  306. message=b"Add LFS tracked file",
  307. committer=b"Test <test@example.com>",
  308. author=b"Test <test@example.com>",
  309. commit_timestamp=1000000000,
  310. author_timestamp=1000000000,
  311. commit_timezone=0,
  312. author_timezone=0,
  313. )
  314. source_repo.close()
  315. # Clone the repository
  316. target_dir = os.path.join(self.test_dir, "target")
  317. client = LocalGitClient()
  318. target_repo = client.clone(source_dir, target_dir)
  319. # Verify no LFS commands in config
  320. target_config = target_repo.get_config_stack()
  321. with self.assertRaises(KeyError):
  322. target_config.get((b"filter", b"lfs"), b"smudge")
  323. # Check the cloned file
  324. cloned_file = os.path.join(target_dir, "test.bin")
  325. with open(cloned_file, "rb") as f:
  326. content = f.read()
  327. # Should still be a pointer (LFS object not in target's store)
  328. self.assertTrue(
  329. content.startswith(b"version https://git-lfs.github.com/spec/v1")
  330. )
  331. self.assertIn(test_oid.encode(), content)
  332. target_repo.close()
  333. def test_builtin_lfs_with_local_objects(self) -> None:
  334. """Test built-in LFS filter when objects are available locally."""
  335. # No LFS config
  336. config = self.repo.get_config()
  337. config.write_to_path()
  338. # Create .gitattributes
  339. gitattributes_path = os.path.join(self.test_dir, ".gitattributes")
  340. with open(gitattributes_path, "wb") as f:
  341. f.write(b"*.dat filter=lfs\n")
  342. # Create LFS store and add object
  343. test_content = b"Hello from LFS!"
  344. lfs_store = LFSStore.from_repo(self.repo, create=True)
  345. test_oid = lfs_store.write_object([test_content])
  346. # Create pointer file
  347. pointer = LFSPointer(test_oid, len(test_content))
  348. pointer_file = os.path.join(self.test_dir, "data.dat")
  349. with open(pointer_file, "wb") as f:
  350. f.write(pointer.to_bytes())
  351. # Commit
  352. worktree = self.repo.get_worktree()
  353. worktree.stage([b".gitattributes", b"data.dat"])
  354. worktree.commit(
  355. message=b"Add LFS file",
  356. committer=b"Test <test@example.com>",
  357. author=b"Test <test@example.com>",
  358. commit_timestamp=1000000000,
  359. author_timestamp=1000000000,
  360. commit_timezone=0,
  361. author_timezone=0,
  362. )
  363. # Reset index to trigger checkout with filter
  364. self.repo.get_worktree().reset_index()
  365. # Check file content
  366. with open(pointer_file, "rb") as f:
  367. content = f.read()
  368. # Built-in filter should have converted pointer to actual content
  369. self.assertEqual(content, test_content)
  370. def test_builtin_lfs_filter_used(self) -> None:
  371. """Verify that built-in LFS filter is used when no config exists."""
  372. # Get filter registry
  373. normalizer = self.repo.get_blob_normalizer()
  374. filter_registry = normalizer.filter_registry
  375. lfs_driver = filter_registry.get_driver("lfs")
  376. # Should be built-in LFS filter
  377. self.assertIsInstance(lfs_driver, LFSFilterDriver)
  378. self.assertEqual(type(lfs_driver).__module__, "dulwich.lfs")
  379. class LFSFilterDriverTests(TestCase):
  380. def setUp(self) -> None:
  381. super().setUp()
  382. self.test_dir = tempfile.mkdtemp()
  383. self.addCleanup(shutil.rmtree, self.test_dir)
  384. self.lfs_store = LFSStore.create(self.test_dir)
  385. self.filter_driver = LFSFilterDriver(self.lfs_store)
  386. def test_clean_new_file(self) -> None:
  387. """Test clean filter on new file content."""
  388. content = b"This is a test file content"
  389. result = self.filter_driver.clean(content)
  390. # Result should be an LFS pointer
  391. pointer = LFSPointer.from_bytes(result)
  392. self.assertIsNotNone(pointer)
  393. self.assertEqual(pointer.size, len(content))
  394. # Content should be stored in LFS
  395. with self.lfs_store.open_object(pointer.oid) as f:
  396. self.assertEqual(f.read(), content)
  397. def test_clean_existing_pointer(self) -> None:
  398. """Test clean filter on already-pointer content."""
  399. # Create a pointer
  400. pointer = LFSPointer(
  401. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 1234
  402. )
  403. pointer_data = pointer.to_bytes()
  404. # Clean should return the pointer unchanged
  405. result = self.filter_driver.clean(pointer_data)
  406. self.assertEqual(result, pointer_data)
  407. def test_smudge_valid_pointer(self) -> None:
  408. """Test smudge filter with valid pointer."""
  409. # Store some content
  410. content = b"This is the actual file content"
  411. sha = self.lfs_store.write_object([content])
  412. # Create pointer
  413. pointer = LFSPointer(sha, len(content))
  414. pointer_data = pointer.to_bytes()
  415. # Smudge should return the actual content
  416. result = self.filter_driver.smudge(pointer_data)
  417. self.assertEqual(result, content)
  418. def test_smudge_missing_object(self) -> None:
  419. """Test smudge filter with missing LFS object."""
  420. # Create pointer to non-existent object
  421. pointer = LFSPointer(
  422. "0000000000000000000000000000000000000000000000000000000000000000", 1234
  423. )
  424. pointer_data = pointer.to_bytes()
  425. # Smudge should return the pointer as-is when object is missing
  426. result = self.filter_driver.smudge(pointer_data)
  427. self.assertEqual(result, pointer_data)
  428. def test_smudge_non_pointer(self) -> None:
  429. """Test smudge filter on non-pointer content."""
  430. content = b"This is not an LFS pointer"
  431. # Smudge should return content unchanged
  432. result = self.filter_driver.smudge(content)
  433. self.assertEqual(result, content)
  434. def test_round_trip(self) -> None:
  435. """Test clean followed by smudge."""
  436. original_content = b"Round trip test content"
  437. # Clean (working tree -> repo)
  438. pointer_data = self.filter_driver.clean(original_content)
  439. # Verify it's a pointer
  440. pointer = LFSPointer.from_bytes(pointer_data)
  441. self.assertIsNotNone(pointer)
  442. # Smudge (repo -> working tree)
  443. restored_content = self.filter_driver.smudge(pointer_data)
  444. # Should get back the original content
  445. self.assertEqual(restored_content, original_content)
  446. def test_clean_empty_file(self) -> None:
  447. """Test clean filter on empty file."""
  448. content = b""
  449. result = self.filter_driver.clean(content)
  450. # Result should be an LFS pointer
  451. pointer = LFSPointer.from_bytes(result)
  452. self.assertIsNotNone(pointer)
  453. self.assertEqual(pointer.size, 0)
  454. # Empty content should be stored in LFS
  455. with self.lfs_store.open_object(pointer.oid) as f:
  456. self.assertEqual(f.read(), content)
  457. def test_clean_large_file(self) -> None:
  458. """Test clean filter on large file."""
  459. # Create a large file (1MB)
  460. content = b"x" * (1024 * 1024)
  461. result = self.filter_driver.clean(content)
  462. # Result should be an LFS pointer
  463. pointer = LFSPointer.from_bytes(result)
  464. self.assertIsNotNone(pointer)
  465. self.assertEqual(pointer.size, len(content))
  466. # Content should be stored in LFS
  467. with self.lfs_store.open_object(pointer.oid) as f:
  468. self.assertEqual(f.read(), content)
  469. def test_smudge_corrupt_pointer(self) -> None:
  470. """Test smudge filter with corrupt pointer data."""
  471. # Create corrupt pointer data
  472. corrupt_data = (
  473. b"version https://git-lfs.github.com/spec/v1\noid sha256:invalid\n"
  474. )
  475. # Smudge should return the data as-is
  476. result = self.filter_driver.smudge(corrupt_data)
  477. self.assertEqual(result, corrupt_data)
  478. def test_clean_unicode_content(self) -> None:
  479. """Test clean filter with unicode content."""
  480. # UTF-8 encoded unicode content
  481. content = "Hello 世界 🌍".encode()
  482. result = self.filter_driver.clean(content)
  483. # Result should be an LFS pointer
  484. pointer = LFSPointer.from_bytes(result)
  485. self.assertIsNotNone(pointer)
  486. # Content should be preserved exactly
  487. with self.lfs_store.open_object(pointer.oid) as f:
  488. self.assertEqual(f.read(), content)
  489. class LFSStoreEdgeCaseTests(TestCase):
  490. """Edge case tests for LFS store."""
  491. def setUp(self) -> None:
  492. super().setUp()
  493. self.test_dir = tempfile.mkdtemp()
  494. self.addCleanup(shutil.rmtree, self.test_dir)
  495. self.lfs = LFSStore.create(self.test_dir)
  496. def test_concurrent_writes(self) -> None:
  497. """Test that concurrent writes to same content work correctly."""
  498. content = b"duplicate content"
  499. # Write the same content multiple times
  500. sha1 = self.lfs.write_object([content])
  501. sha2 = self.lfs.write_object([content])
  502. # Should get the same SHA
  503. self.assertEqual(sha1, sha2)
  504. # Content should be stored only once
  505. with self.lfs.open_object(sha1) as f:
  506. self.assertEqual(f.read(), content)
  507. def test_write_with_generator(self) -> None:
  508. """Test writing object with generator chunks."""
  509. def chunk_generator():
  510. yield b"chunk1"
  511. yield b"chunk2"
  512. yield b"chunk3"
  513. sha = self.lfs.write_object(chunk_generator())
  514. # Verify content
  515. with self.lfs.open_object(sha) as f:
  516. self.assertEqual(f.read(), b"chunk1chunk2chunk3")
  517. def test_partial_write_rollback(self) -> None:
  518. """Test that partial writes don't leave artifacts."""
  519. import os
  520. # Count initial objects
  521. objects_dir = os.path.join(self.test_dir, "objects")
  522. initial_count = sum(len(files) for _, _, files in os.walk(objects_dir))
  523. # Try to write with a failing generator
  524. def failing_generator():
  525. yield b"chunk1"
  526. raise RuntimeError("Simulated error")
  527. # This should fail
  528. with self.assertRaises(RuntimeError):
  529. self.lfs.write_object(failing_generator())
  530. # No new objects should have been created
  531. final_count = sum(len(files) for _, _, files in os.walk(objects_dir))
  532. self.assertEqual(initial_count, final_count)
  533. class LFSPointerEdgeCaseTests(TestCase):
  534. """Edge case tests for LFS pointer parsing."""
  535. def test_pointer_with_windows_line_endings(self) -> None:
  536. """Test parsing pointer with Windows line endings."""
  537. pointer_data = (
  538. b"version https://git-lfs.github.com/spec/v1\r\n"
  539. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\r\n"
  540. b"size 1234\r\n"
  541. )
  542. pointer = LFSPointer.from_bytes(pointer_data)
  543. self.assertIsNotNone(pointer)
  544. self.assertEqual(pointer.size, 1234)
  545. def test_pointer_with_extra_whitespace(self) -> None:
  546. """Test parsing pointer with extra whitespace."""
  547. pointer_data = (
  548. b"version https://git-lfs.github.com/spec/v1 \n"
  549. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  550. b"size 1234 \n"
  551. )
  552. pointer = LFSPointer.from_bytes(pointer_data)
  553. self.assertIsNotNone(pointer)
  554. self.assertEqual(pointer.size, 1234)
  555. def test_pointer_case_sensitivity(self) -> None:
  556. """Test that pointer parsing is case sensitive."""
  557. # Version line must be exact
  558. pointer_data = (
  559. b"Version https://git-lfs.github.com/spec/v1\n" # Capital V
  560. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  561. b"size 1234\n"
  562. )
  563. pointer = LFSPointer.from_bytes(pointer_data)
  564. self.assertIsNone(pointer) # Should fail due to case
  565. def test_pointer_oid_formats(self) -> None:
  566. """Test different OID formats."""
  567. # SHA256 is currently the only supported format
  568. # Test SHA1 format (should fail)
  569. pointer_data = (
  570. b"version https://git-lfs.github.com/spec/v1\n"
  571. b"oid sha1:356a192b7913b04c54574d18c28d46e6395428ab\n" # SHA1
  572. b"size 1234\n"
  573. )
  574. pointer = LFSPointer.from_bytes(pointer_data)
  575. # This might be accepted but marked as invalid OID
  576. if pointer:
  577. self.assertFalse(pointer.is_valid_oid())
  578. def test_pointer_size_limits(self) -> None:
  579. """Test size value limits."""
  580. # Test with very large size
  581. pointer_data = (
  582. b"version https://git-lfs.github.com/spec/v1\n"
  583. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  584. b"size 999999999999999999\n" # Very large number
  585. )
  586. pointer = LFSPointer.from_bytes(pointer_data)
  587. self.assertIsNotNone(pointer)
  588. self.assertEqual(pointer.size, 999999999999999999)
  589. # Test with negative size (should fail)
  590. pointer_data = (
  591. b"version https://git-lfs.github.com/spec/v1\n"
  592. b"oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
  593. b"size -1\n"
  594. )
  595. pointer = LFSPointer.from_bytes(pointer_data)
  596. self.assertIsNone(pointer) # Should fail with negative size
  597. class LFSServerTests(TestCase):
  598. """Tests for the LFS server implementation."""
  599. def setUp(self) -> None:
  600. super().setUp()
  601. import threading
  602. from dulwich.lfs_server import run_lfs_server
  603. # Create temporary directory for LFS storage
  604. self.test_dir = tempfile.mkdtemp()
  605. self.addCleanup(shutil.rmtree, self.test_dir)
  606. # Start LFS server
  607. self.server, self.server_url = run_lfs_server(port=0, lfs_dir=self.test_dir)
  608. self.server_thread = threading.Thread(target=self.server.serve_forever)
  609. self.server_thread.daemon = True
  610. self.server_thread.start()
  611. def cleanup_server():
  612. self.server.shutdown()
  613. self.server.server_close()
  614. self.server_thread.join(timeout=1.0)
  615. self.addCleanup(cleanup_server)
  616. def test_server_batch_endpoint(self) -> None:
  617. """Test the batch endpoint directly."""
  618. from urllib.request import Request, urlopen
  619. # Create batch request
  620. batch_data = {
  621. "operation": "download",
  622. "transfers": ["basic"],
  623. "objects": [{"oid": "abc123", "size": 100}],
  624. }
  625. req = Request(
  626. f"{self.server_url}/objects/batch",
  627. data=json.dumps(batch_data).encode("utf-8"),
  628. headers={
  629. "Content-Type": "application/vnd.git-lfs+json",
  630. "Accept": "application/vnd.git-lfs+json",
  631. },
  632. method="POST",
  633. )
  634. with urlopen(req) as response:
  635. result = json.loads(response.read())
  636. self.assertIn("objects", result)
  637. self.assertEqual(len(result["objects"]), 1)
  638. self.assertEqual(result["objects"][0]["oid"], "abc123")
  639. self.assertIn("error", result["objects"][0]) # Object doesn't exist
  640. def test_server_upload_download(self) -> None:
  641. """Test uploading and downloading an object."""
  642. import hashlib
  643. from urllib.request import Request, urlopen
  644. test_content = b"test server content"
  645. test_oid = hashlib.sha256(test_content).hexdigest()
  646. # Get upload URL via batch
  647. batch_data = {
  648. "operation": "upload",
  649. "transfers": ["basic"],
  650. "objects": [{"oid": test_oid, "size": len(test_content)}],
  651. }
  652. req = Request(
  653. f"{self.server_url}/objects/batch",
  654. data=json.dumps(batch_data).encode("utf-8"),
  655. headers={
  656. "Content-Type": "application/vnd.git-lfs+json",
  657. "Accept": "application/vnd.git-lfs+json",
  658. },
  659. method="POST",
  660. )
  661. with urlopen(req) as response:
  662. batch_result = json.loads(response.read())
  663. upload_url = batch_result["objects"][0]["actions"]["upload"]["href"]
  664. # Upload the object
  665. upload_req = Request(
  666. upload_url,
  667. data=test_content,
  668. headers={"Content-Type": "application/octet-stream"},
  669. method="PUT",
  670. )
  671. with urlopen(upload_req) as response:
  672. self.assertEqual(response.status, 200)
  673. # Download the object
  674. download_batch_data = {
  675. "operation": "download",
  676. "transfers": ["basic"],
  677. "objects": [{"oid": test_oid, "size": len(test_content)}],
  678. }
  679. req = Request(
  680. f"{self.server_url}/objects/batch",
  681. data=json.dumps(download_batch_data).encode("utf-8"),
  682. headers={
  683. "Content-Type": "application/vnd.git-lfs+json",
  684. "Accept": "application/vnd.git-lfs+json",
  685. },
  686. method="POST",
  687. )
  688. with urlopen(req) as response:
  689. download_batch_result = json.loads(response.read())
  690. download_url = download_batch_result["objects"][0]["actions"]["download"][
  691. "href"
  692. ]
  693. # Download the object
  694. download_req = Request(download_url)
  695. with urlopen(download_req) as response:
  696. downloaded_content = response.read()
  697. self.assertEqual(downloaded_content, test_content)
  698. def test_server_verify_endpoint(self) -> None:
  699. """Test the verify endpoint."""
  700. import hashlib
  701. from urllib.error import HTTPError
  702. from urllib.request import Request, urlopen
  703. test_content = b"verify test"
  704. test_oid = hashlib.sha256(test_content).hexdigest()
  705. # First upload the object
  706. self.server.lfs_store.write_object([test_content])
  707. # Test verify for existing object
  708. verify_req = Request(
  709. f"{self.server_url}/objects/{test_oid}/verify",
  710. data=json.dumps({"oid": test_oid, "size": len(test_content)}).encode(
  711. "utf-8"
  712. ),
  713. headers={"Content-Type": "application/vnd.git-lfs+json"},
  714. method="POST",
  715. )
  716. with urlopen(verify_req) as response:
  717. self.assertEqual(response.status, 200)
  718. # Test verify for non-existent object
  719. fake_oid = "0" * 64
  720. verify_req = Request(
  721. f"{self.server_url}/objects/{fake_oid}/verify",
  722. data=json.dumps({"oid": fake_oid, "size": 100}).encode("utf-8"),
  723. headers={"Content-Type": "application/vnd.git-lfs+json"},
  724. method="POST",
  725. )
  726. with self.assertRaises(HTTPError) as cm:
  727. with urlopen(verify_req):
  728. pass
  729. self.assertEqual(cm.exception.code, 404)
  730. def test_server_invalid_endpoints(self) -> None:
  731. """Test invalid endpoints return 404."""
  732. from urllib.error import HTTPError
  733. from urllib.request import Request, urlopen
  734. # Test invalid GET endpoint
  735. with self.assertRaises(HTTPError) as cm:
  736. with urlopen(f"{self.server_url}/invalid"):
  737. pass
  738. self.assertEqual(cm.exception.code, 404)
  739. # Test invalid POST endpoint
  740. req = Request(f"{self.server_url}/invalid", data=b"test", method="POST")
  741. with self.assertRaises(HTTPError) as cm:
  742. with urlopen(req):
  743. pass
  744. self.assertEqual(cm.exception.code, 404)
  745. def test_server_batch_invalid_operation(self) -> None:
  746. """Test batch endpoint with invalid operation."""
  747. from urllib.error import HTTPError
  748. from urllib.request import Request, urlopen
  749. batch_data = {"operation": "invalid", "transfers": ["basic"], "objects": []}
  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 self.assertRaises(HTTPError) as cm:
  757. with urlopen(req):
  758. pass
  759. self.assertEqual(cm.exception.code, 400)
  760. def test_server_batch_missing_fields(self) -> None:
  761. """Test batch endpoint with missing required fields."""
  762. from urllib.request import Request, urlopen
  763. # Missing oid
  764. batch_data = {
  765. "operation": "download",
  766. "transfers": ["basic"],
  767. "objects": [{"size": 100}], # Missing oid
  768. }
  769. req = Request(
  770. f"{self.server_url}/objects/batch",
  771. data=json.dumps(batch_data).encode("utf-8"),
  772. headers={"Content-Type": "application/vnd.git-lfs+json"},
  773. method="POST",
  774. )
  775. with urlopen(req) as response:
  776. result = json.loads(response.read())
  777. self.assertIn("error", result["objects"][0])
  778. self.assertIn("Missing oid", result["objects"][0]["error"]["message"])
  779. def test_server_upload_oid_mismatch(self) -> None:
  780. """Test upload with OID mismatch."""
  781. from urllib.error import HTTPError
  782. from urllib.request import Request, urlopen
  783. # Upload with wrong OID
  784. upload_req = Request(
  785. f"{self.server_url}/objects/wrongoid123",
  786. data=b"test content",
  787. headers={"Content-Type": "application/octet-stream"},
  788. method="PUT",
  789. )
  790. with self.assertRaises(HTTPError) as cm:
  791. with urlopen(upload_req):
  792. pass
  793. self.assertEqual(cm.exception.code, 400)
  794. self.assertIn("OID mismatch", cm.exception.read().decode())
  795. def test_server_download_non_existent(self) -> None:
  796. """Test downloading non-existent object."""
  797. from urllib.error import HTTPError
  798. from urllib.request import urlopen
  799. fake_oid = "0" * 64
  800. with self.assertRaises(HTTPError) as cm:
  801. with urlopen(f"{self.server_url}/objects/{fake_oid}"):
  802. pass
  803. self.assertEqual(cm.exception.code, 404)
  804. def test_server_invalid_json(self) -> None:
  805. """Test batch endpoint with invalid JSON."""
  806. from urllib.error import HTTPError
  807. from urllib.request import Request, urlopen
  808. req = Request(
  809. f"{self.server_url}/objects/batch",
  810. data=b"not json",
  811. headers={"Content-Type": "application/vnd.git-lfs+json"},
  812. method="POST",
  813. )
  814. with self.assertRaises(HTTPError) as cm:
  815. with urlopen(req):
  816. pass
  817. self.assertEqual(cm.exception.code, 400)
  818. class LFSClientTests(TestCase):
  819. """Tests for LFS client network operations."""
  820. def setUp(self) -> None:
  821. super().setUp()
  822. import threading
  823. from dulwich.lfs import HTTPLFSClient
  824. from dulwich.lfs_server import run_lfs_server
  825. # Create temporary directory for LFS storage
  826. self.test_dir = tempfile.mkdtemp()
  827. self.addCleanup(shutil.rmtree, self.test_dir)
  828. # Start LFS server in a thread
  829. self.server, self.server_url = run_lfs_server(port=0, lfs_dir=self.test_dir)
  830. self.server_thread = threading.Thread(target=self.server.serve_forever)
  831. self.server_thread.daemon = True
  832. self.server_thread.start()
  833. def cleanup_server():
  834. self.server.shutdown()
  835. self.server.server_close()
  836. self.server_thread.join(timeout=1.0)
  837. self.addCleanup(cleanup_server)
  838. # Create HTTP LFS client pointing to our test server
  839. self.client = HTTPLFSClient(self.server_url)
  840. def test_client_url_normalization(self) -> None:
  841. """Test that client URL is normalized correctly."""
  842. from dulwich.lfs import LFSClient
  843. # Test with trailing slash
  844. client = LFSClient("https://example.com/repo.git/info/lfs/")
  845. self.assertEqual(client.url, "https://example.com/repo.git/info/lfs")
  846. # Test without trailing slash
  847. client = LFSClient("https://example.com/repo.git/info/lfs")
  848. self.assertEqual(client.url, "https://example.com/repo.git/info/lfs")
  849. def test_batch_request_format(self) -> None:
  850. """Test batch request formatting."""
  851. # Create an object in the store
  852. test_content = b"test content for batch"
  853. sha = self.server.lfs_store.write_object([test_content])
  854. # Request download batch
  855. result = self.client.batch(
  856. "download", [{"oid": sha, "size": len(test_content)}]
  857. )
  858. self.assertIsNotNone(result.objects)
  859. self.assertEqual(len(result.objects), 1)
  860. self.assertEqual(result.objects[0].oid, sha)
  861. self.assertIsNotNone(result.objects[0].actions)
  862. self.assertIn("download", result.objects[0].actions)
  863. def test_download_with_verification(self) -> None:
  864. """Test download with size and hash verification."""
  865. import hashlib
  866. from dulwich.lfs import LFSError
  867. test_content = b"test content for download"
  868. test_oid = hashlib.sha256(test_content).hexdigest()
  869. # Store the object
  870. sha = self.server.lfs_store.write_object([test_content])
  871. self.assertEqual(sha, test_oid) # Verify SHA calculation
  872. # Download the object
  873. content = self.client.download(test_oid, len(test_content))
  874. self.assertEqual(content, test_content)
  875. # Test size mismatch
  876. with self.assertRaises(LFSError) as cm:
  877. self.client.download(test_oid, 999) # Wrong size
  878. self.assertIn("size", str(cm.exception))
  879. def test_upload_with_verify(self) -> None:
  880. """Test upload with verification step."""
  881. import hashlib
  882. test_content = b"upload test content"
  883. test_oid = hashlib.sha256(test_content).hexdigest()
  884. test_size = len(test_content)
  885. # Upload the object
  886. self.client.upload(test_oid, test_size, test_content)
  887. # Verify it was stored
  888. with self.server.lfs_store.open_object(test_oid) as f:
  889. stored_content = f.read()
  890. self.assertEqual(stored_content, test_content)
  891. def test_upload_already_exists(self) -> None:
  892. """Test upload when object already exists on server."""
  893. import hashlib
  894. test_content = b"existing content"
  895. test_oid = hashlib.sha256(test_content).hexdigest()
  896. # Pre-store the object
  897. self.server.lfs_store.write_object([test_content])
  898. # Upload again - should not raise an error
  899. self.client.upload(test_oid, len(test_content), test_content)
  900. # Verify it's still there
  901. with self.server.lfs_store.open_object(test_oid) as f:
  902. self.assertEqual(f.read(), test_content)
  903. def test_error_handling(self) -> None:
  904. """Test error handling for various scenarios."""
  905. from urllib.error import HTTPError
  906. from dulwich.lfs import LFSError
  907. # Test downloading non-existent object
  908. with self.assertRaises(LFSError) as cm:
  909. self.client.download(
  910. "0000000000000000000000000000000000000000000000000000000000000000", 100
  911. )
  912. self.assertIn("Object not found", str(cm.exception))
  913. # Test uploading with wrong OID
  914. with self.assertRaises(HTTPError) as cm:
  915. self.client.upload("wrong_oid", 5, b"hello")
  916. # Server should reject due to OID mismatch
  917. self.assertIn("OID mismatch", str(cm.exception))
  918. def test_from_config_validates_lfs_url(self) -> None:
  919. """Test that from_config validates lfs.url and raises error for invalid URLs."""
  920. from dulwich.config import ConfigFile
  921. from dulwich.lfs import LFSClient
  922. # Test with invalid lfs.url - no scheme/host
  923. config = ConfigFile()
  924. config.set((b"lfs",), b"url", b"objects")
  925. with self.assertRaises(ValueError) as cm:
  926. LFSClient.from_config(config)
  927. self.assertIn("Invalid lfs.url", str(cm.exception))
  928. self.assertIn("objects", str(cm.exception))
  929. # Test with another malformed URL - no scheme
  930. config.set((b"lfs",), b"url", b"//example.com/path")
  931. with self.assertRaises(ValueError) as cm:
  932. LFSClient.from_config(config)
  933. self.assertIn("Invalid lfs.url", str(cm.exception))
  934. # Test with relative path - should be rejected (not supported by git-lfs)
  935. config.set((b"lfs",), b"url", b"../lfs")
  936. with self.assertRaises(ValueError) as cm:
  937. LFSClient.from_config(config)
  938. self.assertIn("Invalid lfs.url", str(cm.exception))
  939. # Test with relative path starting with ./
  940. config.set((b"lfs",), b"url", b"./lfs")
  941. with self.assertRaises(ValueError) as cm:
  942. LFSClient.from_config(config)
  943. self.assertIn("Invalid lfs.url", str(cm.exception))
  944. # Test with unsupported scheme - git://
  945. config.set((b"lfs",), b"url", b"git://example.com/repo.git")
  946. with self.assertRaises(ValueError) as cm:
  947. LFSClient.from_config(config)
  948. self.assertIn("Invalid lfs.url", str(cm.exception))
  949. # Test with unsupported scheme - ssh://
  950. config.set((b"lfs",), b"url", b"ssh://git@example.com/repo.git")
  951. with self.assertRaises(ValueError) as cm:
  952. LFSClient.from_config(config)
  953. self.assertIn("Invalid lfs.url", str(cm.exception))
  954. # Test with http:// but no hostname
  955. config.set((b"lfs",), b"url", b"http://")
  956. with self.assertRaises(ValueError) as cm:
  957. LFSClient.from_config(config)
  958. self.assertIn("Invalid lfs.url", str(cm.exception))
  959. # Test with valid https URL - should succeed
  960. config.set((b"lfs",), b"url", b"https://example.com/repo.git/info/lfs")
  961. client = LFSClient.from_config(config)
  962. self.assertIsNotNone(client)
  963. assert client is not None # for mypy
  964. self.assertEqual(client.url, "https://example.com/repo.git/info/lfs")
  965. # Test with valid http URL - should succeed
  966. config.set((b"lfs",), b"url", b"http://localhost:8080/lfs")
  967. client = LFSClient.from_config(config)
  968. self.assertIsNotNone(client)
  969. assert client is not None # for mypy
  970. self.assertEqual(client.url, "http://localhost:8080/lfs")
  971. # Test with valid file:// URL - should succeed
  972. config.set((b"lfs",), b"url", b"file:///path/to/lfs")
  973. client = LFSClient.from_config(config)
  974. self.assertIsNotNone(client)
  975. assert client is not None # for mypy
  976. self.assertEqual(client.url, "file:///path/to/lfs")
  977. # Test with no lfs.url but valid remote - should derive URL
  978. config2 = ConfigFile()
  979. config2.set(
  980. (b"remote", b"origin"), b"url", b"https://example.com/user/repo.git"
  981. )
  982. client2 = LFSClient.from_config(config2)
  983. self.assertIsNotNone(client2)
  984. assert client2 is not None # for mypy
  985. self.assertEqual(client2.url, "https://example.com/user/repo.git/info/lfs")
  986. class FileLFSClientTests(TestCase):
  987. """Tests for FileLFSClient with file:// URLs."""
  988. def setUp(self) -> None:
  989. super().setUp()
  990. # Create temporary directory for LFS storage
  991. self.test_dir = tempfile.mkdtemp()
  992. self.addCleanup(shutil.rmtree, self.test_dir)
  993. # Create LFS store and populate with test data
  994. from dulwich.lfs import FileLFSClient, LFSStore
  995. self.lfs_store = LFSStore.create(self.test_dir)
  996. self.test_content = b"Test file content for FileLFSClient"
  997. self.test_oid = self.lfs_store.write_object([self.test_content])
  998. # Create FileLFSClient pointing to the test directory
  999. # Use Path.as_uri() to create proper file:// URLs on all platforms
  1000. file_url = Path(self.test_dir).as_uri()
  1001. self.client = FileLFSClient(file_url)
  1002. def test_download_existing_object(self) -> None:
  1003. """Test downloading an existing object from file:// URL."""
  1004. content = self.client.download(self.test_oid, len(self.test_content))
  1005. self.assertEqual(content, self.test_content)
  1006. def test_download_missing_object(self) -> None:
  1007. """Test downloading a non-existent object raises LFSError."""
  1008. from dulwich.lfs import LFSError
  1009. fake_oid = "0" * 64
  1010. with self.assertRaises(LFSError) as cm:
  1011. self.client.download(fake_oid, 100)
  1012. self.assertIn("Object not found", str(cm.exception))
  1013. def test_download_size_mismatch(self) -> None:
  1014. """Test download with wrong size raises LFSError."""
  1015. from dulwich.lfs import LFSError
  1016. with self.assertRaises(LFSError) as cm:
  1017. self.client.download(self.test_oid, 999) # Wrong size
  1018. self.assertIn("Size mismatch", str(cm.exception))
  1019. def test_upload_new_object(self) -> None:
  1020. """Test uploading a new object to file:// URL."""
  1021. import hashlib
  1022. new_content = b"New content to upload"
  1023. new_oid = hashlib.sha256(new_content).hexdigest()
  1024. # Upload
  1025. self.client.upload(new_oid, len(new_content), new_content)
  1026. # Verify it was stored
  1027. with self.lfs_store.open_object(new_oid) as f:
  1028. stored_content = f.read()
  1029. self.assertEqual(stored_content, new_content)
  1030. def test_upload_size_mismatch(self) -> None:
  1031. """Test upload with mismatched size raises LFSError."""
  1032. from dulwich.lfs import LFSError
  1033. content = b"test"
  1034. oid = "0" * 64
  1035. with self.assertRaises(LFSError) as cm:
  1036. self.client.upload(oid, 999, content) # Wrong size
  1037. self.assertIn("Size mismatch", str(cm.exception))
  1038. def test_upload_oid_mismatch(self) -> None:
  1039. """Test upload with mismatched OID raises LFSError."""
  1040. from dulwich.lfs import LFSError
  1041. content = b"test"
  1042. wrong_oid = "0" * 64 # Won't match actual SHA256
  1043. with self.assertRaises(LFSError) as cm:
  1044. self.client.upload(wrong_oid, len(content), content)
  1045. self.assertIn("OID mismatch", str(cm.exception))
  1046. def test_from_config_creates_file_client(self) -> None:
  1047. """Test that from_config creates FileLFSClient for file:// URLs."""
  1048. from dulwich.config import ConfigFile
  1049. from dulwich.lfs import FileLFSClient, LFSClient
  1050. config = ConfigFile()
  1051. file_url = Path(self.test_dir).as_uri()
  1052. config.set((b"lfs",), b"url", file_url.encode())
  1053. client = LFSClient.from_config(config)
  1054. self.assertIsInstance(client, FileLFSClient)
  1055. assert client is not None # for mypy
  1056. self.assertEqual(client.url, file_url)
  1057. def test_round_trip(self) -> None:
  1058. """Test uploading and then downloading an object."""
  1059. import hashlib
  1060. content = b"Round trip test content"
  1061. oid = hashlib.sha256(content).hexdigest()
  1062. # Upload
  1063. self.client.upload(oid, len(content), content)
  1064. # Download
  1065. downloaded = self.client.download(oid, len(content))
  1066. self.assertEqual(downloaded, content)