66.. moduleauthor:: Andrea Cervesato <[email protected] > 77"""
88import os
9+ import re
910import logging
1011import asyncio
1112import libkirk
1415from libkirk import KirkException
1516from libkirk .sut import SUT
1617from libkirk .sut import IOBuffer
18+ from libkirk .data import Suite
1719from libkirk .results import TestResults
1820from libkirk .export import JSONExporter
1921from libkirk .scheduler import SuiteScheduler
@@ -186,16 +188,16 @@ async def _stop_sut(self) -> None:
186188 await libkirk .events .fire ("sut_stop" , self ._sut .name )
187189 await self ._sut .stop (iobuffer = RedirectSUTStdout (self ._sut , False ))
188190
189- async def _read_suites (self , suites : list , restore : str ) -> list :
191+ async def _get_suites_objects (self , names : list ) -> list :
190192 """
191- Read suites and return a list of Suite objects .
193+ Return suites objects by giving their names .
192194 """
193195 coros = []
194- for suite in suites :
196+ for suite in names :
195197 coros .append (self ._framework .find_suite (self ._sut , suite ))
196198
197199 if not coros :
198- raise KirkException (f"Can't find suites: { suites } " )
200+ raise KirkException (f"Can't find suites: { names } " )
199201
200202 suites_obj = await asyncio .gather (* coros , return_exceptions = True )
201203 for suite in suites_obj :
@@ -205,6 +207,39 @@ async def _read_suites(self, suites: list, restore: str) -> list:
205207 if not suite :
206208 raise KirkException ("Can't find suite objects" )
207209
210+ return suites_obj
211+
212+ async def _filter_tests (self , pattern : str ) -> list :
213+ """
214+ Read all tests from all the testing suites which are matching the regex.
215+ """
216+ if not pattern :
217+ raise ValueError ("pattern regex is empty" )
218+
219+ suites = await self ._framework .get_suites (self ._sut )
220+ if not suites :
221+ return []
222+
223+ suites_obj = await self ._get_suites_objects (suites )
224+
225+ tests = []
226+ matcher = re .compile (pattern )
227+
228+ for suite in suites_obj :
229+ for test in suite .tests :
230+ if matcher .search (test .name ):
231+ tests .append (test )
232+
233+ suite = Suite ('ltp_filtered_tests' , tests )
234+
235+ return [suite ]
236+
237+ async def _read_suites (self , suites : list , restore : str ) -> list :
238+ """
239+ Read suites and return a list of Suite objects.
240+ """
241+ suites_obj = await self ._get_suites_objects (suites )
242+
208243 restored = self ._read_restored_session (restore )
209244 if restored :
210245 await libkirk .events .fire ("session_restore" , restore )
@@ -286,10 +321,13 @@ async def stop(self) -> None:
286321 await libkirk .events .fire ("session_stopped" )
287322 self ._stop = False
288323
324+ # consider changing the arguments handling if new ones must be added
325+ # pylint: disable=too-many-positional-arguments
289326 async def run (
290327 self ,
291328 command : str = None ,
292329 suites : list = None ,
330+ pattern : str = None ,
293331 report_path : str = None ,
294332 restore : str = None ) -> None :
295333 """
@@ -298,6 +336,8 @@ async def run(
298336 :type command: str
299337 :param suites: list of suites to execute
300338 :type suites: list
339+ :param pattern: string pattern to match tests
340+ :types pattern: str
301341 :param report_path: JSON report path
302342 :type report_path: str
303343 :param restore: temporary directory generated by a previous session
@@ -317,6 +357,10 @@ async def run(
317357 if command :
318358 await self ._exec_command (command )
319359
360+ if pattern :
361+ suites_obj = await self ._filter_tests (pattern )
362+ await self ._scheduler .schedule (suites_obj )
363+
320364 if suites :
321365 suites_obj = await self ._read_suites (suites , restore )
322366 await self ._scheduler .schedule (suites_obj )
0 commit comments