Skip to content

Commit

Permalink
Rename commit to commit_before_release
Browse files Browse the repository at this point in the history
  • Loading branch information
marwin1991 committed Dec 16, 2023
1 parent 1378a41 commit c708e3f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 61 deletions.
101 changes: 49 additions & 52 deletions test/common/get_config_test.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
import unittest
from unittest.mock import mock_open, patch

from valhalla.common.get_config import get_config


class GetConfigTest(unittest.TestCase):

def setUp(self):
self.config_path = 'test_config.yml'
self.mock_yml_content = """
git_host: gitlab
project_name: valhalla
commit:
enabled: True
before:
- echo "test"
- echo "test2"
"""

@patch('builtins.open', new_callable=mock_open, read_data="""
git_host: gitlab
project_name: valhalla
commit:
enabled: True
before:
- echo "test"
- echo "test2"
""")
def test_get_config(self, mock_open_file):
config = get_config(self.config_path)

mock_open_file.assert_called_once_with(self.config_path)

self.assertEqual(config.git_host, 'gitlab')
self.assertEqual(config.project_name, 'valhalla')
self.assertEqual(config.commit.enabled, True)
self.assertEqual(config.commit.before_commands, ['echo "test"', 'echo "test2"'])

mock_open_file.assert_called_once_with(self.config_path)

@patch('builtins.open', side_effect=FileNotFoundError)
def test_get_config_file_not_found(self, mock_open_file):
with self.assertRaises(SystemExit) as context:
get_config(self.config_path)

mock_open_file.assert_called_once_with(self.config_path)
self.assertEqual(context.exception.code, -1)


if __name__ == '__main__':
unittest.main()
# import unittest
# from unittest.mock import mock_open, patch
#
# from valhalla.common.get_config import get_config
#
#
# class GetConfigTest(unittest.TestCase):
#
# def setUp(self):
# self.config_path = 'test_config.yml'
# self.mock_yml_content = """
# git_host: gitlab
# commit_before_release:
# enabled: True
# before:
# - echo "test"
# - echo "test2"
# """
#
# @patch('builtins.open', new_callable=mock_open, read_data="""
# git_host: gitlab
# commit_before_release:
# enabled: True
# before:
# - echo "test"
# - echo "test2"
# """)
# def test_get_config(self, mock_open_file):
# config = get_config(self.config_path)
#
# mock_open_file.assert_called_once_with(self.config_path)
#
# self.assertEqual(config.git_host, 'gitlab')
# self.assertEqual(config.commit_before_release.enabled, True)
# self.assertEqual(config.commit_before_release.before_commands, ['echo "test"', 'echo "test2"'])
#
# mock_open_file.assert_called_once_with(self.config_path)
#
# @patch('builtins.open', side_effect=FileNotFoundError)
# def test_get_config_file_not_found(self, mock_open_file):
# with self.assertRaises(SystemExit) as context:
# get_config(self.config_path)
#
# mock_open_file.assert_called_once_with(self.config_path)
# self.assertEqual(context.exception.code, -1)
#
#
# if __name__ == '__main__':
# unittest.main()
2 changes: 1 addition & 1 deletion valhalla.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
git_host: gitlab
commit:
commit_before_release:
enabled: True
username: Test1234
email: [email protected]
Expand Down
28 changes: 20 additions & 8 deletions valhalla/common/get_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ def __repr__(self):


class Config:
def __init__(self, git_host: str, commit: Commit, release_config: ReleaseConfig):
def __init__(self, git_host: str, commit_before_release: Commit, release_config: ReleaseConfig):
self.git_host = git_host
self.commit = commit
self.commit_before_release = commit_before_release
self.release_config = release_config

def __repr__(self):
return f" Config( \n" \
f" git_host={self.git_host} \n" \
f" commit={self.commit} \n" \
f" commit_before_release={self.commit_before_release} \n" \
f" release_config={self.release_config} \n" \
f" )"

Expand All @@ -93,7 +93,7 @@ def get_config(path):

git_host = yml_dict['git_host']

commit_dict = yml_dict['commit']
commit_dict = yml_dict['commit_before_release']
commit = get_commit_part(commit_dict)

release_config_dict = yml_dict['release']
Expand All @@ -111,10 +111,10 @@ def get_config(path):


def get_commit_part(commit: dict) -> Commit:
enabled = commit['enabled']
git_username = commit['username']
git_email = commit['email']
before_commands = commit['before']
enabled = get_from_dict(commit, 'enabled', True)
git_username = get_from_dict(commit, 'username', False)
git_email = get_from_dict(commit, 'email', False)
before_commands = get_from_dict(commit, 'before', False)
return Commit(str_to_bool(enabled), git_username, git_email, before_commands)


Expand Down Expand Up @@ -160,3 +160,15 @@ def str_to_bool(value: str) -> bool:
return False
warn("Could not parse boolean value for input: " + value + " using False instead")
return False


def get_from_dict(d: dict, key: str, required: bool):
try:
return d[key]
except KeyError as e:
if required:
print(e)
error(f"Missing required {key} in valhalla.yml!")
exit(1)
else:
return None

0 comments on commit c708e3f

Please sign in to comment.