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 | |
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')
-rw-r--r-- | tests/format/optionarch.py | 32 | ||||
-rw-r--r-- | tests/format/optionos.py | 27 | ||||
-rw-r--r-- | tests/testutils/__init__.py | 1 | ||||
-rw-r--r-- | tests/testutils/platform.py | 51 |
4 files changed, 66 insertions, 45 deletions
diff --git a/tests/format/optionarch.py b/tests/format/optionarch.py index 86363b834..3f779e5ea 100644 --- a/tests/format/optionarch.py +++ b/tests/format/optionarch.py @@ -1,7 +1,6 @@ # Pylint doesn't play well with fixtures and dependency injection from pytest # pylint: disable=redefined-outer-name -from contextlib import contextmanager import os import pytest @@ -10,29 +9,14 @@ from buildstream import _yaml from buildstream._exceptions import ErrorDomain, LoadErrorReason from buildstream.plugintestutils.runcli import cli # pylint: disable=unused-import +from tests.testutils import override_os_uname + # Project directory DATA_DIR = os.path.dirname(os.path.realpath(__file__)) -# Context manager to override the reported value of `os.uname()` -@contextmanager -def override_uname_arch(name): - orig_uname = os.uname - orig_tuple = tuple(os.uname()) - override_result = (orig_tuple[0], orig_tuple[1], - orig_tuple[2], orig_tuple[3], - name) - - def override(): - return override_result - - os.uname = override - yield - os.uname = orig_uname - - @pytest.mark.datafiles(DATA_DIR) -@pytest.mark.parametrize("uname,value,expected", [ +@pytest.mark.parametrize("machine,value,expected", [ # Test explicitly provided arches ('arm', 'aarch32', 'Army'), ('arm', 'aarch64', 'Aarchy'), @@ -46,8 +30,8 @@ def override_uname_arch(name): ('i386', 'aarch32', 'Army'), ('x86_64', 'aarch64', 'Aarchy'), ]) -def test_conditional(cli, datafiles, uname, value, expected): - with override_uname_arch(uname): +def test_conditional(cli, datafiles, machine, value, expected): + with override_os_uname(machine=machine): project = os.path.join(datafiles.dirname, datafiles.basename, 'option-arch') bst_args = [] @@ -70,7 +54,7 @@ def test_conditional(cli, datafiles, uname, value, expected): @pytest.mark.datafiles(DATA_DIR) def test_unsupported_arch(cli, datafiles): - with override_uname_arch("x86_64"): + with override_os_uname(machine="x86_64"): project = os.path.join(datafiles.dirname, datafiles.basename, 'option-arch') result = cli.run(project=project, silent=True, args=[ 'show', @@ -85,7 +69,7 @@ def test_unsupported_arch(cli, datafiles): @pytest.mark.datafiles(DATA_DIR) def test_alias(cli, datafiles): - with override_uname_arch("arm"): + with override_os_uname(machine="arm"): project = os.path.join(datafiles.dirname, datafiles.basename, 'option-arch-alias') result = cli.run(project=project, silent=True, args=[ 'show', @@ -100,7 +84,7 @@ def test_alias(cli, datafiles): @pytest.mark.datafiles(DATA_DIR) def test_unknown_host_arch(cli, datafiles): - with override_uname_arch("x86_128"): + with override_os_uname(machine="x86_128"): project = os.path.join(datafiles.dirname, datafiles.basename, 'option-arch') result = cli.run(project=project, silent=True, args=[ 'show', diff --git a/tests/format/optionos.py b/tests/format/optionos.py index 989989a99..a4e2a6cf9 100644 --- a/tests/format/optionos.py +++ b/tests/format/optionos.py @@ -1,7 +1,6 @@ # Pylint doesn't play well with fixtures and dependency injection from pytest # pylint: disable=redefined-outer-name -from contextlib import contextmanager import os import pytest @@ -10,27 +9,13 @@ from buildstream import _yaml from buildstream._exceptions import ErrorDomain, LoadErrorReason from buildstream.plugintestutils.runcli import cli # pylint: disable=unused-import -DATA_DIR = os.path.dirname(os.path.realpath(__file__)) - - -@contextmanager -def override_uname_os(name): - orig_uname = os.uname - orig_tuple = tuple(os.uname()) - override_result = (name, orig_tuple[1], - orig_tuple[2], orig_tuple[3], - orig_tuple[4]) +from tests.testutils import override_os_uname - def override(): - return override_result - - os.uname = override - yield - os.uname = orig_uname +DATA_DIR = os.path.dirname(os.path.realpath(__file__)) @pytest.mark.datafiles(DATA_DIR) -@pytest.mark.parametrize("uname,value,expected", [ +@pytest.mark.parametrize("system,value,expected", [ # Test explicitly provided arches ('Darwin', 'Linux', 'Linuxy'), ('SunOS', 'FreeBSD', 'FreeBSDy'), @@ -44,8 +29,8 @@ def override_uname_os(name): ('AIX', 'Linux', 'Linuxy'), ('HaikuOS', 'SunOS', 'SunOSy'), ]) -def test_conditionals(cli, datafiles, uname, value, expected): - with override_uname_os(uname): +def test_conditionals(cli, datafiles, system, value, expected): + with override_os_uname(system=system): project = os.path.join(datafiles.dirname, datafiles.basename, 'option-os') bst_args = [] @@ -68,7 +53,7 @@ def test_conditionals(cli, datafiles, uname, value, expected): @pytest.mark.datafiles(DATA_DIR) def test_unsupported_arch(cli, datafiles): - with override_uname_os("AIX"): + with override_os_uname(system="AIX"): project = os.path.join(datafiles.dirname, datafiles.basename, 'option-os') result = cli.run(project=project, silent=True, args=[ 'show', 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 |