77
88from taskbadger .cli import app
99from taskbadger .internal .models import PatchedTaskRequest , PatchedTaskRequestData , StatusEnum , TaskRequest
10- from taskbadger .internal .types import Response
10+ from taskbadger .internal .types import UNSET , Response
1111from taskbadger .sdk import Badger
1212from tests .utils import task_for_test
1313
@@ -36,10 +36,42 @@ def test_cli_long_run():
3636 def _should_update_task (last_update , update_frequency_seconds ):
3737 return True
3838
39- with mock .patch ("taskbadger.cli._should_update_task " , new = _should_update_task ):
39+ with mock .patch ("taskbadger.process._should_update " , new = _should_update_task ):
4040 _test_cli_run (["echo test; sleep 0.11" ], 0 , args = ["task_name" ], update_call_count = 3 )
4141
4242
43+ def test_cli_capture_output ():
44+ update_patch = _test_cli_run (["echo test" ], 0 , args = ["task_name" , "--capture-output" ], update_call_count = 2 )
45+
46+ body = PatchedTaskRequest (status = UNSET , data = PatchedTaskRequestData .from_dict ({"stdout" : "test\n " }))
47+ update_patch .assert_any_call (
48+ client = Badger .current .settings .client ,
49+ organization_slug = "org" ,
50+ project_slug = "project" ,
51+ id = "test_id" ,
52+ json_body = body ,
53+ )
54+
55+
56+ def test_cli_capture_output_append ():
57+ def _should_update_task (last_update , update_frequency_seconds ):
58+ return True
59+
60+ with mock .patch ("taskbadger.process._should_update" , new = _should_update_task ):
61+ update_patch = _test_cli_run (
62+ ["echo test; sleep 0.11; echo 123" ], 0 , args = ["task_name" , "--capture-output" ], update_call_count = 3
63+ )
64+
65+ body = PatchedTaskRequest (status = UNSET , data = PatchedTaskRequestData .from_dict ({"stdout" : "test\n 123\n " }))
66+ update_patch .assert_any_call (
67+ client = Badger .current .settings .client ,
68+ organization_slug = "org" ,
69+ project_slug = "project" ,
70+ id = "test_id" ,
71+ json_body = body ,
72+ )
73+
74+
4375def test_cli_run_error ():
4476 _test_cli_run (["not-a-command" ], 127 , args = ["task_name" ])
4577
@@ -63,14 +95,22 @@ def test_cli_run_webhook():
6395
6496
6597def _test_cli_run (command , return_code , args = None , action = None , update_call_count = 1 ):
98+ update_mock = mock .MagicMock ()
99+
100+ def _update (* args , ** kwargs ):
101+ update_mock (* args , ** kwargs )
102+
103+ # handle updating task data
104+ data = kwargs ["json_body" ].data
105+ return Response (HTTPStatus .OK , b"" , {}, task_for_test (data = data .additional_properties if data else None ))
106+
66107 with (
67108 mock .patch ("taskbadger.sdk.task_create.sync_detailed" ) as create ,
68- mock .patch ("taskbadger.sdk.task_partial_update.sync_detailed" ) as update ,
109+ mock .patch ("taskbadger.sdk.task_partial_update.sync_detailed" , new = _update ) as update ,
69110 ):
70111 task = task_for_test ()
71112 create .return_value = Response (HTTPStatus .OK , b"" , {}, task )
72113
73- update .return_value = Response (HTTPStatus .OK , b"" , {}, task )
74114 args = args or []
75115 result = runner .invoke (app , ["run" ] + args + ["--" ] + command , catch_exceptions = False )
76116 print (result .output )
@@ -91,7 +131,8 @@ def _test_cli_run(command, return_code, args=None, action=None, update_call_coun
91131 status = StatusEnum .ERROR , data = PatchedTaskRequestData .from_dict ({"return_code" : return_code })
92132 )
93133
94- assert update .call_count == update_call_count
95- update .assert_called_with (
134+ assert update_mock .call_count == update_call_count
135+ update_mock .assert_called_with (
96136 client = settings .client , organization_slug = "org" , project_slug = "project" , id = "test_id" , json_body = body
97137 )
138+ return update_mock
0 commit comments