Skip to content

Commit

Permalink
Raising exception when when create ref action could not complete (#1171)
Browse files Browse the repository at this point in the history
* Code changes

* Run style and unit test chagnes

Co-authored-by: Roshan-sy <roshan-sy@github.com>
  • Loading branch information
roshan-sy and Roshan-sy committed Aug 16, 2021
1 parent fad25ac commit 8cf32a4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
9 changes: 6 additions & 3 deletions azure-devops/azext_devops/dev/repos/ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ def create_ref(name, object_id, repository=None, organization=None, project=None
name=resolve_git_refs(name),
new_object_id=object_id,
old_object_id='0000000000000000000000000000000000000000')
return client.update_refs(ref_updates=[ref_update],
repository_id=repository,
project=project)[0]
response = client.update_refs(ref_updates=[ref_update],
repository_id=repository,
project=project)[0]
if response.success is False:
raise CLIError(response.custom_message)
return response


def delete_ref(name, object_id=None, repository=None, organization=None, project=None, detect=None):
Expand Down
24 changes: 23 additions & 1 deletion azure-devops/azext_devops/test/repos/test_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from mock import patch, ANY

from knack.util import CLIError

from msrest.serialization import Model
from azext_devops.devops_sdk.v5_0.git.git_client import GitClient
from azext_devops.dev.common.services import clear_connection_cache
from azext_devops.dev.repos.ref import (list_refs, create_ref, delete_ref, lock_ref, unlock_ref)
Expand All @@ -23,6 +23,11 @@
class MockRef(object):
def __init__(self, object_id):
self.object_id = object_id

class MockRefResponse(Model):
def __init__(self, success, custom_message):
self.success = success
self.custom_message = custom_message

class TestRefMethods(AuthenticatedTests):

Expand Down Expand Up @@ -67,6 +72,23 @@ def test_create_ref(self):
ref_updates=ANY,
repository_id=None)

def test_create_ref2(self):

custom_exception = "Mock exception message"
mockResponse = []
mockResponse.append(MockRefResponse(False,custom_exception))

self.mock_update_refs.return_value = mockResponse

try:
response = create_ref(name='sample_ref',
object_id='1234567890',
organization=TEST_DEVOPS_ORG_URL,
project='sample_project')
self.fail('we should have received an error')
except CLIError as ex:
self.assertEqual(str(ex), custom_exception)

def test_lock_ref(self):

response = lock_ref(name='sample_ref',
Expand Down

0 comments on commit 8cf32a4

Please sign in to comment.