Blame tests/test_pagure_lib_git_diff_pr.py

Pierre-Yves Chibon 839f14
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
"""
Pierre-Yves Chibon 839f14
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
 Authors:
Pierre-Yves Chibon 839f14
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
"""
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 839f14
import pkg_resources  # noqa
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
import json  # noqa
Pierre-Yves Chibon 839f14
import unittest  # noqa
Pierre-Yves Chibon 839f14
import shutil  # noqa
Pierre-Yves Chibon 839f14
import sys  # noqa
Pierre-Yves Chibon 839f14
import tempfile  # noqa
Pierre-Yves Chibon 839f14
import os  # noqa
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
import pygit2  # noqa
Pierre-Yves Chibon c2560f
from mock import patch, MagicMock  # noqa
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 839f14
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
import pagure.lib  # noqa
Pierre-Yves Chibon 839f14
import tests  # noqa
Pierre-Yves Chibon 839f14
from pagure.lib.repo import PagureRepo  # noqa
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
class PagureFlaskForkPrtests(tests.Modeltests):
Pierre-Yves Chibon 839f14
    """ Tests for flask fork controller of pagure regarding diffing PRs """
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon c2560f
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 839f14
    def setUp(self):
Pierre-Yves Chibon 839f14
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 839f14
        super(PagureFlaskForkPrtests, self).setUp()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        pagure.APP.config['GIT_FOLDER'] = os.path.join(self.path, 'repos')
Pierre-Yves Chibon 839f14
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 839f14
            self.path, 'tickets')
Pierre-Yves Chibon 839f14
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 839f14
            self.path, 'docs')
Pierre-Yves Chibon 839f14
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 839f14
            self.path, 'requests')
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon c2560f
        # Create the main project in the DB
