Skip to content

Commit

Permalink
Merge pull request #30 from github/json-testing
Browse files Browse the repository at this point in the history
Json testing
  • Loading branch information
zkoppert committed Jun 6, 2023
2 parents 8a4e34d + 76666a1 commit 6127b45
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = test*.py
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
.PHONY: test
test:
pytest -v --cov=. --cov-fail-under=80
pytest -v --cov=. --cov-config=.coveragerc --cov-fail-under=80 --cov-report term-missing

.PHONY: clean
clean:
rm -rf .pytest_cache .coverage __pycache__
11 changes: 9 additions & 2 deletions stale_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from dotenv import load_dotenv


def main():
def main(): # pragma: no cover
"""
Iterate over all repositories in the specified organization on GitHub,
calculate the number of days since each repository was last pushed to,
Expand Down Expand Up @@ -98,20 +98,25 @@ def write_to_markdown(inactive_repos, inactive_days_threshold, file=None):
with file or open("stale_repos.md", "w", encoding="utf-8") as file:
file.write("# Inactive Repositories\n\n")
file.write(
f"The following repos have not had a push event for more than {inactive_days_threshold} days:\n\n"
f"The following repos have not had a push event for more than "
f"{inactive_days_threshold} days:\n\n"
)
file.write("| Repository URL | Days Inactive |\n")
file.write("| --- | ---: |\n")
for repo_url, days_inactive in inactive_repos:
file.write(f"| {repo_url} | {days_inactive} |\n")
print("Wrote stale repos to stale_repos.md")


def output_to_json(inactive_repos):
"""Convert the list of inactive repos to a json string.
Args:
inactive_repos: A list of tuples containing the repo and days inactive.
Returns:
JSON formatted string of the list of inactive repos.
"""
# json structure is like following
# [
Expand All @@ -126,6 +131,8 @@ def output_to_json(inactive_repos):
inactive_repos_json = json.dumps(inactive_repos_json)

print(f"::set-output name=inactiveRepos::{inactive_repos_json}")
return inactive_repos_json


def auth_to_github():
"""Connect to github.com or GitHub Enterprise, depending on env variables."""
Expand Down
62 changes: 55 additions & 7 deletions test_stale_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
"""

import io
import json
import os
import unittest
from datetime import datetime, timedelta, timezone
from unittest.mock import MagicMock, call, patch

import github3.github
from stale_repos import auth_to_github, get_inactive_repos, write_to_markdown

from stale_repos import (
auth_to_github,
get_inactive_repos,
output_to_json,
write_to_markdown,
)


class AuthToGithubTestCase(unittest.TestCase):
Expand Down Expand Up @@ -198,20 +205,29 @@ def test_print_inactive_repos_with_no_inactive_repos(self):
exceed the specified threshold.
"""
github_connection = MagicMock()
mock_github = MagicMock()
mock_org = MagicMock()

# Create a mock repository object with a last push time of 30 days ago
thirty_days_ago = datetime.now(timezone.utc) - 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]
mock_repo1 = MagicMock()
mock_repo1.pushed_at = thirty_days_ago.isoformat()
mock_repo1.html_url = "https://github.com/example/repo"
mock_repo2 = MagicMock()
mock_repo2.pushed_at = None
mock_repo2.html_url = "https://github.com/example/repo2"

mock_github.organization.return_value = mock_org
mock_org.repositories.return_value = [
mock_repo1,
mock_repo2,
]

# 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:
get_inactive_repos(github_connection, inactive_days_threshold, organization)
get_inactive_repos(mock_github, inactive_days_threshold, organization)
output = mock_stdout.getvalue()

# Check that the output contains the expected repo URL and days inactive
Expand Down Expand Up @@ -262,5 +278,37 @@ def test_write_to_markdown(self):
mock_file.__enter__.return_value.assert_has_calls(expected_calls)


class OutputToJson(unittest.TestCase):
"""
Unit test case for the output_to_json() function.
"""

def test_output_to_json(self):
"""Test that output_to_json returns the expected json string.
This test creates a list of inactive repos and calls the
output_to_json function with the list. It then checks that the
function returns the expected json string.
"""
# Create a list of inactive repos
inactive_repos = [
("https://github.com/example/repo1", 31),
("https://github.com/example/repo2", 30),
("https://github.com/example/repo3", 29),
]

# Call the output_to_json function with the list of inactive repos
expected_json = json.dumps(
[
{"url": "https://github.com/example/repo1", "daysInactive": 31},
{"url": "https://github.com/example/repo2", "daysInactive": 30},
{"url": "https://github.com/example/repo3", "daysInactive": 29},
]
)
actual_json = output_to_json(inactive_repos)
assert actual_json == expected_json


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

0 comments on commit 6127b45

Please sign in to comment.