fix(tests): fixed the creation of non-linear commits creation in tests

- This addresses and makes a fix for #5706
This commit is contained in:
RhodeCode Admin 2023-11-23 09:41:19 +01:00
parent 3d6f5a067e
commit 8a480af3c1
8 changed files with 1130 additions and 830 deletions

View file

@ -342,7 +342,7 @@ class TestCreatePullRequestApi(object):
commits = [
{'message': 'initial'},
{'message': 'change'},
{'message': 'new-feature', 'parents': ['initial']},
{'message': 'new-feature', 'parents': ['initial'], 'branch': 'feature'},
]
self.commit_ids = backend.create_master_repo(commits)
self.source = backend.create_repo(heads=[source_head])

View file

@ -101,9 +101,9 @@ class TestChangelogController(TestController):
# although this is a parent of commit "b1". And branch "b" has commits
# which have a smaller index than commit "a1".
commits = [
{'message': 'a'},
{'message': 'a', 'branch': 'master'},
{'message': 'b', 'branch': 'b'},
{'message': 'a1', 'parents': ['a']},
{'message': 'a1', 'parents': ['a'], 'branch': 'master'},
{'message': 'b1', 'branch': 'b', 'parents': ['b', 'a1']},
]
backend.create_repo(commits)

View file

@ -70,11 +70,11 @@ class TestCompareView(object):
commit3 = commit_change(
fork.repo_name, filename=b'file1', content=b'D',
message='D, child of A', vcs_type=backend.alias, parent=commit0)
message='D, child of A', vcs_type=backend.alias, parent=commit0, branch='feature')
commit4 = commit_change(
fork.repo_name, filename=b'file1', content=b'E',
message='E, child of D', vcs_type=backend.alias, parent=commit3)
message='E, child of D', vcs_type=backend.alias, parent=commit3, branch='feature')
# prepare origin repository, taking just the history up to D

File diff suppressed because it is too large Load diff

View file

@ -442,7 +442,7 @@ class ScmModel(BaseModel):
raise
def commit_change(self, repo, repo_name, commit, user, author, message,
content: bytes, f_path: bytes):
content: bytes, f_path: bytes, branch: str = None):
"""
Commits changes
"""
@ -458,7 +458,7 @@ class ScmModel(BaseModel):
# TODO: handle pre-push action !
tip = imc.commit(
message=message, author=author, parents=[commit],
branch=commit.branch)
branch=branch or commit.branch)
except Exception as e:
log.error(traceback.format_exc())
raise IMCCommitError(str(e))

View file

