Skip to content

Commit 36956f4

Browse files
committed
test monitor command
1 parent e0564f0 commit 36956f4

2 files changed

Lines changed: 75 additions & 2 deletions

File tree

taskbadger/cli.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ def _configure_api(ctx):
2424
def monitor(ctx: typer.Context, name: str):
2525
_configure_api(ctx)
2626
task = tb.Task.create(name)
27-
result = subprocess.run(ctx.args, env={"TASKBADGER_TASK_ID": task.id})
28-
if result.returncode != 0:
27+
try:
28+
result = subprocess.run(ctx.args, env={"TASKBADGER_TASK_ID": task.id}, shell=True)
29+
except Exception as e:
30+
task.error(data={"exception": str(e)})
31+
raise typer.Exit(1)
32+
33+
if result.returncode == 0:
2934
task.success()
3035
else:
3136
task.error(data={"return_code": result.returncode})
37+
raise typer.Exit(result.returncode)
3238

3339

3440
@app.command()

tests/test_monitor.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)