test_pagure_lib_link.py 11 KB

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