|
2 | 2 |
|
3 | 3 | import unittest
|
4 | 4 | import uuid
|
| 5 | +from io import StringIO |
5 | 6 | from unittest.mock import MagicMock, patch
|
6 | 7 |
|
7 | 8 | import github3
|
8 | 9 | from cleanowners import (
|
9 | 10 | commit_changes,
|
| 11 | + get_codeowners_file, |
10 | 12 | get_org,
|
11 | 13 | get_repos_iterator,
|
12 | 14 | get_usernames_from_codeowners,
|
| 15 | + print_stats, |
13 | 16 | )
|
14 | 17 |
|
15 | 18 |
|
@@ -182,3 +185,97 @@ def test_get_repos_iterator_with_repository_list(self, mock_github):
|
182 | 185 |
|
183 | 186 | # Assert that the function returned the expected result
|
184 | 187 | self.assertEqual(result, mock_repository_list)
|
| 188 | + |
| 189 | + |
| 190 | +class TestPrintStats(unittest.TestCase): |
| 191 | + """Test the print_stats function in cleanowners.py""" |
| 192 | + |
| 193 | + @patch("sys.stdout", new_callable=StringIO) |
| 194 | + def test_print_stats_all_counts(self, mock_stdout): |
| 195 | + """Test the print_stats function with all counts.""" |
| 196 | + print_stats(5, 10, 2, 3, 4) |
| 197 | + expected_output = ( |
| 198 | + "Found 4 users to remove\n" |
| 199 | + "Created 5 pull requests successfully\n" |
| 200 | + "Skipped 2 repositories without a CODEOWNERS file\n" |
| 201 | + "Processed 3 repositories with a CODEOWNERS file\n" |
| 202 | + "50.0% of eligible repositories had pull requests created\n" |
| 203 | + "60.0% of repositories had CODEOWNERS files\n" |
| 204 | + ) |
| 205 | + self.assertEqual(mock_stdout.getvalue(), expected_output) |
| 206 | + |
| 207 | + @patch("sys.stdout", new_callable=StringIO) |
| 208 | + def test_print_stats_no_pull_requests_needed(self, mock_stdout): |
| 209 | + """Test the print_stats function with no pull requests needed.""" |
| 210 | + print_stats(0, 0, 2, 3, 4) |
| 211 | + expected_output = ( |
| 212 | + "Found 4 users to remove\n" |
| 213 | + "Created 0 pull requests successfully\n" |
| 214 | + "Skipped 2 repositories without a CODEOWNERS file\n" |
| 215 | + "Processed 3 repositories with a CODEOWNERS file\n" |
| 216 | + "No pull requests were needed\n" |
| 217 | + "60.0% of repositories had CODEOWNERS files\n" |
| 218 | + ) |
| 219 | + self.assertEqual(mock_stdout.getvalue(), expected_output) |
| 220 | + |
| 221 | + @patch("sys.stdout", new_callable=StringIO) |
| 222 | + def test_print_stats_no_repositories_processed(self, mock_stdout): |
| 223 | + """Test the print_stats function with no repositories processed.""" |
| 224 | + print_stats(0, 0, 0, 0, 0) |
| 225 | + expected_output = ( |
| 226 | + "Found 0 users to remove\n" |
| 227 | + "Created 0 pull requests successfully\n" |
| 228 | + "Skipped 0 repositories without a CODEOWNERS file\n" |
| 229 | + "Processed 0 repositories with a CODEOWNERS file\n" |
| 230 | + "No pull requests were needed\n" |
| 231 | + "No repositories were processed\n" |
| 232 | + ) |
| 233 | + self.assertEqual(mock_stdout.getvalue(), expected_output) |
| 234 | + |
| 235 | + |
| 236 | +class TestGetCodeownersFile(unittest.TestCase): |
| 237 | + """Test the get_codeowners_file function in cleanowners.py""" |
| 238 | + |
| 239 | + def setUp(self): |
| 240 | + self.repo = MagicMock() |
| 241 | + |
| 242 | + def test_codeowners_in_github_folder(self): |
| 243 | + """Test that a CODEOWNERS file in the .github folder is considered valid.""" |
| 244 | + self.repo.file_contents.side_effect = lambda path: ( |
| 245 | + MagicMock(size=1) if path == ".github/CODEOWNERS" else None |
| 246 | + ) |
| 247 | + contents, path = get_codeowners_file(self.repo) |
| 248 | + self.assertIsNotNone(contents) |
| 249 | + self.assertEqual(path, ".github/CODEOWNERS") |
| 250 | + |
| 251 | + def test_codeowners_in_root(self): |
| 252 | + """Test that a CODEOWNERS file in the root is considered valid.""" |
| 253 | + self.repo.file_contents.side_effect = lambda path: ( |
| 254 | + MagicMock(size=1) if path == "CODEOWNERS" else None |
| 255 | + ) |
| 256 | + contents, path = get_codeowners_file(self.repo) |
| 257 | + self.assertIsNotNone(contents) |
| 258 | + self.assertEqual(path, "CODEOWNERS") |
| 259 | + |
| 260 | + def test_codeowners_in_docs_folder(self): |
| 261 | + """Test that a CODEOWNERS file in a docs folder is considered valid.""" |
| 262 | + self.repo.file_contents.side_effect = lambda path: ( |
| 263 | + MagicMock(size=1) if path == "docs/CODEOWNERS" else None |
| 264 | + ) |
| 265 | + contents, path = get_codeowners_file(self.repo) |
| 266 | + self.assertIsNotNone(contents) |
| 267 | + self.assertEqual(path, "docs/CODEOWNERS") |
| 268 | + |
| 269 | + def test_codeowners_not_found(self): |
| 270 | + """Test that a missing CODEOWNERS file is not considered valid because it doesn't exist.""" |
| 271 | + self.repo.file_contents.side_effect = lambda path: None |
| 272 | + contents, path = get_codeowners_file(self.repo) |
| 273 | + self.assertIsNone(contents) |
| 274 | + self.assertIsNone(path) |
| 275 | + |
| 276 | + def test_codeowners_empty_file(self): |
| 277 | + """Test that an empty CODEOWNERS file is not considered valid because it is empty.""" |
| 278 | + self.repo.file_contents.side_effect = lambda path: MagicMock(size=0) |
| 279 | + contents, path = get_codeowners_file(self.repo) |
| 280 | + self.assertIsNone(contents) |
| 281 | + self.assertIsNone(path) |
0 commit comments