[tor-commits] [stem/master] Adding the mock module as a test dependency

atagar at torproject.org atagar at torproject.org
Thu Jun 13 16:50:53 UTC 2013


commit 606ccfbb5001d5a13bce8b7a51cea283c074f3c1
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jun 9 12:14:56 2013 -0700

    Adding the mock module as a test dependency
    
    Mocking is hard. We've gotten a lot of mileage by writing our own mocking
    helpers, but they're clunky and complicated. It's about time for us to use a
    more standard, mature option.
    
    The pypi mock module is not only well documented, but looks to be the direction
    the python world is going. As of python 3.3 it's now part of the standard
    library.
    
    As the first step of swapping us over I'm adding it as a dependency for
    run_tests.py. This warns the user if it's unavailable...
    
      atagar at morrigan:~/Desktop/stem$ ./run_tests.py --unit
      To run stem's tests you'll need mock...
    
      https://pypi.python.org/pypi/mock/
    
      You can get it by running 'sudo pip install mock'.
---
 run_tests.py      |   17 +++++++++++++++++
 stem/prereq.py    |   20 ++++++++++++++++++++
 test/settings.cfg |    1 +
 test/util.py      |    8 ++++++++
 4 files changed, 46 insertions(+)

diff --git a/run_tests.py b/run_tests.py
index 6f77b0f..7bcbe5e 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -75,6 +75,12 @@ LOG_TYPE_ERROR = """\
   TRACE, DEBUG, INFO, NOTICE, WARN, ERROR
 """
 
+MOCK_UNAVAILABLE_MSG = """\
+To run stem's tests you'll need mock...
+
+https://pypi.python.org/pypi/mock/
+"""
+
 
 def main():
   start_time = time.time()
@@ -104,11 +110,22 @@ def main():
     println("Nothing to run (for usage provide --help)\n")
     sys.exit()
 
+  if not stem.prereq.is_mock_available():
+    println(MOCK_UNAVAILABLE_MSG)
+
+    if stem.util.system.is_available('pip'):
+      println("You can get it by running 'sudo pip install mock'.")
+    elif stem.util.system.is_available('apt-get'):
+      println("You can get it by running 'sudo apt-get install python-mock'.")
+
+    sys.exit(1)
+
   test.util.run_tasks(
     "INITIALISING",
     Task("checking stem version", test.util.check_stem_version),
     Task("checking python version", test.util.check_python_version),
     Task("checking pycrypto version", test.util.check_pycrypto_version),
+    Task("checking mock version", test.util.check_mock_version),
     Task("checking pyflakes version", test.util.check_pyflakes_version),
     Task("checking pep8 version", test.util.check_pep8_version),
     Task("checking for orphaned .pyc files", test.util.clean_orphaned_pyc, (SRC_PATHS,)),
diff --git a/stem/prereq.py b/stem/prereq.py
index 2104cd5..148b6f8 100644
--- a/stem/prereq.py
+++ b/stem/prereq.py
@@ -22,6 +22,7 @@ Checks for stem dependencies. We require python 2.6 or greater (including the
 import sys
 
 IS_CRYPTO_AVAILABLE = None
+IS_MOCK_AVAILABLE = None
 
 
 def check_requirements():
@@ -85,3 +86,22 @@ def is_crypto_available():
       log.log_once("stem.prereq.is_crypto_available", log.INFO, msg)
 
   return IS_CRYPTO_AVAILABLE
+
+
+def is_mock_available():
+  """
+  Checks if the mock module is available.
+
+  :returns: **True** if the mock module is available and **False** otherwise
+  """
+
+  global IS_MOCK_AVAILABLE
+
+  if IS_MOCK_AVAILABLE is None:
+    try:
+      import mock
+      IS_MOCK_AVAILABLE = True
+    except ImportError:
+      IS_MOCK_AVAILABLE = False
+
+  return IS_MOCK_AVAILABLE
diff --git a/test/settings.cfg b/test/settings.cfg
index db318e4..96fc407 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -131,6 +131,7 @@ pep8.ignore E127
 
 pyflakes.ignore stem/prereq.py => 'RSA' imported but unused
 pyflakes.ignore stem/prereq.py => 'asn1' imported but unused
+pyflakes.ignore stem/prereq.py => 'mock' imported but unused
 pyflakes.ignore stem/prereq.py => 'long_to_bytes' imported but unused
 pyflakes.ignore stem/descriptor/__init__.py => redefinition of unused 'OrderedDict' from line 61
 pyflakes.ignore stem/util/str_tools.py => redefinition of function '_to_bytes_impl' from line 51
diff --git a/test/util.py b/test/util.py
index 4cdbdc2..4ab7460 100644
--- a/test/util.py
+++ b/test/util.py
@@ -378,6 +378,14 @@ def check_pycrypto_version():
     return "missing"
 
 
+def check_mock_version():
+  if stem.prereq.is_mock_available():
+    import mock
+    return mock.__version__
+  else:
+    return "missing"
+
+
 def check_pyflakes_version():
   try:
     import pyflakes





More information about the tor-commits mailing list