From b3ccc50ae24d76e23ebedb5dc80587d5235e433e Mon Sep 17 00:00:00 2001 From: Lukasz Sielski Date: Tue, 24 Sep 2024 08:49:27 +0200 Subject: [PATCH] more tests --- scripts/az_cli.py | 9 +++++++-- scripts/test_script.py | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/scripts/az_cli.py b/scripts/az_cli.py index adb7840..9a6260f 100644 --- a/scripts/az_cli.py +++ b/scripts/az_cli.py @@ -1,10 +1,15 @@ from subprocess import check_output, CalledProcessError, STDOUT +class AzCliException(Exception): + def __init__(self, error): + super().__init__(f'Failed to execute AZ CLI command! Error: {error}') + def tag_work_items(work_items_ids: list[str], tag: str) -> None: for work_item_id in work_items_ids: print(f'Trying to tag {work_item_id} with tag {tag}') try: - check_output(f'az boards work-item update --id {work_item_id} --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags={tag}"', shell=True, stderr=STDOUT) + check_output(f'az boards work-item update --id {work_item_id} --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags={tag}"', shell=True, text=True, stderr=STDOUT) print(f'Successfully updated work item {work_item_id} with tag {tag}') except CalledProcessError as e: - print(f"Update process failed for {work_item_id}, error: {e.output}") \ No newline at end of file + raise AzCliException(f"Update process failed for {work_item_id}, error: {e.output}") + \ No newline at end of file diff --git a/scripts/test_script.py b/scripts/test_script.py index ee1fb41..1ea1c87 100644 --- a/scripts/test_script.py +++ b/scripts/test_script.py @@ -2,6 +2,7 @@ from unittest import mock import argparse from gh_cli import PrNotFoundException, GhCliException +from az_cli import AzCliException import script import subprocess @@ -16,7 +17,7 @@ def test_should_properly_tag_ado_work_item_if_single_work_item_tag_in_pr_body(se script.main() gh_cli_check_output.assert_called_once_with('gh pr list --json body --state merged --search 123', shell=True, text=True, stderr=-2) - az_cli_check_output.assert_called_once_with('az boards work-item update --id 123456 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True, stderr=-2) + az_cli_check_output.assert_called_once_with('az boards work-item update --id 123456 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True, text=True, stderr=-2) def test_should_properly_tag_ado_work_items_if_many_work_item_tag_in_pr_body(self, az_cli_check_output, gh_cli_check_output, parse_args_mock): gh_cli_check_output.return_value = '[{"body":"AB#123456 AB#654321"}]' @@ -25,8 +26,8 @@ def test_should_properly_tag_ado_work_items_if_many_work_item_tag_in_pr_body(sel gh_cli_check_output.assert_called_once_with('gh pr list --json body --state merged --search 123', shell=True, text=True, stderr=-2) az_cli_check_output.assert_has_calls([ - mock.call('az boards work-item update --id 123456 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True, stderr=-2), - mock.call('az boards work-item update --id 654321 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True, stderr=-2), + mock.call('az boards work-item update --id 123456 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True, text=True, stderr=-2), + mock.call('az boards work-item update --id 654321 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True, text=True, stderr=-2), ]) def test_should_throw_exception_if_pr__not_found(self, az_cli_check_output, gh_cli_check_output, parse_args_mock): @@ -42,3 +43,11 @@ def test_should_throw_exception_if_gh_cli_command_failed(self, az_cli_check_outp with self.assertRaises(GhCliException) as e: script.main() self.assertEquals('Failed to execute GH CLI command! Error: Error!', str(e.exception)) + + def test_should_throw_exception_if_az_cli_command_failed(self, az_cli_check_output, gh_cli_check_output, parse_args_mock): + gh_cli_check_output.return_value = '[{"body":"AB#123456 AB#654321"}]' + az_cli_check_output.side_effect = subprocess.CalledProcessError('test', 'test', 'Error!') + + with self.assertRaises(AzCliException) as e: + script.main() + self.assertEquals('Failed to execute AZ CLI command! Error: Update process failed for 123456, error: Error!', str(e.exception))