Bug 1431024 - Modified error criterion check in prefs.py. r=wlach draft
authorVedant Chakravadhanula <vedantc98@gmail.com>
Fri, 26 Jan 2018 01:33:39 +0530
changeset 747285 bfd87ba6ab455617c80dcadb323e00c2da083a49
parent 723950 b0baaec09caf3e1b30ec6b238f5c46ef9b3188be
push id96855
push userbmo:vedantc98@gmail.com
push dateThu, 25 Jan 2018 20:07:29 +0000
reviewerswlach
bugs1431024
milestone60.0a1
Bug 1431024 - Modified error criterion check in prefs.py. r=wlach MozReview-Commit-ID: 6nEXiIQ7awD
testing/mozbase/mozprofile/mozprofile/prefs.py
testing/mozbase/mozprofile/tests/test_preferences.py
--- a/testing/mozbase/mozprofile/mozprofile/prefs.py
+++ b/testing/mozbase/mozprofile/mozprofile/prefs.py
@@ -143,17 +143,17 @@ class Preferences(object):
             if [i for i in prefs if type(i) != list or len(i) != 2]:
                 raise PreferencesReadError("Malformed preferences: %s" % path)
             values = [i[1] for i in prefs]
         elif isinstance(prefs, dict):
             values = prefs.values()
         else:
             raise PreferencesReadError("Malformed preferences: %s" % path)
         types = (bool, string_types, int)
-        if [i for i in values if not [isinstance(i, j) for j in types]]:
+        if [i for i in values if not any([isinstance(i, j) for j in types])]:
             raise PreferencesReadError("Only bool, string, and int values allowed")
         return prefs
 
     @classmethod
     def read_prefs(cls, path, pref_setter='user_pref', interpolation=None):
         """
         Read preferences from (e.g.) prefs.js
 
--- a/testing/mozbase/mozprofile/tests/test_preferences.py
+++ b/testing/mozbase/mozprofile/tests/test_preferences.py
@@ -11,17 +11,17 @@ import mozhttpd
 import os
 import shutil
 import tempfile
 import unittest
 
 import mozunit
 
 from mozprofile.cli import MozProfileCLI
-from mozprofile.prefs import Preferences
+from mozprofile.prefs import Preferences, PreferencesReadError
 from mozprofile.profile import Profile
 
 here = os.path.dirname(os.path.abspath(__file__))
 
 
 class PreferencesTest(unittest.TestCase):
     """test mozprofile preference handling"""
 
@@ -300,16 +300,27 @@ user_pref("webgl.force-enabled", true);
         # just repr it...could use the json module but we don't need it here
         with mozfile.NamedTemporaryFile(suffix='.json') as f:
             f.write(json)
             f.flush()
 
             commandline = ["--preferences", f.name]
             self.compare_generated(_prefs, commandline)
 
+    def test_json_datatypes(self):
+        # minPercent is at 30.1 to test if non-integer data raises an exception
+        json = """{"zoom.minPercent": 30.1, "zoom.maxPercent": 300}"""
+
+        with mozfile.NamedTemporaryFile(suffix='.json') as f:
+            f.write(json)
+            f.flush()
+
+            with self.assertRaises(PreferencesReadError):
+                Preferences.read_json(f._path)
+
     def test_prefs_write(self):
         """test that the Preferences.write() method correctly serializes preferences"""
 
         _prefs = {'browser.startup.homepage': "http://planet.mozilla.org",
                   'zoom.minPercent': 30,
                   'zoom.maxPercent': 300}
 
         # make a Preferences manager with the testing preferences