test_cli.py 23 KB

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