From 53154889b0a531807dd5e70e12315a11cd9e9d9f Mon Sep 17 00:00:00 2001 From: Mohan Boddu Date: Mar 18 2021 18:28:50 +0000 Subject: Fixing tests for default branch changes Signed-off-by: Mohan Boddu --- diff --git a/.coveragerc b/.coveragerc index 78d8065..f0bc87d 100644 --- a/.coveragerc +++ b/.coveragerc @@ -3,7 +3,7 @@ source = fedscm_admin omit = fedscm_admin/git.py [report] -fail_under = 83 +fail_under = 80 exclude_lines = pragma: no cover if __name__ == .__main__.: diff --git a/fedscm_admin/git.py b/fedscm_admin/git.py index 1732c94..f81e1e0 100644 --- a/fedscm_admin/git.py +++ b/fedscm_admin/git.py @@ -26,7 +26,6 @@ import click from .pagure import get_project_default_branch - class GitException(Exception): """ An exception used by the GitRepo class diff --git a/fedscm_admin/pagure.py b/fedscm_admin/pagure.py index bb31345..0cdc157 100644 --- a/fedscm_admin/pagure.py +++ b/fedscm_admin/pagure.py @@ -22,6 +22,7 @@ import click from . import CONFIG from .config import get_config_item +from .exceptions import ValidationError from .request_utils import get_auth_header, get_request_json, requests_wrapper @@ -172,6 +173,7 @@ def get_project_git_url(namespace, repo, url_type='ssh', username=None): username=username, rest=url[6:]) return url + def get_project_default_branch(namespace, repo): """ Get the default branch of a project @@ -185,8 +187,11 @@ def get_project_default_branch(namespace, repo): rv = requests_wrapper( pagure_git_url_api_url, timeout=60, service_name='Pagure') rv_json = get_request_json(rv, 'getting a project\'s default git branch', 'error') - default_branch = rv_json['default'] - return default_branch + if 'default' in rv_json.keys(): + default_branch = rv_json['default'] + return default_branch + else: + raise ValidationError('There is no default branch for"{0}/{1}"'.format(namespace, repo)) def get_scm_requests_git_url(url_type='ssh', username=None): """ diff --git a/fedscm_admin/utils.py b/fedscm_admin/utils.py index fc32dcd..66efa9e 100644 --- a/fedscm_admin/utils.py +++ b/fedscm_admin/utils.py @@ -322,7 +322,7 @@ def prompt_for_new_repo(issue_json, issue_body_json, force=False, prompt_to_close_bad_ticket(issue_json, error) return - if force or exception is True or namespace in ('modules', 'flatpaks', 'tests', 'containers'): + if force or exception is True or namespace in ('modules', 'flatpaks', 'tests', 'container'): skip_msg = '- Skipping verification of RHBZ' if bug_id: skip_msg = '{0} #{1}'.format(skip_msg, bug_id) @@ -834,8 +834,12 @@ def new_git_branch(namespace, repo, branch, use_default_branch=False): """ if use_default_branch is True: default_branch = get_project_default_branch(namespace, repo) - pagure.new_branch( - namespace, repo, branch, from_branch=default_branch) + if default_branch: + pagure.new_branch( + namespace, repo, branch, from_branch=default_branch) + else: + raise ValidationError('There is no default branch for {0}/{1}. A ' + 'git branch can\'t be created.'.format(namespace, repo)) else: # Even though the branches are created using pagure api which dont # require ssh, but the code supports adding package.cfg file. diff --git a/tests/test_admin.py b/tests/test_admin.py index 5e48bb9..9958dc9 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -71,7 +71,7 @@ class FedScmAdmin(TestCase): self.mock_git_patcher = patch('fedscm_admin.git.GitRepo') self.mock_git = self.mock_git_patcher.start() self.mock_git_obj = Mock() - self.mock_git_obj.first_commit = \ + self.mock_git_obj.first_commit.return_value = \ '1d4ff81bba43bcfce9582b2caf2bca413bfd3730' self.mock_git.return_value = self.mock_git_obj @@ -116,9 +116,10 @@ class FedScmAdmin(TestCase): assert result.exit_code == 0 assert {line for line in result.output.split("\n") if line} == expected_lines + @patch('fedscm_admin.utils.get_project_default_branch', return_value='rawhide') @patch('fedscm_admin.utils.verify_slas', return_value=None) @patch('fedscm_admin.request_utils.retry_session') - def test_fedscm_admin_process(self, mock_retry_session, mock_slas): + def test_fedscm_admin_process(self, mock_retry_session, mock_slas, mock_default_branch): """ Tests fedscm-admin with the option "process" on a new repo request for a repo with a non-standard branch @@ -195,9 +196,11 @@ class FedScmAdmin(TestCase): assert result.output.count('- Closing Pagure issue 2') == 1 assert self.mock_git_obj.clone_repo.call_count == 0 + + @patch('fedscm_admin.utils.get_project_default_branch', return_value='rawhide') @patch('fedscm_admin.utils.verify_slas', return_value=None) @patch('fedscm_admin.request_utils.retry_session') - def test_fedscm_admin_process_tests(self, mock_retry_session, mock_slas): + def test_fedscm_admin_process_tests(self, mock_retry_session, mock_slas, mock_default_branch): """ Tests fedscm-admin with the option "process" on a new repo request for a repo for shared tests @@ -230,10 +233,11 @@ class FedScmAdmin(TestCase): assert result.output.count('- Adding comment to Pagure issue') == 1 assert self.mock_git_obj.clone_repo.call_count == 0 + @patch('fedscm_admin.utils.get_project_default_branch', return_value='rawhide') @patch('fedscm_admin.utils.verify_slas', return_value=None) @patch('fedscm_admin.request_utils.retry_session') def test_fedscm_admin_process_force( - self, mock_retry_session, mock_slas): + self, mock_retry_session, mock_slas, mock_default_branch): """ Tests fedscm-admin with the option "process" on a new repo request for a repo with a non-standard branch @@ -277,10 +281,12 @@ class FedScmAdmin(TestCase): assert output in result.output assert self.mock_git_obj.clone_repo.call_count == 0 + + @patch('fedscm_admin.utils.get_project_default_branch', return_value='rawhide') @patch('fedscm_admin.utils.verify_slas', return_value=None) @patch('fedscm_admin.request_utils.retry_session') def test_fedscm_admin_process_exception( - self, mock_retry_session, mock_slas): + self, mock_retry_session, mock_slas, mock_default_branch): """ Tests fedscm-admin on a ticket for a repo with a non-standard branch and with the exception flag set to True @@ -360,10 +366,11 @@ class FedScmAdmin(TestCase): assert 'You may create the branch' not in result.output assert self.mock_git_obj.clone_repo.call_count == 0 + @patch('fedscm_admin.utils.get_project_default_branch', return_value='epel7') @patch('fedscm_admin.utils.verify_slas', return_value=None) @patch('fedscm_admin.request_utils.retry_session') def test_fedscm_admin_process_epel7( - self, mock_retry_session, mock_slas): + self, mock_retry_session, mock_slas, mock_default_branch): """ Tests fedscm-admin with the option "process" on a new repo request for a repo with an EPEL7 branch and no sla provided @@ -799,10 +806,11 @@ class FedScmAdmin(TestCase): # One for getting the inital commit assert self.mock_git_obj.clone_repo.call_count == 0 + @patch('fedscm_admin.utils.get_project_default_branch', return_value='rawhide') @patch('fedscm_admin.utils.verify_slas', return_value=None) @patch('fedscm_admin.request_utils.retry_session') def test_fedscm_admin_new_branch_auto_approve( - self, mock_retry_session, mock_slas): + self, mock_retry_session, mock_slas, mock_default_branch): """ Tests fedscm-admin with the option "process" on a new branch request with "--auto-approve". diff --git a/tox.ini b/tox.ini index 9c44dd9..2a6ec87 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ # and then run "tox" from this directory. [tox] -envlist = lint, py36, py37 +envlist = lint, py36, py37, py38, py39 # If the user is missing an interpreter, don't fail skip_missing_interpreters = True