Blame tests/test_pagure_lib_link.py

Pierre-Yves Chibon 35fa85
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
"""
Pierre-Yves Chibon 35fa85
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
 Authors:
Pierre-Yves Chibon 35fa85
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
"""
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 35fa85
import json
Pierre-Yves Chibon 35fa85
import unittest
Pierre-Yves Chibon 35fa85
import shutil
Pierre-Yves Chibon 35fa85
import sys
Pierre-Yves Chibon 35fa85
import os
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
import pygit2
Pierre-Yves Chibon 35fa85
from mock import patch
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon fe5017
import pagure.lib.link
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 35fa85
import tests
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
COMMENTS = [
Pierre-Yves Chibon 73d120
    "Did you see #1?",
Pierre-Yves Chibon 73d120
    "This is a duplicate of #2",
Pierre-Yves Chibon 73d120
    "This is a fixes #3",
Pierre-Yves Chibon 73d120
    "Might be worth looking at http://localhost.localdomain/pagure/tests2/issue/4",
Pierre-Yves Chibon 73d120
    "This relates to #5",
Pierre-Yves Chibon 73d120
    "Could this be related to http://localhost.localdomain/test/issue/6",
Pierre-Yves Chibon 35fa85
]
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon fe5017
class PagureLibLinktests(tests.Modeltests):
Pierre-Yves Chibon fe5017
    """ Tests for pagure.lib.link """
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
    def test_get_relation_relates(self):
Pierre-Yves Chibon fe5017
        """ Test the get_relation function of pagure.lib.link with relates.
Pierre-Yves Chibon 35fa85
        """
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 5abfd2
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 73d120
            reponame="test",
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[0],
Pierre-Yves Chibon 73d120
            reftype="relates",
Pierre-Yves Chibon 35fa85
        )
Pierre-Yves Chibon 5abfd2
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        tests.create_projects(self.session)
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon fe5017
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 73d120
            reponame="test",
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[4],
Pierre-Yves Chibon 73d120
            reftype="relates",
Pierre-Yves Chibon 5abfd2
        )
