1
0

test_pagure_lib_link.py 9.5 KB

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