test_cli_cherry_pick.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # test_cli_cherry_pick.py -- Tests for CLI cherry-pick 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 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 CLI cherry-pick command."""
  22. import os
  23. import tempfile
  24. from dulwich import porcelain
  25. from dulwich.cli import cmd_cherry_pick
  26. from . import TestCase
  27. class CherryPickCommandTests(TestCase):
  28. """Tests for the cherry-pick CLI command."""
  29. def test_cherry_pick_simple(self):
  30. """Test simple cherry-pick via CLI."""
  31. with tempfile.TemporaryDirectory() as tmpdir:
  32. # Save current directory
  33. orig_cwd = os.getcwd()
  34. try:
  35. os.chdir(tmpdir)
  36. # Initialize repo
  37. porcelain.init(".")
  38. # Create initial commit
  39. with open("file1.txt", "w") as f:
  40. f.write("Initial content\n")
  41. porcelain.add(".", paths=["file1.txt"])
  42. porcelain.commit(".", message=b"Initial commit")
  43. # Create a branch and switch to it
  44. porcelain.branch_create(".", "feature")
  45. porcelain.checkout_branch(".", "feature")
  46. # Add a file on feature branch
  47. with open("file2.txt", "w") as f:
  48. f.write("Feature content\n")
  49. porcelain.add(".", paths=["file2.txt"])
  50. feature_commit = porcelain.commit(".", message=b"Add feature file")
  51. # Go back to master
  52. porcelain.checkout_branch(".", "master")
  53. # Cherry-pick via CLI
  54. cmd = cmd_cherry_pick()
  55. result = cmd.run([feature_commit.decode()])
  56. self.assertIsNone(result) # Success
  57. self.assertTrue(os.path.exists("file2.txt"))
  58. finally:
  59. os.chdir(orig_cwd)
  60. def test_cherry_pick_no_commit(self):
  61. """Test cherry-pick with --no-commit via CLI."""
  62. with tempfile.TemporaryDirectory() as tmpdir:
  63. orig_cwd = os.getcwd()
  64. try:
  65. os.chdir(tmpdir)
  66. # Initialize repo
  67. porcelain.init(".")
  68. # Create initial commit
  69. with open("file1.txt", "w") as f:
  70. f.write("Initial content\n")
  71. porcelain.add(".", paths=["file1.txt"])
  72. porcelain.commit(".", message=b"Initial commit")
  73. # Create a branch and switch to it
  74. porcelain.branch_create(".", "feature")
  75. porcelain.checkout_branch(".", "feature")
  76. # Add a file on feature branch
  77. with open("file2.txt", "w") as f:
  78. f.write("Feature content\n")
  79. porcelain.add(".", paths=["file2.txt"])
  80. feature_commit = porcelain.commit(".", message=b"Add feature file")
  81. # Go back to master
  82. porcelain.checkout_branch(".", "master")
  83. # Cherry-pick with --no-commit
  84. cmd = cmd_cherry_pick()
  85. result = cmd.run(["--no-commit", feature_commit.decode()])
  86. self.assertIsNone(result) # Success
  87. self.assertTrue(os.path.exists("file2.txt"))
  88. # Check that file is staged but not committed
  89. status = porcelain.status(".")
  90. self.assertTrue(
  91. any(b"file2.txt" in changes for changes in status.staged.values())
  92. )
  93. finally:
  94. os.chdir(orig_cwd)
  95. def test_cherry_pick_missing_argument(self):
  96. """Test cherry-pick without commit argument."""
  97. import io
  98. import sys
  99. with tempfile.TemporaryDirectory() as tmpdir:
  100. orig_cwd = os.getcwd()
  101. try:
  102. os.chdir(tmpdir)
  103. porcelain.init(".")
  104. # Try to cherry-pick without argument
  105. cmd = cmd_cherry_pick()
  106. # Capture stderr to prevent argparse from printing to console
  107. old_stderr = sys.stderr
  108. sys.stderr = io.StringIO()
  109. try:
  110. with self.assertRaises(SystemExit) as cm:
  111. cmd.run([])
  112. self.assertEqual(cm.exception.code, 2) # argparse error code
  113. finally:
  114. sys.stderr = old_stderr
  115. finally:
  116. os.chdir(orig_cwd)