Skip to content

Commit

Permalink
fix: fix tests with new summary printing functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Zack Koppert <[email protected]>
  • Loading branch information
zkoppert committed May 23, 2023
1 parent d4c79e0 commit aa29714
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
67 changes: 34 additions & 33 deletions test_stale_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"""

import io
import os
import unittest
from datetime import datetime, timedelta
from unittest.mock import Mock, patch
from unittest.mock import MagicMock, Mock, patch

import github3.github

Expand Down Expand Up @@ -145,32 +146,28 @@ def test_print_inactive_repos_with_inactive_repos(self):
threshold.
"""
github_connection = Mock()
# Create a mock GitHub connection object
github_connection = MagicMock()

# Create a mock repository object with a last push time of 30 days ago
thirty_days_ago = datetime.now() - timedelta(days=30)
mock_repo = MagicMock()
mock_repo.pushed_at = thirty_days_ago.isoformat()
mock_repo.html_url = "https://github.com/example/repo"
github_connection.repositories_by.return_value = [mock_repo]

# Call the function with a threshold of 20 days
inactive_days_threshold = 20
organization = "example"
inactive_days_threshold = 30

repo1 = Mock()
repo1.pushed_at = (datetime.now() - timedelta(days=40)).isoformat()
repo1.html_url = "https://github.com/example/repo1"
repo2 = Mock()
repo2.pushed_at = (datetime.now() - timedelta(days=20)).isoformat()
repo2.html_url = "https://github.com/example/repo2"
repo3 = Mock()
repo3.pushed_at = None
repo3.html_url = "https://github.com/example/repo3"

github_connection.repositories_by.return_value = [repo1, repo2, repo3]

expected_output = [
f"{repo1.html_url}: 40 days inactive",
]

with unittest.mock.patch("builtins.print") as mock_print: # type: ignore
with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
print_inactive_repos(
github_connection, inactive_days_threshold, organization
)
output = mock_stdout.getvalue()

mock_print.assert_called_once_with("\n".join(expected_output))
# Check that the output contains the expected repo URL and days inactive
expected_output = f"{mock_repo.html_url}: 30 days inactive\nFound 1 stale repos in {organization}\n"
self.assertEqual(expected_output, output)

def test_print_inactive_repos_with_no_inactive_repos(self):
"""Test printing no inactive repos.
Expand All @@ -180,23 +177,27 @@ def test_print_inactive_repos_with_no_inactive_repos(self):
exceed the specified threshold.
"""
github_connection = Mock()
organization = "example"
inactive_days_threshold = 30

repo1 = Mock()
repo1.pushed_at = (datetime.now() - timedelta(days=20)).isoformat()
repo2 = Mock()
repo2.pushed_at = (datetime.now() - timedelta(days=10)).isoformat()
github_connection = MagicMock()

github_connection.repositories_by.return_value = [repo1, repo2]
# Create a mock repository object with a last push time of 30 days ago
thirty_days_ago = datetime.now() - timedelta(days=30)
mock_repo = MagicMock()
mock_repo.pushed_at = thirty_days_ago.isoformat()
mock_repo.html_url = "https://github.com/example/repo"
github_connection.repositories_by.return_value = [mock_repo]

with unittest.mock.patch("builtins.print") as mock_print: # type: ignore
# Call the function with a threshold of 40 days
inactive_days_threshold = 40
organization = "example"
with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
print_inactive_repos(
github_connection, inactive_days_threshold, organization
)
output = mock_stdout.getvalue()

mock_print.assert_not_called()
# Check that the output contains the expected repo URL and days inactive
expected_output = f"Found 0 stale repos in {organization}\n"
self.assertEqual(expected_output, output)


if __name__ == "__main__":
Expand Down

0 comments on commit aa29714

Please sign in to comment.