test_porcelain.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. # test_porcelain.py -- porcelain tests
  2. # Copyright (C) 2013 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Tests for dulwich.porcelain."""
  19. from io import BytesIO
  20. import os
  21. import shutil
  22. import tarfile
  23. import tempfile
  24. from dulwich import porcelain
  25. from dulwich.diff_tree import tree_changes
  26. from dulwich.objects import (
  27. Blob,
  28. Tag,
  29. Tree,
  30. )
  31. from dulwich.repo import Repo
  32. from dulwich.tests import (
  33. TestCase,
  34. )
  35. from dulwich.tests.compat.utils import require_git_version
  36. from dulwich.tests.utils import (
  37. build_commit_graph,
  38. make_object,
  39. )
  40. class PorcelainTestCase(TestCase):
  41. def setUp(self):
  42. super(TestCase, self).setUp()
  43. repo_dir = tempfile.mkdtemp()
  44. self.addCleanup(shutil.rmtree, repo_dir)
  45. self.repo = Repo.init(repo_dir)
  46. class ArchiveTests(PorcelainTestCase):
  47. """Tests for the archive command."""
  48. def test_simple(self):
  49. # TODO(jelmer): Remove this once dulwich has its own implementation of archive.
  50. require_git_version((1, 5, 0))
  51. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  52. self.repo.refs["refs/heads/master"] = c3.id
  53. out = BytesIO()
  54. err = BytesIO()
  55. porcelain.archive(self.repo.path, "refs/heads/master", outstream=out,
  56. errstream=err)
  57. self.assertEqual("", err.getvalue())
  58. tf = tarfile.TarFile(fileobj=out)
  59. self.addCleanup(tf.close)
  60. self.assertEqual([], tf.getnames())
  61. class UpdateServerInfoTests(PorcelainTestCase):
  62. def test_simple(self):
  63. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  64. [3, 1, 2]])
  65. self.repo.refs["refs/heads/foo"] = c3.id
  66. porcelain.update_server_info(self.repo.path)
  67. self.assertTrue(os.path.exists(os.path.join(self.repo.controldir(),
  68. 'info', 'refs')))
  69. class CommitTests(PorcelainTestCase):
  70. def test_custom_author(self):
  71. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  72. [3, 1, 2]])
  73. self.repo.refs["refs/heads/foo"] = c3.id
  74. sha = porcelain.commit(self.repo.path, message="Some message",
  75. author="Joe <joe@example.com>", committer="Bob <bob@example.com>")
  76. self.assertTrue(isinstance(sha, str))
  77. self.assertEqual(len(sha), 40)
  78. class CloneTests(PorcelainTestCase):
  79. def test_simple_local(self):
  80. f1_1 = make_object(Blob, data='f1')
  81. commit_spec = [[1], [2, 1], [3, 1, 2]]
  82. trees = {1: [('f1', f1_1), ('f2', f1_1)],
  83. 2: [('f1', f1_1), ('f2', f1_1)],
  84. 3: [('f1', f1_1), ('f2', f1_1)], }
  85. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  86. commit_spec, trees)
  87. self.repo.refs["refs/heads/master"] = c3.id
  88. target_path = tempfile.mkdtemp()
  89. outstream = BytesIO()
  90. self.addCleanup(shutil.rmtree, target_path)
  91. r = porcelain.clone(self.repo.path, target_path,
  92. checkout=False, outstream=outstream)
  93. self.assertEqual(r.path, target_path)
  94. self.assertEqual(Repo(target_path).head(), c3.id)
  95. self.assertTrue('f1' not in os.listdir(target_path))
  96. self.assertTrue('f2' not in os.listdir(target_path))
  97. def test_simple_local_with_checkout(self):
  98. f1_1 = make_object(Blob, data='f1')
  99. commit_spec = [[1], [2, 1], [3, 1, 2]]
  100. trees = {1: [('f1', f1_1), ('f2', f1_1)],
  101. 2: [('f1', f1_1), ('f2', f1_1)],
  102. 3: [('f1', f1_1), ('f2', f1_1)], }
  103. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  104. commit_spec, trees)
  105. self.repo.refs["refs/heads/master"] = c3.id
  106. target_path = tempfile.mkdtemp()
  107. outstream = BytesIO()
  108. self.addCleanup(shutil.rmtree, target_path)
  109. r = porcelain.clone(self.repo.path, target_path,
  110. checkout=True, outstream=outstream)
  111. self.assertEqual(r.path, target_path)
  112. self.assertEqual(Repo(target_path).head(), c3.id)
  113. self.assertTrue('f1' in os.listdir(target_path))
  114. self.assertTrue('f2' in os.listdir(target_path))
  115. def test_bare_local_with_checkout(self):
  116. f1_1 = make_object(Blob, data='f1')
  117. commit_spec = [[1], [2, 1], [3, 1, 2]]
  118. trees = {1: [('f1', f1_1), ('f2', f1_1)],
  119. 2: [('f1', f1_1), ('f2', f1_1)],
  120. 3: [('f1', f1_1), ('f2', f1_1)], }
  121. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  122. commit_spec, trees)
  123. self.repo.refs["refs/heads/master"] = c3.id
  124. target_path = tempfile.mkdtemp()
  125. outstream = BytesIO()
  126. self.addCleanup(shutil.rmtree, target_path)
  127. r = porcelain.clone(self.repo.path, target_path,
  128. bare=True, outstream=outstream)
  129. self.assertEqual(r.path, target_path)
  130. self.assertEqual(Repo(target_path).head(), c3.id)
  131. self.assertFalse('f1' in os.listdir(target_path))
  132. self.assertFalse('f2' in os.listdir(target_path))
  133. def test_no_checkout_with_bare(self):
  134. f1_1 = make_object(Blob, data='f1')
  135. commit_spec = [[1]]
  136. trees = {1: [('f1', f1_1), ('f2', f1_1)]}
  137. (c1, ) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  138. self.repo.refs["refs/heads/master"] = c1.id
  139. target_path = tempfile.mkdtemp()
  140. outstream = BytesIO()
  141. self.addCleanup(shutil.rmtree, target_path)
  142. self.assertRaises(ValueError, porcelain.clone, self.repo.path,
  143. target_path, checkout=True, bare=True, outstream=outstream)
  144. class InitTests(TestCase):
  145. def test_non_bare(self):
  146. repo_dir = tempfile.mkdtemp()
  147. self.addCleanup(shutil.rmtree, repo_dir)
  148. porcelain.init(repo_dir)
  149. def test_bare(self):
  150. repo_dir = tempfile.mkdtemp()
  151. self.addCleanup(shutil.rmtree, repo_dir)
  152. porcelain.init(repo_dir, bare=True)
  153. class AddTests(PorcelainTestCase):
  154. def test_add_default_paths(self):
  155. # create a file for initial commit
  156. with open(os.path.join(self.repo.path, 'blah'), 'w') as f:
  157. f.write("\n")
  158. porcelain.add(repo=self.repo.path, paths=['blah'])
  159. porcelain.commit(repo=self.repo.path, message='test',
  160. author='test', committer='test')
  161. # Add a second test file and a file in a directory
  162. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  163. f.write("\n")
  164. os.mkdir(os.path.join(self.repo.path, 'adir'))
  165. with open(os.path.join(self.repo.path, 'adir', 'afile'), 'w') as f:
  166. f.write("\n")
  167. porcelain.add(self.repo.path)
  168. # Check that foo was added and nothing in .git was modified
  169. index = self.repo.open_index()
  170. self.assertEqual(sorted(index), ['adir/afile', 'blah', 'foo'])
  171. def test_add_file(self):
  172. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  173. f.write("BAR")
  174. porcelain.add(self.repo.path, paths=["foo"])
  175. class RemoveTests(PorcelainTestCase):
  176. def test_remove_file(self):
  177. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  178. f.write("BAR")
  179. porcelain.add(self.repo.path, paths=["foo"])
  180. porcelain.rm(self.repo.path, paths=["foo"])
  181. class LogTests(PorcelainTestCase):
  182. def test_simple(self):
  183. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  184. [3, 1, 2]])
  185. self.repo.refs["HEAD"] = c3.id
  186. outstream = BytesIO()
  187. porcelain.log(self.repo.path, outstream=outstream)
  188. self.assertEqual(3, outstream.getvalue().count("-" * 50))
  189. def test_max_entries(self):
  190. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  191. [3, 1, 2]])
  192. self.repo.refs["HEAD"] = c3.id
  193. outstream = BytesIO()
  194. porcelain.log(self.repo.path, outstream=outstream, max_entries=1)
  195. self.assertEqual(1, outstream.getvalue().count("-" * 50))
  196. class ShowTests(PorcelainTestCase):
  197. def test_nolist(self):
  198. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  199. [3, 1, 2]])
  200. self.repo.refs["HEAD"] = c3.id
  201. outstream = BytesIO()
  202. porcelain.show(self.repo.path, objects=c3.id, outstream=outstream)
  203. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  204. def test_simple(self):
  205. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  206. [3, 1, 2]])
  207. self.repo.refs["HEAD"] = c3.id
  208. outstream = BytesIO()
  209. porcelain.show(self.repo.path, objects=[c3.id], outstream=outstream)
  210. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  211. def test_blob(self):
  212. b = Blob.from_string("The Foo\n")
  213. self.repo.object_store.add_object(b)
  214. outstream = BytesIO()
  215. porcelain.show(self.repo.path, objects=[b.id], outstream=outstream)
  216. self.assertEqual(outstream.getvalue(), "The Foo\n")
  217. class SymbolicRefTests(PorcelainTestCase):
  218. def test_set_wrong_symbolic_ref(self):
  219. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  220. [3, 1, 2]])
  221. self.repo.refs["HEAD"] = c3.id
  222. self.assertRaises(ValueError, porcelain.symbolic_ref, self.repo.path, 'foobar')
  223. def test_set_force_wrong_symbolic_ref(self):
  224. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  225. [3, 1, 2]])
  226. self.repo.refs["HEAD"] = c3.id
  227. porcelain.symbolic_ref(self.repo.path, 'force_foobar', force=True)
  228. #test if we actually changed the file
  229. with self.repo.get_named_file('HEAD') as f:
  230. new_ref = f.read()
  231. self.assertEqual(new_ref, b'ref: refs/heads/force_foobar\n')
  232. def test_set_symbolic_ref(self):
  233. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  234. [3, 1, 2]])
  235. self.repo.refs["HEAD"] = c3.id
  236. porcelain.symbolic_ref(self.repo.path, 'master')
  237. def test_set_symbolic_ref_other_than_master(self):
  238. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  239. [3, 1, 2]], attrs=dict(refs='develop'))
  240. self.repo.refs["HEAD"] = c3.id
  241. self.repo.refs["refs/heads/develop"] = c3.id
  242. porcelain.symbolic_ref(self.repo.path, 'develop')
  243. #test if we actually changed the file
  244. with self.repo.get_named_file('HEAD') as f:
  245. new_ref = f.read()
  246. self.assertEqual(new_ref, b'ref: refs/heads/develop\n')
  247. class DiffTreeTests(PorcelainTestCase):
  248. def test_empty(self):
  249. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  250. [3, 1, 2]])
  251. self.repo.refs["HEAD"] = c3.id
  252. outstream = BytesIO()
  253. porcelain.diff_tree(self.repo.path, c2.tree, c3.tree, outstream=outstream)
  254. self.assertEqual(outstream.getvalue(), "")
  255. class CommitTreeTests(PorcelainTestCase):
  256. def test_simple(self):
  257. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  258. [3, 1, 2]])
  259. b = Blob()
  260. b.data = "foo the bar"
  261. t = Tree()
  262. t.add("somename", 0o100644, b.id)
  263. self.repo.object_store.add_object(t)
  264. self.repo.object_store.add_object(b)
  265. sha = porcelain.commit_tree(
  266. self.repo.path, t.id, message="Withcommit.",
  267. author="Joe <joe@example.com>",
  268. committer="Jane <jane@example.com>")
  269. self.assertTrue(isinstance(sha, str))
  270. self.assertEqual(len(sha), 40)
  271. class RevListTests(PorcelainTestCase):
  272. def test_simple(self):
  273. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  274. [3, 1, 2]])
  275. outstream = BytesIO()
  276. porcelain.rev_list(
  277. self.repo.path, [c3.id], outstream=outstream)
  278. self.assertEqual(
  279. "%s\n%s\n%s\n" % (c3.id, c2.id, c1.id),
  280. outstream.getvalue())
  281. class TagCreateTests(PorcelainTestCase):
  282. def test_annotated(self):
  283. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  284. [3, 1, 2]])
  285. self.repo.refs["HEAD"] = c3.id
  286. porcelain.tag_create(self.repo.path, "tryme", 'foo <foo@bar.com>',
  287. 'bar', annotated=True)
  288. tags = self.repo.refs.as_dict("refs/tags")
  289. self.assertEqual(tags.keys(), ["tryme"])
  290. tag = self.repo['refs/tags/tryme']
  291. self.assertTrue(isinstance(tag, Tag))
  292. self.assertEqual("foo <foo@bar.com>", tag.tagger)
  293. self.assertEqual("bar", tag.message)
  294. def test_unannotated(self):
  295. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  296. [3, 1, 2]])
  297. self.repo.refs["HEAD"] = c3.id
  298. porcelain.tag_create(self.repo.path, "tryme", annotated=False)
  299. tags = self.repo.refs.as_dict("refs/tags")
  300. self.assertEqual(tags.keys(), ["tryme"])
  301. self.repo['refs/tags/tryme']
  302. self.assertEqual(tags.values(), [self.repo.head()])
  303. class TagListTests(PorcelainTestCase):
  304. def test_empty(self):
  305. tags = porcelain.tag_list(self.repo.path)
  306. self.assertEqual([], tags)
  307. def test_simple(self):
  308. self.repo.refs["refs/tags/foo"] = "aa" * 20
  309. self.repo.refs["refs/tags/bar/bla"] = "bb" * 20
  310. tags = porcelain.tag_list(self.repo.path)
  311. self.assertEqual(["bar/bla", "foo"], tags)
  312. class ResetTests(PorcelainTestCase):
  313. def test_hard_head(self):
  314. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  315. f.write("BAR")
  316. porcelain.add(self.repo.path, paths=["foo"])
  317. porcelain.commit(self.repo.path, message="Some message",
  318. committer="Jane <jane@example.com>",
  319. author="John <john@example.com>")
  320. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  321. f.write("OOH")
  322. porcelain.reset(self.repo, "hard", "HEAD")
  323. index = self.repo.open_index()
  324. changes = list(tree_changes(self.repo,
  325. index.commit(self.repo.object_store),
  326. self.repo['HEAD'].tree))
  327. self.assertEqual([], changes)
  328. class PushTests(PorcelainTestCase):
  329. def test_simple(self):
  330. """
  331. Basic test of porcelain push where self.repo is the remote. First
  332. clone the remote, commit a file to the clone, then push the changes
  333. back to the remote.
  334. """
  335. outstream = BytesIO()
  336. errstream = BytesIO()
  337. porcelain.commit(repo=self.repo.path, message='init',
  338. author='', committer='')
  339. # Setup target repo cloned from temp test repo
  340. clone_path = tempfile.mkdtemp()
  341. porcelain.clone(self.repo.path, target=clone_path, outstream=outstream)
  342. # create a second file to be pushed back to origin
  343. handle, fullpath = tempfile.mkstemp(dir=clone_path)
  344. porcelain.add(repo=clone_path, paths=[os.path.basename(fullpath)])
  345. porcelain.commit(repo=clone_path, message='push',
  346. author='', committer='')
  347. # Setup a non-checked out branch in the remote
  348. refs_path = os.path.join('refs', 'heads', 'foo')
  349. self.repo[refs_path] = self.repo['HEAD']
  350. # Push to the remote
  351. porcelain.push(clone_path, self.repo.path, refs_path, outstream=outstream,
  352. errstream=errstream)
  353. # Check that the target and source
  354. r_clone = Repo(clone_path)
  355. # Get the change in the target repo corresponding to the add
  356. # this will be in the foo branch.
  357. change = list(tree_changes(self.repo, self.repo['HEAD'].tree,
  358. self.repo['refs/heads/foo'].tree))[0]
  359. self.assertEqual(r_clone['HEAD'].id, self.repo[refs_path].id)
  360. self.assertEqual(os.path.basename(fullpath), change.new.path)
  361. class PullTests(PorcelainTestCase):
  362. def test_simple(self):
  363. outstream = BytesIO()
  364. errstream = BytesIO()
  365. # create a file for initial commit
  366. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  367. filename = os.path.basename(fullpath)
  368. porcelain.add(repo=self.repo.path, paths=filename)
  369. porcelain.commit(repo=self.repo.path, message='test',
  370. author='test', committer='test')
  371. # Setup target repo
  372. target_path = tempfile.mkdtemp()
  373. porcelain.clone(self.repo.path, target=target_path, outstream=outstream)
  374. # create a second file to be pushed
  375. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  376. filename = os.path.basename(fullpath)
  377. porcelain.add(repo=self.repo.path, paths=filename)
  378. porcelain.commit(repo=self.repo.path, message='test2',
  379. author='test2', committer='test2')
  380. # Pull changes into the cloned repo
  381. porcelain.pull(target_path, self.repo.path, 'refs/heads/master',
  382. outstream=outstream, errstream=errstream)
  383. # Check the target repo for pushed changes
  384. r = Repo(target_path)
  385. self.assertEqual(r['HEAD'].id, self.repo['HEAD'].id)
  386. class StatusTests(PorcelainTestCase):
  387. def test_status(self):
  388. """Integration test for `status` functionality."""
  389. # Commit a dummy file then modify it
  390. fullpath = os.path.join(self.repo.path, 'foo')
  391. with open(fullpath, 'w') as f:
  392. f.write('origstuff')
  393. porcelain.add(repo=self.repo.path, paths=['foo'])
  394. porcelain.commit(repo=self.repo.path, message='test status',
  395. author='', committer='')
  396. # modify access and modify time of path
  397. os.utime(fullpath, (0, 0))
  398. with open(fullpath, 'w') as f:
  399. f.write('stuff')
  400. # Make a dummy file and stage it
  401. filename_add = 'bar'
  402. fullpath = os.path.join(self.repo.path, filename_add)
  403. with open(fullpath, 'w') as f:
  404. f.write('stuff')
  405. porcelain.add(repo=self.repo.path, paths=filename_add)
  406. results = porcelain.status(self.repo)
  407. self.assertEqual(results.staged['add'][0], filename_add)
  408. self.assertEqual(results.unstaged, ['foo'])
  409. def test_get_tree_changes_add(self):
  410. """Unit test for get_tree_changes add."""
  411. # Make a dummy file, stage
  412. filename = 'bar'
  413. with open(os.path.join(self.repo.path, filename), 'w') as f:
  414. f.write('stuff')
  415. porcelain.add(repo=self.repo.path, paths=filename)
  416. porcelain.commit(repo=self.repo.path, message='test status',
  417. author='', committer='')
  418. filename = 'foo'
  419. with open(os.path.join(self.repo.path, filename), 'w') as f:
  420. f.write('stuff')
  421. porcelain.add(repo=self.repo.path, paths=filename)
  422. changes = porcelain.get_tree_changes(self.repo.path)
  423. self.assertEqual(changes['add'][0], filename)
  424. self.assertEqual(len(changes['add']), 1)
  425. self.assertEqual(len(changes['modify']), 0)
  426. self.assertEqual(len(changes['delete']), 0)
  427. def test_get_tree_changes_modify(self):
  428. """Unit test for get_tree_changes modify."""
  429. # Make a dummy file, stage, commit, modify
  430. filename = 'foo'
  431. fullpath = os.path.join(self.repo.path, filename)
  432. with open(fullpath, 'w') as f:
  433. f.write('stuff')
  434. porcelain.add(repo=self.repo.path, paths=filename)
  435. porcelain.commit(repo=self.repo.path, message='test status',
  436. author='', committer='')
  437. with open(fullpath, 'w') as f:
  438. f.write('otherstuff')
  439. porcelain.add(repo=self.repo.path, paths=filename)
  440. changes = porcelain.get_tree_changes(self.repo.path)
  441. self.assertEqual(changes['modify'][0], filename)
  442. self.assertEqual(len(changes['add']), 0)
  443. self.assertEqual(len(changes['modify']), 1)
  444. self.assertEqual(len(changes['delete']), 0)
  445. def test_get_tree_changes_delete(self):
  446. """Unit test for get_tree_changes delete."""
  447. # Make a dummy file, stage, commit, remove
  448. filename = 'foo'
  449. with open(os.path.join(self.repo.path, filename), 'w') as f:
  450. f.write('stuff')
  451. porcelain.add(repo=self.repo.path, paths=filename)
  452. porcelain.commit(repo=self.repo.path, message='test status',
  453. author='', committer='')
  454. porcelain.rm(repo=self.repo.path, paths=[filename])
  455. changes = porcelain.get_tree_changes(self.repo.path)
  456. self.assertEqual(changes['delete'][0], filename)
  457. self.assertEqual(len(changes['add']), 0)
  458. self.assertEqual(len(changes['modify']), 0)
  459. self.assertEqual(len(changes['delete']), 1)
  460. # TODO(jelmer): Add test for dulwich.porcelain.daemon
  461. class UploadPackTests(PorcelainTestCase):
  462. """Tests for upload_pack."""
  463. def test_upload_pack(self):
  464. outf = BytesIO()
  465. exitcode = porcelain.upload_pack(self.repo.path, BytesIO("0000"), outf)
  466. outlines = outf.getvalue().splitlines()
  467. self.assertEqual(["0000"], outlines)
  468. self.assertEqual(0, exitcode)
  469. class ReceivePackTests(PorcelainTestCase):
  470. """Tests for receive_pack."""
  471. def test_receive_pack(self):
  472. filename = 'foo'
  473. with open(os.path.join(self.repo.path, filename), 'w') as f:
  474. f.write('stuff')
  475. porcelain.add(repo=self.repo.path, paths=filename)
  476. self.repo.do_commit(message='test status',
  477. author='', committer='', author_timestamp=1402354300,
  478. commit_timestamp=1402354300, author_timezone=0, commit_timezone=0)
  479. outf = BytesIO()
  480. exitcode = porcelain.receive_pack(self.repo.path, BytesIO("0000"), outf)
  481. outlines = outf.getvalue().splitlines()
  482. self.assertEqual([
  483. '005a9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 HEAD\x00report-status '
  484. 'delete-refs side-band-64k',
  485. '003f9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 refs/heads/master',
  486. '0000'], outlines)
  487. self.assertEqual(0, exitcode)
  488. class BranchListTests(PorcelainTestCase):
  489. def test_standard(self):
  490. self.assertEquals(set([]), set(porcelain.branch_list(self.repo)))
  491. def test_new_branch(self):
  492. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  493. self.repo["HEAD"] = c1.id
  494. porcelain.branch_create(self.repo, "foo")
  495. self.assertEquals(
  496. set(["master", "foo"]),
  497. set(porcelain.branch_list(self.repo)))
  498. class BranchCreateTests(PorcelainTestCase):
  499. def test_branch_exists(self):
  500. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  501. self.repo["HEAD"] = c1.id
  502. porcelain.branch_create(self.repo, "foo")
  503. self.assertRaises(KeyError, porcelain.branch_create, self.repo, "foo")
  504. porcelain.branch_create(self.repo, "foo", force=True)
  505. def test_new_branch(self):
  506. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  507. self.repo["HEAD"] = c1.id
  508. porcelain.branch_create(self.repo, "foo")
  509. self.assertEquals(
  510. set(["master", "foo"]),
  511. set(porcelain.branch_list(self.repo)))
  512. class BranchDeleteTests(PorcelainTestCase):
  513. def test_simple(self):
  514. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  515. self.repo["HEAD"] = c1.id
  516. porcelain.branch_create(self.repo, 'foo')
  517. self.assertTrue("foo" in porcelain.branch_list(self.repo))
  518. porcelain.branch_delete(self.repo, 'foo')
  519. self.assertFalse("foo" in porcelain.branch_list(self.repo))
  520. class FetchTests(PorcelainTestCase):
  521. def test_simple(self):
  522. outstream = BytesIO()
  523. errstream = BytesIO()
  524. # create a file for initial commit
  525. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  526. filename = os.path.basename(fullpath)
  527. porcelain.add(repo=self.repo.path, paths=filename)
  528. porcelain.commit(repo=self.repo.path, message='test',
  529. author='test', committer='test')
  530. # Setup target repo
  531. target_path = tempfile.mkdtemp()
  532. target_repo = porcelain.clone(self.repo.path, target=target_path,
  533. outstream=outstream)
  534. # create a second file to be pushed
  535. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  536. filename = os.path.basename(fullpath)
  537. porcelain.add(repo=self.repo.path, paths=filename)
  538. porcelain.commit(repo=self.repo.path, message='test2',
  539. author='test2', committer='test2')
  540. self.assertFalse(self.repo['HEAD'].id in target_repo)
  541. # Fetch changes into the cloned repo
  542. porcelain.fetch(target_path, self.repo.path, outstream=outstream,
  543. errstream=errstream)
  544. # Check the target repo for pushed changes
  545. r = Repo(target_path)
  546. self.assertTrue(self.repo['HEAD'].id in r)