commit b334679f58c835b7a93b6f93662b7cf7aab80240
Author: Arturo Filastò <art...@filasto.net>
Date:   Fri Sep 2 17:35:36 2016 +0200

    Make settings parsing more robust
    
    * Expand test case to cover more edge cases
---
 ooni/settings.py            | 10 +++++++---
 ooni/tests/test_settings.py | 12 +++++++++---
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/ooni/settings.py b/ooni/settings.py
index 66bd212..9f24f9b 100644
--- a/ooni/settings.py
+++ b/ooni/settings.py
@@ -89,7 +89,7 @@ tor:
     # This is the timeout after which we consider to to not have
     # bootstrapped properly.
     #timeout: 200
-    torrc:
+    #torrc:
         #HTTPProxy: host:port
         #HTTPProxyAuthenticator: user:password
         #HTTPSProxy: host:port
@@ -213,14 +213,18 @@ def _load_config_files_with_defaults(config_files, 
defaults):
             continue
         with open(config_file_path) as in_file:
             c = yaml.safe_load(in_file)
-        config_from_files.update(c)
+        for category in c.keys():
+            if c[category] is None:
+                continue
+            config_from_files[category] = config_from_files.get(category, {})
+            config_from_files[category].update(c[category])
 
     for category in defaults.keys():
         configuration[category] = {}
         for k, v in defaults[category].items():
             try:
                 configuration[category][k] = config_from_files[category][k]
-            except KeyError:
+            except (KeyError, TypeError):
                 configuration[category][k] = defaults[category][k]
     return configuration
 
diff --git a/ooni/tests/test_settings.py b/ooni/tests/test_settings.py
index 30d9e19..b88aa68 100644
--- a/ooni/tests/test_settings.py
+++ b/ooni/tests/test_settings.py
@@ -146,7 +146,8 @@ class TestSettings(ConfigTestCase):
                 'key': 'value'
             },
             'cat2': {
-                'key': 'value'
+                'key': 'value',
+                'key2': 'value2'
             },
             'cat3': {
                 'key': 'value'
@@ -162,11 +163,15 @@ class TestSettings(ConfigTestCase):
             },
             'invalid_category': {
                 'ignored': 'ignored'
-            }
+            },
+            'cat3': None
         }
         config_file_B = {
             'cat1': {
                 'key': 'valueB'
+            },
+            'cat2': {
+                'key2': 'value2B'
             }
         }
         temp_dir = tempfile.mkdtemp()
@@ -188,7 +193,8 @@ class TestSettings(ConfigTestCase):
                 'key': 'valueB'
             },
             'cat2': {
-                'key': 'valueA'
+                'key': 'valueA',
+                'key2': 'value2B'
             },
             'cat3': {
                 'key': 'value'



_______________________________________________
tor-commits mailing list
tor-commits@lists.torproject.org
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits

Reply via email to