Skip to content

Commit 7dca07e

Browse files
committed
update code to have single try-catch
1 parent c7f516e commit 7dca07e

File tree

4 files changed

+180
-215
lines changed

4 files changed

+180
-215
lines changed

bzt/modules/_apiritif/generator.py

Lines changed: 108 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -72,90 +72,33 @@ 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")],
7979
value=ast.Constant(value=actionId, kind="")
8080
)
8181
]
8282

83-
def _gen_teardown_with_actionid(self):
84-
# Print the last actionId if an error/failure occurred
85-
return ast.FunctionDef(
86-
name="tearDown",
87-
args=[ast.Name(id='self', ctx=ast.Param())],
88-
body=[
89-
ast.Expr(ast_call(
90-
func=ast.Name(id="print", ctx=ast.Load()),
91-
args=[ast.Constant("[TAURUS] tearDown called", kind="")],
92-
keywords=[]
93-
)),
94-
ast.If(
95-
test=ast_attr("self.driver"),
96-
body=[ast.Expr(ast_call(func=ast_attr("self.driver.quit")))],
97-
orelse=[]
98-
),
99-
ast.Assign(
100-
targets=[ast.Name(id="outcome", ctx=ast.Store())],
101-
value=ast.Call(
102-
func=ast.Name(id="getattr", ctx=ast.Load()),
103-
args=[ast.Name(id="self", ctx=ast.Load()), ast.Constant("_outcome", kind=""),
104-
ast.Constant(None, kind="")],
105-
keywords=[]
106-
)
107-
),
108-
ast.Assign(
109-
targets=[ast.Name(id="result", ctx=ast.Store())],
110-
value=ast.Call(
111-
func=ast.Name(id="getattr", ctx=ast.Load()),
112-
args=[ast.Name(id="outcome", ctx=ast.Load()), ast.Constant("result", kind=""),
113-
ast.Constant(None, kind="")],
114-
keywords=[]
115-
)
116-
),
117-
ast.If(
118-
test=ast.BoolOp(
119-
op=ast.And(),
120-
values=[
121-
ast.Name(id="result", ctx=ast.Load()),
122-
ast.BoolOp(
123-
op=ast.Or(),
124-
values=[
125-
ast.Attribute(
126-
value=ast.Name(id="result", ctx=ast.Load()),
127-
attr="errors",
128-
ctx=ast.Load()
129-
),
130-
ast.Attribute(
131-
value=ast.Name(id="result", ctx=ast.Load()),
132-
attr="failures",
133-
ctx=ast.Load()
134-
)
135-
]
136-
)
137-
]
138-
),
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-
],
154-
orelse=[]
155-
),
156-
],
157-
decorator_list=[]
158-
)
83+
# def _gen_teardown_with_actionid(self):
84+
# # Print the last actionId if an error/failure occurred
85+
# return ast.FunctionDef(
86+
# name="tearDown",
87+
# args=[ast.Name(id='self', ctx=ast.Param())],
88+
# body=[
89+
# ast.Expr(ast_call(
90+
# func=ast.Name(id="print", ctx=ast.Load()),
91+
# args=[ast.Constant("[TAURUS] tearDown called", kind="")],
92+
# keywords=[]
93+
# )),
94+
# ast.If(
95+
# test=ast_attr("self.driver"),
96+
# body=[ast.Expr(ast_call(func=ast_attr("self.driver.quit")))],
97+
# orelse=[]
98+
# ),
99+
# ],
100+
# decorator_list=[]
101+
# )
159102
BYS = {
160103
'xpath': "XPATH",
161104
'css': "CSS_SELECTOR",
@@ -257,6 +200,7 @@ def __init__(self, scenario, label, wdlog=None, executor=None, ignore_unknown_ac
257200
self.replace_dialogs = True
258201
self.bzm_tdo_settings = bzm_tdo_settings
259202
self.do_testdata_orchestration = False
203+
self.has_action_ids = False
260204

261205
def _parse_action_params(self, expr, name):
262206
res = expr.match(name)
@@ -337,7 +281,11 @@ def _parse_dict_action(self, action_config):
337281
# actionId is an optional unique identifier for this action, used to track or reference
338282
# the action in the generated code and logs. It is passed through multiple layers of the
339283
# code generation process to maintain traceability of actions.
340-
actionId = action_config.get("actionId")
284+
if "actionId" in action_config and action_config.get("actionId") not in (None, "", {}):
285+
actionId = action_config.get("actionId")
286+
self.has_action_ids = True
287+
else:
288+
actionId = None
341289
selectors = []
342290
if action_config.get("locators"):
343291
selectors = action_config.get("locators")
@@ -791,7 +739,7 @@ def _gen_action(self, action_config, parent_request=None, index_label=""):
791739
wrapInTransaction = self._is_report_inside_actions(parent_request)
792740

793741
action_tracking = []
794-
if actionId:
742+
if self.has_action_ids:
795743
action_tracking.extend(self._gen_action_start_with_tracking(actionId))
796744

797745
action_elements = []
@@ -880,50 +828,6 @@ def _gen_action(self, action_config, parent_request=None, index_label=""):
880828
action_elements.append(ast_call(func=ast_attr("waiter"), args=[]))
881829

882830
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-
)]
927831

