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.
This commit is contained in:
Marvin Scholz 2022-03-30 18:41:54 +02:00 committed by Eli Schwartz
parent 08262c3102
commit 49cd97c434
1 changed files with 17 additions and 12 deletions

View File

@ -15,6 +15,7 @@
from pathlib import PurePath from pathlib import PurePath
from unittest import mock, TestCase, SkipTest from unittest import mock, TestCase, SkipTest
import json import json
import io
import os import os
import re import re
import subprocess import subprocess
@ -120,13 +121,17 @@ class BasePlatformTests(TestCase):
newdir = os.path.realpath(newdir) newdir = os.path.realpath(newdir)
self.change_builddir(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') log = os.path.join(self.logdir, 'meson-log.txt')
if not os.path.isfile(log): return open(log, encoding='utf-8')
print(f"{log!r} doesn't exist", file=sys.stderr)
return None def _get_meson_log(self) -> T.Optional[str]:
with open(log, encoding='utf-8') as f: try:
with self._open_meson_log() as f:
return f.read() return f.read()
except FileNotFoundError as e:
print(f"{e.filename!r} doesn't exist", file=sys.stderr)
return None
def _print_meson_log(self) -> None: def _print_meson_log(self) -> None:
log = self._get_meson_log() log = self._get_meson_log()
@ -318,7 +323,7 @@ class BasePlatformTests(TestCase):
return contents return contents
def get_meson_log(self): 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() return f.readlines()
def get_meson_log_compiler_checks(self): def get_meson_log_compiler_checks(self):
@ -326,8 +331,8 @@ class BasePlatformTests(TestCase):
Fetch a list command-lines run by meson for compiler checks. Fetch a list command-lines run by meson for compiler checks.
Each command-line is returned as a list of arguments. Each command-line is returned as a list of arguments.
''' '''
log = self.get_meson_log()
prefix = 'Command line:' prefix = 'Command line:'
with self._open_meson_log() as log:
cmds = [l[len(prefix):].split() for l in log if l.startswith(prefix)] cmds = [l[len(prefix):].split() for l in log if l.startswith(prefix)]
return cmds return cmds
@ -335,8 +340,8 @@ class BasePlatformTests(TestCase):
''' '''
Same as above, but for the sanity checks that were run Same as above, but for the sanity checks that were run
''' '''
log = self.get_meson_log()
prefix = 'Sanity check compiler command line:' prefix = 'Sanity check compiler command line:'
with self._open_meson_log() as log:
cmds = [l[len(prefix):].split() for l in log if l.startswith(prefix)] cmds = [l[len(prefix):].split() for l in log if l.startswith(prefix)]
return cmds return cmds