2
0

test_submodule.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 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 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 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.stage(["file.txt"])
  60. repo.do_commit(b"Initial commit")
  61. # Manually create the raw string for a tree with our file and a submodule
  62. # Format for tree entries: [mode] [name]\0[sha]
  63. # Get the blob SHA for our file using the index
  64. index = repo.open_index()
  65. file_entry = index[b"file.txt"]
  66. file_sha = file_entry.sha
  67. # Convert to binary representation needed for raw tree data
  68. binary_file_sha = bytes.fromhex(file_sha.decode("ascii"))
  69. # Generate a valid SHA for the submodule
  70. submodule_sha = b"1" * 40
  71. binary_submodule_sha = bytes.fromhex(submodule_sha.decode("ascii"))
  72. # Create raw tree data
  73. raw_tree_data = (
  74. # Regular file entry
  75. b"100644 file.txt\0"
  76. + binary_file_sha
  77. +
  78. # Submodule entry with gitlink mode
  79. b"160000 submodule\0"
  80. + binary_submodule_sha
  81. )
  82. # Create a tree from raw data
  83. from dulwich.objects import ShaFile
  84. tree = ShaFile.from_raw_string(Tree.type_num, raw_tree_data)
  85. # Add the tree to the repository
  86. repo.object_store.add_object(tree)
  87. # Now we can test the iter_cached_submodules function
  88. submodules = list(iter_cached_submodules(repo.object_store, tree.id))
  89. # We should find one submodule
  90. self.assertEqual(1, len(submodules))
  91. # Verify the submodule details
  92. path, sha = submodules[0]
  93. self.assertEqual(b"submodule", path)
  94. self.assertEqual(submodule_sha, sha)