Skip to content

Commit 7959f05

Browse files
committed
update code to have single try-catch
1 parent c7f516e commit 7959f05

File tree

1 file changed

+93
-69
lines changed

1 file changed

+93
-69
lines changed

bzt/modules/_apiritif/generator.py

Lines changed: 93 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _gen_class_actionid_tracking(self):
7272
]
7373

7474
def _gen_action_start_with_tracking(self, actionId):
75-
# Assign actionId to self._current_actionId before action_start
75+
# Assign actionId (or None) to self._current_actionId before each action
7676
return [
7777
ast.Assign(
7878
targets=[ast_attr("self._current_actionId")],
@@ -136,21 +136,7 @@ def _gen_teardown_with_actionid(self):
136136
)
137137
]
138138
),
139-
body=[
140-
ast.Expr(ast_call(
141-
func=ast.Name(id="print", ctx=ast.Load()),
142-
args=[ast.BinOp(
143-
left=ast.Constant("[TAURUS][actionId] ", kind=""),
144-
op=ast.Add(),
145-
right=ast.Call(
146-
func=ast.Name(id="str", ctx=ast.Load()),
147-
args=[ast_attr("self._current_actionId")],
148-
keywords=[]
149-
)
150-
)],
151-
keywords=[]
152-
))
153-
],
139+
body=[],
154140
orelse=[]
155141
),
156142
],
@@ -257,6 +243,7 @@ def __init__(self, scenario, label, wdlog=None, executor=None, ignore_unknown_ac
257243
self.replace_dialogs = True
258244
self.bzm_tdo_settings = bzm_tdo_settings
259245
self.do_testdata_orchestration = False
246+
self.has_action_ids = False
260247

261248
def _parse_action_params(self, expr, name):
262249
res = expr.match(name)
@@ -338,6 +325,8 @@ def _parse_dict_action(self, action_config):
338325
# the action in the generated code and logs. It is passed through multiple layers of the
339326
# code generation process to maintain traceability of actions.
340327
actionId = action_config.get("actionId")
328+
if actionId is not None:
329+
self.has_action_ids = True
341330
selectors = []
342331
if action_config.get("locators"):
343332
selectors = action_config.get("locators")
@@ -791,7 +780,7 @@ def _gen_action(self, action_config, parent_request=None, index_label=""):
791780
wrapInTransaction = self._is_report_inside_actions(parent_request)
792781

793782
action_tracking = []
794-
if actionId:
783+
if self.has_action_ids:
795784
action_tracking.extend(self._gen_action_start_with_tracking(actionId))
796785

797786
action_elements = []
@@ -880,50 +869,6 @@ def _gen_action(self, action_config, parent_request=None, index_label=""):
880869
action_elements.append(ast_call(func=ast_attr("waiter"), args=[]))
881870

882871
action_body = [ast.Expr(element) for element in action_elements]
883-
if actionId:
884-
prefixed_error = ast.BinOp(
885-
left=ast.BinOp(
886-
left=ast.Constant("[TAURUS][actionId] ", kind=""),
887-
op=ast.Add(),
888-
right=ast.Call(
889-
func=ast.Name(id="str", ctx=ast.Load()),
890-
args=[ast_attr("self._current_actionId")],
891-
keywords=[]
892-
)
893-
),
894-
op=ast.Add(),
895-
right=ast.BinOp(
896-
left=ast.Constant(" | ", kind=""),
897-
op=ast.Add(),
898-
right=ast.Call(
899-
func=ast.Name(id="str", ctx=ast.Load()),
900-
args=[ast.Name(id="exc", ctx=ast.Load())],
901-
keywords=[]
902-
)
903-
)
904-
)
905-
906-
action_body = [ast.Try(
907-
body=action_body,
908-
handlers=[ast.ExceptHandler(
909-
type=ast.Name(id="Exception", ctx=ast.Load()),
910-
name="exc",
911-
body=[ast.Raise(
912-
exc=ast.Call(
913-
func=ast.Call(
914-
func=ast.Name(id="type", ctx=ast.Load()),
915-
args=[ast.Name(id="exc", ctx=ast.Load())],
916-
keywords=[]
917-
),
918-
args=[prefixed_error],
919-
keywords=[]
920-
),
921-
cause=ast.Name(id="exc", ctx=ast.Load())
922-
)]
923-
)],
924-
orelse=[],
925-
finalbody=[]
926-
)]
927872

928873
if wrapInTransaction:
929874
label = self._create_action_label(parent_request.label, index_label, action)
@@ -1851,7 +1796,8 @@ def _gen_classdef(self):
18511796

18521797
def _gen_class_setup(self):
18531798
data_sources = [self._gen_default_vars()]
1854-
data_sources.extend(self._gen_class_actionid_tracking())
1799+
if self.has_action_ids:
1800+
data_sources.extend(self._gen_class_actionid_tracking())
18551801
for idx in range(len(self.data_sources)):
18561802
data_sources.append(ast.Expr(ast_call(func=ast_attr("reader_%s.read_vars" % (idx + 1)))))
18571803

@@ -1929,7 +1875,17 @@ def _gen_class_setup(self):
19291875
return [setup, gen_empty_line_stmt()]
19301876

19311877
def _gen_class_teardown(self):
1932-
return self._gen_teardown_with_actionid()
1878+
if self.has_action_ids:
1879+
return self._gen_teardown_with_actionid()
1880+
1881+
body = [
1882+
ast.If(
1883+
test=ast_attr("self.driver"),
1884+
body=[ast.Expr(ast_call(func=ast_attr("self.driver.quit")))],
1885+
orelse=[]
1886+
)
1887+
]
1888+
return ast.FunctionDef(name="tearDown", args=[ast_attr("self")], body=body, decorator_list=[])
19331889

19341890
def _nfc_preprocess(self, requests):
19351891
setup = []
@@ -2021,18 +1977,86 @@ def _gen_master_test_method(self, try_block, finally_block):
20211977
if not try_block:
20221978
raise TaurusConfigError("Supported transactions not found, test is empty")
20231979

2024-
body = []
2025-
for slave_name in try_block:
2026-
body.append(ast.Expr(ast_call(func=ast_attr("self." + slave_name))))
1980+
main_body = []
1981+
for step_method_name in try_block:
1982+
main_body.append(ast.Expr(ast_call(func=ast_attr("self." + step_method_name))))
20271983

20281984
finally_body = []
2029-
for slave_name in finally_block:
2030-
finally_body.append(ast.Expr(ast_call(func=ast_attr("self." + slave_name))))
1985+
for step_method_name in finally_block:
1986+
finally_body.append(ast.Expr(ast_call(func=ast_attr("self." + step_method_name))))
20311987

20321988
if finally_body:
20331989
teardown_marker = ast.Expr(ast_call(func=ast_attr("apiritif.set_stage"), args=[self._gen_expr("teardown")]))
20341990
finally_body.insert(0, teardown_marker)
2035-
body = [gen_try_except(try_body=body, final_body=finally_body)]
1991+
1992+
if self.has_action_ids:
1993+
prefixed_error = ast.BinOp(
1994+
left=ast.BinOp(
1995+
left=ast.BinOp(
1996+
left=ast.Constant("actionId: ", kind=""),
1997+
op=ast.Add(),
1998+
right=ast.Call(
1999+
func=ast.Name(id="str", ctx=ast.Load()),
2000+
args=[ast_attr("self._current_actionId")],
2001+
keywords=[]
2002+
)
2003+
),
2004+
op=ast.Add(),
2005+
right=ast.Constant(" | ", kind="")
2006+
),
2007+
op=ast.Add(),
2008+
right=ast.Call(
2009+
func=ast.Name(id="str", ctx=ast.Load()),
2010+
args=[ast.Name(id="exc", ctx=ast.Load())],
2011+
keywords=[]
2012+
)
2013+
)
2014+
2015+
final_error = ast.IfExp(
2016+
test=ast.Compare(
2017+
left=ast_attr("self._current_actionId"),
2018+
ops=[ast.IsNot()],
2019+
comparators=[ast.Constant(value=None, kind="")]
2020+
),
2021+
body=prefixed_error,
2022+
orelse=ast.Call(
2023+
func=ast.Name(id="str", ctx=ast.Load()),
2024+
args=[ast.Name(id="exc", ctx=ast.Load())],
2025+
keywords=[]
2026+
)
2027+
)
2028+
2029+
body = [ast.Try(
2030+
body=main_body,
2031+
handlers=[ast.ExceptHandler(
2032+
type=ast.Name(id="Exception", ctx=ast.Load()),
2033+
name="exc",
2034+
body=[ast.Raise(
2035+
exc=ast.Call(
2036+
func=ast.Call(
2037+
func=ast.Name(id="type", ctx=ast.Load()),
2038+
args=[ast.Name(id="exc", ctx=ast.Load())],
2039+
keywords=[]
2040+
),
2041+
args=[final_error],
2042+
keywords=[]
2043+
),
2044+
cause=ast.Name(id="exc", ctx=ast.Load())
2045+
)]
2046+
)],
2047+
orelse=[],
2048+
finalbody=finally_body
2049+
)]
2050+
else:
2051+
if finally_body:
2052+
body = [ast.Try(
2053+
body=main_body,
2054+
handlers=[],
2055+
orelse=[],
2056+
finalbody=finally_body
2057+
)]
2058+
else:
2059+
body = main_body
20362060

20372061
name = 'test_' + create_method_name(self.label)
20382062
return self._gen_test_method(name=name, body=body)

0 commit comments

Comments
 (0)