diff options
author | Angelos Evripiotis <jevripiotis@bloomberg.net> | 2019-03-27 10:58:57 +0000 |
---|---|---|
committer | Angelos Evripiotis <angelos.evripiotis@gmail.com> | 2019-04-11 13:58:33 +0000 |
commit | 9a9b06a5b52ced084b447c0468617e963555e068 (patch) | |
tree | f587d2f5c5188db0d16dabedfe3ecc9f94857b76 /tests/testutils | |
parent | 887461a584613c304064a636cbda8f4c863f35f9 (diff) | |
download | buildstream-9a9b06a5b52ced084b447c0468617e963555e068.tar.gz |
tests: extract testutils.override_os_uname
Combine the very similar override_uname_arch() and override_uname_os()
in a new helper function, to reduce duplication.
Also use a 'try/finally' block to ensure that the original
os.uname() is restored.
This will make it simpler to adopt platform.uname() instead in later
work.
Diffstat (limited to 'tests/testutils')
-rw-r--r-- | tests/testutils/__init__.py | 1 | ||||
-rw-r--r-- | tests/testutils/platform.py | 51 |
2 files changed, 52 insertions, 0 deletions
diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py index 67378c959..31912fed9 100644 --- a/tests/testutils/__init__.py +++ b/tests/testutils/__init__.py @@ -30,3 +30,4 @@ from .junction import generate_junction from .runner_integration import wait_for_cache_granularity from .python_repo import setup_pypi_repo from .yaml import yaml_file_get_provenance +from .platform import override_os_uname diff --git a/tests/testutils/platform.py b/tests/testutils/platform.py new file mode 100644 index 000000000..e00a131bd --- /dev/null +++ b/tests/testutils/platform.py @@ -0,0 +1,51 @@ +# +# Copyright (C) 2019 Bloomberg Finance LP +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see <http://www.gnu.org/licenses/>. +# +# Authors: +# Angelos Evripiotis <jevripiotis@bloomberg.net> + +from contextlib import contextmanager +import os + + +# override_platform_uname() +# +# Context manager to override the reported value of `os.uname()`. +# +# Args: +# system (str): Optional str to replace the 1st entry of uname with. +# machine (str): Optional str to replace the 5th entry of uname with. +# +@contextmanager +def override_os_uname(*, system=None, machine=None): + orig_func = os.uname + result = orig_func() + + result = list(result) + if system is not None: + result[0] = system + if machine is not None: + result[4] = machine + result = tuple(result) + + def override_func(): + return result + + os.uname = override_func + try: + yield + finally: + os.uname = orig_func |