summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2024-07-16 11:19:39 -0700
committerEli Schwartz <eschwartz93@gmail.com>2024-08-15 14:14:56 -0400
commit5eb4d7336b6ab6e6bf1900bc4140783957052bc9 (patch)
tree165cce9703f9fa06954716358618b71b2e17a35b
parent5d322659567f004a444e0133ec40a61df5a46775 (diff)
downloadmeson-5eb4d7336b6ab6e6bf1900bc4140783957052bc9.tar.gz
unittests: use mock.patch.dict to deal with os.environ
-rw-r--r--unittests/allplatformstests.py1
-rw-r--r--unittests/baseplatformtests.py4
-rw-r--r--unittests/helpers.py14
3 files changed, 7 insertions, 12 deletions
diff --git a/unittests/allplatformstests.py b/unittests/allplatformstests.py
index e790ca493..dac7f9797 100644
--- a/unittests/allplatformstests.py
+++ b/unittests/allplatformstests.py
@@ -1055,6 +1055,7 @@ class AllPlatformTests(BasePlatformTests):
# target internal dependency wrong include_directories: source dir
self.assertPathBasenameEqual(incs[8], 'sub2')
+ @mock.patch.dict(os.environ)
def test_compiler_detection(self):
'''
Test that automatic compiler detection and setting from the environment
diff --git a/unittests/baseplatformtests.py b/unittests/baseplatformtests.py
index 7ba2aaed4..ae012dada 100644
--- a/unittests/baseplatformtests.py
+++ b/unittests/baseplatformtests.py
@@ -42,6 +42,7 @@ from run_tests import (
# e.g. for assertXXX helpers.
__unittest = True
+@mock.patch.dict(os.environ)
class BasePlatformTests(TestCase):
prefix = '/usr'
libdir = 'lib'
@@ -81,7 +82,6 @@ class BasePlatformTests(TestCase):
self.darwin_test_dir = os.path.join(src_root, 'test cases/darwin')
# Misc stuff
- self.orig_env = os.environ.copy()
if self.backend is Backend.ninja:
self.no_rebuild_stdout = ['ninja: no work to do.', 'samu: nothing to do']
else:
@@ -147,8 +147,6 @@ class BasePlatformTests(TestCase):
windows_proof_rmtree(path)
except FileNotFoundError:
pass
- os.environ.clear()
- os.environ.update(self.orig_env)
super().tearDown()
def _run(self, command, *, workdir=None, override_envvars: T.Optional[T.Mapping[str, str]] = None, stderr=True):
diff --git a/unittests/helpers.py b/unittests/helpers.py
index 2f97ab1d4..5cf8845b2 100644
--- a/unittests/helpers.py
+++ b/unittests/helpers.py
@@ -12,6 +12,7 @@ import typing as T
import zipfile
from pathlib import Path
from contextlib import contextmanager
+from unittest import mock
from mesonbuild.compilers import detect_c_compiler, compiler_from_language
from mesonbuild.mesonlib import (
@@ -119,16 +120,11 @@ def skip_if_env_set(key: str) -> T.Callable[[T.Callable[P, R]], T.Callable[P, R]
def wrapper(func: T.Callable[P, R]) -> T.Callable[P, R]:
@functools.wraps(func)
def wrapped(*args: P.args, **kwargs: P.kwargs) -> R:
- old = None
- if key in os.environ:
- if not is_ci():
- raise unittest.SkipTest(f'Env var {key!r} set, skipping')
- old = os.environ.pop(key)
- try:
+ if key in os.environ and not is_ci():
+ raise unittest.SkipTest(f'Env var {key!r} set, skipping')
+ with mock.patch.dict(os.environ):
+ os.environ.pop(key, None)
return func(*args, **kwargs)
- finally:
- if old is not None:
- os.environ[key] = old
return wrapped
return wrapper