@@ -129,7 +129,7 @@ async def test_exec_cmd_findall_no_match(self):
129129 output = {'output_var' : 'Found: $MATCH_0' },
130130 )
131131 await self .executor ._exec_cmd (command )
132- assert 'output_var' not in self . varstore . variables
132+ assert self . varstore . get_variable ( 'output_var' ) == ''
133133 assert self .varstore .get_variable ('REGEX_MATCHES_LIST' ) == []
134134
135135 @pytest .mark .asyncio
@@ -156,7 +156,7 @@ async def test_exec_cmd_search_no_match(self):
156156 output = {'output_var' : 'Found: $MATCH_0' },
157157 )
158158 await self .executor ._exec_cmd (command )
159- assert 'output_var' not in self . varstore . variables
159+ assert self . varstore . get_variable ( 'output_var' ) == ''
160160 assert self .varstore .get_variable ('REGEX_MATCHES_LIST' ) == []
161161
162162 @pytest .mark .asyncio
@@ -175,3 +175,20 @@ async def test_exec_cmd_sub_no_match(self):
175175 await self .executor ._exec_cmd (command )
176176 assert self .varstore .get_variable ('output_var' ) == 'Replaced: no matches here'
177177 assert self .varstore .get_variable ('REGEX_MATCHES_LIST' ) == ['no matches here' ]
178+
179+ @pytest .mark .asyncio
180+ async def test_exec_cmd_search_no_match_clears_stale_value (self ):
181+ # Regression: in a loop a previous iteration might set output
182+ # variable. A following no-match must clear it, not leave the old value.
183+ self .varstore .set_variable ('PORT_STATUS' , 'open' )
184+ self .varstore .set_variable ('input_var' , 'closed' )
185+ command = RegExCommand (
186+ type = 'regex' ,
187+ cmd = 'open' ,
188+ mode = 'search' ,
189+ input = 'input_var' ,
190+ output = {'PORT_STATUS' : '$MATCH_0' },
191+ )
192+ await self .executor ._exec_cmd (command )
193+ assert self .varstore .get_variable ('PORT_STATUS' ) == ''
194+ assert self .varstore .get_variable ('REGEX_MATCHES_LIST' ) == []
0 commit comments