test_pagure_lib_link.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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
  20. import pagure.lib.link
  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 https://fedorahosted.org/pagure/tests2/issue/4',
  27. 'This relates to #5',
  28. 'Could this be related to https://fedorahosted.org/pagure/tests2/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.get_authorized_project(self.session, 'test')
  56. pagure.lib.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.get_authorized_project(self.session, 'test')
  90. pagure.lib.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.get_authorized_project(self.session, 'test')
  141. pagure.lib.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. 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/fork/pingou/test/issue/1'
  167. for index, regex in enumerate(pagure.lib.link.RELATES):
  168. if index == 2:
  169. self.assertNotEqual(regex.match(text), None)
  170. else:
  171. self.assertEqual(regex.match(text), None)
  172. text = 'relates http://209.132.184.222/fork/pingou/test/issue/1'
  173. for index, regex in enumerate(pagure.lib.link.RELATES):
  174. if index == 2:
  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. ' https://fedorahosted.org/pagure/tests2/issue/6'
  186. for index, regex in enumerate(pagure.lib.link.RELATES):
  187. if index == 2:
  188. self.assertNotEqual(regex.match(text), None)
  189. else:
  190. self.assertEqual(regex.match(text), None)
  191. text = 'relates https://localhost/SSSD/ding-libs/issue/31'
  192. for index, regex in enumerate(pagure.lib.link.RELATES):
  193. if index == 2:
  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()), 2)
  208. self.assertEqual(match.groups(), groups)
  209. data = [
  210. # [string, groups]
  211. ]
  212. project_match('fixes http://localhost/fork/pingou/test/issue/1',
  213. ('test', '1'))
  214. project_match('fix http://209.132.184.222/fork/pingou/test/issue/1',
  215. ('test', '1'))
  216. project_match('Could this be fixes '
  217. ' https://fedorahosted.org/pagure/tests2/issue/6',
  218. ('tests2', '6'))
  219. project_match('merged https://pagure.io/myproject/pull-request/70',
  220. ('myproject', '70'))
  221. project_match('Now we merge https://pagure.io/myproject/pull-request/99',
  222. ('myproject', '99'))
  223. project_match('Merges http://localhost/fork/pingou/test/issue/1',
  224. ('test', '1'))
  225. project_match('Merges: http://localhost/fork/pingou/test/issue/12',
  226. ('test', '12'))
  227. project_match('Merged http://localhost/fork/pingou/test/issue/123#',
  228. ('test', '123'))
  229. project_match('Merge: http://localhost/fork/pingou/test/issue/1234#foo',
  230. ('test', '1234'))
  231. project_match('Merges: https://localhost/SSSD/ding-libs/pull-request/3188',
  232. ('ding-libs', '3188'))
  233. # issue matches
  234. def issue_match(text, issue):
  235. match = None
  236. for regex in pagure.lib.link.FIXES:
  237. match = regex.match(text)
  238. if match:
  239. break
  240. self.assertNotEqual(match, None)
  241. self.assertEqual(len(match.groups()), 1)
  242. self.assertEqual(match.group(1), issue)
  243. issue_match('This fixed #5', '5')
  244. issue_match('Merged #17', '17')
  245. issue_match('Fixed: #23', '23')
  246. issue_match('This commit fixes: #42', '42')
  247. issue_match('Merge #137', '137')
  248. issue_match('Merges #137', '137')
  249. issue_match('Merges: #137', '137')
  250. # no match
  251. def no_match(text):
  252. match = None
  253. for regex in pagure.lib.link.FIXES:
  254. match = regex.match(text)
  255. if match:
  256. break
  257. self.assertEqual(match, None)
  258. no_match('nowhitespacemerge: #47')
  259. no_match('This commit unmerges #45')
  260. no_match('Fixed 45 typos')
  261. no_match('Fixed 4 typos')
  262. no_match("Merge branch 'work'")
  263. if __name__ == '__main__':
  264. unittest.main(verbosity=2)