PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/test_utility_commands.py

https://bitbucket.org/palfrey/hgsubversion
Python | 314 lines | 310 code | 4 blank | 0 comment | 0 complexity | a7892b6e8bf41d0b06e1e3c33bc56ad7 MD5 | raw file
Possible License(s): GPL-2.0
  1. import test_util
  2. import os
  3. import unittest
  4. import re
  5. from hgext import rebase
  6. from mercurial import hg
  7. from mercurial import revlog
  8. from mercurial import context
  9. from mercurial import node
  10. from mercurial import commands
  11. from mercurial import util as hgutil
  12. from hgsubversion import util
  13. from hgsubversion import svncommands
  14. from hgsubversion import verify
  15. from hgsubversion import wrappers
  16. expected_info_output = '''URL: %(repourl)s/%(branch)s
  17. Repository Root: %(repourl)s
  18. Repository UUID: df2126f7-00ab-4d49-b42c-7e981dde0bcf
  19. Revision: %(rev)s
  20. Node Kind: directory
  21. Last Changed Author: durin
  22. Last Changed Rev: %(rev)s
  23. Last Changed Date: %(date)s
  24. '''
  25. def repourl(repo_path):
  26. return util.normalize_url(test_util.fileurl(repo_path))
  27. class UtilityTests(test_util.TestBase):
  28. def test_info_output(self):
  29. repo, repo_path = self.load_and_fetch('two_heads.svndump')
  30. hg.update(self.repo, 'the_branch')
  31. u = self.ui()
  32. u.pushbuffer()
  33. svncommands.info(u, self.repo)
  34. actual = u.popbuffer()
  35. expected = (expected_info_output %
  36. {'date': '2008-10-08 01:39:05 +0000 (Wed, 08 Oct 2008)',
  37. 'repourl': repourl(repo_path),
  38. 'branch': 'branches/the_branch',
  39. 'rev': 5,
  40. })
  41. self.assertMultiLineEqual(actual, expected)
  42. hg.update(self.repo, 'default')
  43. u.pushbuffer()
  44. svncommands.info(u, self.repo)
  45. actual = u.popbuffer()
  46. expected = (expected_info_output %
  47. {'date': '2008-10-08 01:39:29 +0000 (Wed, 08 Oct 2008)',
  48. 'repourl': repourl(repo_path),
  49. 'branch': 'trunk',
  50. 'rev': 6,
  51. })
  52. self.assertMultiLineEqual(actual, expected)
  53. hg.update(self.repo, 'default')
  54. u.pushbuffer()
  55. svncommands.info(u, self.repo, rev=3)
  56. actual = u.popbuffer()
  57. expected = (expected_info_output %
  58. {'date': '2008-10-08 01:39:05 +0000 (Wed, 08 Oct 2008)',
  59. 'repourl': repourl(repo_path),
  60. 'branch': 'branches/the_branch',
  61. 'rev': 5,
  62. })
  63. self.assertMultiLineEqual(actual, expected)
  64. def test_info_single(self):
  65. repo, repo_path = self.load_and_fetch('two_heads.svndump', subdir='trunk')
  66. hg.update(self.repo, 'tip')
  67. u = self.ui()
  68. u.pushbuffer()
  69. svncommands.info(u, self.repo)
  70. actual = u.popbuffer()
  71. expected = (expected_info_output %
  72. {'date': '2008-10-08 01:39:29 +0000 (Wed, 08 Oct 2008)',
  73. 'repourl': repourl(repo_path),
  74. 'branch': 'trunk',
  75. 'rev': 6,
  76. })
  77. self.assertMultiLineEqual(expected, actual)
  78. def test_missing_metadata(self):
  79. self._load_fixture_and_fetch('two_heads.svndump')
  80. test_util.rmtree(self.repo.join('svn'))
  81. self.assertRaises(hgutil.Abort,
  82. self.repo.svnmeta)
  83. self.assertRaises(hgutil.Abort,
  84. svncommands.info,
  85. self.ui(), repo=self.repo, args=[])
  86. self.assertRaises(hgutil.Abort,
  87. svncommands.genignore,
  88. self.ui(), repo=self.repo, args=[])
  89. os.remove(self.repo.join('hgrc'))
  90. self.assertRaises(hgutil.Abort,
  91. self.repo.svnmeta)
  92. self.assertRaises(hgutil.Abort,
  93. svncommands.info,
  94. self.ui(), repo=self.repo, args=[])
  95. self.assertRaises(hgutil.Abort,
  96. svncommands.genignore,
  97. self.ui(), repo=self.repo, args=[])
  98. self.assertRaises(hgutil.Abort,
  99. svncommands.rebuildmeta,
  100. self.ui(), repo=self.repo, args=[])
  101. def test_parent_output(self):
  102. self._load_fixture_and_fetch('two_heads.svndump')
  103. u = self.ui()
  104. u.pushbuffer()
  105. parents = (self.repo['the_branch'].node(), revlog.nullid,)
  106. def filectxfn(repo, memctx, path):
  107. return context.memfilectx(path=path,
  108. data='added',
  109. islink=False,
  110. isexec=False,
  111. copied=False)
  112. ctx = context.memctx(self.repo,
  113. parents,
  114. 'automated test',
  115. ['added_bogus_file', 'other_added_file', ],
  116. filectxfn,
  117. 'testy',
  118. '2008-12-21 16:32:00 -0500',
  119. {'branch': 'localbranch', })
  120. new = self.repo.commitctx(ctx)
  121. hg.update(self.repo, new)
  122. wrappers.parents(lambda x, y: None, u, self.repo, svn=True)
  123. actual = u.popbuffer()
  124. self.assertEqual(actual, '3:4e256962fc5d\n')
  125. hg.update(self.repo, 'default')
  126. # Make sure styles work
  127. u.pushbuffer()
  128. wrappers.parents(lambda x, y: None, u, self.repo, svn=True, style='compact')
  129. actual = u.popbuffer()
  130. self.assertEqual(actual, '4:1083037b18d8\n')
  131. # custom templates too
  132. u.pushbuffer()
  133. wrappers.parents(lambda x, y: None, u, self.repo, svn=True, template='{node}\n')
  134. actual = u.popbuffer()
  135. self.assertEqual(actual, '1083037b18d85cd84fa211c5adbaeff0fea2cd9f\n')
  136. u.pushbuffer()
  137. wrappers.parents(lambda x, y: None, u, self.repo, svn=True)
  138. actual = u.popbuffer()
  139. self.assertEqual(actual, '4:1083037b18d8\n')
  140. def test_outgoing_output(self):
  141. repo, repo_path = self.load_and_fetch('two_heads.svndump')
  142. u = self.ui()
  143. parents = (self.repo['the_branch'].node(), revlog.nullid,)
  144. def filectxfn(repo, memctx, path):
  145. return context.memfilectx(path=path,
  146. data='added',
  147. islink=False,
  148. isexec=False,
  149. copied=False)
  150. ctx = context.memctx(self.repo,
  151. parents,
  152. 'automated test',
  153. ['added_bogus_file', 'other_added_file', ],
  154. filectxfn,
  155. 'testy',
  156. '2008-12-21 16:32:00 -0500',
  157. {'branch': 'localbranch', })
  158. new = self.repo.commitctx(ctx)
  159. hg.update(self.repo, new)
  160. u.pushbuffer()
  161. commands.outgoing(u, self.repo, repourl(repo_path))
  162. actual = u.popbuffer()
  163. self.assertTrue(node.hex(self.repo['localbranch'].node())[:8] in actual)
  164. self.assertEqual(actual.strip(), '5:6de15430fa20')
  165. hg.update(self.repo, 'default')
  166. u.pushbuffer()
  167. commands.outgoing(u, self.repo, repourl(repo_path))
  168. actual = u.popbuffer()
  169. self.assertEqual(actual, '')
  170. def test_rebase(self):
  171. self._load_fixture_and_fetch('two_revs.svndump')
  172. parents = (self.repo[0].node(), revlog.nullid,)
  173. def filectxfn(repo, memctx, path):
  174. return context.memfilectx(path=path,
  175. data='added',
  176. islink=False,
  177. isexec=False,
  178. copied=False)
  179. ctx = context.memctx(self.repo,
  180. parents,
  181. 'automated test',
  182. ['added_bogus_file', 'other_added_file', ],
  183. filectxfn,
  184. 'testy',
  185. '2008-12-21 16:32:00 -0500',
  186. {'branch': 'localbranch', })
  187. self.repo.commitctx(ctx)
  188. self.assertEqual(self.repo['tip'].branch(), 'localbranch')
  189. beforerebasehash = self.repo['tip'].node()
  190. hg.update(self.repo, 'tip')
  191. wrappers.rebase(rebase.rebase, self.ui(), self.repo, svn=True)
  192. self.assertEqual(self.repo['tip'].branch(), 'localbranch')
  193. self.assertEqual(self.repo['tip'].parents()[0].parents()[0], self.repo[0])
  194. self.assertNotEqual(beforerebasehash, self.repo['tip'].node())
  195. def test_genignore(self):
  196. """ Test generation of .hgignore file. """
  197. repo = self._load_fixture_and_fetch('ignores.svndump', noupdate=False)
  198. u = self.ui()
  199. u.pushbuffer()
  200. svncommands.genignore(u, repo, self.wc_path)
  201. self.assertMultiLineEqual(open(os.path.join(self.wc_path, '.hgignore')).read(),
  202. '.hgignore\nsyntax:glob\nblah\notherblah\nbaz/magic\n')
  203. def test_genignore_single(self):
  204. self._load_fixture_and_fetch('ignores.svndump', subdir='trunk')
  205. hg.update(self.repo, 'tip')
  206. u = self.ui()
  207. u.pushbuffer()
  208. svncommands.genignore(u, self.repo, self.wc_path)
  209. self.assertMultiLineEqual(open(os.path.join(self.wc_path, '.hgignore')).read(),
  210. '.hgignore\nsyntax:glob\nblah\notherblah\nbaz/magic\n')
  211. def test_list_authors(self):
  212. repo_path = self.load_svndump('replace_trunk_with_branch.svndump')
  213. u = self.ui()
  214. u.pushbuffer()
  215. svncommands.listauthors(u,
  216. args=[test_util.fileurl(repo_path)],
  217. authors=None)
  218. actual = u.popbuffer()
  219. self.assertMultiLineEqual(actual, 'Augie\nevil\n')
  220. def test_list_authors_map(self):
  221. repo_path = self.load_svndump('replace_trunk_with_branch.svndump')
  222. author_path = os.path.join(repo_path, 'authors')
  223. svncommands.listauthors(self.ui(),
  224. args=[test_util.fileurl(repo_path)],
  225. authors=author_path)
  226. self.assertMultiLineEqual(open(author_path).read(), 'Augie=\nevil=\n')
  227. def test_svnverify(self, stupid=False):
  228. repo, repo_path = self.load_and_fetch('binaryfiles.svndump',
  229. noupdate=False, stupid=stupid)
  230. ret = verify.verify(self.ui(), repo, [], rev=1, stupid=stupid)
  231. self.assertEqual(0, ret)
  232. repo_path = self.load_svndump('binaryfiles-broken.svndump')
  233. u = self.ui()
  234. u.pushbuffer()
  235. ret = verify.verify(u, repo, [test_util.fileurl(repo_path)],
  236. rev=1, stupid=stupid)
  237. output = u.popbuffer()
  238. self.assertEqual(1, ret)
  239. output = re.sub(r'file://\S+', 'file://', output)
  240. self.assertMultiLineEqual("""\
  241. verifying d51f46a715a1 against file://
  242. difference in: binary2
  243. unexpected file: binary1
  244. missing file: binary3
  245. """, output)
  246. def test_svnverify_stupid(self):
  247. self.test_svnverify(True)
  248. def test_corruption(self, stupid=False):
  249. SUCCESS = 0
  250. FAILURE = 1
  251. repo, repo_path = self.load_and_fetch('correct.svndump', layout='single',
  252. subdir='', stupid=stupid)
  253. ui = self.ui()
  254. self.assertEqual(SUCCESS, verify.verify(ui, self.repo, rev='tip',
  255. stupid=stupid))
  256. corrupt_source = test_util.fileurl(self.load_svndump('corrupt.svndump'))
  257. repo.ui.setconfig('paths', 'default', corrupt_source)
  258. ui.pushbuffer()
  259. code = verify.verify(ui, repo, rev='tip')
  260. actual = ui.popbuffer()
  261. actual = actual.replace(corrupt_source, '$REPO')
  262. actual = set(actual.splitlines())
  263. expected = set([
  264. 'verifying 78e965230a13 against $REPO@1',
  265. 'missing file: missing-file',
  266. 'wrong flags for: executable-file',
  267. 'wrong flags for: symlink',
  268. 'wrong flags for: regular-file',
  269. 'difference in: another-regular-file',
  270. 'difference in: regular-file',
  271. 'unexpected file: empty-file',
  272. ])
  273. self.assertEqual((FAILURE, expected), (code, actual))
  274. def test_corruption_stupid(self):
  275. self.test_corruption(True)
  276. def suite():
  277. all_tests = [unittest.TestLoader().loadTestsFromTestCase(UtilityTests),
  278. ]
  279. return unittest.TestSuite(all_tests)