test_pagure_lib_link.py 8.7 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.lib.link
  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 https://fedorahosted.org/pagure/tests2/issue/4',
  25. 'This relates to #5',
  26. 'Could this be related to https://fedorahosted.org/pagure/tests2/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.get_project(self.session, 'test')
  54. pagure.lib.new_issue(
  55. self.session,
  56. repo,
  57. title='foo',
  58. content='bar',
  59. user='pingou',
  60. ticketfolder=None,
  61. issue_id=5,
  62. notify=False)
  63. self.session.commit()
  64. for idx, comment in enumerate(COMMENTS):
  65. link = pagure.lib.link.get_relation(
  66. self.session,
  67. reponame='test',
  68. namespace=None,
  69. username=None,
  70. text=comment,
  71. reftype='relates')
  72. if idx == 4:
  73. self.assertEqual(
  74. str(link),
  75. '[Issue(5, project:test, user:pingou, title:foo)]')
  76. else:
  77. self.assertEqual(link, [])
  78. link = pagure.lib.link.get_relation(
  79. self.session,
  80. reponame='test',
  81. namespace=None,
  82. username=None,
  83. text=COMMENTS[5],
  84. reftype='relates',
  85. )
  86. self.assertEqual(link, [])
  87. # Create the issue
  88. repo = pagure.lib.get_project(self.session, 'test')
  89. pagure.lib.new_issue(
  90. self.session,
  91. repo,
  92. title='another foo',
  93. content='another bar',
  94. user='pingou',
  95. ticketfolder=None,
  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_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. ticketfolder=None,
  148. issue_id=3,
  149. notify=False)
  150. self.session.commit()
  151. for idx, comment in enumerate(COMMENTS):
  152. link = pagure.lib.link.get_relation(
  153. self.session,
  154. reponame='test',
  155. namespace=None,
  156. username=None,
  157. text=comment,
  158. reftype='fixes')
  159. if idx == 2:
  160. self.assertEqual(
  161. str(link),
  162. '[Issue(3, project:test, user:pingou, title:issue 3)]')
  163. else:
  164. self.assertEqual(link, [])
  165. def test_relates_regex(self):
  166. ''' Test the relates regex present in pagure.lib.link. '''
  167. text = 'relates to http://localhost/fork/pingou/test/issue/1'
  168. for index, regex in enumerate(pagure.lib.link.RELATES):
  169. if index == 2:
  170. self.assertNotEqual(regex.match(text), None)
  171. else:
  172. self.assertEqual(regex.match(text), None)
  173. text = 'relates http://209.132.184.222/fork/pingou/test/issue/1'
  174. for index, regex in enumerate(pagure.lib.link.RELATES):
  175. if index == 2:
  176. self.assertNotEqual(regex.match(text), None)
  177. else:
  178. self.assertEqual(regex.match(text), None)
  179. text = 'This relates to #5'
  180. for index, regex in enumerate(pagure.lib.link.RELATES):
  181. if index == 0:
  182. self.assertNotEqual(regex.match(text), None)
  183. else:
  184. self.assertEqual(regex.match(text), None)
  185. text = 'Could this be related to '\
  186. ' https://fedorahosted.org/pagure/tests2/issue/6'
  187. for index, regex in enumerate(pagure.lib.link.RELATES):
  188. if index == 2:
  189. self.assertNotEqual(regex.match(text), None)
  190. else:
  191. self.assertEqual(regex.match(text), None)
  192. def test_fixes_regex(self):
  193. ''' Test the fixes regex present in pagure.lib.link. '''
  194. # project/issue matches
  195. def project_match(text, groups):
  196. match = None
  197. for regex in pagure.lib.link.FIXES:
  198. match = regex.match(text)
  199. if match:
  200. break
  201. self.assertNotEqual(match, None)
  202. self.assertEqual(len(match.groups()), 2)
  203. self.assertEqual(match.groups(), groups)
  204. data = [
  205. # [string, groups]
  206. ]
  207. project_match('fixes http://localhost/fork/pingou/test/issue/1',
  208. ('test', '1'))
  209. project_match('fix http://209.132.184.222/fork/pingou/test/issue/1',
  210. ('test', '1'))
  211. project_match('Could this be fixes '
  212. ' https://fedorahosted.org/pagure/tests2/issue/6',
  213. ('tests2', '6'))
  214. project_match('merged https://pagure.io/myproject/pull-request/70',
  215. ('myproject', '70'))
  216. project_match('Now we merge https://pagure.io/myproject/pull-request/99',
  217. ('myproject', '99'))
  218. # issue matches
  219. def issue_match(text, issue):
  220. match = None
  221. for regex in pagure.lib.link.FIXES:
  222. match = regex.match(text)
  223. if match:
  224. break
  225. self.assertNotEqual(match, None)
  226. self.assertEqual(len(match.groups()), 1)
  227. self.assertEqual(match.group(1), issue)
  228. issue_match('This fixed #5', '5')
  229. issue_match('Merged #17', '17')
  230. issue_match('Fixed: #23', '23')
  231. issue_match('This commit fixes: #42', '42')
  232. issue_match('Merge #137', '137')
  233. # no match
  234. def no_match(text):
  235. match = None
  236. for regex in pagure.lib.link.FIXES:
  237. match = regex.match(text)
  238. if match:
  239. break
  240. self.assertEqual(match, None)
  241. no_match('nowhitespacemerge: #47')
  242. no_match('This commit unmerges #45')
  243. no_match('Fixed 45 typos')
  244. no_match('Fixed 4 typos')
  245. no_match("Merge branch 'work'")
  246. if __name__ == '__main__':
  247. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibLinktests)
  248. unittest.TextTestRunner(verbosity=2).run(SUITE)