Pierre-Yves Chibon 35fa85
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        # Create the issue
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        pagure.lib.query.new_issue(
Pierre-Yves Chibon 35fa85
            self.session,
Pierre-Yves Chibon 35fa85
            repo,
Pierre-Yves Chibon 73d120
            title="foo",
Pierre-Yves Chibon 73d120
            content="bar",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 35fa85
            issue_id=5,
Pierre-Yves Chibon 73d120
            notify=False,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 35fa85
        self.session.commit()
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        for idx, comment in enumerate(COMMENTS):
Pierre-Yves Chibon fe5017
            link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
                self.session,
Pierre-Yves Chibon 73d120
                reponame="test",
Pierre-Yves Chibon 5abfd2
                namespace=None,
Pierre-Yves Chibon 5abfd2
                username=None,
Pierre-Yves Chibon 5abfd2
                text=comment,
Pierre-Yves Chibon 73d120
                reftype="relates",
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 35fa85
            if idx == 4:
Pierre-Yves Chibon 35fa85
                self.assertEqual(
Pierre-Yves Chibon 35fa85
                    str(link),
Pierre-Yves Chibon 73d120
                    "[Issue(5, project:test, user:pingou, title:foo)]",
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 35fa85
            else:
Pierre-Yves Chibon 35fa85
                self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon fe5017
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 73d120
            reponame="test",
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[5],
Pierre-Yves Chibon 73d120
            reftype="relates",
Pierre-Yves Chibon 5abfd2
        )
Pierre-Yves Chibon 35fa85
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        # Create the issue
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        pagure.lib.query.new_issue(
Pierre-Yves Chibon 35fa85
            self.session,
Pierre-Yves Chibon 35fa85
            repo,
Pierre-Yves Chibon 73d120
            title="another foo",
Pierre-Yves Chibon 73d120
            content="another bar",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 35fa85
            issue_id=6,
Pierre-Yves Chibon 73d120
            notify=False,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 35fa85
        self.session.commit()
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        for idx, comment in enumerate(COMMENTS):
Pierre-Yves Chibon fe5017
            link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
                self.session,
Pierre-Yves Chibon 73d120
                reponame="test",
Pierre-Yves Chibon 5abfd2
                namespace=None,
Pierre-Yves Chibon 5abfd2
                username=None,
Pierre-Yves Chibon 5abfd2
                text=comment,
Pierre-Yves Chibon 73d120
                reftype="relates",
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 35fa85
            if idx == 4:
Pierre-Yves Chibon 35fa85
                self.assertEqual(
Pierre-Yves Chibon 35fa85
                    str(link),
Pierre-Yves Chibon 73d120
                    "[Issue(5, project:test, user:pingou, title:foo)]",
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 35fa85
            elif idx == 5:
Pierre-Yves Chibon 35fa85
                self.assertEqual(
Pierre-Yves Chibon 35fa85
                    str(link),
Pierre-Yves Chibon 73d120
                    "[Issue(6, project:test, user:pingou, title:another foo)]",
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 35fa85
            else:
Pierre-Yves Chibon 35fa85
                self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
    def test_get_relation_fixes(self):
Pierre-Yves Chibon fe5017
        """ Test the get_relation function of pagure.lib.link with fixes.
Pierre-Yves Chibon 35fa85
        """
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 5abfd2
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 73d120
            reponame="test",
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[0],
Pierre-Yves Chibon 73d120
            reftype="fixes",
Pierre-Yves Chibon 35fa85
        )
Pierre-Yves Chibon 5abfd2
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        tests.create_projects(self.session)
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon fe5017
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 73d120
            reponame="test",
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[2],
Pierre-Yves Chibon 73d120
            reftype="fixes",
Pierre-Yves Chibon 5abfd2
        )
Pierre-Yves Chibon 35fa85
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        # Create the issue
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        pagure.lib.query.new_issue(
Pierre-Yves Chibon 35fa85
            self.session,
Pierre-Yves Chibon 35fa85
            repo,
Pierre-Yves Chibon 73d120
            title="issue 3",
Pierre-Yves Chibon 73d120
            content="content issue 3",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 35fa85
            issue_id=3,
Pierre-Yves Chibon 73d120
            notify=False,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 35fa85
        self.session.commit()
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        for idx, comment in enumerate(COMMENTS):
Pierre-Yves Chibon fe5017
            link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
                self.session,
Pierre-Yves Chibon 73d120
                reponame="test",
Pierre-Yves Chibon 5abfd2
                namespace=None,
Pierre-Yves Chibon 5abfd2
                username=None,
Pierre-Yves Chibon 5abfd2
                text=comment,
Pierre-Yves Chibon 73d120
                reftype="fixes",
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 35fa85
            if idx == 2:
Pierre-Yves Chibon 35fa85
                self.assertEqual(
Pierre-Yves Chibon 35fa85
                    str(link),
Pierre-Yves Chibon 73d120
                    "[Issue(3, project:test, user:pingou, title:issue 3)]",
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 7c5e53
                self.assertEqual(len(link), 1)
Pierre-Yves Chibon 73d120
                self.assertEqual(link[0].project.fullname, "test")
Pierre-Yves Chibon 35fa85
            else:
Pierre-Yves Chibon 35fa85
                self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 29b095
    def test_relates_regex(self):
Pierre-Yves Chibon 73d120
        """ Test the relates regex present in pagure.lib.link. """
Pierre-Yves Chibon 73d120
        text = "relates  to   http://localhost.localdomain/fork/pingou/test/issue/1"
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.RELATES):
Pierre-Yves Chibon 7c5e53
            if index == 1:
Pierre-Yves Chibon 604159
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
            else:
Pierre-Yves Chibon 604159
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
Pierre-Yves Chibon 73d120
        text = "relates http://localhost.localdomain/fork/pingou/test/issue/1"
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.RELATES):
Pierre-Yves Chibon 7c5e53
            if index == 1:
Pierre-Yves Chibon b940a6
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon b940a6
            else:
Pierre-Yves Chibon b940a6
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon b940a6
Pierre-Yves Chibon 73d120
        text = "This relates  to  #5"
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.RELATES):
Pierre-Yves Chibon 604159
            if index == 0:
Pierre-Yves Chibon 604159
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
            else:
Pierre-Yves Chibon 604159
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
Pierre-Yves Chibon 73d120
        text = (
Pierre-Yves Chibon 73d120
            "Could this be related to  "
Pierre-Yves Chibon 73d120
            " http://localhost.localdomain/pagure/tests2/issue/6"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.RELATES):
Pierre-Yves Chibon 7c5e53
            if index == 1:
Pierre-Yves Chibon 4ecd3d
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon 4ecd3d
            else:
Pierre-Yves Chibon 4ecd3d
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon 4ecd3d
Pierre-Yves Chibon 73d120
        text = "relates http://localhost.localdomain/SSSD/ding-libs/issue/31"
Pierre-Yves Chibon 4ecd3d
        for index, regex in enumerate(pagure.lib.link.RELATES):
Pierre-Yves Chibon 7c5e53
            if index == 1:
Pierre-Yves Chibon 604159
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
            else:
Pierre-Yves Chibon 604159
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon b3a3f6
    def test_fixes_regex(self):
Pierre-Yves Chibon 73d120
        """ Test the fixes regex present in pagure.lib.link. """
Pierre-Yves Chibon b3a3f6
Mike McLean fa0f93
        # project/issue matches
Mike McLean fa0f93
        def project_match(text, groups):
Mike McLean fa0f93
            match = None
Mike McLean fa0f93
            for regex in pagure.lib.link.FIXES:
Mike McLean fa0f93
                match = regex.match(text)
Mike McLean fa0f93
                if match:
Mike McLean fa0f93
                    break
Mike McLean fa0f93
            self.assertNotEqual(match, None)
Pierre-Yves Chibon 7c5e53
            self.assertEqual(len(match.groups()), 1)
Mike McLean fa0f93
            self.assertEqual(match.groups(), groups)
Mike McLean fa0f93
Mike McLean fa0f93
        data = [
Mike McLean fa0f93
            # [string, groups]
Mike McLean fa0f93
        ]
Mike McLean fa0f93
Pierre-Yves Chibon 7c5e53
        project_match(
Pierre-Yves Chibon 73d120
            "fixes     "
Pierre-Yves Chibon 73d120
            "http://localhost.localdomain/fork/pingou/test/issue/1",
Pierre-Yves Chibon 73d120
            ("/fork/pingou/test/issue/1",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        project_match(
Pierre-Yves Chibon 73d120
            "Could this be fixes  "
Pierre-Yves Chibon 73d120
            " http://localhost.localdomain/pagure/tests2/issue/6",
Pierre-Yves Chibon 73d120
            ("/pagure/tests2/issue/6",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        project_match(
Pierre-Yves Chibon 73d120
            "merged http://localhost.localdomain/myproject/pull-request/70",
Pierre-Yves Chibon 73d120
            ("/myproject/pull-request/70",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        project_match(
Pierre-Yves Chibon 73d120
            "Now we merge http://localhost.localdomain/myproject/pull-request/99",
Pierre-Yves Chibon 73d120
            ("/myproject/pull-request/99",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        project_match(
Pierre-Yves Chibon 73d120
            "Merges     http://localhost.localdomain/fork/pingou/test/issue/1",
Pierre-Yves Chibon 73d120
            ("/fork/pingou/test/issue/1",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        project_match(
Pierre-Yves Chibon 73d120
            "Merges: http://localhost.localdomain/fork/pingou/test/issue/12",
Pierre-Yves Chibon 73d120
            ("/fork/pingou/test/issue/12",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        project_match(
Pierre-Yves Chibon 73d120
            "Merged http://localhost.localdomain/fork/pingou/test/issue/123#",
Pierre-Yves Chibon 73d120
            ("/fork/pingou/test/issue/123",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        project_match(
Pierre-Yves Chibon 73d120
            "Merge: http://localhost.localdomain/fork/pingou/test/issue/1234#foo",
Pierre-Yves Chibon 73d120
            ("/fork/pingou/test/issue/1234",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        project_match(
Pierre-Yves Chibon 73d120
            "Merges: http://localhost.localdomain/SSSD/ding-libs/pull-request/3188",
Pierre-Yves Chibon 73d120
            ("/SSSD/ding-libs/pull-request/3188",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1a7dcb
        project_match(
Pierre-Yves Chibon 73d120
            "Fixes: http://localhost.localdomain/fedpkg/issue/220",
Pierre-Yves Chibon 73d120
            ("/fedpkg/issue/220",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1a7dcb
        project_match(
Pierre-Yves Chibon 73d120
            "resolved: http://localhost.localdomain/fork/pingou/test/issue/1234#foo",
Pierre-Yves Chibon 73d120
            ("/fork/pingou/test/issue/1234",),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        project_match(
Pierre-Yves Chibon 73d120
            "resolve http://localhost.localdomain/fork/pingou/test/issue/1234#foo",
Pierre-Yves Chibon 73d120
            ("/fork/pingou/test/issue/1234",),
Pierre-Yves Chibon 73d120
        )
Mike McLean fa0f93
Mike McLean fa0f93
        # issue matches
Mike McLean fa0f93
        def issue_match(text, issue):
Mike McLean fa0f93
            match = None
Mike McLean fa0f93
            for regex in pagure.lib.link.FIXES:
Mike McLean fa0f93
                match = regex.match(text)
Mike McLean fa0f93
                if match:
Mike McLean fa0f93
                    break
Mike McLean fa0f93
            self.assertNotEqual(match, None)
Mike McLean fa0f93
            self.assertEqual(len(match.groups()), 1)
Mike McLean fa0f93
            self.assertEqual(match.group(1), issue)
Mike McLean fa0f93
Pierre-Yves Chibon 73d120
        issue_match("This fixed  #5", "5")
Pierre-Yves Chibon 73d120
        issue_match("This fix  #5", "5")
Pierre-Yves Chibon 73d120
        issue_match("Merged  #17", "17")
Pierre-Yves Chibon 73d120
        issue_match("Fixed:  #23", "23")
Pierre-Yves Chibon 73d120
        issue_match("Fix:  #23", "23")
Pierre-Yves Chibon 73d120
        issue_match("This commit fixes:  #42", "42")
Pierre-Yves Chibon 73d120
        issue_match("This commit fix   #42", "42")
Pierre-Yves Chibon 73d120
        issue_match("Merge #137", "137")
Pierre-Yves Chibon 73d120
        issue_match("Merges #137", "137")
Pierre-Yves Chibon 73d120
        issue_match("Merges: #137", "137")
Pierre-Yves Chibon 73d120
        issue_match("resolve #137", "137")
Pierre-Yves Chibon 73d120
        issue_match("Resolves #137", "137")
Pierre-Yves Chibon 73d120
        issue_match("RESOLVED: #137", "137")
Pierre-Yves Chibon 1a7dcb
        issue_match(
Pierre-Yves Chibon 73d120
            "Fixes: http://localhost.localdomain/fedpkg/issue/220",
Pierre-Yves Chibon 73d120
            "/fedpkg/issue/220",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1a7dcb
        issue_match(
Pierre-Yves Chibon 73d120
            "Resolved: http://localhost.localdomain/fedpkg/issue/222",
Pierre-Yves Chibon 73d120
            "/fedpkg/issue/222",
Pierre-Yves Chibon 73d120
        )
Mike McLean fa0f93
Mike McLean fa0f93
        # no match
Mike McLean fa0f93
        def no_match(text):
Mike McLean fa0f93
            match = None
Mike McLean fa0f93
            for regex in pagure.lib.link.FIXES:
Mike McLean fa0f93
                match = regex.match(text)
Mike McLean fa0f93
                if match:
Mike McLean fa0f93
                    break
Mike McLean fa0f93
            self.assertEqual(match, None)
Mike McLean fa0f93
Pierre-Yves Chibon 73d120
        no_match("nowhitespacemerge: #47")
Pierre-Yves Chibon 73d120
        no_match("This commit unmerges #45")
Pierre-Yves Chibon 73d120
        no_match("Fixed 45 typos")
Pierre-Yves Chibon 73d120
        no_match("Fixed 4 typos")
Mike McLean fa0f93
        no_match("Merge branch 'work'")
Pierre-Yves Chibon b3a3f6
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)