test_submodule.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # test_submodule.py -- tests for porcelain submodule functions
  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 porcelain submodule functions."""
  22. import os
  23. import shutil
  24. import tempfile
  25. from dulwich import porcelain
  26. from dulwich.config import ConfigFile
  27. from dulwich.objects import Blob, Commit, Tree
  28. from dulwich.repo import Repo
  29. from .. import TestCase
  30. class SubmoduleAddTests(TestCase):
  31. """Tests for submodule_add function."""
  32. def setUp(self):
  33. super().setUp()
  34. self.test_dir = tempfile.mkdtemp()
  35. self.repo_path = os.path.join(self.test_dir, "repo")
  36. self.repo = Repo.init(self.repo_path, mkdir=True)
  37. def tearDown(self):
  38. shutil.rmtree(self.test_dir)
  39. super().tearDown()
  40. def test_submodule_add_basic(self) -> None:
  41. """Test basic submodule_add with URL."""
  42. url = "https://github.com/dulwich/dulwich.git"
  43. path = "libs/dulwich"
  44. porcelain.submodule_add(self.repo, url, path, name="dulwich")
  45. # Check that .gitmodules was created
  46. gitmodules_path = os.path.join(self.repo_path, ".gitmodules")
  47. self.assertTrue(os.path.exists(gitmodules_path))
  48. # Check .gitmodules content
  49. config = ConfigFile.from_path(gitmodules_path)
  50. self.assertEqual(url.encode(), config.get(("submodule", "dulwich"), "url"))
  51. self.assertEqual(path.encode(), config.get(("submodule", "dulwich"), "path"))
  52. def test_submodule_add_without_name(self) -> None:
  53. """Test submodule_add derives name from path when not specified."""
  54. url = "https://github.com/dulwich/dulwich.git"
  55. path = "libs/dulwich"
  56. porcelain.submodule_add(self.repo, url, path)
  57. # Check that .gitmodules was created
  58. gitmodules_path = os.path.join(self.repo_path, ".gitmodules")
  59. config = ConfigFile.from_path(gitmodules_path)
  60. # Name should be derived from path
  61. self.assertEqual(url.encode(), config.get(("submodule", "libs/dulwich"), "url"))
  62. def test_submodule_add_without_path(self) -> None:
  63. """Test submodule_add derives path from URL when not specified."""
  64. url = "https://github.com/dulwich/dulwich.git"
  65. porcelain.submodule_add(self.repo, url)
  66. # Check that .gitmodules was created
  67. gitmodules_path = os.path.join(self.repo_path, ".gitmodules")
  68. config = ConfigFile.from_path(gitmodules_path)
  69. # Path should be derived from URL (just "dulwich")
  70. # The actual value depends on _canonical_part implementation
  71. # We just check that something was written
  72. sections = list(config.keys())
  73. self.assertEqual(1, len([s for s in sections if s[0] == b"submodule"]))
  74. def test_submodule_add_updates_existing_gitmodules(self) -> None:
  75. """Test that submodule_add updates existing .gitmodules file."""
  76. # Add first submodule
  77. url1 = "https://github.com/dulwich/dulwich.git"
  78. path1 = "libs/dulwich"
  79. porcelain.submodule_add(self.repo, url1, path1, name="dulwich")
  80. # Add second submodule
  81. url2 = "https://github.com/dulwich/dulwich-tests.git"
  82. path2 = "libs/tests"
  83. porcelain.submodule_add(self.repo, url2, path2, name="tests")
  84. # Check both submodules are in .gitmodules
  85. gitmodules_path = os.path.join(self.repo_path, ".gitmodules")
  86. config = ConfigFile.from_path(gitmodules_path)
  87. self.assertEqual(url1.encode(), config.get(("submodule", "dulwich"), "url"))
  88. self.assertEqual(url2.encode(), config.get(("submodule", "tests"), "url"))
  89. class SubmoduleInitTests(TestCase):
  90. """Tests for submodule_init function."""
  91. def setUp(self):
  92. super().setUp()
  93. self.test_dir = tempfile.mkdtemp()
  94. self.repo_path = os.path.join(self.test_dir, "repo")
  95. self.repo = Repo.init(self.repo_path, mkdir=True)
  96. def tearDown(self):
  97. shutil.rmtree(self.test_dir)
  98. super().tearDown()
  99. def test_submodule_init(self) -> None:
  100. """Test submodule_init reads from .gitmodules and updates config."""
  101. # Create .gitmodules file
  102. gitmodules_path = os.path.join(self.repo_path, ".gitmodules")
  103. config = ConfigFile()
  104. config.set(
  105. ("submodule", "dulwich"), "url", "https://github.com/dulwich/dulwich.git"
  106. )
  107. config.set(("submodule", "dulwich"), "path", "libs/dulwich")
  108. config.path = gitmodules_path
  109. config.write_to_path()
  110. # Initialize submodules
  111. porcelain.submodule_init(self.repo)
  112. # Check that repo config was updated
  113. repo_config = self.repo.get_config()
  114. self.assertEqual(
  115. b"true", repo_config.get((b"submodule", b"dulwich"), b"active")
  116. )
  117. self.assertEqual(
  118. b"https://github.com/dulwich/dulwich.git",
  119. repo_config.get((b"submodule", b"dulwich"), b"url"),
  120. )
  121. def test_submodule_init_no_gitmodules(self) -> None:
  122. """Test submodule_init raises FileNotFoundError when .gitmodules is missing."""
  123. # Should raise FileNotFoundError when .gitmodules doesn't exist
  124. with self.assertRaises(FileNotFoundError):
  125. porcelain.submodule_init(self.repo)
  126. class SubmoduleListTests(TestCase):
  127. """Tests for submodule_list function."""
  128. def setUp(self):
  129. super().setUp()
  130. self.test_dir = tempfile.mkdtemp()
  131. self.repo_path = os.path.join(self.test_dir, "repo")
  132. self.repo = Repo.init(self.repo_path, mkdir=True)
  133. def tearDown(self):
  134. shutil.rmtree(self.test_dir)
  135. super().tearDown()
  136. def test_submodule_list_empty(self) -> None:
  137. """Test submodule_list with no submodules."""
  138. # Create an initial commit
  139. blob = Blob.from_string(b"test content")
  140. self.repo.object_store.add_object(blob)
  141. tree = Tree()
  142. tree.add(b"test.txt", 0o100644, blob.id)
  143. self.repo.object_store.add_object(tree)
  144. commit = Commit()
  145. commit.tree = tree.id
  146. commit.author = commit.committer = b"Test User <test@example.com>"
  147. commit.author_time = commit.commit_time = 1234567890
  148. commit.author_timezone = commit.commit_timezone = 0
  149. commit.encoding = b"UTF-8"
  150. commit.message = b"Initial commit"
  151. self.repo.object_store.add_object(commit)
  152. self.repo.refs[b"refs/heads/main"] = commit.id
  153. self.repo.refs.set_symbolic_ref(b"HEAD", b"refs/heads/main")
  154. # List should be empty
  155. submodules = list(porcelain.submodule_list(self.repo))
  156. self.assertEqual([], submodules)
  157. def test_submodule_list_with_submodule(self) -> None:
  158. """Test submodule_list with a submodule in the tree."""
  159. # Create a tree with a submodule entry
  160. tree = Tree()
  161. tree.add(b"test.txt", 0o100644, b"a" * 40) # Dummy file
  162. # Add a submodule entry with gitlink mode (0o160000)
  163. submodule_sha = b"1" * 40
  164. tree.add(b"libs/mylib", 0o160000, submodule_sha)
  165. self.repo.object_store.add_object(tree)
  166. commit = Commit()
  167. commit.tree = tree.id
  168. commit.author = commit.committer = b"Test User <test@example.com>"
  169. commit.author_time = commit.commit_time = 1234567890
  170. commit.author_timezone = commit.commit_timezone = 0
  171. commit.encoding = b"UTF-8"
  172. commit.message = b"Add submodule"
  173. self.repo.object_store.add_object(commit)
  174. self.repo.refs[b"refs/heads/main"] = commit.id
  175. self.repo.refs.set_symbolic_ref(b"HEAD", b"refs/heads/main")
  176. # List should contain the submodule
  177. submodules = list(porcelain.submodule_list(self.repo))
  178. self.assertEqual(1, len(submodules))
  179. path, sha = submodules[0]
  180. self.assertEqual("libs/mylib", path)
  181. self.assertEqual(submodule_sha.decode(), sha)