Added tests for the system config function.
This commit is contained in:
parent
221fa5848f
commit
ad3640d7a4
|
@ -135,10 +135,10 @@ class SimpleConfig(object):
|
||||||
import stat
|
import stat
|
||||||
os.chmod(path, stat.S_IREAD | stat.S_IWRITE)
|
os.chmod(path, stat.S_IREAD | stat.S_IWRITE)
|
||||||
|
|
||||||
def read_system_config():
|
def read_system_config(path=SYSTEM_CONFIG_PATH):
|
||||||
"""Parse and return the system config settings in /etc/electrum.conf."""
|
"""Parse and return the system config settings in /etc/electrum.conf."""
|
||||||
result = {}
|
result = {}
|
||||||
if os.path.exists(SYSTEM_CONFIG_PATH):
|
if os.path.exists(path):
|
||||||
try:
|
try:
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -146,13 +146,13 @@ def read_system_config():
|
||||||
return
|
return
|
||||||
|
|
||||||
p = ConfigParser.ConfigParser()
|
p = ConfigParser.ConfigParser()
|
||||||
p.read(SYSTEM_CONFIG_PATH)
|
|
||||||
result = {}
|
|
||||||
try:
|
try:
|
||||||
|
p.read(path)
|
||||||
for k, v in p.items('client'):
|
for k, v in p.items('client'):
|
||||||
result[k] = v
|
result[k] = v
|
||||||
except ConfigParser.NoSectionError:
|
except (ConfigParser.NoSectionError, ConfigParser.MissingSectionHeaderError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def read_user_config(path):
|
def read_user_config(path):
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
import tempfile
|
import tempfile
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
from lib.simple_config import SimpleConfig
|
from lib.simple_config import SimpleConfig, read_system_config
|
||||||
|
|
||||||
|
|
||||||
class Test_SimpleConfig(unittest.TestCase):
|
class Test_SimpleConfig(unittest.TestCase):
|
||||||
|
@ -132,3 +133,41 @@ class Test_SimpleConfig(unittest.TestCase):
|
||||||
read_user_dir_function=read_user_dir)
|
read_user_dir_function=read_user_dir)
|
||||||
config.set_key("electrum_path", another_path)
|
config.set_key("electrum_path", another_path)
|
||||||
self.assertEqual(another_path, config.get("electrum_path"))
|
self.assertEqual(another_path, config.get("electrum_path"))
|
||||||
|
|
||||||
|
|
||||||
|
class TestSystemConfig(unittest.TestCase):
|
||||||
|
|
||||||
|
sample_conf = """
|
||||||
|
[client]
|
||||||
|
gap_limit = 5
|
||||||
|
|
||||||
|
[something_else]
|
||||||
|
everything = 42
|
||||||
|
"""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestSystemConfig, self).setUp()
|
||||||
|
self.thefile = tempfile.mkstemp(suffix=".electrum.test.conf")[1]
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
os.remove(self.thefile)
|
||||||
|
|
||||||
|
def test_read_system_config_file_does_not_exist(self):
|
||||||
|
somefile = "/foo/I/do/not/exist/electrum.conf"
|
||||||
|
result = read_system_config(somefile)
|
||||||
|
self.assertEqual({}, result)
|
||||||
|
|
||||||
|
def test_read_system_config_file_returns_file_options(self):
|
||||||
|
with open(self.thefile, "w") as f:
|
||||||
|
f.write(self.sample_conf)
|
||||||
|
|
||||||
|
result = read_system_config(self.thefile)
|
||||||
|
self.assertEqual({"gap_limit": "5"}, result)
|
||||||
|
|
||||||
|
def test_read_system_config_file_no_sections(self):
|
||||||
|
|
||||||
|
with open(self.thefile, "w") as f:
|
||||||
|
f.write("gap_limit = 5") # The file has no sections at all
|
||||||
|
|
||||||
|
result = read_system_config(self.thefile)
|
||||||
|
self.assertEqual({}, result)
|
||||||
|
|
Loading…
Reference in New Issue