test_cli.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. #!/usr/bin/env python
  2. # test_cli.py -- tests for dulwich.cli
  3. # vim: expandtab
  4. #
  5. # Copyright (C) 2024 Jelmer Vernooij <jelmer@jelmer.uk>
  6. #
  7. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  8. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  9. # General Public License as public by the Free Software Foundation; version 2.0
  10. # or (at your option) any later version. You can redistribute it and/or
  11. # modify it under the terms of either of these two licenses.
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. # You should have received a copy of the licenses; if not, see
  20. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  21. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  22. # License, Version 2.0.
  23. """Tests for dulwich.cli."""
  24. import io
  25. import os
  26. import shutil
  27. import sys
  28. import tempfile
  29. import unittest
  30. from unittest.mock import MagicMock, patch
  31. from dulwich import cli
  32. from dulwich.repo import Repo
  33. from dulwich.tests.utils import (
  34. build_commit_graph,
  35. )
  36. from . import TestCase
  37. class DulwichCliTestCase(TestCase):
  38. """Base class for CLI tests."""
  39. def setUp(self) -> None:
  40. super().setUp()
  41. self.test_dir = tempfile.mkdtemp()
  42. self.addCleanup(shutil.rmtree, self.test_dir)
  43. self.repo_path = os.path.join(self.test_dir, "repo")
  44. os.mkdir(self.repo_path)
  45. self.repo = Repo.init(self.repo_path)
  46. self.addCleanup(self.repo.close)
  47. def _run_cli(self, *args, stdout_stream=None):
  48. """Run CLI command and capture output."""
  49. old_stdout = sys.stdout
  50. old_stderr = sys.stderr
  51. old_cwd = os.getcwd()
  52. try:
  53. sys.stdout = stdout_stream or io.StringIO()
  54. sys.stderr = io.StringIO()
  55. os.chdir(self.repo_path)
  56. result = cli.main(list(args))
  57. return result, sys.stdout.getvalue(), sys.stderr.getvalue()
  58. finally:
  59. sys.stdout = old_stdout
  60. sys.stderr = old_stderr
  61. os.chdir(old_cwd)
  62. class InitCommandTest(DulwichCliTestCase):
  63. """Tests for init command."""
  64. def test_init_basic(self):
  65. # Create a new directory for init
  66. new_repo_path = os.path.join(self.test_dir, "new_repo")
  67. result, stdout, stderr = self._run_cli("init", new_repo_path)
  68. self.assertTrue(os.path.exists(os.path.join(new_repo_path, ".git")))
  69. def test_init_bare(self):
  70. # Create a new directory for bare repo
  71. bare_repo_path = os.path.join(self.test_dir, "bare_repo")
  72. result, stdout, stderr = self._run_cli("init", "--bare", bare_repo_path)
  73. self.assertTrue(os.path.exists(os.path.join(bare_repo_path, "HEAD")))
  74. self.assertFalse(os.path.exists(os.path.join(bare_repo_path, ".git")))
  75. class AddCommandTest(DulwichCliTestCase):
  76. """Tests for add command."""
  77. def test_add_single_file(self):
  78. # Create a file to add
  79. test_file = os.path.join(self.repo_path, "test.txt")
  80. with open(test_file, "w") as f:
  81. f.write("test content")
  82. result, stdout, stderr = self._run_cli("add", "test.txt")
  83. # Check that file is in index
  84. self.assertIn(b"test.txt", self.repo.open_index())
  85. def test_add_multiple_files(self):
  86. # Create multiple files
  87. for i in range(3):
  88. test_file = os.path.join(self.repo_path, f"test{i}.txt")
  89. with open(test_file, "w") as f:
  90. f.write(f"content {i}")
  91. result, stdout, stderr = self._run_cli(
  92. "add", "test0.txt", "test1.txt", "test2.txt"
  93. )
  94. index = self.repo.open_index()
  95. self.assertIn(b"test0.txt", index)
  96. self.assertIn(b"test1.txt", index)
  97. self.assertIn(b"test2.txt", index)
  98. class RmCommandTest(DulwichCliTestCase):
  99. """Tests for rm command."""
  100. def test_rm_file(self):
  101. # Create, add and commit a file first
  102. test_file = os.path.join(self.repo_path, "test.txt")
  103. with open(test_file, "w") as f:
  104. f.write("test content")
  105. self._run_cli("add", "test.txt")
  106. self._run_cli("commit", "--message=Add test file")
  107. # Now remove it from index and working directory
  108. result, stdout, stderr = self._run_cli("rm", "test.txt")
  109. # Check that file is not in index
  110. self.assertNotIn(b"test.txt", self.repo.open_index())
  111. class CommitCommandTest(DulwichCliTestCase):
  112. """Tests for commit command."""
  113. def test_commit_basic(self):
  114. # Create and add a file
  115. test_file = os.path.join(self.repo_path, "test.txt")
  116. with open(test_file, "w") as f:
  117. f.write("test content")
  118. self._run_cli("add", "test.txt")
  119. # Commit
  120. result, stdout, stderr = self._run_cli("commit", "--message=Initial commit")
  121. # Check that HEAD points to a commit
  122. self.assertIsNotNone(self.repo.head())
  123. class LogCommandTest(DulwichCliTestCase):
  124. """Tests for log command."""
  125. def test_log_empty_repo(self):
  126. result, stdout, stderr = self._run_cli("log")
  127. # Empty repo should not crash
  128. def test_log_with_commits(self):
  129. # Create some commits
  130. c1, c2, c3 = build_commit_graph(
  131. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  132. )
  133. self.repo.refs[b"HEAD"] = c3.id
  134. result, stdout, stderr = self._run_cli("log")
  135. self.assertIn("Commit 3", stdout)
  136. self.assertIn("Commit 2", stdout)
  137. self.assertIn("Commit 1", stdout)
  138. def test_log_reverse(self):
  139. # Create some commits
  140. c1, c2, c3 = build_commit_graph(
  141. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  142. )
  143. self.repo.refs[b"HEAD"] = c3.id
  144. result, stdout, stderr = self._run_cli("log", "--reverse")
  145. # Check order - commit 1 should appear before commit 3
  146. pos1 = stdout.index("Commit 1")
  147. pos3 = stdout.index("Commit 3")
  148. self.assertLess(pos1, pos3)
  149. class StatusCommandTest(DulwichCliTestCase):
  150. """Tests for status command."""
  151. def test_status_empty(self):
  152. result, stdout, stderr = self._run_cli("status")
  153. # Should not crash on empty repo
  154. def test_status_with_untracked(self):
  155. # Create an untracked file
  156. test_file = os.path.join(self.repo_path, "untracked.txt")
  157. with open(test_file, "w") as f:
  158. f.write("untracked content")
  159. result, stdout, stderr = self._run_cli("status")
  160. self.assertIn("Untracked files:", stdout)
  161. self.assertIn("untracked.txt", stdout)
  162. class BranchCommandTest(DulwichCliTestCase):
  163. """Tests for branch command."""
  164. def test_branch_create(self):
  165. # Create initial commit
  166. test_file = os.path.join(self.repo_path, "test.txt")
  167. with open(test_file, "w") as f:
  168. f.write("test")
  169. self._run_cli("add", "test.txt")
  170. self._run_cli("commit", "--message=Initial")
  171. # Create branch
  172. result, stdout, stderr = self._run_cli("branch", "test-branch")
  173. self.assertIn(b"refs/heads/test-branch", self.repo.refs.keys())
  174. def test_branch_delete(self):
  175. # Create initial commit and branch
  176. test_file = os.path.join(self.repo_path, "test.txt")
  177. with open(test_file, "w") as f:
  178. f.write("test")
  179. self._run_cli("add", "test.txt")
  180. self._run_cli("commit", "--message=Initial")
  181. self._run_cli("branch", "test-branch")
  182. # Delete branch
  183. result, stdout, stderr = self._run_cli("branch", "-d", "test-branch")
  184. self.assertNotIn(b"refs/heads/test-branch", self.repo.refs.keys())
  185. class CheckoutCommandTest(DulwichCliTestCase):
  186. """Tests for checkout command."""
  187. def test_checkout_branch(self):
  188. # Create initial commit and branch
  189. test_file = os.path.join(self.repo_path, "test.txt")
  190. with open(test_file, "w") as f:
  191. f.write("test")
  192. self._run_cli("add", "test.txt")
  193. self._run_cli("commit", "--message=Initial")
  194. self._run_cli("branch", "test-branch")
  195. # Checkout branch
  196. result, stdout, stderr = self._run_cli("checkout", "test-branch")
  197. self.assertEqual(
  198. self.repo.refs.read_ref(b"HEAD"), b"ref: refs/heads/test-branch"
  199. )
  200. class TagCommandTest(DulwichCliTestCase):
  201. """Tests for tag command."""
  202. def test_tag_create(self):
  203. # Create initial commit
  204. test_file = os.path.join(self.repo_path, "test.txt")
  205. with open(test_file, "w") as f:
  206. f.write("test")
  207. self._run_cli("add", "test.txt")
  208. self._run_cli("commit", "--message=Initial")
  209. # Create tag
  210. result, stdout, stderr = self._run_cli("tag", "v1.0")
  211. self.assertIn(b"refs/tags/v1.0", self.repo.refs.keys())
  212. class ShowCommandTest(DulwichCliTestCase):
  213. """Tests for show command."""
  214. def test_show_commit(self):
  215. # Create a commit
  216. test_file = os.path.join(self.repo_path, "test.txt")
  217. with open(test_file, "w") as f:
  218. f.write("test content")
  219. self._run_cli("add", "test.txt")
  220. self._run_cli("commit", "--message=Test commit")
  221. result, stdout, stderr = self._run_cli("show", "HEAD")
  222. self.assertIn("Test commit", stdout)
  223. class FetchPackCommandTest(DulwichCliTestCase):
  224. """Tests for fetch-pack command."""
  225. @patch("dulwich.cli.get_transport_and_path")
  226. def test_fetch_pack_basic(self, mock_transport):
  227. # Mock the transport
  228. mock_client = MagicMock()
  229. mock_transport.return_value = (mock_client, "/path/to/repo")
  230. mock_client.fetch.return_value = None
  231. result, stdout, stderr = self._run_cli(
  232. "fetch-pack", "git://example.com/repo.git"
  233. )
  234. mock_client.fetch.assert_called_once()
  235. class PullCommandTest(DulwichCliTestCase):
  236. """Tests for pull command."""
  237. @patch("dulwich.porcelain.pull")
  238. def test_pull_basic(self, mock_pull):
  239. result, stdout, stderr = self._run_cli("pull", "origin")
  240. mock_pull.assert_called_once()
  241. @patch("dulwich.porcelain.pull")
  242. def test_pull_with_refspec(self, mock_pull):
  243. result, stdout, stderr = self._run_cli("pull", "origin", "master")
  244. mock_pull.assert_called_once()
  245. class PushCommandTest(DulwichCliTestCase):
  246. """Tests for push command."""
  247. @patch("dulwich.porcelain.push")
  248. def test_push_basic(self, mock_push):
  249. result, stdout, stderr = self._run_cli("push", "origin")
  250. mock_push.assert_called_once()
  251. @patch("dulwich.porcelain.push")
  252. def test_push_force(self, mock_push):
  253. result, stdout, stderr = self._run_cli("push", "-f", "origin")
  254. mock_push.assert_called_with(".", "origin", None, force=True)
  255. class ArchiveCommandTest(DulwichCliTestCase):
  256. """Tests for archive command."""
  257. def test_archive_basic(self):
  258. # Create a commit
  259. test_file = os.path.join(self.repo_path, "test.txt")
  260. with open(test_file, "w") as f:
  261. f.write("test content")
  262. self._run_cli("add", "test.txt")
  263. self._run_cli("commit", "--message=Initial")
  264. # Archive produces binary output, so use BytesIO
  265. result, stdout, stderr = self._run_cli(
  266. "archive", "HEAD", stdout_stream=io.BytesIO()
  267. )
  268. # Should complete without error and produce some binary output
  269. self.assertIsInstance(stdout, bytes)
  270. self.assertGreater(len(stdout), 0)
  271. class ForEachRefCommandTest(DulwichCliTestCase):
  272. """Tests for for-each-ref command."""
  273. def test_for_each_ref(self):
  274. # Create a commit
  275. test_file = os.path.join(self.repo_path, "test.txt")
  276. with open(test_file, "w") as f:
  277. f.write("test")
  278. self._run_cli("add", "test.txt")
  279. self._run_cli("commit", "--message=Initial")
  280. result, stdout, stderr = self._run_cli("for-each-ref")
  281. self.assertIn("refs/heads/master", stdout)
  282. class PackRefsCommandTest(DulwichCliTestCase):
  283. """Tests for pack-refs command."""
  284. def test_pack_refs(self):
  285. # Create some refs
  286. test_file = os.path.join(self.repo_path, "test.txt")
  287. with open(test_file, "w") as f:
  288. f.write("test")
  289. self._run_cli("add", "test.txt")
  290. self._run_cli("commit", "--message=Initial")
  291. self._run_cli("branch", "test-branch")
  292. result, stdout, stderr = self._run_cli("pack-refs", "--all")
  293. # Check that packed-refs file exists
  294. self.assertTrue(
  295. os.path.exists(os.path.join(self.repo_path, ".git", "packed-refs"))
  296. )
  297. class SubmoduleCommandTest(DulwichCliTestCase):
  298. """Tests for submodule commands."""
  299. def test_submodule_list(self):
  300. # Create an initial commit so repo has a HEAD
  301. test_file = os.path.join(self.repo_path, "test.txt")
  302. with open(test_file, "w") as f:
  303. f.write("test")
  304. self._run_cli("add", "test.txt")
  305. self._run_cli("commit", "--message=Initial")
  306. result, stdout, stderr = self._run_cli("submodule")
  307. # Should not crash on repo without submodules
  308. def test_submodule_init(self):
  309. # Create .gitmodules file for init to work
  310. gitmodules = os.path.join(self.repo_path, ".gitmodules")
  311. with open(gitmodules, "w") as f:
  312. f.write("") # Empty .gitmodules file
  313. result, stdout, stderr = self._run_cli("submodule", "init")
  314. # Should not crash
  315. class StashCommandTest(DulwichCliTestCase):
  316. """Tests for stash commands."""
  317. def test_stash_list_empty(self):
  318. result, stdout, stderr = self._run_cli("stash", "list")
  319. # Should not crash on empty stash
  320. def test_stash_push_pop(self):
  321. # Create a file and modify it
  322. test_file = os.path.join(self.repo_path, "test.txt")
  323. with open(test_file, "w") as f:
  324. f.write("initial")
  325. self._run_cli("add", "test.txt")
  326. self._run_cli("commit", "--message=Initial")
  327. # Modify file
  328. with open(test_file, "w") as f:
  329. f.write("modified")
  330. # Stash changes
  331. result, stdout, stderr = self._run_cli("stash", "push")
  332. self.assertIn("Saved working directory", stdout)
  333. # Note: Dulwich stash doesn't currently update the working tree
  334. # so the file remains modified after stash push
  335. # Note: stash pop is not fully implemented in Dulwich yet
  336. # so we only test stash push here
  337. class MergeCommandTest(DulwichCliTestCase):
  338. """Tests for merge command."""
  339. def test_merge_basic(self):
  340. # Create initial commit
  341. test_file = os.path.join(self.repo_path, "test.txt")
  342. with open(test_file, "w") as f:
  343. f.write("initial")
  344. self._run_cli("add", "test.txt")
  345. self._run_cli("commit", "--message=Initial")
  346. # Create and checkout new branch
  347. self._run_cli("branch", "feature")
  348. self._run_cli("checkout", "feature")
  349. # Make changes in feature branch
  350. with open(test_file, "w") as f:
  351. f.write("feature changes")
  352. self._run_cli("add", "test.txt")
  353. self._run_cli("commit", "--message=Feature commit")
  354. # Go back to main
  355. self._run_cli("checkout", "master")
  356. # Merge feature branch
  357. result, stdout, stderr = self._run_cli("merge", "feature")
  358. class HelpCommandTest(DulwichCliTestCase):
  359. """Tests for help command."""
  360. def test_help_basic(self):
  361. result, stdout, stderr = self._run_cli("help")
  362. self.assertIn("dulwich command line tool", stdout)
  363. def test_help_all(self):
  364. result, stdout, stderr = self._run_cli("help", "-a")
  365. self.assertIn("Available commands:", stdout)
  366. self.assertIn("add", stdout)
  367. self.assertIn("commit", stdout)
  368. class RemoteCommandTest(DulwichCliTestCase):
  369. """Tests for remote commands."""
  370. def test_remote_add(self):
  371. result, stdout, stderr = self._run_cli(
  372. "remote", "add", "origin", "https://github.com/example/repo.git"
  373. )
  374. # Check remote was added to config
  375. config = self.repo.get_config()
  376. self.assertEqual(
  377. config.get((b"remote", b"origin"), b"url"),
  378. b"https://github.com/example/repo.git",
  379. )
  380. class CheckIgnoreCommandTest(DulwichCliTestCase):
  381. """Tests for check-ignore command."""
  382. def test_check_ignore(self):
  383. # Create .gitignore
  384. gitignore = os.path.join(self.repo_path, ".gitignore")
  385. with open(gitignore, "w") as f:
  386. f.write("*.log\n")
  387. result, stdout, stderr = self._run_cli("check-ignore", "test.log", "test.txt")
  388. self.assertIn("test.log", stdout)
  389. self.assertNotIn("test.txt", stdout)
  390. class LsFilesCommandTest(DulwichCliTestCase):
  391. """Tests for ls-files command."""
  392. def test_ls_files(self):
  393. # Add some files
  394. for name in ["a.txt", "b.txt", "c.txt"]:
  395. path = os.path.join(self.repo_path, name)
  396. with open(path, "w") as f:
  397. f.write(f"content of {name}")
  398. self._run_cli("add", "a.txt", "b.txt", "c.txt")
  399. result, stdout, stderr = self._run_cli("ls-files")
  400. self.assertIn("a.txt", stdout)
  401. self.assertIn("b.txt", stdout)
  402. self.assertIn("c.txt", stdout)
  403. class LsTreeCommandTest(DulwichCliTestCase):
  404. """Tests for ls-tree command."""
  405. def test_ls_tree(self):
  406. # Create a directory structure
  407. os.mkdir(os.path.join(self.repo_path, "subdir"))
  408. with open(os.path.join(self.repo_path, "file.txt"), "w") as f:
  409. f.write("file content")
  410. with open(os.path.join(self.repo_path, "subdir", "nested.txt"), "w") as f:
  411. f.write("nested content")
  412. self._run_cli("add", ".")
  413. self._run_cli("commit", "--message=Initial")
  414. result, stdout, stderr = self._run_cli("ls-tree", "HEAD")
  415. self.assertIn("file.txt", stdout)
  416. self.assertIn("subdir", stdout)
  417. def test_ls_tree_recursive(self):
  418. # Create nested structure
  419. os.mkdir(os.path.join(self.repo_path, "subdir"))
  420. with open(os.path.join(self.repo_path, "subdir", "nested.txt"), "w") as f:
  421. f.write("nested")
  422. self._run_cli("add", ".")
  423. self._run_cli("commit", "--message=Initial")
  424. result, stdout, stderr = self._run_cli("ls-tree", "-r", "HEAD")
  425. self.assertIn("subdir/nested.txt", stdout)
  426. class DescribeCommandTest(DulwichCliTestCase):
  427. """Tests for describe command."""
  428. def test_describe(self):
  429. # Create tagged commit
  430. test_file = os.path.join(self.repo_path, "test.txt")
  431. with open(test_file, "w") as f:
  432. f.write("test")
  433. self._run_cli("add", "test.txt")
  434. self._run_cli("commit", "--message=Initial")
  435. self._run_cli("tag", "v1.0")
  436. result, stdout, stderr = self._run_cli("describe")
  437. self.assertIn("v1.0", stdout)
  438. class FsckCommandTest(DulwichCliTestCase):
  439. """Tests for fsck command."""
  440. def test_fsck(self):
  441. # Create a commit
  442. test_file = os.path.join(self.repo_path, "test.txt")
  443. with open(test_file, "w") as f:
  444. f.write("test")
  445. self._run_cli("add", "test.txt")
  446. self._run_cli("commit", "--message=Initial")
  447. result, stdout, stderr = self._run_cli("fsck")
  448. # Should complete without errors
  449. class RepackCommandTest(DulwichCliTestCase):
  450. """Tests for repack command."""
  451. def test_repack(self):
  452. # Create some objects
  453. for i in range(5):
  454. test_file = os.path.join(self.repo_path, f"test{i}.txt")
  455. with open(test_file, "w") as f:
  456. f.write(f"content {i}")
  457. self._run_cli("add", f"test{i}.txt")
  458. self._run_cli("commit", f"--message=Commit {i}")
  459. result, stdout, stderr = self._run_cli("repack")
  460. # Should create pack files
  461. pack_dir = os.path.join(self.repo_path, ".git", "objects", "pack")
  462. self.assertTrue(any(f.endswith(".pack") for f in os.listdir(pack_dir)))
  463. class ResetCommandTest(DulwichCliTestCase):
  464. """Tests for reset command."""
  465. def test_reset_soft(self):
  466. # Create commits
  467. test_file = os.path.join(self.repo_path, "test.txt")
  468. with open(test_file, "w") as f:
  469. f.write("first")
  470. self._run_cli("add", "test.txt")
  471. self._run_cli("commit", "--message=First")
  472. first_commit = self.repo.head()
  473. with open(test_file, "w") as f:
  474. f.write("second")
  475. self._run_cli("add", "test.txt")
  476. self._run_cli("commit", "--message=Second")
  477. # Reset soft
  478. result, stdout, stderr = self._run_cli("reset", "--soft", first_commit.decode())
  479. # HEAD should be at first commit
  480. self.assertEqual(self.repo.head(), first_commit)
  481. class WriteTreeCommandTest(DulwichCliTestCase):
  482. """Tests for write-tree command."""
  483. def test_write_tree(self):
  484. # Create and add files
  485. test_file = os.path.join(self.repo_path, "test.txt")
  486. with open(test_file, "w") as f:
  487. f.write("test")
  488. self._run_cli("add", "test.txt")
  489. result, stdout, stderr = self._run_cli("write-tree")
  490. # Should output tree SHA
  491. self.assertEqual(len(stdout.strip()), 40)
  492. class UpdateServerInfoCommandTest(DulwichCliTestCase):
  493. """Tests for update-server-info command."""
  494. def test_update_server_info(self):
  495. result, stdout, stderr = self._run_cli("update-server-info")
  496. # Should create info/refs file
  497. info_refs = os.path.join(self.repo_path, ".git", "info", "refs")
  498. self.assertTrue(os.path.exists(info_refs))
  499. class SymbolicRefCommandTest(DulwichCliTestCase):
  500. """Tests for symbolic-ref command."""
  501. def test_symbolic_ref(self):
  502. # Create a branch
  503. test_file = os.path.join(self.repo_path, "test.txt")
  504. with open(test_file, "w") as f:
  505. f.write("test")
  506. self._run_cli("add", "test.txt")
  507. self._run_cli("commit", "--message=Initial")
  508. self._run_cli("branch", "test-branch")
  509. result, stdout, stderr = self._run_cli(
  510. "symbolic-ref", "HEAD", "refs/heads/test-branch"
  511. )
  512. # HEAD should now point to test-branch
  513. self.assertEqual(
  514. self.repo.refs.read_ref(b"HEAD"), b"ref: refs/heads/test-branch"
  515. )
  516. if __name__ == "__main__":
  517. unittest.main()