test_porcelain.py 26 KB

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