test_porcelain.py 27 KB

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