Added more tests for user config parsing.
This commit is contained in:
parent
ad3640d7a4
commit
34f0a65c49
|
@ -157,19 +157,22 @@ def read_system_config(path=SYSTEM_CONFIG_PATH):
|
||||||
|
|
||||||
def read_user_config(path):
|
def read_user_config(path):
|
||||||
"""Parse and store the user config settings in electrum.conf into user_config[]."""
|
"""Parse and store the user config settings in electrum.conf into user_config[]."""
|
||||||
if not path: return
|
if not path: return {} # Return a dict, since we will call update() on it.
|
||||||
|
|
||||||
config_path = os.path.join(path, "config")
|
config_path = os.path.join(path, "config")
|
||||||
|
result = {}
|
||||||
if os.path.exists(config_path):
|
if os.path.exists(config_path):
|
||||||
try:
|
try:
|
||||||
|
|
||||||
with open(config_path, "r") as f:
|
with open(config_path, "r") as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
except IOError:
|
result = ast.literal_eval( data ) #parse raw data from reading wallet file
|
||||||
return
|
|
||||||
try:
|
|
||||||
d = ast.literal_eval( data ) #parse raw data from reading wallet file
|
|
||||||
except Exception:
|
except Exception:
|
||||||
print_msg("Error: Cannot read config file.")
|
print_msg("Error: Cannot read config file.")
|
||||||
return
|
result = {}
|
||||||
|
|
||||||
return d
|
if not type(result) is dict:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
|
@ -5,7 +5,8 @@ import tempfile
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
from lib.simple_config import SimpleConfig, read_system_config
|
from lib.simple_config import (SimpleConfig, read_system_config,
|
||||||
|
read_user_config)
|
||||||
|
|
||||||
|
|
||||||
class Test_SimpleConfig(unittest.TestCase):
|
class Test_SimpleConfig(unittest.TestCase):
|
||||||
|
@ -150,6 +151,7 @@ everything = 42
|
||||||
self.thefile = tempfile.mkstemp(suffix=".electrum.test.conf")[1]
|
self.thefile = tempfile.mkstemp(suffix=".electrum.test.conf")[1]
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
super(TestSystemConfig, self).tearDown()
|
||||||
os.remove(self.thefile)
|
os.remove(self.thefile)
|
||||||
|
|
||||||
def test_read_system_config_file_does_not_exist(self):
|
def test_read_system_config_file_does_not_exist(self):
|
||||||
|
@ -171,3 +173,50 @@ everything = 42
|
||||||
|
|
||||||
result = read_system_config(self.thefile)
|
result = read_system_config(self.thefile)
|
||||||
self.assertEqual({}, result)
|
self.assertEqual({}, result)
|
||||||
|
|
||||||
|
|
||||||
|
class TestUserConfig(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestUserConfig, self).setUp()
|
||||||
|
self._saved_stdout = sys.stdout
|
||||||
|
self._stdout_buffer = StringIO()
|
||||||
|
sys.stdout = self._stdout_buffer
|
||||||
|
|
||||||
|
self.user_dir = tempfile.mkdtemp()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
super(TestUserConfig, self).tearDown()
|
||||||
|
shutil.rmtree(self.user_dir)
|
||||||
|
sys.stdout = self._saved_stdout
|
||||||
|
|
||||||
|
def test_no_path_means_no_result(self):
|
||||||
|
result = read_user_config(None)
|
||||||
|
self.assertEqual({}, result)
|
||||||
|
|
||||||
|
def test_path_with_reprd_dict(self):
|
||||||
|
thefile = os.path.join(self.user_dir, "config")
|
||||||
|
payload = {"gap_limit": 5}
|
||||||
|
with open(thefile, "w") as f:
|
||||||
|
f.write(repr(payload))
|
||||||
|
|
||||||
|
result = read_user_config(self.user_dir)
|
||||||
|
self.assertEqual(payload, result)
|
||||||
|
|
||||||
|
def test_path_without_config_file(self):
|
||||||
|
"""We pass a path but if does not contain a "config" file."""
|
||||||
|
result = read_user_config(self.user_dir)
|
||||||
|
self.assertEqual({}, result)
|
||||||
|
|
||||||
|
def test_path_with_reprd_object(self):
|
||||||
|
|
||||||
|
class something(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
thefile = os.path.join(self.user_dir, "config")
|
||||||
|
payload = something()
|
||||||
|
with open(thefile, "w") as f:
|
||||||
|
f.write(repr(payload))
|
||||||
|
|
||||||
|
result = read_user_config(self.user_dir)
|
||||||
|
self.assertEqual({}, result)
|
||||||
|
|
Loading…
Reference in New Issue