From 4f9593a5530169390eec4168018f06f5a659188a Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Mon, 27 Sep 2021 16:02:17 +0530 Subject: [PATCH] Added readfp function for python3, updated StringIO & readfb based changes in setUp, teadDown and test_reset functions --- src/bmconfigparser.py | 2 + src/tests/test_config.py | 78 ++++++++++++-------------- src/tests/test_pattern/test_config.ini | 27 --------- 3 files changed, 37 insertions(+), 70 deletions(-) delete mode 100755 src/tests/test_pattern/test_config.ini diff --git a/src/bmconfigparser.py b/src/bmconfigparser.py index 1c830ab8..ae0b46fe 100644 --- a/src/bmconfigparser.py +++ b/src/bmconfigparser.py @@ -200,6 +200,8 @@ class BMConfigParser(SafeConfigParser): except configparser.InterpolationError: continue + def readfp(self, file_obj): + SafeConfigParser.read_file(self, file_obj) else: @staticmethod def addresses(): diff --git a/src/tests/test_config.py b/src/tests/test_config.py index 2c52c73c..dcde4e6f 100644 --- a/src/tests/test_config.py +++ b/src/tests/test_config.py @@ -7,54 +7,52 @@ import tempfile import sys import os +from six import StringIO from pybitmessage import paths from pybitmessage.bmconfigparser import BMConfigParser -test_config = { - "bitmessagesettings": { - "maxaddrperstreamsend": 100, - "maxbootstrapconnections": 20, - "maxdownloadrate": 0, - "maxoutboundconnections": 8, - "maxtotalconnections": 200, - "maxuploadrate": 0, - "apiinterface": "127.0.0.1", - "apiport": 8442, - "udp": "True" - }, - "threads": { - "receive": 3, - }, - "network": { - "bind": "", - "dandelion": 90, - }, - "inventory": { - "storage": "sqlite", - "acceptmismatch": "False", - }, - "knownnodes": { - "maxnodes": 20000, - }, - "zlib": { - "maxsize": 1048576 - } -} +test_config = """[bitmessagesettings] +maxaddrperstreamsend = 100 +maxbootstrapconnections = 10 +maxdownloadrate = 0 +maxoutboundconnections = 8 +maxtotalconnections = 100 +maxuploadrate = 0 +apiinterface = 127.0.0.1 +apiport = 8442 +udp = True + +[threads] +receive = 3 + +[network] +bind = None +dandelion = 90 + +[inventory] +storage = sqlite +acceptmismatch = False + +[knownnodes] +maxnodes = 15000 + +[zlib] +maxsize = 1048576""" class TestConfig(unittest.TestCase): """A test case for bmconfigparser""" - config_backup = tempfile.NamedTemporaryFile(suffix='.cfg').name + configfile = StringIO('') def setUp(self): """creates a backup of BMConfigparser current state""" - with open(self.config_backup, 'wb') as configfile: - BMConfigParser().write(configfile) + BMConfigParser().write(self.configfile) + self.configfile.seek(0) def tearDown(self): """restore to the backup of BMConfigparser""" - BMConfigParser().read(self.config_backup) - os.remove(self.config_backup) + BMConfigParser()._reset() + BMConfigParser().readfp(self.configfile) def test_safeGet(self): """safeGet retuns provided default for nonexistent option or None""" @@ -70,7 +68,6 @@ class TestConfig(unittest.TestCase): False ) # no arg for default - # pylint: disable=too-many-function-args with self.assertRaises(TypeError): BMConfigParser().safeGetBoolean( 'nonexistent', 'nonexistent', True) @@ -91,13 +88,8 @@ class TestConfig(unittest.TestCase): def test_reset(self): """safeGetInt retuns provided default for bitmessagesettings option or 0""" - - BMConfigParser().read( - os.path.join( - os.path.abspath(os.path.dirname(__file__)), - 'test_pattern', 'test_config.ini' - ) - ) + test_config_object = StringIO(test_config) + BMConfigParser().readfp(test_config_object) self.assertEqual( BMConfigParser().safeGetInt('bitmessagesettings', 'maxaddrperstreamsend'), 100) diff --git a/src/tests/test_pattern/test_config.ini b/src/tests/test_pattern/test_config.ini deleted file mode 100755 index e17b4a6d..00000000 --- a/src/tests/test_pattern/test_config.ini +++ /dev/null @@ -1,27 +0,0 @@ -[bitmessagesettings] -maxaddrperstreamsend=100 -maxbootstrapconnections=10 -maxdownloadrate=0 -maxoutboundconnections=8 -maxtotalconnections=100 -maxuploadrate=0 -apiinterface=127.0.0.1 -apiport=8442 -udp=True - -[threads] -receive=3 - -[network] -bind=None -dandelion= 90 - -[inventory] -storage= sqlite -acceptmismatch= False - -[knownnodes] -maxnodes= 15000 - -[zlib] -maxsize= 1048576 \ No newline at end of file