Skip to content

Commit

Permalink
test: Expand test coverage to 100% on evergreen.py
Browse files Browse the repository at this point in the history
Signed-off-by: Zack Koppert <[email protected]>
  • Loading branch information
zkoppert committed May 26, 2024
1 parent 53b33a8 commit 7520e54
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
9 changes: 9 additions & 0 deletions evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,16 @@ def check_existing_config(repo, filename, update_existing):
"""
Check if the dependabot file already exists in the
repository and return the existing config if it does
Args:
repo (github3.repos.repo.Repository): The repository to check
filename (str): The dependabot configuration filename to check
update_existing (bool): Whether to update existing dependabot configuration files
Returns:
github3.repos.contents.Contents | None: The existing config if it exists, otherwise None
"""
existing_config = None
try:
existing_config = repo.file_contents(filename)
if existing_config.size > 0:
Expand Down
25 changes: 24 additions & 1 deletion test_dependabot_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from unittest.mock import MagicMock

import github3
from dependabot_file import build_dependabot_file
import yaml
from dependabot_file import add_existing_ecosystem_to_exempt_list, build_dependabot_file


class TestDependabotFile(unittest.TestCase):
Expand Down Expand Up @@ -261,6 +262,28 @@ def test_build_dependabot_file_with_exempt_ecosystems(self):
result = build_dependabot_file(repo, False, ["docker"], None)
self.assertEqual(result, None)

def test_add_existing_ecosystem_to_exempt_list(self):
"""Test that existing ecosystems are added to the exempt list"""
exempt_ecosystems = ["npm", "pip", "github-actions"]
existing_config = MagicMock()
existing_config.decoded = yaml.dump(
{
"updates": [
{"package-ecosystem": "npm"},
{"package-ecosystem": "pip"},
{"package-ecosystem": "bundler"},
]
}
).encode()

add_existing_ecosystem_to_exempt_list(exempt_ecosystems, existing_config)

# Check new ecosystem is added to exempt list
self.assertIn("bundler", exempt_ecosystems)
# Keep existing ecosystems in exempt list
for ecosystem in exempt_ecosystems:
self.assertIn(ecosystem, exempt_ecosystems)


if __name__ == "__main__":
unittest.main()
48 changes: 48 additions & 0 deletions test_evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import uuid
from unittest.mock import MagicMock, patch

import github3
import requests
from evergreen import (
check_existing_config,
check_pending_issues_for_duplicates,
check_pending_pulls_for_duplicates,
commit_changes,
Expand Down Expand Up @@ -639,5 +641,51 @@ def test_is_repo_created_date_and_created_after_date_is_not_a_date(self):
is_repo_created_date_before(repo_created_at, created_after_date)


class TestCheckExistingConfig(unittest.TestCase):
"""
Test cases for the check_existing_config function
"""

def test_check_existing_config_with_existing_config(self):
"""
Test the case where there is an existing configuration
"""
mock_repo = MagicMock()
filename = "dependabot.yaml"
mock_repo.file_contents.return_value.size = 5

result = check_existing_config(mock_repo, filename, True)

self.assertIsNotNone(result)

def test_check_existing_config_without_existing_config(self):
"""
Test the case where there is no existing configuration
"""
mock_repo = MagicMock()
mock_response = MagicMock()
mock_repo.file_contents.side_effect = github3.exceptions.NotFoundError(
mock_response
)

result = check_existing_config(mock_repo, "dependabot.yml", True)

self.assertIsNone(result)

def test_check_existing_config_with_existing_config_without_update_existing_set(
self,
):
"""
Test the case where there is an existing configuration but UPDATE_EXISTING is False
"""
mock_repo = MagicMock()
filename = "dependabot.yaml"
mock_repo.file_contents.return_value.size = 5

result = check_existing_config(mock_repo, filename, False)

self.assertIsNone(result)


if __name__ == "__main__":
unittest.main()

0 comments on commit 7520e54

Please sign in to comment.