test_submodule.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # test_submodule.py -- tests for submodule.py
  2. # Copyright (C) 2025 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 submodule handling."""
  22. import os
  23. import shutil
  24. import tempfile
  25. from dulwich.objects import (
  26. S_ISGITLINK,
  27. Tree,
  28. )
  29. from dulwich.repo import Repo
  30. from dulwich.submodule import ensure_submodule_placeholder, iter_cached_submodules
  31. from . import TestCase
  32. class SubmoduleTests(TestCase):
  33. """Tests for submodule functions."""
  34. def setUp(self):
  35. super().setUp()
  36. self.test_dir = tempfile.mkdtemp()
  37. def tearDown(self):
  38. shutil.rmtree(self.test_dir)
  39. super().tearDown()
  40. def test_S_ISGITLINK(self) -> None:
  41. """Test the S_ISGITLINK function for checking gitlink mode."""
  42. # 0o160000 is the mode used for submodules
  43. self.assertTrue(S_ISGITLINK(0o160000))
  44. # Test some other modes to ensure they're not detected as gitlinks
  45. self.assertFalse(S_ISGITLINK(0o100644)) # regular file
  46. self.assertFalse(S_ISGITLINK(0o100755)) # executable file
  47. self.assertFalse(S_ISGITLINK(0o040000)) # directory
  48. def test_iter_cached_submodules(self) -> None:
  49. """Test the function to detect and iterate through submodules."""
  50. # Create a repository and add some content
  51. repo_dir = os.path.join(self.test_dir, "repo")
  52. os.makedirs(repo_dir)
  53. repo = Repo.init(repo_dir)
  54. # Create a file to add to our tree
  55. file_path = os.path.join(repo_dir, "file.txt")
  56. with open(file_path, "wb") as f:
  57. f.write(b"test file content")
  58. # Stage and commit the file to create some basic content
  59. repo.get_worktree().stage(["file.txt"])
  60. repo.get_worktree().commit(
  61. message=b"Initial commit",
  62. )
  63. # Manually create the raw string for a tree with our file and a submodule
  64. # Format for tree entries: [mode] [name]\0[sha]
  65. # Get the blob SHA for our file using the index
  66. index = repo.open_index()
  67. file_entry = index[b"file.txt"]
  68. file_sha = file_entry.sha
  69. # Convert to binary representation needed for raw tree data
  70. binary_file_sha = bytes.fromhex(file_sha.decode("ascii"))
  71. # Generate a valid SHA for the submodule
  72. submodule_sha = b"1" * 40
  73. binary_submodule_sha = bytes.fromhex(submodule_sha.decode("ascii"))
  74. # Create raw tree data
  75. raw_tree_data = (
  76. # Regular file entry
  77. b"100644 file.txt\0"
  78. + binary_file_sha
  79. +
  80. # Submodule entry with gitlink mode
  81. b"160000 submodule\0"
  82. + binary_submodule_sha
  83. )
  84. # Create a tree from raw data
  85. from dulwich.objects import ShaFile
  86. tree = ShaFile.from_raw_string(Tree.type_num, raw_tree_data)
  87. # Add the tree to the repository
  88. repo.object_store.add_object(tree)
  89. # Now we can test the iter_cached_submodules function
  90. submodules = list(iter_cached_submodules(repo.object_store, tree.id))
  91. # We should find one submodule
  92. self.assertEqual(1, len(submodules))
  93. # Verify the submodule details
  94. path, sha = submodules[0]
  95. self.assertEqual(b"submodule", path)
  96. self.assertEqual(submodule_sha, sha)
  97. def test_ensure_submodule_placeholder(self) -> None:
  98. """Test creating submodule placeholder directories."""
  99. # Create a repository
  100. repo_path = os.path.join(self.test_dir, "testrepo")
  101. repo = Repo.init(repo_path, mkdir=True)
  102. # Test creating a simple submodule placeholder
  103. ensure_submodule_placeholder(repo, b"libs/mylib")
  104. # Check that the directory was created
  105. submodule_path = os.path.join(repo_path, "libs", "mylib")
  106. self.assertTrue(os.path.isdir(submodule_path))
  107. # Check that the .git file was created
  108. git_file_path = os.path.join(submodule_path, ".git")
  109. self.assertTrue(os.path.isfile(git_file_path))
  110. # Check the content of the .git file
  111. with open(git_file_path, "rb") as f:
  112. content = f.read()
  113. self.assertEqual(b"gitdir: ../../.git/modules/libs/mylib\n", content)
  114. # Test with string path
  115. ensure_submodule_placeholder(repo, "libs/another")
  116. another_path = os.path.join(repo_path, "libs", "another")
  117. self.assertTrue(os.path.isdir(another_path))
  118. # Test idempotency - calling again should not fail
  119. ensure_submodule_placeholder(repo, b"libs/mylib")