1
0

test_pagure_lib_link.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. import pagure.lib.link
  19. import pagure.lib.query
  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 http://localhost.localdomain/pagure/tests2/issue/4",
  26. "This relates to #5",
  27. "Could this be related to http://localhost.localdomain/test/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.query.get_authorized_project(self.session, "test")
  55. pagure.lib.query.new_issue(
  56. self.session,
  57. repo,
  58. title="foo",
  59. content="bar",
  60. user="pingou",
  61. issue_id=5,
  62. notify=False,
  63. )
  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. )
  74. if idx == 4:
  75. self.assertEqual(
  76. str(link),
  77. "[Issue(5, project:test, user:pingou, title:foo)]",
  78. )
  79. else:
  80. self.assertEqual(link, [])
  81. link = pagure.lib.link.get_relation(
  82. self.session,
  83. reponame="test",
  84. namespace=None,
  85. username=None,
  86. text=COMMENTS[5],
  87. reftype="relates",
  88. )
  89. self.assertEqual(link, [])
  90. # Create the issue
  91. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  92. pagure.lib.query.new_issue(
  93. self.session,
  94. repo,
  95. title="another foo",
  96. content="another bar",
  97. user="pingou",
  98. issue_id=6,
  99. notify=False,
  100. )
  101. self.session.commit()
  102. for idx, comment in enumerate(COMMENTS):
  103. link = pagure.lib.link.get_relation(
  104. self.session,
  105. reponame="test",
  106. namespace=None,
  107. username=None,
  108. text=comment,
  109. reftype="relates",
  110. )
  111. if idx == 4:
  112. self.assertEqual(
  113. str(link),
  114. "[Issue(5, project:test, user:pingou, title:foo)]",
  115. )
  116. elif idx == 5:
  117. self.assertEqual(
  118. str(link),
  119. "[Issue(6, project:test, user:pingou, title:another foo)]",
  120. )
  121. else:
  122. self.assertEqual(link, [])
  123. def test_get_relation_fixes(self):
  124. """ Test the get_relation function of pagure.lib.link with fixes.
  125. """
  126. link = pagure.lib.link.get_relation(
  127. self.session,
  128. reponame="test",
  129. namespace=None,
  130. username=None,
  131. text=COMMENTS[0],
  132. reftype="fixes",
  133. )
  134. self.assertEqual(link, [])
  135. tests.create_projects(self.session)
  136. link = pagure.lib.link.get_relation(
  137. self.session,
  138. reponame="test",
  139. namespace=None,
  140. username=None,
  141. text=COMMENTS[2],
  142. reftype="fixes",
  143. )
  144. self.assertEqual(link, [])
  145. # Create the issue
  146. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  147. pagure.lib.query.new_issue(
  148. self.session,
  149. repo,
  150. title="issue 3",
  151. content="content issue 3",
  152. user="pingou",
  153. issue_id=3,
  154. notify=False,
  155. )
  156. self.session.commit()
  157. for idx, comment in enumerate(COMMENTS):
  158. link = pagure.lib.link.get_relation(
  159. self.session,
  160. reponame="test",
  161. namespace=None,
  162. username=None,
  163. text=comment,
  164. reftype="fixes",
  165. )
  166. if idx == 2:
  167. self.assertEqual(
  168. str(link),
  169. "[Issue(3, project:test, user:pingou, title:issue 3)]",
  170. )
  171. self.assertEqual(len(link), 1)
  172. self.assertEqual(link[0].project.fullname, "test")
  173. else:
  174. self.assertEqual(link, [])
  175. def test_relates_regex(self):
  176. """ Test the relates regex present in pagure.lib.link. """
  177. text = "relates to http://localhost.localdomain/fork/pingou/test/issue/1"
  178. for index, regex in enumerate(pagure.lib.link.RELATES):
  179. if index == 1:
  180. self.assertNotEqual(regex.match(text), None)
  181. else:
  182. self.assertEqual(regex.match(text), None)
  183. text = "relates http://localhost.localdomain/fork/pingou/test/issue/1"
  184. for index, regex in enumerate(pagure.lib.link.RELATES):
  185. if index == 1:
  186. self.assertNotEqual(regex.match(text), None)
  187. else:
  188. self.assertEqual(regex.match(text), None)
  189. text = "This relates to #5"
  190. for index, regex in enumerate(pagure.lib.link.RELATES):
  191. if index == 0:
  192. self.assertNotEqual(regex.match(text), None)
  193. else:
  194. self.assertEqual(regex.match(text), None)
  195. text = (
  196. "Could this be related to "
  197. " http://localhost.localdomain/pagure/tests2/issue/6"
  198. )
  199. for index, regex in enumerate(pagure.lib.link.RELATES):
  200. if index == 1:
  201. self.assertNotEqual(regex.match(text), None)
  202. else:
  203. self.assertEqual(regex.match(text), None)
  204. text = "relates http://localhost.localdomain/SSSD/ding-libs/issue/31"
  205. for index, regex in enumerate(pagure.lib.link.RELATES):
  206. if index == 1:
  207. self.assertNotEqual(regex.match(text), None)
  208. else:
  209. self.assertEqual(regex.match(text), None)
  210. def test_fixes_regex(self):
  211. """ Test the fixes regex present in pagure.lib.link. """
  212. # project/issue matches
  213. def project_match(text, groups):
  214. match = None
  215. for regex in pagure.lib.link.FIXES:
  216. match = regex.match(text)
  217. if match:
  218. break
  219. self.assertNotEqual(match, None)
  220. self.assertEqual(len(match.groups()), 1)
  221. self.assertEqual(match.groups(), groups)
  222. data = [
  223. # [string, groups]
  224. ]
  225. project_match(
  226. "fixes "
  227. "http://localhost.localdomain/fork/pingou/test/issue/1",
  228. ("/fork/pingou/test/issue/1",),
  229. )
  230. project_match(
  231. "Could this be fixes "
  232. " http://localhost.localdomain/pagure/tests2/issue/6",
  233. ("/pagure/tests2/issue/6",),
  234. )
  235. project_match(
  236. "merged http://localhost.localdomain/myproject/pull-request/70",
  237. ("/myproject/pull-request/70",),
  238. )
  239. project_match(
  240. "Now we merge http://localhost.localdomain/myproject/pull-request/99",
  241. ("/myproject/pull-request/99",),
  242. )
  243. project_match(
  244. "Merges http://localhost.localdomain/fork/pingou/test/issue/1",
  245. ("/fork/pingou/test/issue/1",),
  246. )
  247. project_match(
  248. "Merges: http://localhost.localdomain/fork/pingou/test/issue/12",
  249. ("/fork/pingou/test/issue/12",),
  250. )
  251. project_match(
  252. "Merged http://localhost.localdomain/fork/pingou/test/issue/123#",
  253. ("/fork/pingou/test/issue/123",),
  254. )
  255. project_match(
  256. "Merge: http://localhost.localdomain/fork/pingou/test/issue/1234#foo",
  257. ("/fork/pingou/test/issue/1234",),
  258. )
  259. project_match(
  260. "Merges: http://localhost.localdomain/SSSD/ding-libs/pull-request/3188",
  261. ("/SSSD/ding-libs/pull-request/3188",),
  262. )
  263. project_match(
  264. "Fixes: http://localhost.localdomain/fedpkg/issue/220",
  265. ("/fedpkg/issue/220",),
  266. )
  267. project_match(
  268. "resolved: http://localhost.localdomain/fork/pingou/test/issue/1234#foo",
  269. ("/fork/pingou/test/issue/1234",),
  270. )
  271. project_match(
  272. "resolve http://localhost.localdomain/fork/pingou/test/issue/1234#foo",
  273. ("/fork/pingou/test/issue/1234",),
  274. )
  275. # issue matches
  276. def issue_match(text, issue):
  277. match = None
  278. for regex in pagure.lib.link.FIXES:
  279. match = regex.match(text)
  280. if match:
  281. break
  282. self.assertNotEqual(match, None)
  283. self.assertEqual(len(match.groups()), 1)
  284. self.assertEqual(match.group(1), issue)
  285. issue_match("This fixed #5", "5")
  286. issue_match("This fix #5", "5")
  287. issue_match("Merged #17", "17")
  288. issue_match("Fixed: #23", "23")
  289. issue_match("Fix: #23", "23")
  290. issue_match("This commit fixes: #42", "42")
  291. issue_match("This commit fix #42", "42")
  292. issue_match("Merge #137", "137")
  293. issue_match("Merges #137", "137")
  294. issue_match("Merges: #137", "137")
  295. issue_match("resolve #137", "137")
  296. issue_match("Resolves #137", "137")
  297. issue_match("RESOLVED: #137", "137")
  298. issue_match(
  299. "Fixes: http://localhost.localdomain/fedpkg/issue/220",
  300. "/fedpkg/issue/220",
  301. )
  302. issue_match(
  303. "Resolved: http://localhost.localdomain/fedpkg/issue/222",
  304. "/fedpkg/issue/222",
  305. )
  306. # no match
  307. def no_match(text):
  308. match = None
  309. for regex in pagure.lib.link.FIXES:
  310. match = regex.match(text)
  311. if match:
  312. break
  313. self.assertEqual(match, None)
  314. no_match("nowhitespacemerge: #47")
  315. no_match("This commit unmerges #45")
  316. no_match("Fixed 45 typos")
  317. no_match("Fixed 4 typos")
  318. no_match("Merge branch 'work'")
  319. if __name__ == "__main__":
  320. unittest.main(verbosity=2)