aboutsummaryrefslogtreecommitdiff
path: root/run_tests.py
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2020-09-14 16:32:39 -0700
committerNirbheek Chauhan <nirbheek.chauhan@gmail.com>2020-09-17 11:26:50 +0000
commit80ec7ec6810dfd8b8bf84f089d59b9320b6da36c (patch)
tree49b005d6e1e58f69a3d938be2afedb9f7a20ac64 /run_tests.py
parent807f88739ebfa002c9a0b9acd3e24c9610fb02a2 (diff)
downloadmeson-80ec7ec6810dfd8b8bf84f089d59b9320b6da36c.zip
meson-80ec7ec6810dfd8b8bf84f089d59b9320b6da36c.tar.gz
meson-80ec7ec6810dfd8b8bf84f089d59b9320b6da36c.tar.bz2
tests: Make monkey patching smarter
There are two problems here. The first is that we're doing manual monkey patching which is fragile and verbose, when unittest.mock is designed specifically to solve this exact problem. The second is that we're monkey patching os.environ at two different levels with the same information. So let's only do it once.
Diffstat (limited to 'run_tests.py')
-rwxr-xr-xrun_tests.py38
1 files changed, 13 insertions, 25 deletions
diff --git a/run_tests.py b/run_tests.py
index b3bcee2..6e9c6ce 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -26,6 +26,7 @@ from io import StringIO
from enum import Enum
from glob import glob
from pathlib import Path
+from unittest import mock
from mesonbuild import compilers
from mesonbuild import dependencies
from mesonbuild import mesonlib
@@ -255,16 +256,11 @@ def ensure_backend_detects_changes(backend):
time.sleep(1)
def run_mtest_inprocess(commandlist):
- old_stdout = sys.stdout
- sys.stdout = mystdout = StringIO()
- old_stderr = sys.stderr
- sys.stderr = mystderr = StringIO()
- try:
+ stderr = StringIO()
+ stdout = StringIO()
+ with mock.patch.object(sys, 'stdout', stdout), mock.patch.object(sys, 'stderr', stderr):
returncode = mtest.run_with_args(commandlist)
- finally:
- sys.stdout = old_stdout
- sys.stderr = old_stderr
- return returncode, mystdout.getvalue(), mystderr.getvalue()
+ return returncode, stdout.getvalue(), stderr.getvalue()
def clear_meson_configure_class_caches():
compilers.CCompiler.library_dirs_cache = {}
@@ -275,22 +271,14 @@ def clear_meson_configure_class_caches():
dependencies.PkgConfigDependency.class_pkgbin = mesonlib.PerMachine(None, None)
def run_configure_inprocess(commandlist, env=None):
- old_stdout = sys.stdout
- sys.stdout = mystdout = StringIO()
- old_stderr = sys.stderr
- sys.stderr = mystderr = StringIO()
- old_environ = os.environ.copy()
- if env is not None:
- os.environ.update(env)
- try:
- returncode = mesonmain.run(commandlist, get_meson_script())
- finally:
- sys.stdout = old_stdout
- sys.stderr = old_stderr
- clear_meson_configure_class_caches()
- os.environ.clear()
- os.environ.update(old_environ)
- return returncode, mystdout.getvalue(), mystderr.getvalue()
+ stderr = StringIO()
+ stdout = StringIO()
+ with mock.patch.dict(os.environ, env or {}), mock.patch.object(sys, 'stdout', stdout), mock.patch.object(sys, 'stderr', stderr):
+ try:
+ returncode = mesonmain.run(commandlist, get_meson_script())
+ finally:
+ clear_meson_configure_class_caches()
+ return returncode, stdout.getvalue(), stderr.getvalue()
def run_configure_external(full_command, env=None):
pc, o, e = mesonlib.Popen_safe(full_command, env=env)