2
0

test_lfs.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # test_lfs.py -- tests for LFS
  2. # Copyright (C) 2020 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for LFS support."""
  22. import shutil
  23. import tempfile
  24. from dulwich.lfs import LFSStore
  25. from . import TestCase
  26. class LFSTests(TestCase):
  27. def setUp(self) -> None:
  28. super().setUp()
  29. self.test_dir = tempfile.mkdtemp()
  30. self.addCleanup(shutil.rmtree, self.test_dir)
  31. self.lfs = LFSStore.create(self.test_dir)
  32. def test_create(self) -> None:
  33. sha = self.lfs.write_object([b"a", b"b"])
  34. with self.lfs.open_object(sha) as f:
  35. self.assertEqual(b"ab", f.read())
  36. def test_missing(self) -> None:
  37. self.assertRaises(KeyError, self.lfs.open_object, "abcdeabcdeabcdeabcde")
  38. def test_write_object_empty(self) -> None:
  39. """Test writing an empty object."""
  40. sha = self.lfs.write_object([])
  41. with self.lfs.open_object(sha) as f:
  42. self.assertEqual(b"", f.read())
  43. def test_write_object_multiple_chunks(self) -> None:
  44. """Test writing an object with multiple chunks."""
  45. chunks = [b"chunk1", b"chunk2", b"chunk3"]
  46. sha = self.lfs.write_object(chunks)
  47. with self.lfs.open_object(sha) as f:
  48. self.assertEqual(b"".join(chunks), f.read())
  49. def test_sha_path_calculation(self) -> None:
  50. """Test the internal sha path calculation."""
  51. # The implementation splits the sha into parts for directory structure
  52. # Write and verify we can read it back
  53. sha = self.lfs.write_object([b"test data"])
  54. self.assertEqual(len(sha), 64) # SHA-256 is 64 hex chars
  55. # Open should succeed, which verifies the path calculation works
  56. with self.lfs.open_object(sha) as f:
  57. self.assertEqual(b"test data", f.read())
  58. def test_create_lfs_dir(self) -> None:
  59. """Test creating an LFS directory when it doesn't exist."""
  60. import os
  61. # Create a temporary directory for the test
  62. lfs_parent_dir = tempfile.mkdtemp()
  63. self.addCleanup(shutil.rmtree, lfs_parent_dir)
  64. # Create a path for the LFS directory
  65. lfs_dir = os.path.join(lfs_parent_dir, "lfs")
  66. # Create the LFS store
  67. LFSStore.create(lfs_dir)
  68. # Verify the directories were created
  69. self.assertTrue(os.path.isdir(lfs_dir))
  70. self.assertTrue(os.path.isdir(os.path.join(lfs_dir, "tmp")))
  71. self.assertTrue(os.path.isdir(os.path.join(lfs_dir, "objects")))