1818import  shlex 
1919import  logging 
2020import  signal 
21+ import  json 
22+ import  re 
23+ from  pathlib  import  Path 
2124from  pprint  import  pprint 
2225from  tempfile  import  mkdtemp 
23- from  etos_test_runner .test_regex  import  TEST_REGEX 
2426
2527BASE  =  os .path .dirname (os .path .abspath (__file__ ))
2628
@@ -40,6 +42,7 @@ class Executor:  # pylint: disable=too-many-instance-attributes
4042    report_path  =  "test_output.log" 
4143    test_name  =  "" 
4244    current_test  =  None 
45+     test_regex  =  {}
4346    test_checkouts  =  {}
4447    logger  =  logging .getLogger ("Executor" )
4548
@@ -52,6 +55,7 @@ def __init__(self, test, iut, etos):
5255        :param iut: IUT to execute test on. 
5356        :type iut: :obj:`etr.lib.iut.Iut` 
5457        """ 
58+         self .load_regex ()
5559        self .test  =  test 
5660        self .tests  =  {}
5761
@@ -81,6 +85,34 @@ def __init__(self, test, iut, etos):
8185        self .context  =  self .etos .config .get ("context" )
8286        self .result  =  True 
8387
88+     def  load_regex (self ):
89+         """Attempt to load regex file from environment variables. 
90+ 
91+         The regex file is used to determine when a test case has triggered, 
92+         started, passed, failed, been skipped, raise error and the test name. 
93+         """ 
94+         if  os .getenv ("self.test_regex" ):
95+             try :
96+                 path  =  Path (os .getenv ("TEST_REGEX" ))
97+                 if  path .exists () and  path .is_file ():
98+                     regex  =  json .load (path .open ())
99+                     for  key , value  in  regex .items ():
100+                         self .test_regex [key ] =  re .compile (value )
101+                 else :
102+                     self .logger .warning ("%r is not a file or does not exist." , path )
103+             except  TypeError  as  exception :
104+                 self .logger .error ("%r" , exception )
105+                 self .logger .error ("Wrong type when loading %r" , path )
106+             except  re .error  as  exception :
107+                 self .logger .error ("%r" , exception )
108+                 self .logger .error ("Failed to parse regex in file %r (%r)" , path , value )
109+             except  json .decoder .JSONDecodeError  as  exception :
110+                 self .logger .error ("%r" , exception )
111+                 self .logger .error ("Failed to load JSON %r" , path )
112+             except  Exception  as  exception :
113+                 self .logger .error ("%r" , exception )
114+                 self .logger .error ("Unknown error when loading regex JSON file." )
115+ 
84116    def  _checkout_tests (self , test_checkout ):
85117        """Check out tests for this execution. 
86118
@@ -238,29 +270,29 @@ def parse(self, line):
238270        """ 
239271        if  not  isinstance (line , str ):
240272            return 
241-         test_name  =  TEST_REGEX ["test_name" ].findall (line )
273+         test_name  =  self . test_regex ["test_name" ].findall (line )
242274        if  test_name :
243275            self .current_test  =  test_name [0 ]
244276            self .tests .setdefault (self .current_test , {})
245-         if  TEST_REGEX ["triggered" ].match (line ):
277+         if  self . test_regex ["triggered" ].match (line ):
246278            self .tests [self .current_test ]["triggered" ] =  self ._triggered (
247279                self .current_test 
248280            )
249-         if  TEST_REGEX ["started" ].match (line ):
281+         if  self . test_regex ["started" ].match (line ):
250282            self .tests [self .current_test ]["started" ] =  self ._started (self .current_test )
251-         if  TEST_REGEX ["passed" ].match (line ):
283+         if  self . test_regex ["passed" ].match (line ):
252284            self .tests [self .current_test ]["finished" ] =  self ._finished (
253285                self .current_test , "PASSED" 
254286            )
255-         if  TEST_REGEX ["failed" ].match (line ):
287+         if  self . test_regex ["failed" ].match (line ):
256288            self .tests [self .current_test ]["finished" ] =  self ._finished (
257289                self .current_test , "FAILED" 
258290            )
259-         if  TEST_REGEX ["error" ].match (line ):
291+         if  self . test_regex ["error" ].match (line ):
260292            self .tests [self .current_test ]["finished" ] =  self ._finished (
261293                self .current_test , "ERROR" 
262294            )
263-         if  TEST_REGEX ["skipped" ].match (line ):
295+         if  self . test_regex ["skipped" ].match (line ):
264296            self .tests [self .current_test ]["finished" ] =  self ._finished (
265297                self .current_test , "SKIPPED" 
266298            )
@@ -284,7 +316,7 @@ def execute(self):
284316            )
285317            self .logger .info ("Start test." )
286318            for  _ , line  in  iterator :
287-                 if  TEST_REGEX :
319+                 if  self . test_regex :
288320                    self .parse (line )
289321            self .logger .info ("Finished." )
290322            self .result  =  line 
0 commit comments