Fix new pylint-3.0.0 detected issues 69/108169/1
authorGilles Thouenon <gilles.thouenon@orange.com>
Tue, 3 Oct 2023 07:35:06 +0000 (09:35 +0200)
committerGilles Thouenon <gilles.thouenon@orange.com>
Tue, 3 Oct 2023 08:01:44 +0000 (10:01 +0200)
Refactor wait_until_log_contains method of test_utils taking advantage
of the with block in order to remove the finally block. That simplifies
the return flow.

JIRA: TRNSPRTPCE-764
Signed-off-by: Gilles Thouenon <gilles.thouenon@orange.com>
Change-Id: I8b3fa004eddc9e8688640205490c385645f4d6e3

tests/transportpce_tests/common/test_utils.py

index 8ba812204b97e611af1e3d109124831d95a164e9..888a8e91fc228751b85ecd386c41bd49cd2f4537 100644 (file)
@@ -273,34 +273,29 @@ def wait_until_log_contains(log_file, regexp, time_to_wait=60):
     # pylint: disable=lost-exception
     # pylint: disable=consider-using-with
     stringfound = False
-    filefound = False
     line = None
     try:
         with TimeOut(seconds=time_to_wait):
             while not os.path.exists(log_file):
                 time.sleep(0.2)
-            filelogs = open(log_file, 'r', encoding='utf-8')
-            filelogs.seek(0, 2)
-            filefound = True
-            print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True)
-            compiled_regexp = re.compile(regexp)
-            while True:
-                line = filelogs.readline()
-                if compiled_regexp.search(line):
-                    print('Pattern found!', end=' ')
-                    stringfound = True
-                    break
-                if not line:
-                    time.sleep(0.1)
+            with open(log_file, 'r', encoding='utf-8') as filelogs:
+                filelogs.seek(0, 2)
+                print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True)
+                compiled_regexp = re.compile(regexp)
+                while True:
+                    line = filelogs.readline()
+                    if compiled_regexp.search(line):
+                        print('Pattern found!', end=' ')
+                        stringfound = True
+                        break
+                    if not line:
+                        time.sleep(0.1)
+        return stringfound
     except TimeoutError:
         print('Pattern not found after ' + str(time_to_wait), end=' seconds! ', flush=True)
+        return stringfound
     except PermissionError:
         print('Permission Error when trying to access the log file', end=' ... ', flush=True)
-    finally:
-        if filefound:
-            filelogs.close()
-        else:
-            print('log file does not exist or is not accessible... ', flush=True)
         return stringfound