2
0

test_cli_merge.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # test_cli_merge.py -- Tests for dulwich merge CLI command
  2. # Copyright (C) 2024 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 dulwich merge CLI command."""
  22. import os
  23. import tempfile
  24. import unittest
  25. from dulwich import porcelain
  26. from dulwich.cli import main
  27. from . import TestCase
  28. class CLIMergeTests(TestCase):
  29. """Tests for the dulwich merge CLI command."""
  30. def test_merge_fast_forward(self):
  31. """Test CLI merge with fast-forward."""
  32. with tempfile.TemporaryDirectory() as tmpdir:
  33. # Initialize repo
  34. porcelain.init(tmpdir)
  35. # Create initial commit
  36. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  37. f.write("Initial content\n")
  38. porcelain.add(tmpdir, paths=["file1.txt"])
  39. porcelain.commit(tmpdir, message=b"Initial commit")
  40. # Create a branch
  41. porcelain.branch_create(tmpdir, "feature")
  42. porcelain.checkout(tmpdir, "feature")
  43. # Add a file on feature branch
  44. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  45. f.write("Feature content\n")
  46. porcelain.add(tmpdir, paths=["file2.txt"])
  47. porcelain.commit(tmpdir, message=b"Add feature")
  48. # Go back to master
  49. porcelain.checkout(tmpdir, "master")
  50. # Test merge via CLI
  51. old_cwd = os.getcwd()
  52. try:
  53. os.chdir(tmpdir)
  54. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  55. ret = main(["merge", "feature"])
  56. log_output = "\n".join(cm.output)
  57. self.assertEqual(ret, 0) # Success
  58. self.assertIn("Merge successful", log_output)
  59. # Check that file2.txt exists
  60. self.assertTrue(os.path.exists(os.path.join(tmpdir, "file2.txt")))
  61. finally:
  62. os.chdir(old_cwd)
  63. def test_merge_with_conflicts(self):
  64. """Test CLI merge with conflicts."""
  65. with tempfile.TemporaryDirectory() as tmpdir:
  66. # Initialize repo
  67. porcelain.init(tmpdir)
  68. # Create initial commit
  69. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  70. f.write("Initial content\n")
  71. porcelain.add(tmpdir, paths=["file1.txt"])
  72. porcelain.commit(tmpdir, message=b"Initial commit")
  73. # Create a branch and modify file1
  74. porcelain.branch_create(tmpdir, "feature")
  75. porcelain.checkout(tmpdir, "feature")
  76. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  77. f.write("Feature content\n")
  78. porcelain.add(tmpdir, paths=["file1.txt"])
  79. porcelain.commit(tmpdir, message=b"Modify file1 in feature")
  80. # Go back to master and modify file1 differently
  81. porcelain.checkout(tmpdir, "master")
  82. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  83. f.write("Master content\n")
  84. porcelain.add(tmpdir, paths=["file1.txt"])
  85. porcelain.commit(tmpdir, message=b"Modify file1 in master")
  86. # Test merge via CLI - should exit with error
  87. old_cwd = os.getcwd()
  88. try:
  89. os.chdir(tmpdir)
  90. with self.assertLogs("dulwich.cli", level="WARNING") as cm:
  91. retcode = main(["merge", "feature"])
  92. self.assertEqual(retcode, 1)
  93. log_output = "\n".join(cm.output)
  94. self.assertIn("Merge conflicts", log_output)
  95. self.assertIn("file1.txt", log_output)
  96. finally:
  97. os.chdir(old_cwd)
  98. def test_merge_already_up_to_date(self):
  99. """Test CLI merge when already up to date."""
  100. with tempfile.TemporaryDirectory() as tmpdir:
  101. # Initialize repo
  102. porcelain.init(tmpdir)
  103. # Create initial commit
  104. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  105. f.write("Initial content\n")
  106. porcelain.add(tmpdir, paths=["file1.txt"])
  107. porcelain.commit(tmpdir, message=b"Initial commit")
  108. # Test merge via CLI
  109. old_cwd = os.getcwd()
  110. try:
  111. os.chdir(tmpdir)
  112. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  113. ret = main(["merge", "HEAD"])
  114. log_output = "\n".join(cm.output)
  115. self.assertEqual(ret, 0) # Success
  116. self.assertIn("Already up to date", log_output)
  117. finally:
  118. os.chdir(old_cwd)
  119. def test_merge_no_commit(self):
  120. """Test CLI merge with --no-commit."""
  121. with tempfile.TemporaryDirectory() as tmpdir:
  122. # Initialize repo
  123. porcelain.init(tmpdir)
  124. # Create initial commit
  125. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  126. f.write("Initial content\n")
  127. porcelain.add(tmpdir, paths=["file1.txt"])
  128. porcelain.commit(tmpdir, message=b"Initial commit")
  129. # Create a branch
  130. porcelain.branch_create(tmpdir, "feature")
  131. porcelain.checkout(tmpdir, "feature")
  132. # Add a file on feature branch
  133. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  134. f.write("Feature content\n")
  135. porcelain.add(tmpdir, paths=["file2.txt"])
  136. porcelain.commit(tmpdir, message=b"Add feature")
  137. # Go back to master and add another file
  138. porcelain.checkout(tmpdir, "master")
  139. with open(os.path.join(tmpdir, "file3.txt"), "w") as f:
  140. f.write("Master content\n")
  141. porcelain.add(tmpdir, paths=["file3.txt"])
  142. porcelain.commit(tmpdir, message=b"Add file3")
  143. # Test merge via CLI with --no-commit
  144. old_cwd = os.getcwd()
  145. try:
  146. os.chdir(tmpdir)
  147. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  148. ret = main(["merge", "--no-commit", "feature"])
  149. log_output = "\n".join(cm.output)
  150. self.assertEqual(ret, 0) # Success
  151. self.assertIn("not committing", log_output)
  152. # Check that files are merged
  153. self.assertTrue(os.path.exists(os.path.join(tmpdir, "file2.txt")))
  154. self.assertTrue(os.path.exists(os.path.join(tmpdir, "file3.txt")))
  155. finally:
  156. os.chdir(old_cwd)
  157. def test_merge_no_ff(self):
  158. """Test CLI merge with --no-ff."""
  159. with tempfile.TemporaryDirectory() as tmpdir:
  160. # Initialize repo
  161. porcelain.init(tmpdir)
  162. # Create initial commit
  163. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  164. f.write("Initial content\n")
  165. porcelain.add(tmpdir, paths=["file1.txt"])
  166. porcelain.commit(tmpdir, message=b"Initial commit")
  167. # Create a branch
  168. porcelain.branch_create(tmpdir, "feature")
  169. porcelain.checkout(tmpdir, "feature")
  170. # Add a file on feature branch
  171. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  172. f.write("Feature content\n")
  173. porcelain.add(tmpdir, paths=["file2.txt"])
  174. porcelain.commit(tmpdir, message=b"Add feature")
  175. # Go back to master
  176. porcelain.checkout(tmpdir, "master")
  177. # Test merge via CLI with --no-ff
  178. old_cwd = os.getcwd()
  179. try:
  180. os.chdir(tmpdir)
  181. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  182. ret = main(["merge", "--no-ff", "feature"])
  183. log_output = "\n".join(cm.output)
  184. self.assertEqual(ret, 0) # Success
  185. self.assertIn("Merge successful", log_output)
  186. self.assertIn("Created merge commit", log_output)
  187. finally:
  188. os.chdir(old_cwd)
  189. def test_merge_with_message(self):
  190. """Test CLI merge with custom message."""
  191. with tempfile.TemporaryDirectory() as tmpdir:
  192. # Initialize repo
  193. porcelain.init(tmpdir)
  194. # Create initial commit
  195. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  196. f.write("Initial content\n")
  197. porcelain.add(tmpdir, paths=["file1.txt"])
  198. porcelain.commit(tmpdir, message=b"Initial commit")
  199. # Create a branch
  200. porcelain.branch_create(tmpdir, "feature")
  201. porcelain.checkout(tmpdir, "feature")
  202. # Add a file on feature branch
  203. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  204. f.write("Feature content\n")
  205. porcelain.add(tmpdir, paths=["file2.txt"])
  206. porcelain.commit(tmpdir, message=b"Add feature")
  207. # Go back to master and add another file
  208. porcelain.checkout(tmpdir, "master")
  209. with open(os.path.join(tmpdir, "file3.txt"), "w") as f:
  210. f.write("Master content\n")
  211. porcelain.add(tmpdir, paths=["file3.txt"])
  212. porcelain.commit(tmpdir, message=b"Add file3")
  213. # Test merge via CLI with custom message
  214. old_cwd = os.getcwd()
  215. try:
  216. os.chdir(tmpdir)
  217. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  218. ret = main(["merge", "-m", "Custom merge message", "feature"])
  219. log_output = "\n".join(cm.output)
  220. self.assertEqual(ret, 0) # Success
  221. self.assertIn("Merge successful", log_output)
  222. finally:
  223. os.chdir(old_cwd)
  224. if __name__ == "__main__":
  225. unittest.main()