Bug 1466211 - Run mozterm tests using pipenv; r?ahal draft
authorDave Hunt <dhunt@mozilla.com>
Wed, 06 Jun 2018 19:43:08 +0100
changeset 804946 4b92b88dba5f9685f6fd697637c4dbbacb1036dc
parent 804945 991f64440a3d0be5ec4c9177b02c07817c147a1c
push id112505
push userbmo:dave.hunt@gmail.com
push dateWed, 06 Jun 2018 20:48:11 +0000
reviewersahal
bugs1466211
milestone62.0a1
Bug 1466211 - Run mozterm tests using pipenv; r?ahal MozReview-Commit-ID: 9WxSwwXkLS2
python/mozterm/test/test_terminal.py
--- a/python/mozterm/test/test_terminal.py
+++ b/python/mozterm/test/test_terminal.py
@@ -2,43 +2,57 @@
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import, unicode_literals
 
 import os
 import sys
 
+from six.moves import builtins
+import blessings
 import mozunit
 import pytest
 
 from mozterm import Terminal, NullTerminal
 
 
+@pytest.fixture
+def without_blessings(monkeypatch):
+    builtins_import = builtins.__import__
+
+    def import_error(name, *args, **kwargs):
+        if name == 'blessings':
+            raise ImportError(name)
+
+        return builtins_import(name, *args, **kwargs)
+
+    monkeypatch.setattr('six.moves.builtins.__import__', import_error)
+
+
 def test_terminal():
-    blessings = pytest.importorskip('blessings')
     term = Terminal()
     assert isinstance(term, blessings.Terminal)
 
+
+def test_terminal_with_disabled_styling():
     term = Terminal(disable_styling=True)
     assert isinstance(term, NullTerminal)
 
-    del sys.modules['blessings']
-    orig = sys.path[:]
-    for path in orig:
-        if 'blessings' in path:
-            sys.path.remove(path)
 
+@pytest.mark.usefixtures('without_blessings')
+def test_terminal_without_blessings():
     term = Terminal()
     assert isinstance(term, NullTerminal)
 
+
+@pytest.mark.usefixtures('without_blessings')
+def test_terminal_without_blessings_raises():
     with pytest.raises(ImportError):
-        term = Terminal(raises=True)
-
-    sys.path = orig
+        Terminal(raises=True)
 
 
 def test_null_terminal():
     term = NullTerminal()
     assert term.red("foo") == "foo"
     assert term.red == ""
     assert term.color(1) == ""
     assert term.number_of_colors == 0