diff --git a/Makefile b/Makefile index 9244677..23e9bde 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: test test: - uv run python -m pytest -v --cov=. --cov-config=.coveragerc --cov-fail-under=80 --cov-report term-missing + uv run python -m pytest -v --cov=. --cov-config=.coveragerc --cov-fail-under=100 --cov-report term-missing .PHONY: clean clean: diff --git a/test_env_get_bool.py b/test_env_get_bool.py index 3165de1..0e86cbf 100644 --- a/test_env_get_bool.py +++ b/test_env_get_bool.py @@ -1,10 +1,10 @@ -"""Test the get_bool_env_var function""" +"""Test the get_bool_env_var and get_int_env_var functions""" import os import unittest from unittest.mock import patch -from env import get_bool_env_var +from env import get_bool_env_var, get_int_env_var class TestEnv(unittest.TestCase): @@ -77,5 +77,21 @@ def test_get_bool_env_var_that_does_not_exist_and_default_value_returns_false(se self.assertFalse(result) +class TestGetIntEnvVar(unittest.TestCase): + """Test the get_int_env_var function""" + + @patch.dict( + os.environ, + { + "TEST_INT": "not_a_number", + }, + clear=True, + ) + def test_get_int_env_var_with_non_integer_value(self): + """Test that get_int_env_var returns None for non-integer values""" + result = get_int_env_var("TEST_INT") + self.assertIsNone(result) + + if __name__ == "__main__": unittest.main() diff --git a/test_evergreen.py b/test_evergreen.py index 90ce263..de5e5af 100644 --- a/test_evergreen.py +++ b/test_evergreen.py @@ -1,3 +1,4 @@ +# pylint: disable=too-many-lines """Test the evergreen.py module.""" import unittest @@ -253,6 +254,50 @@ def test_commit_changes(self, mock_uuid): # Assert that the function returned the expected result self.assertEqual(result, "MockPullRequest") + @patch("uuid.uuid4") + def test_commit_changes_with_existing_config(self, mock_uuid): + """Test the commit_changes function when updating an existing config.""" + mock_uuid.return_value = uuid.UUID("12345678123456781234567812345678") + mock_repo = MagicMock() + mock_repo.default_branch = "main" + mock_repo.ref.return_value.object.sha = "abc123" + mock_repo.create_ref.return_value = True + mock_repo.create_pull.return_value = "MockPullRequest" + dependabot_file_name = ".github/dependabot.yml" + + title = "Test Title" + body = "Test Body" + dependabot_file = 'dependencies:\n - package_manager: "python"\n directory: "/"\n update_schedule: "live"' + branch_name = "dependabot-12345678-1234-5678-1234-567812345678" + commit_message = "Update " + dependabot_file_name + existing_config = b"existing content" + + result = commit_changes( + title, + body, + mock_repo, + dependabot_file, + commit_message, + dependabot_file_name, + existing_config, + ) + + # Assert that file_contents().update was called instead of create_file + mock_repo.file_contents.assert_called_once_with(dependabot_file_name) + mock_repo.file_contents.return_value.update.assert_called_once_with( + message=commit_message, + content=dependabot_file.encode(), + branch=branch_name, + ) + mock_repo.create_file.assert_not_called() + mock_repo.create_pull.assert_called_once_with( + title=title, + body=body, + head=branch_name, + base="main", + ) + self.assertEqual(result, "MockPullRequest") + class TestCheckPendingPullsForDuplicates(unittest.TestCase): """Test the check_pending_pulls_for_duplicates function.""" @@ -403,6 +448,30 @@ def test_get_repos_iterator_with_team(self, mock_github): # Assert that the function returned the expected result self.assertEqual(result, mock_team_repositories) + @patch("github3.login") + def test_get_repos_iterator_with_team_no_repos(self, mock_github): + """Test the get_repos_iterator function with a team that has no repositories""" + organization = "my_organization" + repository_list = [] + team_name = "empty_team" + search_query = "" + github_connection = mock_github.return_value + + github_connection.organization.return_value.team_by_name.return_value.repos_count = ( + 0 + ) + + with self.assertRaises(SystemExit) as context: + get_repos_iterator( + organization, + team_name, + repository_list, + search_query, + github_connection, + ) + + self.assertEqual(context.exception.code, 1) + @patch("github3.login") def test_get_repos_iterator_with_search_query(self, mock_github): """Test the get_repos_iterator function with a search query"""