|
| 1 | +import dataclasses |
1 | 2 | import datetime
|
| 3 | +import typing |
2 | 4 | import unittest
|
3 | 5 | from unittest import mock
|
4 | 6 |
|
@@ -42,45 +44,46 @@ def encode(payload: dict, _: str, algorithm: str, *args, **kwargs) -> str:
|
42 | 44 | class CheckRunTests(unittest.TestCase):
|
43 | 45 | """Tests the check_run_status utility."""
|
44 | 46 |
|
| 47 | + run_kwargs: typing.Mapping = { |
| 48 | + "name": "run_name", |
| 49 | + "head_sha": "sha", |
| 50 | + "status": "completed", |
| 51 | + "conclusion": "success", |
| 52 | + "created_at": datetime.datetime.now().strftime(github_utils.ISO_FORMAT_STRING), |
| 53 | + "artifacts_url": "url", |
| 54 | + } |
| 55 | + |
45 | 56 | def test_completed_run(self):
|
46 | 57 | """Test that an already completed run returns the correct URL."""
|
47 | 58 | final_url = "some_url_string_1234"
|
48 | 59 |
|
49 |
| - result = github_utils.check_run_status({ |
50 |
| - "status": "completed", |
51 |
| - "conclusion": "success", |
52 |
| - "created_at": datetime.datetime.now().strftime(github_utils.ISO_FORMAT_STRING), |
53 |
| - "artifacts_url": final_url, |
54 |
| - }) |
| 60 | + kwargs = dict(self.run_kwargs, artifacts_url=final_url) |
| 61 | + result = github_utils.check_run_status(github_utils.WorkflowRun(**kwargs)) |
55 | 62 | self.assertEqual(final_url, result)
|
56 | 63 |
|
57 | 64 | def test_pending_run(self):
|
58 | 65 | """Test that a pending run raises the proper exception."""
|
| 66 | + kwargs = dict(self.run_kwargs, status="pending") |
59 | 67 | with self.assertRaises(github_utils.RunPendingError):
|
60 |
| - github_utils.check_run_status({ |
61 |
| - "status": "pending", |
62 |
| - "created_at": datetime.datetime.now().strftime(github_utils.ISO_FORMAT_STRING), |
63 |
| - }) |
| 68 | + github_utils.check_run_status(github_utils.WorkflowRun(**kwargs)) |
64 | 69 |
|
65 | 70 | def test_timeout_error(self):
|
66 | 71 | """Test that a timeout is declared after a certain duration."""
|
| 72 | + kwargs = dict(self.run_kwargs, status="pending") |
67 | 73 | # Set the creation time to well before the MAX_RUN_TIME
|
68 | 74 | # to guarantee the right conclusion
|
69 |
| - created = ( |
| 75 | + kwargs["created_at"] = ( |
70 | 76 | datetime.datetime.now() - github_utils.MAX_RUN_TIME - datetime.timedelta(minutes=10)
|
71 | 77 | ).strftime(github_utils.ISO_FORMAT_STRING)
|
72 | 78 |
|
73 | 79 | with self.assertRaises(github_utils.RunTimeoutError):
|
74 |
| - github_utils.check_run_status({"status": "pending", "created_at": created}) |
| 80 | + github_utils.check_run_status(github_utils.WorkflowRun(**kwargs)) |
75 | 81 |
|
76 | 82 | def test_failed_run(self):
|
77 | 83 | """Test that a failed run raises the proper exception."""
|
| 84 | + kwargs = dict(self.run_kwargs, conclusion="failed") |
78 | 85 | with self.assertRaises(github_utils.ActionFailedError):
|
79 |
| - github_utils.check_run_status({ |
80 |
| - "status": "completed", |
81 |
| - "conclusion": "failed", |
82 |
| - "created_at": datetime.datetime.now().strftime(github_utils.ISO_FORMAT_STRING), |
83 |
| - }) |
| 86 | + github_utils.check_run_status(github_utils.WorkflowRun(**kwargs)) |
84 | 87 |
|
85 | 88 |
|
86 | 89 | def get_response_authorize(_: httpx.Client, request: httpx.Request, **__) -> httpx.Response:
|
@@ -172,11 +175,16 @@ def get_response_get_artifact(request: httpx.Request, **_) -> httpx.Response:
|
172 | 175 |
|
173 | 176 | if request.method == "GET":
|
174 | 177 | if path == "/repos/owner/repo/actions/runs":
|
| 178 | + run = github_utils.WorkflowRun( |
| 179 | + name="action_name", |
| 180 | + head_sha="action_sha", |
| 181 | + created_at=datetime.datetime.now().strftime(github_utils.ISO_FORMAT_STRING), |
| 182 | + status="completed", |
| 183 | + conclusion="success", |
| 184 | + artifacts_url="artifacts_url" |
| 185 | + ) |
175 | 186 | return httpx.Response(
|
176 |
| - 200, request=request, json={"workflow_runs": [{ |
177 |
| - "name": "action_name", |
178 |
| - "head_sha": "action_sha" |
179 |
| - }]} |
| 187 | + 200, request=request, json={"workflow_runs": [dataclasses.asdict(run)]} |
180 | 188 | )
|
181 | 189 | elif path == "/artifact_url":
|
182 | 190 | return httpx.Response(
|
|
0 commit comments