@ -564,7 +564,7 @@ class Backend(object):
self._cleanup_repos.append(repo.repo_name)
commits = commits or [
{'message': 'Commit %s of %s' % (x, self.repo_name)}
{'message': f'Commit {x} of {self.repo_name}'}
for x in range(number_of_commits)]
vcs_repo = repo.scm_instance()
vcs_repo.count()
@ -574,13 +574,15 @@ class Backend(object):
return repo
def pull_heads(self, repo, heads):
def pull_heads(self, repo, heads, do_fetch=False):
"""
Make sure that repo contains all commits mentioned in `heads`
"""
vcsrepo = repo.scm_instance()
vcsrepo.config.clear_section('hooks')
commit_ids = [self._commit_ids[h] for h in heads]
if do_fetch:
vcsrepo.fetch(self._master_repo_path, commit_ids=commit_ids)
vcsrepo.pull(self._master_repo_path, commit_ids=commit_ids)
def create_fork(self):
@ -596,10 +598,10 @@ class Backend(object):
return self.repo_name
def _next_repo_name(self):
return u"%s_%s" % (
return "%s_%s" % (
self.invalid_repo_name.sub('_', self._test_name), len(self._cleanup_repos))
def ensure_file(self, filename, content='Test content\n'):
def ensure_file(self, filename, content=b'Test content\n'):
assert self._cleanup_repos, "Avoid writing into vcs_test repos"
commits = [
{'added': [
@ -628,15 +630,14 @@ class Backend(object):
if self.alias == 'git':
refs = {}
for message in self._commit_ids:
# TODO: mikhail: do more special chars replacements
ref_name = 'refs/test-refs/{}'.format(
message.replace(' ', ''))
cleanup_message = message.replace(' ', '')
ref_name = f'refs/test-refs/{cleanup_message}'
refs[ref_name] = self._commit_ids[message]
self._create_refs(repo, refs)
def _create_refs(self, repo, refs):
for ref_name in refs:
repo.set_refs(ref_name, refs[ref_name])
for ref_name, ref_val in refs.items():
repo.set_refs(ref_name, ref_val)
class VcsBackend(object):
@ -786,10 +787,9 @@ def _add_commits_to_repo(vcs_repo, commits):
return commit_ids
imc = vcs_repo.in_memory_commit
commit = None
for idx, commit in enumerate(commits):
message = str(commit.get('message', 'Commit %s' % idx))
message = str(commit.get('message', f'Commit {idx}'))
for node in commit.get('added', []):
imc.add(FileNode(safe_bytes(node.path), content=node.content))
@ -880,6 +880,7 @@ class PRTestUtility(object):
mergeable_patcher = None
mergeable_mock = None
notification_patcher = None
commit_ids: dict
def __init__(self, backend):
self.backend = backend
@ -888,7 +889,7 @@ class PRTestUtility(object):
self, commits=None, target_head=None, source_head=None,
revisions=None, approved=False, author=None, mergeable=False,
enable_notifications=True, name_suffix='', reviewers=None, observers=None,
title=u"Test", description=u"Description"):
title="Test", description="Description"):
self.set_mergeable(mergeable)
if not enable_notifications:
# mock notification side effect
@ -949,11 +950,11 @@ class PRTestUtility(object):
def close(self):
PullRequestModel().close_pull_request(self.pull_request, self.author)
def _default_branch_reference(self, commit_message):
reference = '%s:%s:%s' % (
'branch',
self.backend.default_branch_name,
self.commit_ids[commit_message])
def _default_branch_reference(self, commit_message, branch: str = None) -> str:
default_branch = branch or self.backend.default_branch_name
message = self.commit_ids[commit_message]
reference = f'branch:{default_branch}:{message}'
return reference
def _get_reviewers(self):
@ -968,9 +969,23 @@ class PRTestUtility(object):
]
def update_source_repository(self, head=None):
def update_source_repository(self, head=None, do_fetch=False):
heads = [head or 'c3']
self.backend.pull_heads(self.source_repository, heads=heads)
self.backend.pull_heads(self.source_repository, heads=heads, do_fetch=do_fetch)
def update_target_repository(self, head=None, do_fetch=False):
heads = [head or 'c3']
self.backend.pull_heads(self.target_repository, heads=heads, do_fetch=do_fetch)
def set_pr_target_ref(self, ref_type: str = "branch", ref_name: str = "branch", ref_commit_id: str = "") -> str:
full_ref = f"{ref_type}:{ref_name}:{ref_commit_id}"
self.pull_request.target_ref = full_ref
return full_ref
def set_pr_source_ref(self, ref_type: str = "branch", ref_name: str = "branch", ref_commit_id: str = "") -> str:
full_ref = f"{ref_type}:{ref_name}:{ref_commit_id}"
self.pull_request.source_ref = full_ref
return full_ref
def add_one_commit(self, head=None):
self.update_source_repository(head=head)
@ -1000,7 +1015,7 @@ class PRTestUtility(object):
def create_comment(self, linked_to=None):
comment = CommentsModel().create(
text=u"Test comment",
text="Test comment",
repo=self.target_repository.repo_name,
user=self.author,
pull_request=self.pull_request)
@ -1014,7 +1029,7 @@ class PRTestUtility(object):
def create_inline_comment(
self, linked_to=None, line_no='n1', file_path='file_1'):
comment = CommentsModel().create(
text=u"Test comment",
text="Test comment",
repo=self.target_repository.repo_name,
user=self.author,
line_no=line_no,

View file

@ -89,8 +89,8 @@ def test_strip_with_multiple_heads(backend_hg):
{'message': 'A'},
{'message': 'a'},
{'message': 'b'},
{'message': 'B', 'parents': ['A']},
{'message': 'a1'},
{'message': 'B', 'parents': ['A'], 'branch': 'feature'},
{'message': 'a1', 'branch': 'feature'},
]
repo = backend_hg.create_repo(commits=commits)
commit_ids = backend_hg.commit_ids

View file

@ -431,7 +431,7 @@ def repo_on_filesystem(repo_name):
def commit_change(
repo, filename: bytes, content: bytes, message, vcs_type, parent=None, newfile=False):
repo, filename: bytes, content: bytes, message, vcs_type, parent=None, branch=None, newfile=False):
from rhodecode.tests import TEST_USER_ADMIN_LOGIN
repo = Repository.get_by_repo_name(repo)
@ -459,7 +459,8 @@ def commit_change(
author=f'{TEST_USER_ADMIN_LOGIN} <admin@rhodecode.com>',
message=message,
content=content,
f_path=filename
f_path=filename,
branch=branch
)
return commit