2
0

test_porcelain.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 cStringIO import StringIO
  20. import os
  21. import shutil
  22. import tarfile
  23. import tempfile
  24. from dulwich import porcelain
  25. from dulwich.objects import (
  26. Blob,
  27. Tree,
  28. )
  29. from dulwich.repo import Repo
  30. from dulwich.tests import (
  31. TestCase,
  32. )
  33. from dulwich.tests.utils import (
  34. build_commit_graph,
  35. )
  36. class PorcelainTestCase(TestCase):
  37. def setUp(self):
  38. super(TestCase, self).setUp()
  39. repo_dir = tempfile.mkdtemp()
  40. self.addCleanup(shutil.rmtree, repo_dir)
  41. self.repo = Repo.init(repo_dir)
  42. class ArchiveTests(PorcelainTestCase):
  43. """Tests for the archive command."""
  44. def test_simple(self):
  45. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  46. self.repo.refs["refs/heads/master"] = c3.id
  47. out = StringIO()
  48. err = StringIO()
  49. porcelain.archive(self.repo.path, "refs/heads/master", outstream=out,
  50. errstream=err)
  51. self.assertEquals("", err.getvalue())
  52. tf = tarfile.TarFile(fileobj=out)
  53. self.addCleanup(tf.close)
  54. self.assertEquals([], tf.getnames())
  55. class UpdateServerInfoTests(PorcelainTestCase):
  56. def test_simple(self):
  57. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  58. [3, 1, 2]])
  59. self.repo.refs["refs/heads/foo"] = c3.id
  60. porcelain.update_server_info(self.repo.path)
  61. self.assertTrue(os.path.exists(os.path.join(self.repo.controldir(),
  62. 'info', 'refs')))
  63. class CommitTests(PorcelainTestCase):
  64. def test_custom_author(self):
  65. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  66. [3, 1, 2]])
  67. self.repo.refs["refs/heads/foo"] = c3.id
  68. sha = porcelain.commit(self.repo.path, message="Some message",
  69. author="Joe <joe@example.com>", committer="Bob <bob@example.com>")
  70. self.assertTrue(type(sha) is str)
  71. self.assertEquals(len(sha), 40)
  72. class CloneTests(PorcelainTestCase):
  73. def test_simple_local(self):
  74. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  75. [3, 1, 2]])
  76. self.repo.refs["refs/heads/master"] = c3.id
  77. target_path = tempfile.mkdtemp()
  78. outstream = StringIO()
  79. self.addCleanup(shutil.rmtree, target_path)
  80. r = porcelain.clone(self.repo.path, target_path, outstream=outstream)
  81. self.assertEquals(r.path, target_path)
  82. self.assertEquals(Repo(target_path).head(), c3.id)
  83. class InitTests(TestCase):
  84. def test_non_bare(self):
  85. repo_dir = tempfile.mkdtemp()
  86. self.addCleanup(shutil.rmtree, repo_dir)
  87. porcelain.init(repo_dir)
  88. def test_bare(self):
  89. repo_dir = tempfile.mkdtemp()
  90. self.addCleanup(shutil.rmtree, repo_dir)
  91. porcelain.init(repo_dir, bare=True)
  92. class AddTests(PorcelainTestCase):
  93. def test_add_file(self):
  94. f = open(os.path.join(self.repo.path, 'foo'), 'w')
  95. try:
  96. f.write("BAR")
  97. finally:
  98. f.close()
  99. porcelain.add(self.repo.path, paths=["foo"])
  100. class RemoveTests(PorcelainTestCase):
  101. def test_remove_file(self):
  102. f = open(os.path.join(self.repo.path, 'foo'), 'w')
  103. try:
  104. f.write("BAR")
  105. finally:
  106. f.close()
  107. porcelain.add(self.repo.path, paths=["foo"])
  108. porcelain.rm(self.repo.path, paths=["foo"])
  109. class LogTests(PorcelainTestCase):
  110. def test_simple(self):
  111. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  112. [3, 1, 2]])
  113. self.repo.refs["HEAD"] = c3.id
  114. outstream = StringIO()
  115. porcelain.log(self.repo.path, outstream=outstream)
  116. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  117. class ShowTests(PorcelainTestCase):
  118. def test_simple(self):
  119. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  120. [3, 1, 2]])
  121. self.repo.refs["HEAD"] = c3.id
  122. outstream = StringIO()
  123. porcelain.show(self.repo.path, committish=c3.id, outstream=outstream)
  124. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  125. class SymbolicRefTests(PorcelainTestCase):
  126. def test_set_wrong_symbolic_ref(self):
  127. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  128. [3, 1, 2]])
  129. self.repo.refs["HEAD"] = c3.id
  130. outstream = StringIO()
  131. self.assertRaises(ValueError, porcelain.symbolic_ref, self.repo.path, 'foobar')
  132. def test_set_force_wrong_symbolic_ref(self):
  133. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  134. [3, 1, 2]])
  135. self.repo.refs["HEAD"] = c3.id
  136. porcelain.symbolic_ref(self.repo.path, 'force_foobar', force=True)
  137. #test if we actually changed the file
  138. new_ref = self.repo.get_named_file('HEAD').read()
  139. self.assertEqual(new_ref, 'ref: refs/heads/force_foobar\n')
  140. def test_set_symbolic_ref(self):
  141. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  142. [3, 1, 2]])
  143. self.repo.refs["HEAD"] = c3.id
  144. porcelain.symbolic_ref(self.repo.path, 'master')
  145. def test_set_symbolic_ref_other_than_master(self):
  146. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  147. [3, 1, 2]], attrs=dict(refs='develop'))
  148. self.repo.refs["HEAD"] = c3.id
  149. self.repo.refs["refs/heads/develop"] = c3.id
  150. porcelain.symbolic_ref(self.repo.path, 'develop')
  151. #test if we actually changed the file
  152. new_ref = self.repo.get_named_file('HEAD').read()
  153. self.assertEqual(new_ref, 'ref: refs/heads/develop\n')
  154. class DiffTreeTests(PorcelainTestCase):
  155. def test_empty(self):
  156. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  157. [3, 1, 2]])
  158. self.repo.refs["HEAD"] = c3.id
  159. outstream = StringIO()
  160. porcelain.diff_tree(self.repo.path, c2.tree, c3.tree, outstream=outstream)
  161. self.assertEquals(outstream.getvalue(), "")
  162. class CommitTreeTests(PorcelainTestCase):
  163. def test_simple(self):
  164. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  165. [3, 1, 2]])
  166. b = Blob()
  167. b.data = "foo the bar"
  168. t = Tree()
  169. t.add("somename", 0100644, b.id)
  170. self.repo.object_store.add_object(t)
  171. self.repo.object_store.add_object(b)
  172. sha = porcelain.commit_tree(
  173. self.repo.path, t.id, message="Withcommit.",
  174. author="Joe <joe@example.com>",
  175. committer="Jane <jane@example.com>")
  176. self.assertTrue(type(sha) is str)
  177. self.assertEquals(len(sha), 40)