diff options
author | Marvin Scholz <epirat07@gmail.com> | 2022-03-30 18:41:54 +0200 |
---|---|---|
committer | Eli Schwartz <eschwartz93@gmail.com> | 2022-11-22 22:53:07 -0500 |
commit | 49cd97c43487551a6b8bac4c5560bb2053270009 (patch) | |
tree | 6b718e9ee9c3330c3d3124af9291e3489dc68e8d | |
parent | 08262c3102e250759c09f5cdde80f13fbbf3dc08 (diff) | |
download | meson-49cd97c43487551a6b8bac4c5560bb2053270009.zip meson-49cd97c43487551a6b8bac4c5560bb2053270009.tar.gz meson-49cd97c43487551a6b8bac4c5560bb2053270009.tar.bz2 |
unittests: add and use _open_meson_log function
Factors out opening the meson log into its own function so that
it can be used in the places where with was done before.
Additionally instead of checking if the file exists before opening it,
try to open it and handle the exception when it is not found.
-rw-r--r-- | unittests/baseplatformtests.py | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/unittests/baseplatformtests.py b/unittests/baseplatformtests.py index e97c880..02dc923 100644 --- a/unittests/baseplatformtests.py +++ b/unittests/baseplatformtests.py @@ -15,6 +15,7 @@ from pathlib import PurePath from unittest import mock, TestCase, SkipTest import json +import io import os import re import subprocess @@ -120,13 +121,17 @@ class BasePlatformTests(TestCase): newdir = os.path.realpath(newdir) self.change_builddir(newdir) - def _get_meson_log(self) -> T.Optional[str]: + def _open_meson_log(self) -> io.TextIOWrapper: log = os.path.join(self.logdir, 'meson-log.txt') - if not os.path.isfile(log): - print(f"{log!r} doesn't exist", file=sys.stderr) + return open(log, encoding='utf-8') + + def _get_meson_log(self) -> T.Optional[str]: + try: + with self._open_meson_log() as f: + return f.read() + except FileNotFoundError as e: + print(f"{e.filename!r} doesn't exist", file=sys.stderr) return None - with open(log, encoding='utf-8') as f: - return f.read() def _print_meson_log(self) -> None: log = self._get_meson_log() @@ -318,7 +323,7 @@ class BasePlatformTests(TestCase): return contents def get_meson_log(self): - with open(os.path.join(self.builddir, 'meson-logs', 'meson-log.txt'), encoding='utf-8') as f: + with self._open_meson_log() as f: return f.readlines() def get_meson_log_compiler_checks(self): @@ -326,19 +331,19 @@ class BasePlatformTests(TestCase): Fetch a list command-lines run by meson for compiler checks. Each command-line is returned as a list of arguments. ''' - log = self.get_meson_log() prefix = 'Command line:' - cmds = [l[len(prefix):].split() for l in log if l.startswith(prefix)] - return cmds + with self._open_meson_log() as log: + cmds = [l[len(prefix):].split() for l in log if l.startswith(prefix)] + return cmds def get_meson_log_sanitychecks(self): ''' Same as above, but for the sanity checks that were run ''' - log = self.get_meson_log() prefix = 'Sanity check compiler command line:' - cmds = [l[len(prefix):].split() for l in log if l.startswith(prefix)] - return cmds + with self._open_meson_log() as log: + cmds = [l[len(prefix):].split() for l in log if l.startswith(prefix)] + return cmds def introspect(self, args): if isinstance(args, str): |