test_pagure_lib_link.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import pygit2
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure
  19. import pagure.lib.link
  20. import tests
  21. COMMENTS = [
  22. 'Did you see #1?',
  23. 'This is a duplicate of #2',
  24. 'This is a fixes #3',
  25. 'Might be worth looking at https://fedorahosted.org/pagure/tests2/issue/4',
  26. 'This relates to #5',
  27. 'Could this be related to https://fedorahosted.org/pagure/tests2/issue/6',
  28. ]
  29. class PagureLibLinktests(tests.Modeltests):
  30. """ Tests for pagure.lib.link """
  31. def test_get_relation_relates(self):
  32. """ Test the get_relation function of pagure.lib.link with relates.
  33. """
  34. link = pagure.lib.link.get_relation(
  35. self.session,
  36. reponame='test',
  37. namespace=None,
  38. username=None,
  39. text=COMMENTS[0],
  40. reftype='relates',
  41. )
  42. self.assertEqual(link, [])
  43. tests.create_projects(self.session)
  44. link = pagure.lib.link.get_relation(
  45. self.session,
  46. reponame='test',
  47. namespace=None,
  48. username=None,
  49. text=COMMENTS[4],
  50. reftype='relates',
  51. )
  52. self.assertEqual(link, [])
  53. # Create the issue
  54. repo = pagure.get_authorized_project(self.session, 'test')
  55. pagure.lib.new_issue(
  56. self.session,
  57. repo,
  58. title='foo',
  59. content='bar',
  60. user='pingou',
  61. ticketfolder=None,
  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.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. ticketfolder=None,
  97. issue_id=6,
  98. notify=False)
  99. self.session.commit()
  100. for idx, comment in enumerate(COMMENTS):
  101. link = pagure.lib.link.get_relation(
  102. self.session,
  103. reponame='test',
  104. namespace=None,
  105. username=None,
  106. text=comment,
  107. reftype='relates')
  108. if idx == 4:
  109. self.assertEqual(
  110. str(link),
  111. '[Issue(5, project:test, user:pingou, title:foo)]')
  112. elif idx == 5:
  113. self.assertEqual(
  114. str(link),
  115. '[Issue(6, project:test, user:pingou, title:another foo)]')
  116. else:
  117. self.assertEqual(link, [])
  118. def test_get_relation_fixes(self):
  119. """ Test the get_relation function of pagure.lib.link with fixes.
  120. """
  121. link = pagure.lib.link.get_relation(
  122. self.session,
  123. reponame='test',
  124. namespace=None,
  125. username=None,
  126. text=COMMENTS[0],
  127. reftype='fixes',
  128. )
  129. self.assertEqual(link, [])
  130. tests.create_projects(self.session)
  131. link = pagure.lib.link.get_relation(
  132. self.session,
  133. reponame='test',
  134. namespace=None,
  135. username=None,
  136. text=COMMENTS[2],
  137. reftype='fixes',
  138. )
  139. self.assertEqual(link, [])
  140. # Create the issue
  141. repo = pagure.get_authorized_project(self.session, 'test')
  142. pagure.lib.new_issue(
  143. self.session,
  144. repo,
  145. title='issue 3',
  146. content='content issue 3',
  147. user='pingou',
  148. ticketfolder=None,
  149. issue_id=3,
  150. notify=False)
  151. self.session.commit()
  152. for idx, comment in enumerate(COMMENTS):
  153. link = pagure.lib.link.get_relation(
  154. self.session,
  155. reponame='test',
  156. namespace=None,
  157. username=None,
  158. text=comment,
  159. reftype='fixes')
  160. if idx == 2:
  161. self.assertEqual(
  162. str(link),
  163. '[Issue(3, project:test, user:pingou, title:issue 3)]')
  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/fork/pingou/test/issue/1'
  169. for index, regex in enumerate(pagure.lib.link.RELATES):
  170. if index == 2:
  171. self.assertNotEqual(regex.match(text), None)
  172. else:
  173. self.assertEqual(regex.match(text), None)
  174. text = 'relates http://209.132.184.222/fork/pingou/test/issue/1'
  175. for index, regex in enumerate(pagure.lib.link.RELATES):
  176. if index == 2:
  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. ' https://fedorahosted.org/pagure/tests2/issue/6'
  188. for index, regex in enumerate(pagure.lib.link.RELATES):
  189. if index == 2:
  190. self.assertNotEqual(regex.match(text), None)
  191. else:
  192. self.assertEqual(regex.match(text), None)
  193. def test_fixes_regex(self):
  194. ''' Test the fixes regex present in pagure.lib.link. '''
  195. # project/issue matches
  196. def project_match(text, groups):
  197. match = None
  198. for regex in pagure.lib.link.FIXES:
  199. match = regex.match(text)
  200. if match:
  201. break
  202. self.assertNotEqual(match, None)
  203. self.assertEqual(len(match.groups()), 2)
  204. self.assertEqual(match.groups(), groups)
  205. data = [
  206. # [string, groups]
  207. ]
  208. project_match('fixes http://localhost/fork/pingou/test/issue/1',
  209. ('test', '1'))
  210. project_match('fix http://209.132.184.222/fork/pingou/test/issue/1',
  211. ('test', '1'))
  212. project_match('Could this be fixes '
  213. ' https://fedorahosted.org/pagure/tests2/issue/6',
  214. ('tests2', '6'))
  215. project_match('merged https://pagure.io/myproject/pull-request/70',
  216. ('myproject', '70'))
  217. project_match('Now we merge https://pagure.io/myproject/pull-request/99',
  218. ('myproject', '99'))
  219. # issue matches
  220. def issue_match(text, issue):
  221. match = None
  222. for regex in pagure.lib.link.FIXES:
  223. match = regex.match(text)
  224. if match:
  225. break
  226. self.assertNotEqual(match, None)
  227. self.assertEqual(len(match.groups()), 1)
  228. self.assertEqual(match.group(1), issue)
  229. issue_match('This fixed #5', '5')
  230. issue_match('Merged #17', '17')
  231. issue_match('Fixed: #23', '23')
  232. issue_match('This commit fixes: #42', '42')
  233. issue_match('Merge #137', '137')
  234. # no match
  235. def no_match(text):
  236. match = None
  237. for regex in pagure.lib.link.FIXES:
  238. match = regex.match(text)
  239. if match:
  240. break
  241. self.assertEqual(match, None)
  242. no_match('nowhitespacemerge: #47')
  243. no_match('This commit unmerges #45')
  244. no_match('Fixed 45 typos')
  245. no_match('Fixed 4 typos')
  246. no_match("Merge branch 'work'")
  247. if __name__ == '__main__':
  248. unittest.main(verbosity=2)