928832
if wrapInTransaction:
929833
label = self._create_action_label(parent_request.label, index_label, action)
@@ -1851,7 +1755,8 @@ def _gen_classdef(self):
18511755

18521756
def _gen_class_setup(self):
18531757
data_sources = [self._gen_default_vars()]
1854-
data_sources.extend(self._gen_class_actionid_tracking())
1758+
if self.has_action_ids:
1759+
data_sources.extend(self._gen_class_actionid_tracking())
18551760
for idx in range(len(self.data_sources)):
18561761
data_sources.append(ast.Expr(ast_call(func=ast_attr("reader_%s.read_vars" % (idx + 1)))))
18571762

@@ -1929,7 +1834,14 @@ def _gen_class_setup(self):
19291834
return [setup, gen_empty_line_stmt()]
19301835

19311836
def _gen_class_teardown(self):
1932-
return self._gen_teardown_with_actionid()
1837+
body = [
1838+
ast.If(
1839+
test=ast_attr("self.driver"),
1840+
body=[ast.Expr(ast_call(func=ast_attr("self.driver.quit")))],
1841+
orelse=[]
1842+
)
1843+
]
1844+
return ast.FunctionDef(name="tearDown", args=[ast_attr("self")], body=body, decorator_list=[])
19331845

19341846
def _nfc_preprocess(self, requests):
19351847
setup = []
@@ -2021,9 +1933,9 @@ def _gen_master_test_method(self, try_block, finally_block):
20211933
if not try_block:
20221934
raise TaurusConfigError("Supported transactions not found, test is empty")
20231935

2024-
body = []
1936+
main_body = []
20251937
for slave_name in try_block:
2026-
body.append(ast.Expr(ast_call(func=ast_attr("self." + slave_name))))
1938+
main_body.append(ast.Expr(ast_call(func=ast_attr("self." + slave_name))))
20271939

20281940
finally_body = []
20291941
for slave_name in finally_block:
@@ -2032,7 +1944,75 @@ def _gen_master_test_method(self, try_block, finally_block):
20321944
if finally_body:
20331945
teardown_marker = ast.Expr(ast_call(func=ast_attr("apiritif.set_stage"), args=[self._gen_expr("teardown")]))
20341946
finally_body.insert(0, teardown_marker)
2035-
body = [gen_try_except(try_body=body, final_body=finally_body)]
1947+
1948+
if self.has_action_ids:
1949+
prefixed_error = ast.BinOp(
1950+
left=ast.BinOp(
1951+
left=ast.BinOp(
1952+
left=ast.Constant("actionId: ", kind=""),
1953+
op=ast.Add(),
1954+
right=ast.Call(
1955+
func=ast.Name(id="str", ctx=ast.Load()),
1956+
args=[ast_attr("self._current_actionId")],
1957+
keywords=[]
1958+
)
1959+
),
1960+
op=ast.Add(),
1961+
right=ast.Constant(" | ", kind="")
1962+
),
1963+
op=ast.Add(),
1964+
right=ast.Call(
1965+
func=ast.Name(id="str", ctx=ast.Load()),
1966+
args=[ast.Name(id="exc", ctx=ast.Load())],
1967+
keywords=[]
1968+
)
1969+
)
1970+
1971+
final_error = ast.IfExp(
1972+
test=ast.Compare(
1973+
left=ast_attr("self._current_actionId"),
1974+
ops=[ast.IsNot()],
1975+
comparators=[ast.Constant(value=None, kind="")]
1976+
),
1977+
body=prefixed_error,
1978+
orelse=ast.Call(
1979+
func=ast.Name(id="str", ctx=ast.Load()),
1980+
args=[ast.Name(id="exc", ctx=ast.Load())],
1981+
keywords=[]
1982+
)
1983+
)
1984+
1985+
body = [ast.Try(
1986+
body=main_body,
1987+
handlers=[ast.ExceptHandler(
1988+
type=ast.Name(id="Exception", ctx=ast.Load()),
1989+
name="exc",
1990+
body=[ast.Raise(
1991+
exc=ast.Call(
1992+
func=ast.Call(
1993+
func=ast.Name(id="type", ctx=ast.Load()),
1994+
args=[ast.Name(id="exc", ctx=ast.Load())],
1995+
keywords=[]
1996+
),
1997+
args=[final_error],
1998+
keywords=[]
1999+
),
2000+
cause=ast.Name(id="exc", ctx=ast.Load())
2001+
)]
2002+
)],
2003+
orelse=[],
2004+
finalbody=finally_body
2005+
)]
2006+
else:
2007+
if finally_body:
2008+
body = [ast.Try(
2009+
body=main_body,
2010+
handlers=[],
2011+
orelse=[],
2012+
finalbody=finally_body
2013+
)]
2014+
else:
2015+
body = main_body
20362016

20372017
name = 'test_' + create_method_name(self.label)
20382018
return self._gen_test_method(name=name, body=body)

0 commit comments

Comments
 (0)