Pierre-Yves Chibon c2560f
        item = pagure.lib.model.Project(
Pierre-Yves Chibon c2560f
            user_id=1,  # pingou
Pierre-Yves Chibon c2560f
            name='test',
Pierre-Yves Chibon c2560f
            description='test project #1',
Pierre-Yves Chibon c2560f
            hook_token='aaabbbccc',
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
        item.close_status = [
Pierre-Yves Chibon c2560f
            'Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon c2560f
        self.session.add(item)
Pierre-Yves Chibon c2560f
        self.session.commit()
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        # Create the fork
Pierre-Yves Chibon c2560f
        item = pagure.lib.model.Project(
Pierre-Yves Chibon c2560f
            user_id=1,  # pingou
Pierre-Yves Chibon c2560f
            name='test',
Pierre-Yves Chibon c2560f
            description='test project #1',
Pierre-Yves Chibon c2560f
            hook_token='aaabbbcccdd',
Pierre-Yves Chibon c2560f
            parent_id=1,
Pierre-Yves Chibon c2560f
            is_fork=True,
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
        item.close_status = [
Pierre-Yves Chibon c2560f
            'Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon c2560f
        self.session.add(item)
Pierre-Yves Chibon c2560f
        self.session.commit()
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon 839f14
        # Create two git repos, one has 6 commits, the other 4 of which only
Pierre-Yves Chibon 839f14
        # 1 isn't present in the first repo
Pierre-Yves Chibon 839f14
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon 839f14
        pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        gitrepo2 = os.path.join(
Pierre-Yves Chibon 839f14
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon 839f14
        pygit2.init_repository(gitrepo2, bare=True)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
Pierre-Yves Chibon 839f14
        repopath = os.path.join(newpath, 'test')
Pierre-Yves Chibon 839f14
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Do 3 commits to the main repo
Pierre-Yves Chibon 839f14
        for i in range(3):
Pierre-Yves Chibon 839f14
            with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 839f14
                stream.write('foo%s\n bar%s\n' % (i, i))
Pierre-Yves Chibon 839f14
            clone_repo.index.add('sources')
Pierre-Yves Chibon 839f14
            clone_repo.index.write()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
            parents = []
Pierre-Yves Chibon 839f14
            try:
Pierre-Yves Chibon 839f14
                last_commit = clone_repo.revparse_single('HEAD')
Pierre-Yves Chibon 839f14
                parents = [last_commit.oid.hex]
Pierre-Yves Chibon 839f14
            except KeyError:
Pierre-Yves Chibon 839f14
                pass
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
            # Commits the files added
Pierre-Yves Chibon 839f14
            tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 839f14
            author = pygit2.Signature(
Pierre-Yves Chibon 839f14
                'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 839f14
            committer = pygit2.Signature(
Pierre-Yves Chibon 839f14
                'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 839f14
            clone_repo.create_commit(
Pierre-Yves Chibon 839f14
                'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 839f14
                author,
Pierre-Yves Chibon 839f14
                committer,
Pierre-Yves Chibon 839f14
                'Editing the file sources for testing #%s' % i,
Pierre-Yves Chibon 839f14
                # binary string representing the tree object ID
Pierre-Yves Chibon 839f14
                tree,
Pierre-Yves Chibon 839f14
                # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 839f14
                parents
Pierre-Yves Chibon 839f14
            )
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Push to the main repo
Pierre-Yves Chibon 839f14
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 839f14
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 839f14
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Push to the fork repo
Pierre-Yves Chibon 839f14
        remote = clone_repo.create_remote('pingou_fork', gitrepo2)
Pierre-Yves Chibon 839f14
        PagureRepo.push(remote, refname)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Do another 3 commits to the main repo
Pierre-Yves Chibon 839f14
        for i in range(3, 6):
Pierre-Yves Chibon 839f14
            with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 839f14
                stream.write('foo%s\n bar%s\n' % (i, i))
Pierre-Yves Chibon 839f14
            clone_repo.index.add('sources')
Pierre-Yves Chibon 839f14
            clone_repo.index.write()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
            last_commit = clone_repo.revparse_single('HEAD')
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
            # Commits the files added
Pierre-Yves Chibon 839f14
            tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 839f14
            author = pygit2.Signature(
Pierre-Yves Chibon 839f14
                'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 839f14
            committer = pygit2.Signature(
Pierre-Yves Chibon 839f14
                'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 839f14
            clone_repo.create_commit(
Pierre-Yves Chibon 839f14
                'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 839f14
                author,
Pierre-Yves Chibon 839f14
                committer,
Pierre-Yves Chibon 839f14
                'Editing the file sources for testing #%s' % i,
Pierre-Yves Chibon 839f14
                # binary string representing the tree object ID
Pierre-Yves Chibon 839f14
                tree,
Pierre-Yves Chibon 839f14
                # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 839f14
                [last_commit.oid.hex]
Pierre-Yves Chibon 839f14
            )
Pierre-Yves Chibon 839f14
        # Push to the main repo
Pierre-Yves Chibon 839f14
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 839f14
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 839f14
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Add one commit to the fork repo
Pierre-Yves Chibon 839f14
        repopath = os.path.join(newpath, 'pingou_test')
Pierre-Yves Chibon 839f14
        clone_repo = pygit2.clone_repository(gitrepo2, repopath)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 839f14
                stream.write('foo\n bar\n')
Pierre-Yves Chibon 839f14
        clone_repo.index.add('sources')
Pierre-Yves Chibon 839f14
        clone_repo.index.write()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        last_commit = clone_repo.revparse_single('HEAD')
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Commits the files added
Pierre-Yves Chibon 839f14
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 839f14
        author = pygit2.Signature(
Pierre-Yves Chibon 839f14
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 839f14
        committer = pygit2.Signature(
Pierre-Yves Chibon 839f14
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 839f14
        clone_repo.create_commit(
Pierre-Yves Chibon 839f14
            'refs/heads/feature_foo',  # the name of the reference to update
Pierre-Yves Chibon 839f14
            author,
Pierre-Yves Chibon 839f14
            committer,
Pierre-Yves Chibon 839f14
            'New edition on side branch of the file sources for testing',
Pierre-Yves Chibon 839f14
            # binary string representing the tree object ID
Pierre-Yves Chibon 839f14
            tree,
Pierre-Yves Chibon 839f14
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 839f14
            [last_commit.oid.hex]
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Push to the fork repo
Pierre-Yves Chibon 839f14
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 839f14
        refname = 'refs/heads/feature_foo:refs/heads/feature_foo'
Pierre-Yves Chibon 839f14
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 875a08
        shutil.rmtree(newpath)
Pierre-Yves Chibon 875a08
Pierre-Yves Chibon c2560f
        # Create the PR between the two repos
Pierre-Yves Chibon c2560f
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon c2560f
        forked_repo = pagure.lib.get_project(
Pierre-Yves Chibon c2560f
            self.session, 'test', user='pingou')
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon c2560f
            session=self.session,
Pierre-Yves Chibon c2560f
            repo_from=forked_repo,
Pierre-Yves Chibon c2560f
            branch_from='feature_foo',
Pierre-Yves Chibon c2560f
            repo_to=repo,
Pierre-Yves Chibon c2560f
            branch_to='master',
Pierre-Yves Chibon c2560f
            title='test pull-request',
Pierre-Yves Chibon c2560f
            user='pingou',
Pierre-Yves Chibon c2560f
            requestfolder=None,
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon c2560f
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon 839f14
    def test_get_pr_info(self):
Pierre-Yves Chibon 839f14
        """ Test pagure.ui.fork._get_pr_info """
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon 839f14
        gitrepo2 = os.path.join(
Pierre-Yves Chibon 839f14
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 875a08
        diff, diff_commits, orig_commit = pagure.lib.git.get_diff_info(
Pierre-Yves Chibon 839f14
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon 839f14
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon 839f14
            branch_from='feature_foo',
Pierre-Yves Chibon 839f14
            branch_to='master'
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon 839f14
        self.assertEqual(len(diff_commits), 1)
Pierre-Yves Chibon 839f14
        self.assertEqual(
Pierre-Yves Chibon 839f14
            diff_commits[0].message,
Pierre-Yves Chibon 839f14
            'New edition on side branch of the file sources for testing'
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon 839f14
        self.assertEqual(
Pierre-Yves Chibon 839f14
            orig_commit.message,
Pierre-Yves Chibon 839f14
            'Editing the file sources for testing #5'
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon c2560f
    def test_get_pr_info_raises(self):
Pierre-Yves Chibon c2560f
        """ Test pagure.ui.fork._get_pr_info """
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon c2560f
        gitrepo2 = os.path.join(
Pierre-Yves Chibon c2560f
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        self.assertRaises(
Pierre-Yves Chibon c2560f
            pagure.exceptions.BranchNotFoundException,
Pierre-Yves Chibon c2560f
            pagure.lib.git.get_diff_info,
Pierre-Yves Chibon c2560f
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon c2560f
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon c2560f
            branch_from='feature',
Pierre-Yves Chibon c2560f
            branch_to='master'
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        self.assertRaises(
Pierre-Yves Chibon c2560f
            pagure.exceptions.BranchNotFoundException,
Pierre-Yves Chibon c2560f
            pagure.lib.git.get_diff_info,
Pierre-Yves Chibon c2560f
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon c2560f
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon c2560f
            branch_from='feature_foo',
Pierre-Yves Chibon c2560f
            branch_to='bar'
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
    def test_diff_pull_request(self):
Pierre-Yves Chibon c2560f
        """ Test pagure.lib.git.diff_pull_request """
Pierre-Yves Chibon c2560f
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon c2560f
        gitrepo2 = os.path.join(
Pierre-Yves Chibon c2560f
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon c2560f
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon c2560f
            self.session, requestid=1, project_id=1)
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        diff_commits, diff = pagure.lib.git.diff_pull_request(
Pierre-Yves Chibon c2560f
            self.session,
Pierre-Yves Chibon c2560f
            request=request,
Pierre-Yves Chibon c2560f
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon c2560f
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon c2560f
            requestfolder=None,
Pierre-Yves Chibon c2560f
            with_diff=True
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        self.assertEqual(len(diff_commits), 1)
Pierre-Yves Chibon c2560f
        self.assertEqual(
Pierre-Yves Chibon c2560f
            diff_commits[0].message,
Pierre-Yves Chibon c2560f
            'New edition on side branch of the file sources for testing'
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
if __name__ == '__main__':
Pierre-Yves Chibon 839f14
    unittest.main(verbosity=2)