[tor-commits] [stem/master] Using python3's unittest.mock module when available
atagar at torproject.org
atagar at torproject.org
Mon Oct 14 00:42:15 UTC 2013
commit e89c829fb75331fdc0d5aa03475c751371cb6c7e
Author: Damian Johnson <atagar at torproject.org>
Date: Sat Oct 12 16:57:50 2013 -0700
Using python3's unittest.mock module when available
Guess it's been a while since I last tried to test under python3. From python
3.3 on up the Mock library is built into the unittest module. Accounting for
this so we can test under python3 again.
---
run_tests.py | 6 +++++-
stem/prereq.py | 19 ++++++++++++++++++-
test/integ/process.py | 6 +++++-
test/integ/response/protocolinfo.py | 8 ++++++--
test/integ/util/system.py | 6 +++++-
test/settings.cfg | 2 ++
test/unit/connection/authentication.py | 8 ++++++--
test/unit/control/controller.py | 8 ++++++--
test/unit/descriptor/export.py | 8 ++++++--
test/unit/descriptor/reader.py | 6 +++++-
test/unit/descriptor/remote.py | 6 +++++-
test/unit/descriptor/server_descriptor.py | 8 ++++++--
test/unit/response/events.py | 8 ++++++--
test/unit/response/protocolinfo.py | 8 ++++++--
test/unit/tutorial.py | 8 ++++++--
test/unit/util/connection.py | 8 ++++++--
test/unit/util/proc.py | 8 ++++++--
test/unit/util/system.py | 8 ++++++--
test/unit/version.py | 8 ++++++--
test/util.py | 6 +++++-
20 files changed, 122 insertions(+), 31 deletions(-)
diff --git a/run_tests.py b/run_tests.py
index 0b590a1..08143c1 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -119,7 +119,11 @@ def main():
if not stem.prereq.is_mock_available():
try:
- import mock
+ try:
+ import unittest.mock
+ except ImportError:
+ import mock
+
println(MOCK_OUT_OF_DATE_MSG % mock.__version__)
except ImportError:
println(MOCK_UNAVAILABLE_MSG)
diff --git a/stem/prereq.py b/stem/prereq.py
index d088480..22b5819 100644
--- a/stem/prereq.py
+++ b/stem/prereq.py
@@ -91,12 +91,29 @@ def is_crypto_available():
@lru_cache()
def is_mock_available():
"""
- Checks if the mock module is available.
+ Checks if the mock module is available. In python 3.3 and up it is a builtin
+ unittest module, but before this it needed to be `installed separately
+ <https://pypi.python.org/pypi/mock/>`_. Imports should be as follows....
+
+ ::
+
+ try:
+ # added in python 3.3
+ from unittest.mock import Mock
+ except ImportError:
+ from mock import Mock
:returns: **True** if the mock module is available and **False** otherwise
"""
try:
+ # checks for python 3.3 version
+ import unittest.mock
+ return True
+ except ImportError:
+ pass
+
+ try:
import mock
# check for mock's patch.dict() which was introduced in version 0.7.0
diff --git a/test/integ/process.py b/test/integ/process.py
index f6a862a..0f309c8 100644
--- a/test/integ/process.py
+++ b/test/integ/process.py
@@ -15,7 +15,11 @@ import stem.util.system
import stem.version
import test.runner
-from mock import patch
+try:
+ # added in python 3.3
+ from unittest.mock import patch
+except ImportError:
+ from mock import patch
class TestProcess(unittest.TestCase):
diff --git a/test/integ/response/protocolinfo.py b/test/integ/response/protocolinfo.py
index 98f0c03..6469b8d 100644
--- a/test/integ/response/protocolinfo.py
+++ b/test/integ/response/protocolinfo.py
@@ -11,10 +11,14 @@ import stem.util.system
import stem.version
import test.runner
-from mock import Mock, patch
-
from test.integ.util.system import filter_system_call
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
+
class TestProtocolInfo(unittest.TestCase):
def test_parsing(self):
diff --git a/test/integ/util/system.py b/test/integ/util/system.py
index 0694ffc..df389b1 100644
--- a/test/integ/util/system.py
+++ b/test/integ/util/system.py
@@ -12,7 +12,11 @@ import stem.util.proc
import stem.util.system
import test.runner
-from mock import Mock, patch
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
def filter_system_call(prefixes):
diff --git a/test/settings.cfg b/test/settings.cfg
index 9838012..222a7a4 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -129,9 +129,11 @@ pep8.ignore E127
# False positives from pyflakes. These are mappings between the path and the
# issue.
+pyflakes.ignore run_tests.py => 'unittest' imported but unused
pyflakes.ignore stem/control.py => undefined name 'control'
pyflakes.ignore stem/prereq.py => 'RSA' imported but unused
pyflakes.ignore stem/prereq.py => 'asn1' imported but unused
+pyflakes.ignore stem/prereq.py => 'unittest' 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 62
diff --git a/test/unit/connection/authentication.py b/test/unit/connection/authentication.py
index 2157a5b..4b50bcf 100644
--- a/test/unit/connection/authentication.py
+++ b/test/unit/connection/authentication.py
@@ -13,11 +13,15 @@ import unittest
import stem.connection
-from mock import Mock, patch
-
from stem.util import log
from test import mocking
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
+
class TestAuthenticate(unittest.TestCase):
@patch('stem.connection.get_protocolinfo')
diff --git a/test/unit/control/controller.py b/test/unit/control/controller.py
index 7095f5a..14dceea 100644
--- a/test/unit/control/controller.py
+++ b/test/unit/control/controller.py
@@ -12,13 +12,17 @@ import stem.socket
import stem.util.system
import stem.version
-from mock import Mock, patch
-
from stem import InvalidArguments, InvalidRequest, ProtocolError, UnsatisfiableRequest
from stem.control import _parse_circ_path, Controller, EventType
from stem.exit_policy import ExitPolicy
from test import mocking
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
+
class TestControl(unittest.TestCase):
def setUp(self):
diff --git a/test/unit/descriptor/export.py b/test/unit/descriptor/export.py
index 32a7809..d2eeaa0 100644
--- a/test/unit/descriptor/export.py
+++ b/test/unit/descriptor/export.py
@@ -8,13 +8,17 @@ import unittest
import stem.prereq
import test.runner
-from mock import Mock, patch
-
from stem.descriptor.export import export_csv, export_csv_file
from test.mocking import get_relay_server_descriptor, \
get_bridge_server_descriptor
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
+
class TestExport(unittest.TestCase):
@patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock())
diff --git a/test/unit/descriptor/reader.py b/test/unit/descriptor/reader.py
index 826f436..62b561b 100644
--- a/test/unit/descriptor/reader.py
+++ b/test/unit/descriptor/reader.py
@@ -7,7 +7,11 @@ import unittest
import stem.descriptor.reader
-from mock import patch
+try:
+ # added in python 3.3
+ from unittest.mock import patch
+except ImportError:
+ from mock import patch
class TestDescriptorReader(unittest.TestCase):
diff --git a/test/unit/descriptor/remote.py b/test/unit/descriptor/remote.py
index e20dd06..4eda9e8 100644
--- a/test/unit/descriptor/remote.py
+++ b/test/unit/descriptor/remote.py
@@ -8,7 +8,11 @@ import unittest
import stem.descriptor.remote
-from mock import patch
+try:
+ # added in python 3.3
+ from unittest.mock import patch
+except ImportError:
+ from mock import patch
# Output from requesting moria1's descriptor from itself...
# % curl http://128.31.0.39:9131/tor/server/fp/9695DFC35FFEB861329B9F1AB04C46397020CE31
diff --git a/test/unit/descriptor/server_descriptor.py b/test/unit/descriptor/server_descriptor.py
index 50f27f6..b0af7cc 100644
--- a/test/unit/descriptor/server_descriptor.py
+++ b/test/unit/descriptor/server_descriptor.py
@@ -11,14 +11,18 @@ import stem.exit_policy
import stem.prereq
import stem.util.str_tools
-from mock import Mock, patch
-
from stem.descriptor.server_descriptor import RelayDescriptor, BridgeDescriptor
from test.mocking import get_relay_server_descriptor, \
get_bridge_server_descriptor, \
CRYPTO_BLOB
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
+
class TestServerDescriptor(unittest.TestCase):
@patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock())
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
index 23b7006..281dbec 100644
--- a/test/unit/response/events.py
+++ b/test/unit/response/events.py
@@ -10,11 +10,15 @@ import stem.response
import stem.response.events
import stem.util.log
-from mock import Mock
-
from stem import * # enums and exceptions
from test import mocking
+try:
+ # added in python 3.3
+ from unittest.mock import Mock
+except ImportError:
+ from mock import Mock
+
# ADDRMAP event
ADDRMAP = '650 ADDRMAP www.atagar.com 75.119.206.243 "2012-11-19 00:50:13" \
diff --git a/test/unit/response/protocolinfo.py b/test/unit/response/protocolinfo.py
index 30b218d..0eed900 100644
--- a/test/unit/response/protocolinfo.py
+++ b/test/unit/response/protocolinfo.py
@@ -12,11 +12,15 @@ import stem.util.proc
import stem.util.system
import stem.version
-from mock import Mock, patch
-
from stem.response.protocolinfo import AuthMethod
from test import mocking
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
+
NO_AUTH = """250-PROTOCOLINFO 1
250-AUTH METHODS=NULL
250-VERSION Tor="0.2.1.30"
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 7a582be..8c76de7 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -6,13 +6,17 @@ import io
import StringIO
import unittest
-from mock import Mock, patch
-
from stem.control import Controller
from stem.descriptor.reader import DescriptorReader
from stem.descriptor.server_descriptor import RelayDescriptor
from test import mocking
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
+
MIRROR_MIRROR_OUTPUT = """\
1. speedyexit (102.13 KB/s)
2. speedyexit (102.13 KB/s)
diff --git a/test/unit/util/connection.py b/test/unit/util/connection.py
index 1d9e6f6..e9c8980 100644
--- a/test/unit/util/connection.py
+++ b/test/unit/util/connection.py
@@ -5,12 +5,16 @@ Unit tests for the stem.util.connection functions.
import platform
import unittest
-from mock import patch
-
import stem.util.connection
from stem.util.connection import Resolver, Connection
+try:
+ # added in python 3.3
+ from unittest.mock import patch
+except ImportError:
+ from mock import patch
+
NETSTAT_OUTPUT = """\
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
diff --git a/test/unit/util/proc.py b/test/unit/util/proc.py
index caf9d7a..55ed9e7 100644
--- a/test/unit/util/proc.py
+++ b/test/unit/util/proc.py
@@ -5,11 +5,15 @@ Unit testing code for the stem.util.proc functions.
import StringIO
import unittest
-from mock import Mock, patch
-
from stem.util import proc
from test import mocking
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
+
class TestProc(unittest.TestCase):
@patch('stem.util.proc._get_line')
diff --git a/test/unit/util/system.py b/test/unit/util/system.py
index 36204f6..d36546d 100644
--- a/test/unit/util/system.py
+++ b/test/unit/util/system.py
@@ -10,10 +10,14 @@ import ntpath
import posixpath
import unittest
-from mock import Mock, patch
-
from stem.util import system
+try:
+ # added in python 3.3
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch
+
# Base responses for the get_pid_by_name tests. The 'success' and
# 'multiple_results' entries are filled in by tests.
diff --git a/test/unit/version.py b/test/unit/version.py
index 273d3e8..77551aa 100644
--- a/test/unit/version.py
+++ b/test/unit/version.py
@@ -7,10 +7,14 @@ import unittest
import stem.util.system
import stem.version
-from mock import patch
-
from stem.version import Version
+try:
+ # added in python 3.3
+ from unittest.mock import patch
+except ImportError:
+ from mock import patch
+
TOR_VERSION_OUTPUT = """Mar 22 23:09:37.088 [notice] Tor v0.2.2.35 \
(git-73ff13ab3cc9570d). This is experimental software. Do not rely on it for \
strong anonymity. (Running on Linux i686)
diff --git a/test/util.py b/test/util.py
index 3746e55..fc9386e 100644
--- a/test/util.py
+++ b/test/util.py
@@ -380,7 +380,11 @@ def check_pycrypto_version():
def check_mock_version():
if stem.prereq.is_mock_available():
- import mock
+ try:
+ import unittest.mock as mock
+ except ImportError:
+ import mock
+
return mock.__version__
else:
return "missing"
More information about the tor-commits
mailing list