Skip to content

Commit fb05049

Browse files
Jake AdamsJake Adams
authored andcommitted
Add TestEvaluateMarkdownFileSize and split block to separate function
1 parent 1c857c0 commit fb05049

File tree

2 files changed

+72
-14
lines changed

2 files changed

+72
-14
lines changed

issue_metrics.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ def get_per_issue_metrics(
166166

167167
return issues_with_metrics, num_issues_open, num_issues_closed
168168

169+
def evaluate_markdown_file_size(output_file: str) -> None:
170+
file_name_without_extension = Path(output_file).stem
171+
max_char_count = 65535
172+
if markdown_too_large_for_issue_body(output_file, max_char_count):
173+
split_markdown_file(output_file, max_char_count)
174+
shutil.move(output_file, f"{file_name_without_extension}_full.md")
175+
shutil.move(f"{file_name_without_extension}_0.md", output_file)
176+
print(
177+
f"Issue metrics markdown file is too large for GitHub issue body and has been \
178+
split into multiple files. ie. {output_file}, {file_name_without_extension}_1.md, etc. \
179+
The full file is saved as {file_name_without_extension}_full.md\n\
180+
See https://github.com/github/issue-metrics/blob/main/docs/dealing-with-large-issue-metrics.md"
181+
)
169182

170183
def main(): # pragma: no cover
171184
"""Run the issue-metrics script.
@@ -351,18 +364,7 @@ def main(): # pragma: no cover
351364
output_file=output_file,
352365
)
353366

354-
file_name_without_extension = Path(output_file).stem
355-
max_char_count = 65535
356-
if markdown_too_large_for_issue_body(output_file, max_char_count):
357-
split_markdown_file(output_file, max_char_count)
358-
shutil.move(output_file, f"{file_name_without_extension}_full.md")
359-
shutil.move(f"{file_name_without_extension}_0.md", output_file)
360-
print(
361-
f"Issue metrics markdown file is too large for GitHub issue body and has been \
362-
split into multiple files. ie. {output_file}, {file_name_without_extension}_1.md, etc. \
363-
The full file is saved as {file_name_without_extension}_full.md\n\
364-
See https://github.com/github/issue-metrics/blob/main/docs/dealing-with-large-issue-metrics.md"
365-
)
367+
evaluate_markdown_file_size(output_file)
366368

367369

368370
if __name__ == "__main__":

test_issue_metrics.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
TestSearchIssues: A class to test the search_issues function.
99
TestGetPerIssueMetrics: A class to test the get_per_issue_metrics function.
1010
TestGetEnvVars: A class to test the get_env_vars function.
11-
11+
TestEvaluateMarkdownFileSize: A class to test the evaluate_markdown_file_size function.
1212
"""
1313

1414
import os
1515
import unittest
1616
from datetime import datetime, timedelta
17-
from unittest.mock import MagicMock, patch
17+
from unittest.mock import MagicMock, call, patch
1818

1919
from issue_metrics import (
2020
IssueWithMetrics,
21+
evaluate_markdown_file_size,
2122
get_env_vars,
2223
get_per_issue_metrics,
2324
measure_time_to_close,
@@ -176,6 +177,7 @@ def test_get_per_issue_metrics_with_hide_envs(self):
176177
"HIDE_TIME_TO_ANSWER": "false",
177178
"HIDE_TIME_TO_CLOSE": "false",
178179
"HIDE_TIME_TO_FIRST_RESPONSE": "false",
180+
"OUTPUT_FILE": "test_issue_metrics.md"
179181
},
180182
)
181183
def test_get_per_issue_metrics_without_hide_envs(self):
@@ -478,5 +480,59 @@ def test_get_per_issue_metrics_with_discussion_with_hide_envs(self):
478480
self.assertEqual(metrics[0][1].time_to_first_response, None)
479481

480482

483+
class TestEvaluateMarkdownFileSize(unittest.TestCase):
484+
"""Test suite for the evaluate_markdown_file_size function."""
485+
486+
@patch.dict(
487+
os.environ,
488+
{
489+
"GH_TOKEN": "test_token",
490+
"SEARCH_QUERY": "is:issue is:open repo:user/repo",
491+
"OUTPUT_FILE": "test_issue_metrics.md"
492+
}
493+
)
494+
@patch("issue_metrics.markdown_too_large_for_issue_body")
495+
def test_markdown_too_large_for_issue_body_called_with_output_file(self, mock_evaluate):
496+
"""
497+
Test that the function uses the output_file.
498+
"""
499+
mock_evaluate.return_value = False
500+
evaluate_markdown_file_size("test_issue_metrics.md")
501+
502+
mock_evaluate.assert_called_with("test_issue_metrics.md", 65535)
503+
504+
@patch.dict(
505+
os.environ,
506+
{
507+
"GH_TOKEN": "test_token",
508+
"SEARCH_QUERY": "is:issue is:open repo:user/repo",
509+
"OUTPUT_FILE": "test_issue_metrics.md"
510+
}
511+
)
512+
@patch("issue_metrics.print")
513+
@patch("shutil.move")
514+
@patch("issue_metrics.split_markdown_file")
515+
@patch("issue_metrics.markdown_too_large_for_issue_body")
516+
def test_split_markdown_file_when_file_size_too_large(self, mock_evaluate, mock_split, mock_move, mock_print):
517+
"""
518+
Test that the function is called with the output_file
519+
environment variable.
520+
"""
521+
mock_evaluate.return_value = True
522+
evaluate_markdown_file_size("test_issue_metrics.md")
523+
524+
mock_split.assert_called_with("test_issue_metrics.md", 65535)
525+
mock_move.assert_has_calls([
526+
call("test_issue_metrics.md", "test_issue_metrics_full.md"),
527+
call("test_issue_metrics_0.md", "test_issue_metrics.md")
528+
])
529+
mock_print.assert_called_with(
530+
"Issue metrics markdown file is too large for GitHub issue body and has been \
531+
split into multiple files. ie. test_issue_metrics.md, test_issue_metrics_1.md, etc. \
532+
The full file is saved as test_issue_metrics_full.md\n\
533+
See https://github.com/github/issue-metrics/blob/main/docs/dealing-with-large-issue-metrics.md"
534+
)
535+
536+
481537
if __name__ == "__main__":
482538
unittest.main()

0 commit comments

Comments
 (0)