Skip to content

Commit 6127b45

Browse files
authored
Merge pull request #30 from github/json-testing
Json testing
2 parents 8a4e34d + 76666a1 commit 6127b45

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[run]
2+
omit = test*.py

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
.PHONY: test
12
test:
2-
pytest -v --cov=. --cov-fail-under=80
3+
pytest -v --cov=. --cov-config=.coveragerc --cov-fail-under=80 --cov-report term-missing
4+
5+
.PHONY: clean
6+
clean:
7+
rm -rf .pytest_cache .coverage __pycache__

stale_repos.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from dotenv import load_dotenv
1212

1313

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

110+
109111
def output_to_json(inactive_repos):
110112
"""Convert the list of inactive repos to a json string.
111113
112114
Args:
113115
inactive_repos: A list of tuples containing the repo and days inactive.
114116
117+
Returns:
118+
JSON formatted string of the list of inactive repos.
119+
115120
"""
116121
# json structure is like following
117122
# [
@@ -126,6 +131,8 @@ def output_to_json(inactive_repos):
126131
inactive_repos_json = json.dumps(inactive_repos_json)
127132

128133
print(f"::set-output name=inactiveRepos::{inactive_repos_json}")
134+
return inactive_repos_json
135+
129136

130137
def auth_to_github():
131138
"""Connect to GitHub.com or GitHub Enterprise, depending on env variables."""

test_stale_repos.py

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717
"""
1818

1919
import io
20+
import json
2021
import os
2122
import unittest
2223
from datetime import datetime, timedelta, timezone
2324
from unittest.mock import MagicMock, call, patch
2425

2526
import github3.github
26-
from stale_repos import auth_to_github, get_inactive_repos, write_to_markdown
27+
28+
from stale_repos import (
29+
auth_to_github,
30+
get_inactive_repos,
31+
output_to_json,
32+
write_to_markdown,
33+
)
2734

2835

2936
class AuthToGithubTestCase(unittest.TestCase):
@@ -198,20 +205,29 @@ def test_print_inactive_repos_with_no_inactive_repos(self):
198205
exceed the specified threshold.
199206
200207
"""
201-
github_connection = MagicMock()
208+
mock_github = MagicMock()
209+
mock_org = MagicMock()
202210

203211
# Create a mock repository object with a last push time of 30 days ago
204212
thirty_days_ago = datetime.now(timezone.utc) - timedelta(days=30)
205-
mock_repo = MagicMock()
206-
mock_repo.pushed_at = thirty_days_ago.isoformat()
207-
mock_repo.html_url = "https://github.com/example/repo"
208-
github_connection.repositories_by.return_value = [mock_repo]
213+
mock_repo1 = MagicMock()
214+
mock_repo1.pushed_at = thirty_days_ago.isoformat()
215+
mock_repo1.html_url = "https://github.com/example/repo"
216+
mock_repo2 = MagicMock()
217+
mock_repo2.pushed_at = None
218+
mock_repo2.html_url = "https://github.com/example/repo2"
219+
220+
mock_github.organization.return_value = mock_org
221+
mock_org.repositories.return_value = [
222+
mock_repo1,
223+
mock_repo2,
224+
]
209225

210226
# Call the function with a threshold of 40 days
211227
inactive_days_threshold = 40
212228
organization = "example"
213229
with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
214-
get_inactive_repos(github_connection, inactive_days_threshold, organization)
230+
get_inactive_repos(mock_github, inactive_days_threshold, organization)
215231
output = mock_stdout.getvalue()
216232

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

264280

281+
class OutputToJson(unittest.TestCase):
282+
"""
283+
Unit test case for the output_to_json() function.
284+
"""
285+
286+
def test_output_to_json(self):
287+
"""Test that output_to_json returns the expected json string.
288+
289+
This test creates a list of inactive repos and calls the
290+
output_to_json function with the list. It then checks that the
291+
function returns the expected json string.
292+
293+
"""
294+
# Create a list of inactive repos
295+
inactive_repos = [
296+
("https://github.com/example/repo1", 31),
297+
("https://github.com/example/repo2", 30),
298+
("https://github.com/example/repo3", 29),
299+
]
300+
301+
# Call the output_to_json function with the list of inactive repos
302+
expected_json = json.dumps(
303+
[
304+
{"url": "https://github.com/example/repo1", "daysInactive": 31},
305+
{"url": "https://github.com/example/repo2", "daysInactive": 30},
306+
{"url": "https://github.com/example/repo3", "daysInactive": 29},
307+
]
308+
)
309+
actual_json = output_to_json(inactive_repos)
310+
assert actual_json == expected_json
311+
312+
265313
if __name__ == "__main__":
266314
unittest.main()

0 commit comments

Comments
 (0)