unittests/base: Allow init method to fail

This adds a new keyword argument to the init method, `allow_fail`. When
set to True (default is False) then a failure to configure is not an
error, and output is still returned. This can be useful for cases where
we expect initialization to fail, and want to check the output.
pull/9132/head
Dylan Baker 3 years ago
parent 310d7d13f4
commit 5fe03cf8d9
  1. 40
      unittests/baseplatformtests.py

@ -112,13 +112,18 @@ class BasePlatformTests(TestCase):
newdir = os.path.realpath(newdir) newdir = os.path.realpath(newdir)
self.change_builddir(newdir) self.change_builddir(newdir)
def _print_meson_log(self): def _get_meson_log(self) -> T.Optional[str]:
log = os.path.join(self.logdir, 'meson-log.txt') log = os.path.join(self.logdir, 'meson-log.txt')
if not os.path.isfile(log): if not os.path.isfile(log):
print(f"{log!r} doesn't exist") print(f"{log!r} doesn't exist", file=sys.stderr)
return return None
with open(log, encoding='utf-8') as f: with open(log, encoding='utf-8') as f:
print(f.read()) return f.read()
def _print_meson_log(self) -> None:
log = self._get_meson_log()
if log:
print(log)
def tearDown(self): def tearDown(self):
for path in self.builddirs: for path in self.builddirs:
@ -160,7 +165,15 @@ class BasePlatformTests(TestCase):
default_args=True, default_args=True,
inprocess=False, inprocess=False,
override_envvars=None, override_envvars=None,
workdir=None): workdir=None,
allow_fail: bool = False) -> str:
"""Call `meson setup`
:param allow_fail: If set to true initialization is allowed to fail.
When it does the log will be returned instead of stdout.
:return: the value of stdout on success, or the meson log on failure
when :param allow_fail: is true
"""
self.assertPathExists(srcdir) self.assertPathExists(srcdir)
if extra_args is None: if extra_args is None:
extra_args = [] extra_args = []
@ -180,10 +193,12 @@ class BasePlatformTests(TestCase):
try: try:
returncode, out, err = run_configure_inprocess(self.meson_args + args + extra_args, override_envvars) returncode, out, err = run_configure_inprocess(self.meson_args + args + extra_args, override_envvars)
except Exception as e: except Exception as e:
# Don't double print if not allow_fail:
if str(e) != 'Configure failed':
self._print_meson_log() self._print_meson_log()
raise raise
out = self._get_meson_log() # Best we can do here
err = '' # type checkers can't figure out that on this path returncode will always be 0
returncode = 0
finally: finally:
# Close log file to satisfy Windows file locking # Close log file to satisfy Windows file locking
mesonbuild.mlog.shutdown() mesonbuild.mlog.shutdown()
@ -198,15 +213,18 @@ class BasePlatformTests(TestCase):
print(out) print(out)
print('Stderr:\n') print('Stderr:\n')
print(err) print(err)
raise RuntimeError('Configure failed') if not allow_fail:
raise RuntimeError('Configure failed')
else: else:
try: try:
out = self._run(self.setup_command + args + extra_args, override_envvars=override_envvars, workdir=workdir) out = self._run(self.setup_command + args + extra_args, override_envvars=override_envvars, workdir=workdir)
except SkipTest: except SkipTest:
raise SkipTest('Project requested skipping: ' + srcdir) raise SkipTest('Project requested skipping: ' + srcdir)
except Exception: except Exception:
self._print_meson_log() if not allow_fail:
raise self._print_meson_log()
raise
out = self._get_meson_log() # best we can do here
return out return out
def build(self, target=None, *, extra_args=None, override_envvars=None): def build(self, target=None, *, extra_args=None, override_envvars=None):

Loading…
Cancel
Save