Added tests for the system config function.
[electrum-nvc.git] / lib / tests / test_simple_config.py
index 8704838..f288b7f 100644 (file)
@@ -1,10 +1,11 @@
 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
 
 
 class Test_SimpleConfig(unittest.TestCase):
@@ -57,9 +58,22 @@ class Test_SimpleConfig(unittest.TestCase):
         self.assertEqual(self.options.get("electrum_path"),
                          config.get("electrum_path"))
 
+    def test_simple_config_system_config_ignored_if_portable(self):
+        """If electrum is started with the "portable" flag, system
+        configuration is completely ignored."""
+        another_path = tempfile.mkdtemp()
+        fake_read_system = lambda : {"electrum_path": self.electrum_dir}
+        fake_read_user = lambda _: {"electrum_path": another_path}
+        read_user_dir = lambda : self.user_dir
+        config = SimpleConfig(options={"portable": True},
+                              read_system_config_function=fake_read_system,
+                              read_user_config_function=fake_read_user,
+                              read_user_dir_function=read_user_dir)
+        self.assertEqual(another_path, config.get("electrum_path"))
+
     def test_simple_config_user_config_is_used_if_others_arent_specified(self):
-        """Options passed by command line override all other configuration
-        sources"""
+        """If no system-wide configuration and no command-line options are
+        specified, the user configuration is used instead."""
         fake_read_system = lambda : {}
         fake_read_user = lambda _: {"electrum_path": self.electrum_dir}
         read_user_dir = lambda : self.user_dir
@@ -105,3 +119,55 @@ 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_can_set_options_from_system_config_if_portable(self):
+        """If the "portable" flag is set, the user can overwrite system
+        configuration options."""
+        another_path = tempfile.mkdtemp()
+        fake_read_system = lambda : {"electrum_path": self.electrum_dir}
+        fake_read_user = lambda _: {}
+        read_user_dir = lambda : self.user_dir
+        config = SimpleConfig(options={"portable": True},
+                              read_system_config_function=fake_read_system,
+                              read_user_config_function=fake_read_user,
+                              read_user_dir_function=read_user_dir)
+        config.set_key("electrum_path", another_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)