test_pagure_lib_link.py 11 KB

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