|
| 1 | +import os |
| 2 | +from datetime import datetime |
| 3 | +from http import HTTPStatus |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +from typer.testing import CliRunner |
| 7 | + |
| 8 | +from taskbadger.cli import app |
| 9 | +from taskbadger.internal.models import Task, TaskRequest, StatusEnum, PatchedTaskRequest, PatchedTaskRequestData |
| 10 | +from taskbadger.internal.types import Response |
| 11 | +from taskbadger.sdk import _get_settings |
| 12 | + |
| 13 | +runner = CliRunner() |
| 14 | + |
| 15 | +@mock.patch.dict(os.environ, { |
| 16 | + "TASKBADGER_ORG": "org", |
| 17 | + "TASKBADGER_PROJECT": "project", |
| 18 | + "TASKBADGER_TOKEN": "token", |
| 19 | +}, clear=True) |
| 20 | +def test_monitor_success(): |
| 21 | + _test_monitor(["echo", "test"], 0) |
| 22 | + |
| 23 | + |
| 24 | +@mock.patch.dict(os.environ, { |
| 25 | + "TASKBADGER_ORG": "org", |
| 26 | + "TASKBADGER_PROJECT": "project", |
| 27 | + "TASKBADGER_TOKEN": "token", |
| 28 | +}, clear=True) |
| 29 | +def test_monitor_error(): |
| 30 | + _test_monitor(["not-a-command"], 127) |
| 31 | + |
| 32 | + |
| 33 | +def _test_monitor(command, return_code): |
| 34 | + with ( |
| 35 | + mock.patch("taskbadger.sdk.task_create.sync_detailed") as create, |
| 36 | + mock.patch("taskbadger.sdk.task_partial_update.sync_detailed") as update |
| 37 | + ): |
| 38 | + task = Task("task_id", "org", "project", "task_name", datetime.utcnow(), datetime.utcnow(), None) |
| 39 | + create.return_value = Response(HTTPStatus.OK, b"", {}, task) |
| 40 | + |
| 41 | + update.return_value = Response(HTTPStatus.OK, b"", {}, task) |
| 42 | + result = runner.invoke(app, ["monitor", "task_name", "--"] + command) |
| 43 | + assert result.exit_code == return_code |
| 44 | + |
| 45 | + settings = _get_settings() |
| 46 | + create.assert_called_with( |
| 47 | + client=settings.client, |
| 48 | + organization_slug="org", |
| 49 | + project_slug="project", |
| 50 | + json_body=TaskRequest(name="task_name", status=StatusEnum.PENDING) |
| 51 | + ) |
| 52 | + |
| 53 | + if return_code == 0: |
| 54 | + body = PatchedTaskRequest(status=StatusEnum.SUCCESS) |
| 55 | + else: |
| 56 | + body = PatchedTaskRequest( |
| 57 | + status=StatusEnum.ERROR, |
| 58 | + data=PatchedTaskRequestData.from_dict({'return_code': return_code}) |
| 59 | + ) |
| 60 | + |
| 61 | + update.assert_called_with( |
| 62 | + client=settings.client, |
| 63 | + organization_slug="org", |
| 64 | + project_slug="project", |
| 65 | + id="task_id", |
| 66 | + json_body=body |
| 67 | + ) |
0 commit comments