##// END OF EJS Templates
comments: added rcextensions hoooks for comment editing, and renamed methods to remove odd log_ prefix which...
comments: added rcextensions hoooks for comment editing, and renamed methods to remove odd log_ prefix which isn't exactly what those functions were actually doing.

File last commit:

r4306:09801de9 default
r4445:5a5c90c0 default
Show More
test_pullrequest.py
981 lines | 39.3 KiB | text/x-python | PythonLexer
project: added all source files and assets
r1 # -*- coding: utf-8 -*-
code: update copyrights to 2020
r4306 # Copyright (C) 2010-2020 RhodeCode GmbH
project: added all source files and assets
r1 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import mock
import pytest
import textwrap
import rhodecode
from rhodecode.lib.utils2 import safe_unicode
from rhodecode.lib.vcs.backends import get_backend
Martin Bornhold
tests: Use merge reference object instead of raw commit id.
r1058 from rhodecode.lib.vcs.backends.base import (
MergeResponse, MergeFailureReason, Reference)
project: added all source files and assets
r1 from rhodecode.lib.vcs.exceptions import RepositoryError
from rhodecode.lib.vcs.nodes import FileNode
comments: renamed ChangesetCommentsModel to CommentsModel to reflect what it actually does....
r1323 from rhodecode.model.comment import CommentsModel
project: added all source files and assets
r1 from rhodecode.model.db import PullRequest, Session
from rhodecode.model.pull_request import PullRequestModel
from rhodecode.model.user import UserModel
from rhodecode.tests import TEST_USER_ADMIN_LOGIN
pytestmark = [
pytest.mark.backends("git", "hg"),
]
home: moved home and repo group views into pyramid....
r1774 @pytest.mark.usefixtures('config_stub')
class TestPullRequestModel(object):
project: added all source files and assets
r1
pytest: use consistent way of creating a fixture by using pytest.fixture()
r3946 @pytest.fixture()
project: added all source files and assets
r1 def pull_request(self, request, backend, pr_util):
"""
A pull request combined with multiples patches.
"""
BackendClass = get_backend(backend.alias)
dan
pull-requests: ensure merge response provide more details...
r3339 merge_resp = MergeResponse(
False, False, None, MergeFailureReason.UNKNOWN,
metadata={'exception': 'MockError'})
audit-logs: implemented pull request and comment events.
r1807 self.merge_patcher = mock.patch.object(
dan
pull-requests: ensure merge response provide more details...
r3339 BackendClass, 'merge', return_value=merge_resp)
project: added all source files and assets
r1 self.workspace_remove_patcher = mock.patch.object(
BackendClass, 'cleanup_merge_workspace')
self.workspace_remove_mock = self.workspace_remove_patcher.start()
self.merge_mock = self.merge_patcher.start()
self.comment_patcher = mock.patch(
'rhodecode.model.changeset_status.ChangesetStatusModel.set_status')
self.comment_patcher.start()
self.notification_patcher = mock.patch(
'rhodecode.model.notification.NotificationModel.create')
self.notification_patcher.start()
self.helper_patcher = mock.patch(
pylons: fixed code and test suite after removal of pylons.
r2358 'rhodecode.lib.helpers.route_path')
project: added all source files and assets
r1 self.helper_patcher.start()
self.hook_patcher = mock.patch.object(PullRequestModel,
events: trigger 'review_status_change' when reviewers are updated....
r3415 'trigger_pull_request_hook')
project: added all source files and assets
r1 self.hook_mock = self.hook_patcher.start()
self.invalidation_patcher = mock.patch(
'rhodecode.model.pull_request.ScmModel.mark_for_invalidation')
self.invalidation_mock = self.invalidation_patcher.start()
self.pull_request = pr_util.create_pull_request(
mergeable=True, name_suffix=u'ąć')
self.source_commit = self.pull_request.source_ref_parts.commit_id
self.target_commit = self.pull_request.target_ref_parts.commit_id
self.workspace_id = 'pr-%s' % self.pull_request.pull_request_id
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 self.repo_id = self.pull_request.target_repo.repo_id
project: added all source files and assets
r1
@request.addfinalizer
def cleanup_pull_request():
calls = [mock.call(
self.pull_request, self.pull_request.author, 'create')]
self.hook_mock.assert_has_calls(calls)
self.workspace_remove_patcher.stop()
self.merge_patcher.stop()
self.comment_patcher.stop()
self.notification_patcher.stop()
self.helper_patcher.stop()
self.hook_patcher.stop()
self.invalidation_patcher.stop()
return self.pull_request
def test_get_all(self, pull_request):
prs = PullRequestModel().get_all(pull_request.target_repo)
assert isinstance(prs, list)
assert len(prs) == 1
def test_count_all(self, pull_request):
pr_count = PullRequestModel().count_all(pull_request.target_repo)
assert pr_count == 1
def test_get_awaiting_review(self, pull_request):
prs = PullRequestModel().get_awaiting_review(pull_request.target_repo)
assert isinstance(prs, list)
assert len(prs) == 1
def test_count_awaiting_review(self, pull_request):
pr_count = PullRequestModel().count_awaiting_review(
pull_request.target_repo)
assert pr_count == 1
def test_get_awaiting_my_review(self, pull_request):
PullRequestModel().update_reviewers(
default-reviewers: introduce new voting rule logic that allows...
r2484 pull_request, [(pull_request.author, ['author'], False, [])],
audit-logs: implemented pull request and comment events.
r1807 pull_request.author)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
project: added all source files and assets
r1 prs = PullRequestModel().get_awaiting_my_review(
pull_request.target_repo, user_id=pull_request.author.user_id)
assert isinstance(prs, list)
assert len(prs) == 1
def test_count_awaiting_my_review(self, pull_request):
PullRequestModel().update_reviewers(
default-reviewers: introduce new voting rule logic that allows...
r2484 pull_request, [(pull_request.author, ['author'], False, [])],
audit-logs: implemented pull request and comment events.
r1807 pull_request.author)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
project: added all source files and assets
r1 pr_count = PullRequestModel().count_awaiting_my_review(
pull_request.target_repo, user_id=pull_request.author.user_id)
assert pr_count == 1
def test_delete_calls_cleanup_merge(self, pull_request):
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 repo_id = pull_request.target_repo.repo_id
audit-logs: implemented pull request and comment events.
r1807 PullRequestModel().delete(pull_request, pull_request.author)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
project: added all source files and assets
r1
self.workspace_remove_mock.assert_called_once_with(
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 repo_id, self.workspace_id)
project: added all source files and assets
r1
def test_close_calls_cleanup_and_hook(self, pull_request):
PullRequestModel().close_pull_request(
pull_request, pull_request.author)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 repo_id = pull_request.target_repo.repo_id
project: added all source files and assets
r1
self.workspace_remove_mock.assert_called_once_with(
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 repo_id, self.workspace_id)
project: added all source files and assets
r1 self.hook_mock.assert_called_with(
self.pull_request, self.pull_request.author, 'close')
def test_merge_status(self, pull_request):
self.merge_mock.return_value = MergeResponse(
True, False, None, MergeFailureReason.NONE)
assert pull_request._last_merge_source_rev is None
assert pull_request._last_merge_target_rev is None
db: use a wrapper on pull requests _last_merge_status to ensure this is always INT....
r1968 assert pull_request.last_merge_status is None
project: added all source files and assets
r1
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 merge_response, status, msg = PullRequestModel().merge_status(pull_request)
project: added all source files and assets
r1 assert status is True
dan
pull-requests: ensure merge response provide more details...
r3339 assert msg == 'This pull request can be automatically merged.'
pull-requests: trigger merge simulation during PR creation. Fixes #5396
r2168 self.merge_mock.assert_called_with(
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 self.repo_id, self.workspace_id,
project: added all source files and assets
r1 pull_request.target_ref_parts,
pull_request.source_repo.scm_instance(),
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 pull_request.source_ref_parts, dry_run=True,
pull-requests: use close action with proper --close-commit solution....
r2056 use_rebase=False, close_branch=False)
project: added all source files and assets
r1
assert pull_request._last_merge_source_rev == self.source_commit
assert pull_request._last_merge_target_rev == self.target_commit
db: use a wrapper on pull requests _last_merge_status to ensure this is always INT....
r1968 assert pull_request.last_merge_status is MergeFailureReason.NONE
project: added all source files and assets
r1
self.merge_mock.reset_mock()
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 merge_response, status, msg = PullRequestModel().merge_status(pull_request)
project: added all source files and assets
r1 assert status is True
dan
pull-requests: ensure merge response provide more details...
r3339 assert msg == 'This pull request can be automatically merged.'
project: added all source files and assets
r1 assert self.merge_mock.called is False
def test_merge_status_known_failure(self, pull_request):
self.merge_mock.return_value = MergeResponse(
pull-requests: expose unresolved files in merge response.
r4080 False, False, None, MergeFailureReason.MERGE_FAILED,
metadata={'unresolved_files': 'file1'})
project: added all source files and assets
r1
assert pull_request._last_merge_source_rev is None
assert pull_request._last_merge_target_rev is None
db: use a wrapper on pull requests _last_merge_status to ensure this is always INT....
r1968 assert pull_request.last_merge_status is None
project: added all source files and assets
r1
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 merge_response, status, msg = PullRequestModel().merge_status(pull_request)
project: added all source files and assets
r1 assert status is False
pull-requests: expose unresolved files in merge response.
r4080 assert msg == 'This pull request cannot be merged because of merge conflicts. file1'
pull-requests: trigger merge simulation during PR creation. Fixes #5396
r2168 self.merge_mock.assert_called_with(
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 self.repo_id, self.workspace_id,
project: added all source files and assets
r1 pull_request.target_ref_parts,
pull_request.source_repo.scm_instance(),
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 pull_request.source_ref_parts, dry_run=True,
pull-requests: use close action with proper --close-commit solution....
r2056 use_rebase=False, close_branch=False)
project: added all source files and assets
r1
assert pull_request._last_merge_source_rev == self.source_commit
assert pull_request._last_merge_target_rev == self.target_commit
pull-requests: expose unresolved files in merge response.
r4080 assert pull_request.last_merge_status is MergeFailureReason.MERGE_FAILED
project: added all source files and assets
r1
self.merge_mock.reset_mock()
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 merge_response, status, msg = PullRequestModel().merge_status(pull_request)
project: added all source files and assets
r1 assert status is False
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 assert msg == 'This pull request cannot be merged because of merge conflicts. file1'
project: added all source files and assets
r1 assert self.merge_mock.called is False
def test_merge_status_unknown_failure(self, pull_request):
self.merge_mock.return_value = MergeResponse(
dan
pull-requests: ensure merge response provide more details...
r3339 False, False, None, MergeFailureReason.UNKNOWN,
metadata={'exception': 'MockError'})
project: added all source files and assets
r1
assert pull_request._last_merge_source_rev is None
assert pull_request._last_merge_target_rev is None
db: use a wrapper on pull requests _last_merge_status to ensure this is always INT....
r1968 assert pull_request.last_merge_status is None
project: added all source files and assets
r1
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 merge_response, status, msg = PullRequestModel().merge_status(pull_request)
project: added all source files and assets
r1 assert status is False
dan
pull-requests: ensure merge response provide more details...
r3339 assert msg == (
'This pull request cannot be merged because of an unhandled exception. '
'MockError')
pull-requests: trigger merge simulation during PR creation. Fixes #5396
r2168 self.merge_mock.assert_called_with(
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 self.repo_id, self.workspace_id,
project: added all source files and assets
r1 pull_request.target_ref_parts,
pull_request.source_repo.scm_instance(),
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 pull_request.source_ref_parts, dry_run=True,
pull-requests: use close action with proper --close-commit solution....
r2056 use_rebase=False, close_branch=False)
project: added all source files and assets
r1
assert pull_request._last_merge_source_rev is None
assert pull_request._last_merge_target_rev is None
db: use a wrapper on pull requests _last_merge_status to ensure this is always INT....
r1968 assert pull_request.last_merge_status is None
project: added all source files and assets
r1
self.merge_mock.reset_mock()
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 merge_response, status, msg = PullRequestModel().merge_status(pull_request)
project: added all source files and assets
r1 assert status is False
dan
pull-requests: ensure merge response provide more details...
r3339 assert msg == (
'This pull request cannot be merged because of an unhandled exception. '
'MockError')
project: added all source files and assets
r1 assert self.merge_mock.called is True
def test_merge_status_when_target_is_locked(self, pull_request):
pull_request.target_repo.locked = [1, u'12345.50', 'lock_web']
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 merge_response, status, msg = PullRequestModel().merge_status(pull_request)
project: added all source files and assets
r1 assert status is False
dan
pull-requests: ensure merge response provide more details...
r3339 assert msg == (
'This pull request cannot be merged because the target repository '
'is locked by user:1.')
project: added all source files and assets
r1
def test_merge_status_requirements_check_target(self, pull_request):
def has_largefiles(self, repo):
return repo == pull_request.source_repo
pull-requests: introduce operation state for pull requests to prevent from...
r3371 patcher = mock.patch.object(PullRequestModel, '_has_largefiles', has_largefiles)
project: added all source files and assets
r1 with patcher:
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 merge_response, status, msg = PullRequestModel().merge_status(pull_request)
project: added all source files and assets
r1
assert status is False
assert msg == 'Target repository large files support is disabled.'
def test_merge_status_requirements_check_source(self, pull_request):
def has_largefiles(self, repo):
return repo == pull_request.target_repo
pull-requests: introduce operation state for pull requests to prevent from...
r3371 patcher = mock.patch.object(PullRequestModel, '_has_largefiles', has_largefiles)
project: added all source files and assets
r1 with patcher:
pull-requests: fixed case for GIT repositories when a merge check failed due to merge conflicts the pull request wrongly reported missing commits....
r4299 merge_response, status, msg = PullRequestModel().merge_status(pull_request)
project: added all source files and assets
r1
assert status is False
assert msg == 'Source repository large files support is disabled.'
def test_merge(self, pull_request, merge_extras):
user = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
Martin Bornhold
tests: Use merge reference object instead of raw commit id.
r1058 merge_ref = Reference(
'type', 'name', '6126b7bfcc82ad2d3deaee22af926b082ce54cc6')
project: added all source files and assets
r1 self.merge_mock.return_value = MergeResponse(
Martin Bornhold
tests: Use merge reference object instead of raw commit id.
r1058 True, True, merge_ref, MergeFailureReason.NONE)
project: added all source files and assets
r1
dan
events: add event system for RepoEvents
r375 merge_extras['repository'] = pull_request.target_repo.repo_name
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 PullRequestModel().merge_repo(
project: added all source files and assets
r1 pull_request, pull_request.author, extras=merge_extras)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
project: added all source files and assets
r1
message = (
dan
pull-requests: change the naming from #NUM into !NUM....
r4039 u'Merge pull request !{pr_id} from {source_repo} {source_ref_name}'
project: added all source files and assets
r1 u'\n\n {pr_title}'.format(
pr_id=pull_request.pull_request_id,
source_repo=safe_unicode(
pull_request.source_repo.scm_instance().name),
source_ref_name=pull_request.source_ref_parts.name,
pr_title=safe_unicode(pull_request.title)
)
)
pull-requests: trigger merge simulation during PR creation. Fixes #5396
r2168 self.merge_mock.assert_called_with(
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 self.repo_id, self.workspace_id,
project: added all source files and assets
r1 pull_request.target_ref_parts,
pull_request.source_repo.scm_instance(),
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 pull_request.source_ref_parts,
vcs: use a real two part name for merge operation....
r3040 user_name=user.short_contact, user_email=user.email, message=message,
pull-requests: use close action with proper --close-commit solution....
r2056 use_rebase=False, close_branch=False
project: added all source files and assets
r1 )
self.invalidation_mock.assert_called_once_with(
pull_request.target_repo.repo_name)
self.hook_mock.assert_called_with(
self.pull_request, self.pull_request.author, 'merge')
pull_request = PullRequest.get(pull_request.pull_request_id)
pull-requests: introduce operation state for pull requests to prevent from...
r3371 assert pull_request.merge_rev == '6126b7bfcc82ad2d3deaee22af926b082ce54cc6'
def test_merge_with_status_lock(self, pull_request, merge_extras):
user = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
merge_ref = Reference(
'type', 'name', '6126b7bfcc82ad2d3deaee22af926b082ce54cc6')
self.merge_mock.return_value = MergeResponse(
True, True, merge_ref, MergeFailureReason.NONE)
merge_extras['repository'] = pull_request.target_repo.repo_name
with pull_request.set_state(PullRequest.STATE_UPDATING):
assert pull_request.pull_request_state == PullRequest.STATE_UPDATING
PullRequestModel().merge_repo(
pull_request, pull_request.author, extras=merge_extras)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
pull-requests: introduce operation state for pull requests to prevent from...
r3371
assert pull_request.pull_request_state == PullRequest.STATE_CREATED
message = (
dan
pull-requests: change the naming from #NUM into !NUM....
r4039 u'Merge pull request !{pr_id} from {source_repo} {source_ref_name}'
pull-requests: introduce operation state for pull requests to prevent from...
r3371 u'\n\n {pr_title}'.format(
pr_id=pull_request.pull_request_id,
source_repo=safe_unicode(
pull_request.source_repo.scm_instance().name),
source_ref_name=pull_request.source_ref_parts.name,
pr_title=safe_unicode(pull_request.title)
)
)
self.merge_mock.assert_called_with(
self.repo_id, self.workspace_id,
pull_request.target_ref_parts,
pull_request.source_repo.scm_instance(),
pull_request.source_ref_parts,
user_name=user.short_contact, user_email=user.email, message=message,
use_rebase=False, close_branch=False
)
self.invalidation_mock.assert_called_once_with(
pull_request.target_repo.repo_name)
self.hook_mock.assert_called_with(
self.pull_request, self.pull_request.author, 'merge')
pull_request = PullRequest.get(pull_request.pull_request_id)
assert pull_request.merge_rev == '6126b7bfcc82ad2d3deaee22af926b082ce54cc6'
project: added all source files and assets
r1
def test_merge_failed(self, pull_request, merge_extras):
user = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
Martin Bornhold
tests: Use merge reference object instead of raw commit id.
r1058 merge_ref = Reference(
'type', 'name', '6126b7bfcc82ad2d3deaee22af926b082ce54cc6')
project: added all source files and assets
r1 self.merge_mock.return_value = MergeResponse(
Martin Bornhold
tests: Use merge reference object instead of raw commit id.
r1058 False, False, merge_ref, MergeFailureReason.MERGE_FAILED)
project: added all source files and assets
r1
dan
events: add event system for RepoEvents
r375 merge_extras['repository'] = pull_request.target_repo.repo_name
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 PullRequestModel().merge_repo(
project: added all source files and assets
r1 pull_request, pull_request.author, extras=merge_extras)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
project: added all source files and assets
r1
message = (
dan
pull-requests: change the naming from #NUM into !NUM....
r4039 u'Merge pull request !{pr_id} from {source_repo} {source_ref_name}'
project: added all source files and assets
r1 u'\n\n {pr_title}'.format(
pr_id=pull_request.pull_request_id,
source_repo=safe_unicode(
pull_request.source_repo.scm_instance().name),
source_ref_name=pull_request.source_ref_parts.name,
pr_title=safe_unicode(pull_request.title)
)
)
pull-requests: trigger merge simulation during PR creation. Fixes #5396
r2168 self.merge_mock.assert_called_with(
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 self.repo_id, self.workspace_id,
project: added all source files and assets
r1 pull_request.target_ref_parts,
pull_request.source_repo.scm_instance(),
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 pull_request.source_ref_parts,
vcs: use a real two part name for merge operation....
r3040 user_name=user.short_contact, user_email=user.email, message=message,
pull-requests: use close action with proper --close-commit solution....
r2056 use_rebase=False, close_branch=False
project: added all source files and assets
r1 )
pull_request = PullRequest.get(pull_request.pull_request_id)
assert self.invalidation_mock.called is False
assert pull_request.merge_rev is None
def test_get_commit_ids(self, pull_request):
pull-requests: add merge check that detects WIP marker in title. This will prevent merges in such case....
r4099 # The PR has been not merged yet, so expect an exception
project: added all source files and assets
r1 with pytest.raises(ValueError):
PullRequestModel()._get_commit_ids(pull_request)
# Merge revision is in the revisions list
pull_request.merge_rev = pull_request.revisions[0]
commit_ids = PullRequestModel()._get_commit_ids(pull_request)
assert commit_ids == pull_request.revisions
# Merge revision is not in the revisions list
pull_request.merge_rev = 'f000' * 10
commit_ids = PullRequestModel()._get_commit_ids(pull_request)
assert commit_ids == pull_request.revisions + [pull_request.merge_rev]
def test_get_diff_from_pr_version(self, pull_request):
test: fixes for tests for PR versions.
r1373 source_repo = pull_request.source_repo
source_ref_id = pull_request.source_ref_parts.commit_id
target_ref_id = pull_request.target_ref_parts.commit_id
project: added all source files and assets
r1 diff = PullRequestModel()._get_diff_from_pr_or_version(
dan
diffs: introducing diff menu for whitespace toggle and context changes
r3134 source_repo, source_ref_id, target_ref_id,
hide_whitespace_changes=False, diff_context=6)
project: added all source files and assets
r1 assert 'file_1' in diff.raw
Martin Bornhold
tests: Add a test that checks return type of pr title generate function. (has to be unicode)
r843 def test_generate_title_returns_unicode(self):
title = PullRequestModel().generate_pullrequest_title(
source='source-dummy',
source_ref='source-ref-dummy',
target='target-dummy',
)
assert type(title) == unicode
pull-requests: add merge check that detects WIP marker in title. This will prevent merges in such case....
r4099 @pytest.mark.parametrize('title, has_wip', [
('hello', False),
('hello wip', False),
('hello wip: xxx', False),
('[wip] hello', True),
('[wip] hello', True),
('wip: hello', True),
('wip hello', True),
])
def test_wip_title_marker(self, pull_request, title, has_wip):
pull_request.title = title
assert pull_request.work_in_progress == has_wip
project: added all source files and assets
r1
home: moved home and repo group views into pyramid....
r1774 @pytest.mark.usefixtures('config_stub')
project: added all source files and assets
r1 class TestIntegrationMerge(object):
@pytest.mark.parametrize('extra_config', (
{'vcs.hooks.protocol': 'http', 'vcs.hooks.direct_calls': False},
))
def test_merge_triggers_push_hooks(
self, pr_util, user_admin, capture_rcextensions, merge_extras,
extra_config):
rcextensions: new builtin rcextensions....
r3133
project: added all source files and assets
r1 pull_request = pr_util.create_pull_request(
approved=True, mergeable=True)
# TODO: johbo: Needed for sqlite, try to find an automatic way for it
dan
events: add event system for RepoEvents
r375 merge_extras['repository'] = pull_request.target_repo.repo_name
project: added all source files and assets
r1 Session().commit()
with mock.patch.dict(rhodecode.CONFIG, extra_config, clear=False):
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 merge_state = PullRequestModel().merge_repo(
project: added all source files and assets
r1 pull_request, user_admin, extras=merge_extras)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
project: added all source files and assets
r1
assert merge_state.executed
rcextensions: new builtin rcextensions....
r3133 assert '_pre_push_hook' in capture_rcextensions
assert '_push_hook' in capture_rcextensions
project: added all source files and assets
r1
def test_merge_can_be_rejected_by_pre_push_hook(
self, pr_util, user_admin, capture_rcextensions, merge_extras):
pull_request = pr_util.create_pull_request(
approved=True, mergeable=True)
# TODO: johbo: Needed for sqlite, try to find an automatic way for it
dan
events: add event system for RepoEvents
r375 merge_extras['repository'] = pull_request.target_repo.repo_name
project: added all source files and assets
r1 Session().commit()
with mock.patch('rhodecode.EXTENSIONS.PRE_PUSH_HOOK') as pre_pull:
pre_pull.side_effect = RepositoryError("Disallow push!")
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 merge_status = PullRequestModel().merge_repo(
project: added all source files and assets
r1 pull_request, user_admin, extras=merge_extras)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
project: added all source files and assets
r1
assert not merge_status.executed
assert 'pre_push' not in capture_rcextensions
assert 'post_push' not in capture_rcextensions
def test_merge_fails_if_target_is_locked(
self, pr_util, user_regular, merge_extras):
pull_request = pr_util.create_pull_request(
approved=True, mergeable=True)
locked_by = [user_regular.user_id + 1, 12345.50, 'lock_web']
pull_request.target_repo.locked = locked_by
# TODO: johbo: Check if this can work based on the database, currently
# all data is pre-computed, that's why just updating the DB is not
# enough.
merge_extras['locked_by'] = locked_by
dan
events: add event system for RepoEvents
r375 merge_extras['repository'] = pull_request.target_repo.repo_name
project: added all source files and assets
r1 # TODO: johbo: Needed for sqlite, try to find an automatic way for it
Session().commit()
shadow-repos: use numeric repo id for creation of shadow repos....
r2810 merge_status = PullRequestModel().merge_repo(
project: added all source files and assets
r1 pull_request, user_regular, extras=merge_extras)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
project: added all source files and assets
r1 assert not merge_status.executed
@pytest.mark.parametrize('use_outdated, inlines_count, outdated_count', [
(False, 1, 0),
(True, 0, 1),
])
def test_outdated_comments(
home: moved home and repo group views into pyramid....
r1774 pr_util, use_outdated, inlines_count, outdated_count, config_stub):
project: added all source files and assets
r1 pull_request = pr_util.create_pull_request()
pr_util.create_inline_comment(file_path='not_in_updated_diff')
with outdated_comments_patcher(use_outdated) as outdated_comment_mock:
pr_util.add_one_commit()
assert_inline_comments(
pull_request, visible=inlines_count, outdated=outdated_count)
outdated_comment_mock.assert_called_with(pull_request)
dan
pull-requests: ensure merge response provide more details...
r3339 @pytest.mark.parametrize('mr_type, expected_msg', [
(MergeFailureReason.NONE,
'This pull request can be automatically merged.'),
(MergeFailureReason.UNKNOWN,
'This pull request cannot be merged because of an unhandled exception. CRASH'),
(MergeFailureReason.MERGE_FAILED,
pull-requests: expose unresolved files in merge response.
r4080 'This pull request cannot be merged because of merge conflicts. CONFLICT_FILE'),
dan
pull-requests: ensure merge response provide more details...
r3339 (MergeFailureReason.PUSH_FAILED,
'This pull request could not be merged because push to target:`some-repo@merge_commit` failed.'),
(MergeFailureReason.TARGET_IS_NOT_HEAD,
'This pull request cannot be merged because the target `ref_name` is not a head.'),
(MergeFailureReason.HG_SOURCE_HAS_MORE_BRANCHES,
'This pull request cannot be merged because the source contains more branches than the target.'),
(MergeFailureReason.HG_TARGET_HAS_MULTIPLE_HEADS,
pull-requests: updated metadata information for failed merges with multiple heads.
r3627 'This pull request cannot be merged because the target `ref_name` has multiple heads: `a,b,c`.'),
dan
pull-requests: ensure merge response provide more details...
r3339 (MergeFailureReason.TARGET_IS_LOCKED,
'This pull request cannot be merged because the target repository is locked by user:123.'),
(MergeFailureReason.MISSING_TARGET_REF,
'This pull request cannot be merged because the target reference `ref_name` is missing.'),
(MergeFailureReason.MISSING_SOURCE_REF,
'This pull request cannot be merged because the source reference `ref_name` is missing.'),
(MergeFailureReason.SUBREPO_MERGE_FAILED,
'This pull request cannot be merged because of conflicts related to sub repositories.'),
])
def test_merge_response_message(mr_type, expected_msg):
merge_ref = Reference('type', 'ref_name', '6126b7bfcc82ad2d3deaee22af926b082ce54cc6')
metadata = {
pull-requests: expose unresolved files in merge response.
r4080 'unresolved_files': 'CONFLICT_FILE',
dan
pull-requests: ensure merge response provide more details...
r3339 'exception': "CRASH",
'target': 'some-repo',
'merge_commit': 'merge_commit',
'target_ref': merge_ref,
'source_ref': merge_ref,
'heads': ','.join(['a', 'b', 'c']),
pull-requests: expose unresolved files in merge response.
r4080 'locked_by': 'user:123'
}
dan
pull-requests: ensure merge response provide more details...
r3339
merge_response = MergeResponse(True, True, merge_ref, mr_type, metadata=metadata)
assert merge_response.merge_status_message == expected_msg
pytest: use consistent way of creating a fixture by using pytest.fixture()
r3946 @pytest.fixture()
project: added all source files and assets
r1 def merge_extras(user_regular):
"""
Context for the vcs operation when running a merge.
"""
extras = {
'ip': '127.0.0.1',
'username': user_regular.username,
vcs-ops: store user_id inside the extras for vcs context operations....
r2411 'user_id': user_regular.user_id,
project: added all source files and assets
r1 'action': 'push',
'repository': 'fake_target_repo_name',
'scm': 'git',
'config': 'fake_config_ini_path',
hooks: pass in store_path into env for hooks.
r3094 'repo_store': '',
project: added all source files and assets
r1 'make_lock': None,
'locked_by': [None, None, None],
'server_url': 'http://test.example.com:5000',
'hooks': ['push', 'pull'],
Martin Bornhold
tests: Add missing key/value to vcs operation dummy dict.
r907 'is_shadow_repo': False,
project: added all source files and assets
r1 }
return extras
home: moved home and repo group views into pyramid....
r1774 @pytest.mark.usefixtures('config_stub')
project: added all source files and assets
r1 class TestUpdateCommentHandling(object):
@pytest.fixture(autouse=True, scope='class')
pylons: remove pylons as dependency...
r2351 def enable_outdated_comments(self, request, baseapp):
project: added all source files and assets
r1 config_patch = mock.patch.dict(
'rhodecode.CONFIG', {'rhodecode_use_outdated_comments': True})
config_patch.start()
@request.addfinalizer
def cleanup():
config_patch.stop()
def test_comment_stays_unflagged_on_unchanged_diff(self, pr_util):
commits = [
{'message': 'a'},
{'message': 'b', 'added': [FileNode('file_b', 'test_content\n')]},
{'message': 'c', 'added': [FileNode('file_c', 'test_content\n')]},
]
pull_request = pr_util.create_pull_request(
commits=commits, target_head='a', source_head='b', revisions=['b'])
pr_util.create_inline_comment(file_path='file_b')
pr_util.add_one_commit(head='c')
assert_inline_comments(pull_request, visible=1, outdated=0)
def test_comment_stays_unflagged_on_change_above(self, pr_util):
original_content = ''.join(
['line {}\n'.format(x) for x in range(1, 11)])
updated_content = 'new_line_at_top\n' + original_content
commits = [
{'message': 'a'},
{'message': 'b', 'added': [FileNode('file_b', original_content)]},
{'message': 'c', 'changed': [FileNode('file_b', updated_content)]},
]
pull_request = pr_util.create_pull_request(
commits=commits, target_head='a', source_head='b', revisions=['b'])
with outdated_comments_patcher():
comment = pr_util.create_inline_comment(
line_no=u'n8', file_path='file_b')
pr_util.add_one_commit(head='c')
assert_inline_comments(pull_request, visible=1, outdated=0)
assert comment.line_no == u'n9'
def test_comment_stays_unflagged_on_change_below(self, pr_util):
original_content = ''.join(['line {}\n'.format(x) for x in range(10)])
updated_content = original_content + 'new_line_at_end\n'
commits = [
{'message': 'a'},
{'message': 'b', 'added': [FileNode('file_b', original_content)]},
{'message': 'c', 'changed': [FileNode('file_b', updated_content)]},
]
pull_request = pr_util.create_pull_request(
commits=commits, target_head='a', source_head='b', revisions=['b'])
pr_util.create_inline_comment(file_path='file_b')
pr_util.add_one_commit(head='c')
assert_inline_comments(pull_request, visible=1, outdated=0)
@pytest.mark.parametrize('line_no', ['n4', 'o4', 'n10', 'o9'])
def test_comment_flagged_on_change_around_context(self, pr_util, line_no):
base_lines = ['line {}\n'.format(x) for x in range(1, 13)]
change_lines = list(base_lines)
change_lines.insert(6, 'line 6a added\n')
# Changes on the last line of sight
update_lines = list(change_lines)
update_lines[0] = 'line 1 changed\n'
update_lines[-1] = 'line 12 changed\n'
def file_b(lines):
return FileNode('file_b', ''.join(lines))
commits = [
{'message': 'a', 'added': [file_b(base_lines)]},
{'message': 'b', 'changed': [file_b(change_lines)]},
{'message': 'c', 'changed': [file_b(update_lines)]},
]
pull_request = pr_util.create_pull_request(
commits=commits, target_head='a', source_head='b', revisions=['b'])
pr_util.create_inline_comment(line_no=line_no, file_path='file_b')
with outdated_comments_patcher():
pr_util.add_one_commit(head='c')
assert_inline_comments(pull_request, visible=0, outdated=1)
@pytest.mark.parametrize("change, content", [
('changed', 'changed\n'),
('removed', ''),
], ids=['changed', 'removed'])
def test_comment_flagged_on_change(self, pr_util, change, content):
commits = [
{'message': 'a'},
{'message': 'b', 'added': [FileNode('file_b', 'test_content\n')]},
{'message': 'c', change: [FileNode('file_b', content)]},
]
pull_request = pr_util.create_pull_request(
commits=commits, target_head='a', source_head='b', revisions=['b'])
pr_util.create_inline_comment(file_path='file_b')
with outdated_comments_patcher():
pr_util.add_one_commit(head='c')
assert_inline_comments(pull_request, visible=0, outdated=1)
home: moved home and repo group views into pyramid....
r1774 @pytest.mark.usefixtures('config_stub')
project: added all source files and assets
r1 class TestUpdateChangedFiles(object):
def test_no_changes_on_unchanged_diff(self, pr_util):
commits = [
{'message': 'a'},
{'message': 'b',
'added': [FileNode('file_b', 'test_content b\n')]},
{'message': 'c',
'added': [FileNode('file_c', 'test_content c\n')]},
]
# open a PR from a to b, adding file_b
pull_request = pr_util.create_pull_request(
commits=commits, target_head='a', source_head='b', revisions=['b'],
name_suffix='per-file-review')
# modify PR adding new file file_c
pr_util.add_one_commit(head='c')
assert_pr_file_changes(
pull_request,
added=['file_c'],
modified=[],
removed=[])
def test_modify_and_undo_modification_diff(self, pr_util):
commits = [
{'message': 'a'},
{'message': 'b',
'added': [FileNode('file_b', 'test_content b\n')]},
{'message': 'c',
'changed': [FileNode('file_b', 'test_content b modified\n')]},
{'message': 'd',
'changed': [FileNode('file_b', 'test_content b\n')]},
]
# open a PR from a to b, adding file_b
pull_request = pr_util.create_pull_request(
commits=commits, target_head='a', source_head='b', revisions=['b'],
name_suffix='per-file-review')
# modify PR modifying file file_b
pr_util.add_one_commit(head='c')
assert_pr_file_changes(
pull_request,
added=[],
modified=['file_b'],
removed=[])
# move the head again to d, which rollbacks change,
# meaning we should indicate no changes
pr_util.add_one_commit(head='d')
assert_pr_file_changes(
pull_request,
added=[],
modified=[],
removed=[])
def test_updated_all_files_in_pr(self, pr_util):
commits = [
{'message': 'a'},
{'message': 'b', 'added': [
FileNode('file_a', 'test_content a\n'),
FileNode('file_b', 'test_content b\n'),
FileNode('file_c', 'test_content c\n')]},
{'message': 'c', 'changed': [
FileNode('file_a', 'test_content a changed\n'),
FileNode('file_b', 'test_content b changed\n'),
FileNode('file_c', 'test_content c changed\n')]},
]
# open a PR from a to b, changing 3 files
pull_request = pr_util.create_pull_request(
commits=commits, target_head='a', source_head='b', revisions=['b'],
name_suffix='per-file-review')
pr_util.add_one_commit(head='c')
assert_pr_file_changes(
pull_request,
added=[],
modified=['file_a', 'file_b', 'file_c'],
removed=[])
def test_updated_and_removed_all_files_in_pr(self, pr_util):
commits = [
{'message': 'a'},
{'message': 'b', 'added': [
FileNode('file_a', 'test_content a\n'),
FileNode('file_b', 'test_content b\n'),
FileNode('file_c', 'test_content c\n')]},
{'message': 'c', 'removed': [
FileNode('file_a', 'test_content a changed\n'),
FileNode('file_b', 'test_content b changed\n'),
FileNode('file_c', 'test_content c changed\n')]},
]
# open a PR from a to b, removing 3 files
pull_request = pr_util.create_pull_request(
commits=commits, target_head='a', source_head='b', revisions=['b'],
name_suffix='per-file-review')
pr_util.add_one_commit(head='c')
assert_pr_file_changes(
pull_request,
added=[],
modified=[],
removed=['file_a', 'file_b', 'file_c'])
home: moved home and repo group views into pyramid....
r1774 def test_update_writes_snapshot_into_pull_request_version(pr_util, config_stub):
project: added all source files and assets
r1 model = PullRequestModel()
pull_request = pr_util.create_pull_request()
pr_util.update_source_repository()
pull-requests: added update pull-requests email+notifications...
r4120 model.update_commits(pull_request, pull_request.author)
project: added all source files and assets
r1
# Expect that it has a version entry now
assert len(model.get_versions(pull_request)) == 1
home: moved home and repo group views into pyramid....
r1774 def test_update_skips_new_version_if_unchanged(pr_util, config_stub):
project: added all source files and assets
r1 pull_request = pr_util.create_pull_request()
model = PullRequestModel()
pull-requests: added update pull-requests email+notifications...
r4120 model.update_commits(pull_request, pull_request.author)
project: added all source files and assets
r1
# Expect that it still has no versions
assert len(model.get_versions(pull_request)) == 0
home: moved home and repo group views into pyramid....
r1774 def test_update_assigns_comments_to_the_new_version(pr_util, config_stub):
project: added all source files and assets
r1 model = PullRequestModel()
pull_request = pr_util.create_pull_request()
comment = pr_util.create_comment()
pr_util.update_source_repository()
pull-requests: added update pull-requests email+notifications...
r4120 model.update_commits(pull_request, pull_request.author)
project: added all source files and assets
r1
# Expect that the comment is linked to the pr version now
assert comment.pull_request_version == model.get_versions(pull_request)[0]
home: moved home and repo group views into pyramid....
r1774 def test_update_adds_a_comment_to_the_pull_request_about_the_change(pr_util, config_stub):
project: added all source files and assets
r1 model = PullRequestModel()
pull_request = pr_util.create_pull_request()
pr_util.update_source_repository()
pr_util.update_source_repository()
pull-requests: added update pull-requests email+notifications...
r4120 update_response = model.update_commits(pull_request, pull_request.author)
project: added all source files and assets
r1
pull-requests: added update pull-requests email+notifications...
r4120 commit_id = update_response.common_ancestor_id
project: added all source files and assets
r1 # Expect to find a new comment about the change
expected_message = textwrap.dedent(
"""\
pull-requests: expose version browsing of pull requests....
r1255 Pull request updated. Auto status change to |under_review|
project: added all source files and assets
r1
.. role:: added
.. role:: removed
.. parsed-literal::
Changed commits:
* :added:`1 added`
* :removed:`0 removed`
Changed files:
pull-requests: added update pull-requests email+notifications...
r4120 * `A file_2 <#a_c-{}-92ed3b5f07b4>`_
project: added all source files and assets
r1
.. |under_review| replace:: *"Under Review"*"""
pull-requests: added update pull-requests email+notifications...
r4120 ).format(commit_id[:12])
project: added all source files and assets
r1 pull_request_comments = sorted(
pull_request.comments, key=lambda c: c.modified_at)
update_comment = pull_request_comments[-1]
assert update_comment.text == expected_message
home: moved home and repo group views into pyramid....
r1774 def test_create_version_from_snapshot_updates_attributes(pr_util, config_stub):
project: added all source files and assets
r1 pull_request = pr_util.create_pull_request()
# Avoiding default values
pull_request.status = PullRequest.STATUS_CLOSED
pull_request._last_merge_source_rev = "0" * 40
pull_request._last_merge_target_rev = "1" * 40
db: use a wrapper on pull requests _last_merge_status to ensure this is always INT....
r1968 pull_request.last_merge_status = 1
project: added all source files and assets
r1 pull_request.merge_rev = "2" * 40
# Remember automatic values
created_on = pull_request.created_on
updated_on = pull_request.updated_on
# Create a new version of the pull request
version = PullRequestModel()._create_version_from_snapshot(pull_request)
# Check attributes
assert version.title == pr_util.create_parameters['title']
assert version.description == pr_util.create_parameters['description']
assert version.status == PullRequest.STATUS_CLOSED
pull-requests: when creating a new version set the created_date to now instead of...
r1191
# versions get updated created_on
assert version.created_on != created_on
project: added all source files and assets
r1 assert version.updated_on == updated_on
assert version.user_id == pull_request.user_id
assert version.revisions == pr_util.create_parameters['revisions']
assert version.source_repo == pr_util.source_repository
assert version.source_ref == pr_util.create_parameters['source_ref']
assert version.target_repo == pr_util.target_repository
assert version.target_ref == pr_util.create_parameters['target_ref']
assert version._last_merge_source_rev == pull_request._last_merge_source_rev
assert version._last_merge_target_rev == pull_request._last_merge_target_rev
db: use a wrapper on pull requests _last_merge_status to ensure this is always INT....
r1968 assert version.last_merge_status == pull_request.last_merge_status
project: added all source files and assets
r1 assert version.merge_rev == pull_request.merge_rev
assert version.pull_request == pull_request
home: moved home and repo group views into pyramid....
r1774 def test_link_comments_to_version_only_updates_unlinked_comments(pr_util, config_stub):
project: added all source files and assets
r1 version1 = pr_util.create_version_of_pull_request()
comment_linked = pr_util.create_comment(linked_to=version1)
comment_unlinked = pr_util.create_comment()
version2 = pr_util.create_version_of_pull_request()
PullRequestModel()._link_comments_to_version(version2)
tests: stabilize tests for mysql/postgres.
r3981 Session().commit()
project: added all source files and assets
r1
# Expect that only the new comment is linked to version2
assert (
comment_unlinked.pull_request_version_id ==
version2.pull_request_version_id)
assert (
comment_linked.pull_request_version_id ==
version1.pull_request_version_id)
assert (
comment_unlinked.pull_request_version_id !=
comment_linked.pull_request_version_id)
def test_calculate_commits():
test: fixes for tests for PR versions.
r1373 old_ids = [1, 2, 3]
new_ids = [1, 3, 4, 5]
change = PullRequestModel()._calculate_commit_id_changes(old_ids, new_ids)
assert change.added == [4, 5]
assert change.common == [1, 3]
assert change.removed == [2]
assert change.total == [1, 3, 4, 5]
project: added all source files and assets
r1
def assert_inline_comments(pull_request, visible=None, outdated=None):
if visible is not None:
comments: renamed ChangesetCommentsModel to CommentsModel to reflect what it actually does....
r1323 inline_comments = CommentsModel().get_inline_comments(
project: added all source files and assets
r1 pull_request.target_repo.repo_id, pull_request=pull_request)
comments: renamed ChangesetCommentsModel to CommentsModel to reflect what it actually does....
r1323 inline_cnt = CommentsModel().get_inline_comments_count(
inline-comments: added helper to properly count inline comments.
r1190 inline_comments)
assert inline_cnt == visible
project: added all source files and assets
r1 if outdated is not None:
comments: renamed ChangesetCommentsModel to CommentsModel to reflect what it actually does....
r1323 outdated_comments = CommentsModel().get_outdated_comments(
project: added all source files and assets
r1 pull_request.target_repo.repo_id, pull_request)
assert len(outdated_comments) == outdated
def assert_pr_file_changes(
pull_request, added=None, modified=None, removed=None):
pr_versions = PullRequestModel().get_versions(pull_request)
# always use first version, ie original PR to calculate changes
pull_request_version = pr_versions[0]
old_diff_data, new_diff_data = PullRequestModel()._generate_update_diffs(
pull_request, pull_request_version)
file_changes = PullRequestModel()._calculate_file_changes(
old_diff_data, new_diff_data)
assert added == file_changes.added, \
'expected added:%s vs value:%s' % (added, file_changes.added)
assert modified == file_changes.modified, \
'expected modified:%s vs value:%s' % (modified, file_changes.modified)
assert removed == file_changes.removed, \
'expected removed:%s vs value:%s' % (removed, file_changes.removed)
def outdated_comments_patcher(use_outdated=True):
return mock.patch.object(
comments: renamed ChangesetCommentsModel to CommentsModel to reflect what it actually does....
r1323 CommentsModel, 'use_outdated_comments',
project: added all source files and assets
r1 return_value=use_outdated)