| 1 | #!/usr/bin/env python3 |
| 2 | import importlib.util |
| 3 | import json |
| 4 | from pathlib import Path |
| 5 | import subprocess |
| 6 | import unittest |
| 7 | from unittest import mock |
| 8 | |
| 9 | SPEC = importlib.util.spec_from_file_location( |
| 10 | "contributor_wall", |
| 11 | Path(__file__).resolve().with_name("contributor-wall.py"), |
| 12 | ) |
| 13 | WALL = importlib.util.module_from_spec(SPEC) |
| 14 | SPEC.loader.exec_module(WALL) |
| 15 | |
| 16 | |
| 17 | class ContributorWallTests(unittest.TestCase): |
| 18 | def test_users_exist_accepts_not_found_entries(self): |
| 19 | payload = { |
| 20 | "data": {"u0": {"login": "OctoCat", "name": "Octo"}, "u1": None}, |
| 21 | "errors": [{"type": "NOT_FOUND", "path": ["u1"]}], |
| 22 | } |
| 23 | completed = subprocess.CompletedProcess( |
| 24 | args=["gh"], returncode=1, stdout=json.dumps(payload), stderr="gh: not found" |
| 25 | ) |
| 26 | with mock.patch.object(WALL.subprocess, "run", return_value=completed): |
| 27 | found = WALL.users_exist({"octocat", "zzz-missing-handle"}) |
| 28 | self.assertEqual(found, {"octocat": {"login": "OctoCat", "name": "Octo"}}) |
| 29 | |
| 30 | def test_users_exist_fails_loud_on_rate_limit_or_empty_response(self): |
| 31 | rate_limited = subprocess.CompletedProcess( |
| 32 | args=["gh"], |
| 33 | returncode=1, |
| 34 | stdout=json.dumps({"data": None, "errors": [{"type": "RATE_LIMITED"}]}), |
| 35 | stderr="API rate limit exceeded", |
| 36 | ) |
| 37 | with mock.patch.object(WALL.subprocess, "run", return_value=rate_limited): |
| 38 | with self.assertRaises(RuntimeError): |
| 39 | WALL.users_exist({"octocat"}) |
| 40 | |
| 41 | empty = subprocess.CompletedProcess( |
| 42 | args=["gh"], returncode=1, stdout="", stderr="HTTP 502" |
| 43 | ) |
| 44 | with mock.patch.object(WALL.subprocess, "run", return_value=empty): |
| 45 | with self.assertRaises(RuntimeError): |
| 46 | WALL.users_exist({"octocat"}) |
| 47 | |
| 48 | |
| 49 | if __name__ == "__main__": |
| 50 | unittest.main() |
| 51 |