test_pagure_lib_link.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. import json
  11. import unittest
  12. import shutil
  13. import sys
  14. import os
  15. import pygit2
  16. from mock import patch
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure.lib.link
  20. import pagure.lib.query
  21. import tests
  22. COMMENTS = [
  23. 'Did you see #1?',
  24. 'This is a duplicate of #2',
  25. 'This is a fixes #3',
  26. 'Might be worth looking at http://localhost.localdomain/pagure/tests2/issue/4',
  27. 'This relates to #5',
  28. 'Could this be related to http://localhost.localdomain/test/issue/6',
  29. ]
  30. class PagureLibLinktests(tests.Modeltests):
  31. """ Tests for pagure.lib.link """
  32. def test_get_relation_relates(self):
  33. """ Test the get_relation function of pagure.lib.link with relates.
  34. """
  35. link = pagure.lib.link.get_relation(
  36. self.session,
  37. reponame='test',
  38. namespace=None,
  39. username=None,
  40. text=COMMENTS[0],
  41. reftype='relates',
  42. )
  43. self.assertEqual(link, [])
  44. tests.create_projects(self.session)
  45. link = pagure.lib.link.get_relation(
  46. self.session,
  47. reponame='test',
  48. namespace=None,
  49. username=None,
  50. text=COMMENTS[4],
  51. reftype='relates',
  52. )
  53. self.assertEqual(link, [])
  54. # Create the issue
  55. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  56. pagure.lib.query.new_issue(
  57. self.session,
  58. repo,
  59. title='foo',
  60. content='bar',
  61. user='pingou',
  62. issue_id=5,
  63. notify=False)
  64. self.session.commit()
  65. for idx, comment in enumerate(COMMENTS):
  66. link = pagure.lib.link.get_relation(
  67. self.session,
  68. reponame='test',
  69. namespace=None,
  70. username=None,
  71. text=comment,
  72. reftype='relates')
  73. if idx == 4:
  74. self.assertEqual(
  75. str(link),
  76. '[Issue(5, project:test, user:pingou, title:foo)]')
  77. else:
  78. self.assertEqual(link, [])
  79. link = pagure.lib.link.get_relation(
  80. self.session,
  81. reponame='test',
  82. namespace=None,
  83. username=None,
  84. text=COMMENTS[5],
  85. reftype='relates',
  86. )
  87. self.assertEqual(link, [])
  88. # Create the issue
  89. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  90. pagure.lib.query.new_issue(
  91. self.session,
  92. repo,
  93. title='another foo',
  94. content='another bar',
  95. user='pingou',
  96. issue_id=6,
  97. notify=False)
  98. self.session.commit()
  99. for idx, comment in enumerate(COMMENTS):
  100. link = pagure.lib.link.get_relation(
  101. self.session,
  102. reponame='test',
  103. namespace=None,
  104. username=None,
  105. text=comment,
  106. reftype='relates')
  107. if idx == 4:
  108. self.assertEqual(
  109. str(link),
  110. '[Issue(5, project:test, user:pingou, title:foo)]')
  111. elif idx == 5:
  112. self.assertEqual(
  113. str(link),
  114. '[Issue(6, project:test, user:pingou, title:another foo)]')
  115. else:
  116. self.assertEqual(link, [])
  117. def test_get_relation_fixes(self):
  118. """ Test the get_relation function of pagure.lib.link with fixes.
  119. """
  120. link = pagure.lib.link.get_relation(
  121. self.session,
  122. reponame='test',
  123. namespace=None,
  124. username=None,
  125. text=COMMENTS[0],
  126. reftype='fixes',
  127. )
  128. self.assertEqual(link, [])
  129. tests.create_projects(self.session)
  130. link = pagure.lib.link.get_relation(
  131. self.session,
  132. reponame='test',
  133. namespace=None,
  134. username=None,
  135. text=COMMENTS[2],
  136. reftype='fixes',
  137. )
  138. self.assertEqual(link, [])
  139. # Create the issue
  140. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  141. pagure.lib.query.new_issue(
  142. self.session,
  143. repo,
  144. title='issue 3',
  145. content='content issue 3',
  146. user='pingou',
  147. issue_id=3,
  148. notify=False)
  149. self.session.commit()
  150. for idx, comment in enumerate(COMMENTS):
  151. link = pagure.lib.link.get_relation(
  152. self.session,
  153. reponame='test',
  154. namespace=None,
  155. username=None,
  156. text=comment,
  157. reftype='fixes')
  158. if idx == 2:
  159. self.assertEqual(
  160. str(link),
  161. '[Issue(3, project:test, user:pingou, title:issue 3)]')
  162. self.assertEqual(len(link), 1)
  163. self.assertEqual(link[0].project.fullname, 'test')
  164. else:
  165. self.assertEqual(link, [])
  166. def test_relates_regex(self):
  167. ''' Test the relates regex present in pagure.lib.link. '''
  168. text = 'relates to http://localhost.localdomain/fork/pingou/test/issue/1'
  169. for index, regex in enumerate(pagure.lib.link.RELATES):
  170. if index == 1:
  171. self.assertNotEqual(regex.match(text), None)
  172. else:
  173. self.assertEqual(regex.match(text), None)
  174. text = 'relates http://localhost.localdomain/fork/pingou/test/issue/1'
  175. for index, regex in enumerate(pagure.lib.link.RELATES):
  176. if index == 1:
  177. self.assertNotEqual(regex.match(text), None)
  178. else:
  179. self.assertEqual(regex.match(text), None)
  180. text = 'This relates to #5'
  181. for index, regex in enumerate(pagure.lib.link.RELATES):
  182. if index == 0:
  183. self.assertNotEqual(regex.match(text), None)
  184. else:
  185. self.assertEqual(regex.match(text), None)
  186. text = 'Could this be related to '\
  187. ' http://localhost.localdomain/pagure/tests2/issue/6'
  188. for index, regex in enumerate(pagure.lib.link.RELATES):
  189. if index == 1:
  190. self.assertNotEqual(regex.match(text), None)
  191. else:
  192. self.assertEqual(regex.match(text), None)
  193. text = 'relates http://localhost.localdomain/SSSD/ding-libs/issue/31'
  194. for index, regex in enumerate(pagure.lib.link.RELATES):
  195. if index == 1:
  196. self.assertNotEqual(regex.match(text), None)
  197. else:
  198. self.assertEqual(regex.match(text), None)
  199. def test_fixes_regex(self):
  200. ''' Test the fixes regex present in pagure.lib.link. '''
  201. # project/issue matches
  202. def project_match(text, groups):
  203. match = None
  204. for regex in pagure.lib.link.FIXES:
  205. match = regex.match(text)
  206. if match:
  207. break
  208. self.assertNotEqual(match, None)
  209. self.assertEqual(len(match.groups()), 1)
  210. self.assertEqual(match.groups(), groups)
  211. data = [
  212. # [string, groups]
  213. ]
  214. project_match(
  215. 'fixes '
  216. 'http://localhost.localdomain/fork/pingou/test/issue/1',
  217. ('/fork/pingou/test/issue/1',))
  218. project_match(
  219. 'Could this be fixes '
  220. ' http://localhost.localdomain/pagure/tests2/issue/6',
  221. ('/pagure/tests2/issue/6',))
  222. project_match(
  223. 'merged http://localhost.localdomain/myproject/pull-request/70',
  224. ('/myproject/pull-request/70',))
  225. project_match(
  226. 'Now we merge http://localhost.localdomain/myproject/pull-request/99',
  227. ('/myproject/pull-request/99',))
  228. project_match(
  229. 'Merges http://localhost.localdomain/fork/pingou/test/issue/1',
  230. ('/fork/pingou/test/issue/1',))
  231. project_match(
  232. 'Merges: http://localhost.localdomain/fork/pingou/test/issue/12',
  233. ('/fork/pingou/test/issue/12',))
  234. project_match(
  235. 'Merged http://localhost.localdomain/fork/pingou/test/issue/123#',
  236. ('/fork/pingou/test/issue/123',))
  237. project_match('Merge: http://localhost.localdomain/fork/pingou/test/issue/1234#foo',
  238. ('/fork/pingou/test/issue/1234',))
  239. project_match(
  240. 'Merges: http://localhost.localdomain/SSSD/ding-libs/pull-request/3188',
  241. ('/SSSD/ding-libs/pull-request/3188',))
  242. project_match(
  243. 'Fixes: http://localhost.localdomain/fedpkg/issue/220',
  244. ('/fedpkg/issue/220',))
  245. # issue matches
  246. def issue_match(text, issue):
  247. match = None
  248. for regex in pagure.lib.link.FIXES:
  249. match = regex.match(text)
  250. if match:
  251. break
  252. self.assertNotEqual(match, None)
  253. self.assertEqual(len(match.groups()), 1)
  254. self.assertEqual(match.group(1), issue)
  255. issue_match('This fixed #5', '5')
  256. issue_match('Merged #17', '17')
  257. issue_match('Fixed: #23', '23')
  258. issue_match('This commit fixes: #42', '42')
  259. issue_match('Merge #137', '137')
  260. issue_match('Merges #137', '137')
  261. issue_match('Merges: #137', '137')
  262. # no match
  263. def no_match(text):
  264. match = None
  265. for regex in pagure.lib.link.FIXES:
  266. match = regex.match(text)
  267. if match:
  268. break
  269. self.assertEqual(match, None)
  270. no_match('nowhitespacemerge: #47')
  271. no_match('This commit unmerges #45')
  272. no_match('Fixed 45 typos')
  273. no_match('Fixed 4 typos')
  274. no_match("Merge branch 'work'")
  275. if __name__ == '__main__':
  276. unittest.main(verbosity=2)