From ef051757137c95a0f929f7e2f4b71b1e1efc06a2 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 9 May 2014 07:44:37 +0200 Subject: [PATCH] Add test helper to set temporary value --- qutebrowser/test/helpers.py | 35 ++++++++++++++++++++++++++ qutebrowser/test/test_helpers.py | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 qutebrowser/test/helpers.py create mode 100644 qutebrowser/test/test_helpers.py diff --git a/qutebrowser/test/helpers.py b/qutebrowser/test/helpers.py new file mode 100644 index 000000000..3215239fb --- /dev/null +++ b/qutebrowser/test/helpers.py @@ -0,0 +1,35 @@ +# Copyright 2014 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Helpers needed by tests.""" + +import os +from contextlib import contextmanager + +@contextmanager +def environ_set_temp(name, value): + """Set a temporary environment variable.""" + try: + oldval = os.environ[name] + except KeyError: + oldval = None + os.environ[name] = value + yield + if oldval is not None: + os.environ[name] = oldval + else: + del os.environ[name] diff --git a/qutebrowser/test/test_helpers.py b/qutebrowser/test/test_helpers.py new file mode 100644 index 000000000..5792e366f --- /dev/null +++ b/qutebrowser/test/test_helpers.py @@ -0,0 +1,42 @@ +# Copyright 2014 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Test test helpers.""" + +import os +import unittest +from unittest import TestCase + +from qutebrowser.test.helpers import environ_set_temp + +class TestEnvironSetTemp(TestCase): + + def test_environ_set(self): + os.environ['QUTEBROWSER_ENVIRON_TEST'] = 'oldval' + with environ_set_temp('QUTEBROWSER_ENVIRON_TEST', 'newval'): + self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'newval') + self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'oldval') + + def test_environ_unset(self): + with environ_set_temp('QUTEBROWSER_ENVIRON_TEST', 'newval'): + self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'newval') + self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ) + + def tearDown(self): + if 'QUTEBROWSER_ENVIRON_TEST' in os.environ: + # if some test failed + del os.environ['QUTEBROWSER_ENVIRON_TEST']