test_pagure_lib_git_diff_pr.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources # noqa
  9. import json # noqa
  10. import unittest # noqa
  11. import shutil # noqa
  12. import sys # noqa
  13. import tempfile # noqa
  14. import os # noqa
  15. import pygit2 # noqa
  16. from mock import patch, MagicMock # noqa
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure.lib # noqa
  20. import tests # noqa
  21. from pagure.lib.repo import PagureRepo # noqa
  22. class PagureFlaskForkPrtests(tests.Modeltests):
  23. """ Tests for flask fork controller of pagure regarding diffing PRs """
  24. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskForkPrtests, self).setUp()
  28. # Create the main project in the DB
  29. item = pagure.lib.model.Project(
  30. user_id=1, # pingou
  31. name='test',
  32. description='test project #1',
  33. hook_token='aaabbbccc',
  34. )
  35. item.close_status = [
  36. 'Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  37. self.session.add(item)
  38. self.session.commit()
  39. # Create the fork
  40. item = pagure.lib.model.Project(
  41. user_id=1, # pingou
  42. name='test',
  43. description='test project #1',
  44. hook_token='aaabbbcccdd',
  45. parent_id=1,
  46. is_fork=True,
  47. )
  48. item.close_status = [
  49. 'Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  50. self.session.add(item)
  51. self.session.commit()
  52. # Create two git repos, one has 6 commits, the other 4 of which only
  53. # 1 isn't present in the first repo
  54. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  55. pygit2.init_repository(gitrepo, bare=True)
  56. gitrepo2 = os.path.join(
  57. self.path, 'repos', 'forks', 'pingou', 'test.git')
  58. pygit2.init_repository(gitrepo2, bare=True)
  59. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  60. repopath = os.path.join(newpath, 'test')
  61. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  62. # Do 3 commits to the main repo
  63. for i in range(3):
  64. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  65. stream.write('foo%s\n bar%s\n' % (i, i))
  66. clone_repo.index.add('sources')
  67. clone_repo.index.write()
  68. parents = []
  69. try:
  70. last_commit = clone_repo.revparse_single('HEAD')
  71. parents = [last_commit.oid.hex]
  72. except KeyError:
  73. pass
  74. # Commits the files added
  75. tree = clone_repo.index.write_tree()
  76. author = pygit2.Signature(
  77. 'Alice Author', 'alice@authors.tld')
  78. committer = pygit2.Signature(
  79. 'Cecil Committer', 'cecil@committers.tld')
  80. clone_repo.create_commit(
  81. 'refs/heads/master', # the name of the reference to update
  82. author,
  83. committer,
  84. 'Editing the file sources for testing #%s' % i,
  85. # binary string representing the tree object ID
  86. tree,
  87. # list of binary strings representing parents of the new commit
  88. parents
  89. )
  90. # Push to the main repo
  91. refname = 'refs/heads/master:refs/heads/master'
  92. ori_remote = clone_repo.remotes[0]
  93. PagureRepo.push(ori_remote, refname)
  94. # Push to the fork repo
  95. remote = clone_repo.create_remote('pingou_fork', gitrepo2)
  96. PagureRepo.push(remote, refname)
  97. # Do another 3 commits to the main repo
  98. for i in range(3, 6):
  99. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  100. stream.write('foo%s\n bar%s\n' % (i, i))
  101. clone_repo.index.add('sources')
  102. clone_repo.index.write()
  103. last_commit = clone_repo.revparse_single('HEAD')
  104. # Commits the files added
  105. tree = clone_repo.index.write_tree()
  106. author = pygit2.Signature(
  107. 'Alice Author', 'alice@authors.tld')
  108. committer = pygit2.Signature(
  109. 'Cecil Committer', 'cecil@committers.tld')
  110. clone_repo.create_commit(
  111. 'refs/heads/master', # the name of the reference to update
  112. author,
  113. committer,
  114. 'Editing the file sources for testing #%s' % i,
  115. # binary string representing the tree object ID
  116. tree,
  117. # list of binary strings representing parents of the new commit
  118. [last_commit.oid.hex]
  119. )
  120. # Push to the main repo
  121. refname = 'refs/heads/master:refs/heads/master'
  122. ori_remote = clone_repo.remotes[0]
  123. PagureRepo.push(ori_remote, refname)
  124. # Add one commit to the fork repo
  125. repopath = os.path.join(newpath, 'pingou_test')
  126. clone_repo = pygit2.clone_repository(gitrepo2, repopath)
  127. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  128. stream.write('foo\n bar\n')
  129. clone_repo.index.add('sources')
  130. clone_repo.index.write()
  131. last_commit = clone_repo.revparse_single('HEAD')
  132. # Commits the files added
  133. tree = clone_repo.index.write_tree()
  134. author = pygit2.Signature(
  135. 'Alice Author', 'alice@authors.tld')
  136. committer = pygit2.Signature(
  137. 'Cecil Committer', 'cecil@committers.tld')
  138. clone_repo.create_commit(
  139. 'refs/heads/feature_foo', # the name of the reference to update
  140. author,
  141. committer,
  142. 'New edition on side branch of the file sources for testing',
  143. # binary string representing the tree object ID
  144. tree,
  145. # list of binary strings representing parents of the new commit
  146. [last_commit.oid.hex]
  147. )
  148. # Push to the fork repo
  149. ori_remote = clone_repo.remotes[0]
  150. refname = 'refs/heads/feature_foo:refs/heads/feature_foo'
  151. PagureRepo.push(ori_remote, refname)
  152. shutil.rmtree(newpath)
  153. # Create the PR between the two repos
  154. repo = pagure.get_authorized_project(self.session, 'test')
  155. forked_repo = pagure.get_authorized_project(
  156. self.session, 'test', user='pingou')
  157. req = pagure.lib.new_pull_request(
  158. session=self.session,
  159. repo_from=forked_repo,
  160. branch_from='feature_foo',
  161. repo_to=repo,
  162. branch_to='master',
  163. title='test pull-request',
  164. user='pingou',
  165. requestfolder=None,
  166. )
  167. self.assertEqual(req.id, 1)
  168. self.assertEqual(req.title, 'test pull-request')
  169. def test_get_pr_info(self):
  170. """ Test pagure.ui.fork._get_pr_info """
  171. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  172. gitrepo2 = os.path.join(
  173. self.path, 'repos', 'forks', 'pingou', 'test.git')
  174. diff, diff_commits, orig_commit = pagure.lib.git.get_diff_info(
  175. repo_obj=PagureRepo(gitrepo2),
  176. orig_repo=PagureRepo(gitrepo),
  177. branch_from='feature_foo',
  178. branch_to='master'
  179. )
  180. self.assertEqual(len(diff_commits), 1)
  181. self.assertEqual(
  182. diff_commits[0].message,
  183. 'New edition on side branch of the file sources for testing'
  184. )
  185. self.assertEqual(
  186. orig_commit.message,
  187. 'Editing the file sources for testing #5'
  188. )
  189. def test_get_pr_info_raises(self):
  190. """ Test pagure.ui.fork._get_pr_info """
  191. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  192. gitrepo2 = os.path.join(
  193. self.path, 'repos', 'forks', 'pingou', 'test.git')
  194. self.assertRaises(
  195. pagure.exceptions.BranchNotFoundException,
  196. pagure.lib.git.get_diff_info,
  197. repo_obj=PagureRepo(gitrepo2),
  198. orig_repo=PagureRepo(gitrepo),
  199. branch_from='feature',
  200. branch_to='master'
  201. )
  202. self.assertRaises(
  203. pagure.exceptions.BranchNotFoundException,
  204. pagure.lib.git.get_diff_info,
  205. repo_obj=PagureRepo(gitrepo2),
  206. orig_repo=PagureRepo(gitrepo),
  207. branch_from='feature_foo',
  208. branch_to='bar'
  209. )
  210. def test_diff_pull_request(self):
  211. """ Test pagure.lib.git.diff_pull_request """
  212. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  213. gitrepo2 = os.path.join(
  214. self.path, 'repos', 'forks', 'pingou', 'test.git')
  215. request = pagure.lib.search_pull_requests(
  216. self.session, requestid=1, project_id=1)
  217. diff_commits, diff = pagure.lib.git.diff_pull_request(
  218. self.session,
  219. request=request,
  220. repo_obj=PagureRepo(gitrepo2),
  221. orig_repo=PagureRepo(gitrepo),
  222. requestfolder=None,
  223. with_diff=True
  224. )
  225. self.assertEqual(len(diff_commits), 1)
  226. self.assertEqual(
  227. diff_commits[0].message,
  228. 'New edition on side branch of the file sources for testing'
  229. )
  230. if __name__ == '__main__':
  231. unittest.main(verbosity=2)