Fixes previously introduced bug
[electrum-nvc.git] / lib / tests / test_simple_config.py
index cee5211..e7c13a7 100644 (file)
@@ -1,10 +1,13 @@
+import ast
 import sys
+import os
 import unittest
 import tempfile
 import shutil
 
 from StringIO import StringIO
-from lib.simple_config import SimpleConfig
+from lib.simple_config import (SimpleConfig, read_system_config,
+                               read_user_config)
 
 
 class Test_SimpleConfig(unittest.TestCase):
@@ -132,3 +135,106 @@ class Test_SimpleConfig(unittest.TestCase):
                               read_user_dir_function=read_user_dir)
         config.set_key("electrum_path", another_path)
         self.assertEqual(another_path, config.get("electrum_path"))
+
+    def test_user_config_is_not_written_with_system_config(self):
+        """The user config does not contain command-line options when saved."""
+        fake_read_system = lambda : {"something": "b"}
+        fake_read_user = lambda _: {"something": "a"}
+        read_user_dir = lambda : self.user_dir
+        self.options.update({"something": "c"})
+        config = SimpleConfig(options=self.options,
+                              read_system_config_function=fake_read_system,
+                              read_user_config_function=fake_read_user,
+                              read_user_dir_function=read_user_dir)
+        config.save_user_config()
+        contents = None
+        with open(os.path.join(self.electrum_dir, "config"), "r") as f:
+            contents = f.read()
+        result = ast.literal_eval(contents)
+        self.assertEqual({"something": "a"}, result)
+
+
+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):
+        super(TestSystemConfig, self).tearDown()
+        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)
+
+
+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)