Skip to content

Commit e23d9a6

Browse files
authored
[Test Proxy] Account for unexpected TypeErrors (Azure#29476)
1 parent 10a4783 commit e23d9a6

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

tools/azure-sdk-tools/devtools_testutils/aio/proxy_testcase_async.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,38 @@ async def combined_call(*args, **kwargs):
6767
AioHttpTransport.send = combined_call
6868

6969
# call the modified function
70-
# we define test_output before invoking the test so the variable is defined in case of an exception
71-
test_output = None
70+
# we define test_variables before invoking the test so the variable is defined in case of an exception
71+
test_variables = None
72+
# this tracks whether the test has been run yet; used when calling the test function with/without `variables`
73+
# running without `variables` in the `except` block leads to unnecessary exceptions in test execution output
74+
test_run = False
7275
try:
7376
try:
74-
test_output = await test_func(*args, variables=variables, **trimmed_kwargs)
75-
except TypeError:
76-
logger = logging.getLogger()
77-
logger.info(
78-
"This test can't accept variables as input. The test method should accept `**kwargs` and/or a "
79-
"`variables` parameter to make use of recorded test variables."
80-
)
81-
test_output = await test_func(*args, **trimmed_kwargs)
77+
test_variables = await test_func(*args, variables=variables, **trimmed_kwargs)
78+
test_run = True
79+
except TypeError as error:
80+
if "unexpected keyword argument" in str(error) and "variables" in str(error):
81+
logger = logging.getLogger()
82+
logger.info(
83+
"This test can't accept variables as input. The test method should accept `**kwargs` and/or a "
84+
"`variables` parameter to make use of recorded test variables."
85+
)
86+
else:
87+
raise error
88+
# if the test couldn't accept `variables`, run the test without passing them
89+
if not test_run:
90+
test_variables = await test_func(*args, **trimmed_kwargs)
91+
8292
except ResourceNotFoundError as error:
8393
error_body = ContentDecodePolicy.deserialize_from_http_generics(error.response)
8494
message = error_body.get("message") or error_body.get("Message")
8595
error_with_message = ResourceNotFoundError(message=message, response=error.response)
8696
raise error_with_message from error
97+
8798
finally:
8899
AioHttpTransport.send = original_transport_func
89-
stop_record_or_playback(test_id, recording_id, test_output)
100+
stop_record_or_playback(test_id, recording_id, test_variables)
90101

91-
return test_output
102+
return test_variables
92103

93104
return record_wrap

tools/azure-sdk-tools/devtools_testutils/proxy_testcase.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,32 @@ def combined_call(*args, **kwargs):
220220
# call the modified function
221221
# we define test_variables before invoking the test so the variable is defined in case of an exception
222222
test_variables = None
223+
# this tracks whether the test has been run yet; used when calling the test function with/without `variables`
224+
# running without `variables` in the `except` block leads to unnecessary exceptions in test execution output
225+
test_run = False
223226
try:
224227
try:
225228
test_variables = test_func(*args, variables=variables, **trimmed_kwargs)
226-
except TypeError:
227-
logger = logging.getLogger()
228-
logger.info(
229-
"This test can't accept variables as input. The test method should accept `**kwargs` and/or a "
230-
"`variables` parameter to make use of recorded test variables."
231-
)
229+
test_run = True
230+
except TypeError as error:
231+
if "unexpected keyword argument" in str(error) and "variables" in str(error):
232+
logger = logging.getLogger()
233+
logger.info(
234+
"This test can't accept variables as input. The test method should accept `**kwargs` and/or a "
235+
"`variables` parameter to make use of recorded test variables."
236+
)
237+
else:
238+
raise error
239+
# if the test couldn't accept `variables`, run the test without passing them
240+
if not test_run:
232241
test_variables = test_func(*args, **trimmed_kwargs)
242+
233243
except ResourceNotFoundError as error:
234244
error_body = ContentDecodePolicy.deserialize_from_http_generics(error.response)
235245
message = error_body.get("message") or error_body.get("Message")
236246
error_with_message = ResourceNotFoundError(message=message, response=error.response)
237247
six.raise_from(error_with_message, error)
248+
238249
finally:
239250
RequestsTransport.send = original_transport_func
240251
stop_record_or_playback(test_id, recording_id, test_variables)

0 commit comments

Comments
 (0)