@@ -91,17 +91,22 @@ def _get_type(self, data, first, datacondition):
91
91
92
92
def run (self , data , name = None ):
93
93
IfRunner .current_if_stack .append (data )
94
- first = True
95
- condition_matched = False
96
- for datacondition , body in data .bodies :
97
- branch_to_execute , result = self ._branch_to_be_executed (data , first , datacondition , body , condition_matched )
98
- condition_matched = condition_matched or branch_to_execute
99
- with StatusReporter (self ._context , result , dry_run_lib_kw = not branch_to_execute ):
100
- if branch_to_execute :
101
- runner = StepRunner (self ._context , self ._templated )
102
- runner .run_steps (body )
103
- first = False
104
- IfRunner .current_if_stack .pop ()
94
+ try :
95
+ first = True
96
+ condition_matched = False
97
+ for datacondition , body in data .bodies :
98
+ condition_matched = self ._run_if_branch (body , condition_matched , data , datacondition , first )
99
+ first = False
100
+ finally :
101
+ IfRunner .current_if_stack .pop ()
102
+
103
+ def _run_if_branch (self , body , condition_matched , data , datacondition , first ):
104
+ branch_to_execute , result = self ._branch_to_be_executed (data , first , datacondition , body , condition_matched )
105
+ with StatusReporter (self ._context , result , dry_run_lib_kw = not branch_to_execute ):
106
+ if branch_to_execute :
107
+ runner = StepRunner (self ._context , self ._templated )
108
+ runner .run_steps (body )
109
+ return condition_matched or branch_to_execute
105
110
106
111
def _branch_to_be_executed (self , data , first , datacondition , body , condition_matched_already ):
107
112
data_type = self ._get_type (data , first , datacondition )
@@ -110,25 +115,30 @@ def _branch_to_be_executed(self, data, first, datacondition, body, condition_mat
110
115
if not condition_result :
111
116
unresolved_condition = datacondition [0 ]
112
117
if not condition_matched_already and not self ._context .dry_run :
113
- condition , _ = VariableReplacer ().replace ([unresolved_condition ], (), variables = self ._context .variables )
114
- resolved_condition = condition [0 ]
115
- if is_unicode (resolved_condition ):
116
- condition_result = evaluate_expression (resolved_condition , self ._context .variables )
117
- else :
118
- condition_result = bool (resolved_condition )
118
+ condition_result = self ._resolve_condition (unresolved_condition )
119
119
branch_to_execute = self ._is_branch_to_execute (condition_matched_already ,
120
120
condition_result ,
121
121
body )
122
122
result = KeywordResult (kwname = self ._get_name (unresolved_condition ),
123
123
type = data_type )
124
124
return branch_to_execute , result
125
125
126
+ def _resolve_condition (self , unresolved_condition ):
127
+ condition , _ = VariableReplacer ().replace ([unresolved_condition ], (), variables = self ._context .variables )
128
+ resolved_condition = condition [0 ]
129
+ if is_unicode (resolved_condition ):
130
+ return evaluate_expression (resolved_condition , self ._context .variables )
131
+ return bool (resolved_condition )
132
+
126
133
def _is_branch_to_execute (self , condition_matched_already , condition_result , body ):
127
134
if self ._context .dry_run :
128
- current = IfRunner .current_if_stack [- 1 ]
129
- return current not in IfRunner .current_if_stack [:- 1 ]
135
+ return not self ._is_already_executing_this_if ()
130
136
return not condition_matched_already and condition_result and body
131
137
138
+ def _is_already_executing_this_if (self ):
139
+ current = IfRunner .current_if_stack [- 1 ]
140
+ return current in IfRunner .current_if_stack [:- 1 ]
141
+
132
142
def _get_name (self , condition ):
133
143
if not condition :
134
144
return ''
0 commit comments