From f8844f4d749139763d08a7602280fc67c88ad1dc Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 29 Jan 2021 17:02:55 +0200 Subject: [PATCH] Use common.skip_python3() to skip tests modules not supporting python3 --- setup.py | 7 +++++++ src/tests/apinotify_handler.py | 2 +- src/tests/common.py | 15 +++++++++++++++ src/tests/test_api.py | 14 +++++++++----- src/tests/test_config.py | 2 +- src/tests/test_crypto.py | 2 ++ src/tests/test_logger.py | 2 +- src/tests/test_networkgroup.py | 4 ++++ src/tests/test_openclpow.py | 5 ++++- src/tests/test_process.py | 7 ++----- src/tests/test_protocol.py | 4 ++++ 11 files changed, 50 insertions(+), 14 deletions(-) diff --git a/setup.py b/setup.py index 7a3bcf6a..570a4224 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ import os import platform import shutil import sys +import unittest from setuptools import setup, Extension from setuptools.command.install import install @@ -45,6 +46,11 @@ class InstallCmd(install): return install.run(self) +def unittest_discover(): + """Explicit test suite creation""" + return unittest.TestLoader().discover('pybitmessage.tests') + + if __name__ == "__main__": here = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(here, 'README.md')) as f: @@ -116,6 +122,7 @@ if __name__ == "__main__": #keywords='', install_requires=installRequires, tests_require=requirements, + test_suite='setup.unittest_discover', extras_require=EXTRAS_REQUIRE, classifiers=[ "License :: OSI Approved :: MIT License" diff --git a/src/tests/apinotify_handler.py b/src/tests/apinotify_handler.py index 4574d46a..d3993f4d 100755 --- a/src/tests/apinotify_handler.py +++ b/src/tests/apinotify_handler.py @@ -7,7 +7,7 @@ when pybitmessage started in test mode. import sys import tempfile -from test_process import put_signal_file +from common import put_signal_file if __name__ == '__main__': diff --git a/src/tests/common.py b/src/tests/common.py index 2f130e25..e87765a4 100644 --- a/src/tests/common.py +++ b/src/tests/common.py @@ -1,4 +1,7 @@ import os +import sys +import time +import unittest _files = ( @@ -17,3 +20,15 @@ def cleanup(home=None, files=_files): os.remove(os.path.join(home, pfile)) except OSError: pass + + +def skip_python3(): + """Raise unittest.SkipTest() if detected python3""" + if sys.hexversion >= 0x3000000: + raise unittest.SkipTest('Module is not ported to python3') + + +def put_signal_file(path, filename): + """Creates file, presence of which is a signal about some event.""" + with open(os.path.join(path, filename), 'wb') as outfile: + outfile.write(b'%i' % time.time()) diff --git a/src/tests/test_api.py b/src/tests/test_api.py index e3756f54..43c97233 100644 --- a/src/tests/test_api.py +++ b/src/tests/test_api.py @@ -5,9 +5,13 @@ Tests using API. import base64 import json import time -import xmlrpclib # nosec -from test_process import TestProcessProto, TestProcessShutdown +try: # nosec + from xmlrpclib import ServerProxy, ProtocolError +except ImportError: + from xmlrpc.client import ServerProxy, ProtocolError + +from .test_process import TestProcessProto, TestProcessShutdown class TestAPIProto(TestProcessProto): @@ -19,7 +23,7 @@ class TestAPIProto(TestProcessProto): """Setup XMLRPC proxy for pybitmessage API""" super(TestAPIProto, cls).setUpClass() cls.addresses = [] - cls.api = xmlrpclib.ServerProxy( + cls.api = ServerProxy( "http://username:password@127.0.0.1:8442/") for _ in range(5): if cls._get_readline('.api_started'): @@ -65,8 +69,8 @@ class TestAPI(TestAPIProto): def test_user_password(self): """Trying to connect with wrong username/password""" - api_wrong = xmlrpclib.ServerProxy("http://test:wrong@127.0.0.1:8442/") - with self.assertRaises(xmlrpclib.ProtocolError): + api_wrong = ServerProxy("http://test:wrong@127.0.0.1:8442/") + with self.assertRaises(ProtocolError): api_wrong.clientStatus() def test_connection(self): diff --git a/src/tests/test_config.py b/src/tests/test_config.py index fcf8c498..a3b90a4c 100644 --- a/src/tests/test_config.py +++ b/src/tests/test_config.py @@ -6,8 +6,8 @@ import os import unittest import tempfile +from .test_process import TestProcessProto from pybitmessage.bmconfigparser import BMConfigParser -from test_process import TestProcessProto class TestConfig(unittest.TestCase): diff --git a/src/tests/test_crypto.py b/src/tests/test_crypto.py index b53105cb..c17bac29 100644 --- a/src/tests/test_crypto.py +++ b/src/tests/test_crypto.py @@ -6,8 +6,10 @@ import hashlib import unittest from abc import ABCMeta, abstractmethod from binascii import hexlify, unhexlify + from pybitmessage.pyelliptic import arithmetic + try: from Crypto.Hash import RIPEMD except ImportError: diff --git a/src/tests/test_logger.py b/src/tests/test_logger.py index a24667fd..da0f9341 100644 --- a/src/tests/test_logger.py +++ b/src/tests/test_logger.py @@ -5,7 +5,7 @@ Testing the logger configuration import os import tempfile -from test_process import TestProcessProto +from .test_process import TestProcessProto class TestLogger(TestProcessProto): diff --git a/src/tests/test_networkgroup.py b/src/tests/test_networkgroup.py index 76cfb033..79163402 100644 --- a/src/tests/test_networkgroup.py +++ b/src/tests/test_networkgroup.py @@ -3,6 +3,10 @@ Test for network group """ import unittest +from .common import skip_python3 + +skip_python3() + class TestNetworkGroup(unittest.TestCase): """ diff --git a/src/tests/test_openclpow.py b/src/tests/test_openclpow.py index 63551ee2..6cfb5bd2 100644 --- a/src/tests/test_openclpow.py +++ b/src/tests/test_openclpow.py @@ -3,9 +3,12 @@ Tests for openclpow module """ import hashlib import unittest - from struct import pack, unpack +from .common import skip_python3 + +skip_python3() # noqa:E402 + from pybitmessage import openclpow diff --git a/src/tests/test_process.py b/src/tests/test_process.py index 5e08ddd0..0888c21d 100644 --- a/src/tests/test_process.py +++ b/src/tests/test_process.py @@ -12,13 +12,10 @@ import unittest import psutil -from common import cleanup +from .common import cleanup, put_signal_file, skip_python3 -def put_signal_file(path, filename): - """Creates file, presence of which is a signal about some event.""" - with open(os.path.join(path, filename), 'wb') as outfile: - outfile.write(b'%i' % time.time()) +skip_python3() class TestProcessProto(unittest.TestCase): diff --git a/src/tests/test_protocol.py b/src/tests/test_protocol.py index 84d87a0f..a3c73a73 100644 --- a/src/tests/test_protocol.py +++ b/src/tests/test_protocol.py @@ -4,6 +4,10 @@ Tests for common protocol functions import unittest +from .common import skip_python3 + +skip_python3() + class TestProtocol(unittest.TestCase): """Main protocol test case"""