summaryrefslogtreecommitdiff
path: root/tests/testutils/platform.py
diff options
context:
space:
mode:
authorAngelos Evripiotis <jevripiotis@bloomberg.net>2019-03-27 10:58:57 +0000
committerAngelos Evripiotis <angelos.evripiotis@gmail.com>2019-04-11 13:58:33 +0000
commit9a9b06a5b52ced084b447c0468617e963555e068 (patch)
treef587d2f5c5188db0d16dabedfe3ecc9f94857b76 /tests/testutils/platform.py
parent887461a584613c304064a636cbda8f4c863f35f9 (diff)
downloadbuildstream-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/platform.py')
-rw-r--r--tests/testutils/platform.py51
1 files changed, 51 insertions, 0 deletions
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