Skip to content

Commit

Permalink
Merge pull request #47 from LukaszSielski/extract_commons
Browse files Browse the repository at this point in the history
more tests
  • Loading branch information
LukaszSielski authored Sep 24, 2024
2 parents b166565 + b3ccc50 commit 5ee7f95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
9 changes: 7 additions & 2 deletions scripts/az_cli.py
Original file line number Diff line number Diff line change
@@ -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}")
raise AzCliException(f"Update process failed for {work_item_id}, error: {e.output}")

15 changes: 12 additions & 3 deletions scripts/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"}]'
Expand All @@ -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):
Expand All @@ -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))

0 comments on commit 5ee7f95

Please sign in to comment.