Bläddra i källkod

git_config: add GetInt helper

Change-Id: Ic034ae2fd962299d1b352e597b391b6582ecf44b
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/256052
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Jonathan Nieder <jrn@google.com>
Mike Frysinger 6 år sedan
förälder
incheckning
77b4397a73
3 ändrade filer med 84 tillägg och 0 borttagningar
  1. 37 0
      git_config.py
  2. 10 0
      tests/fixtures/test.gitconfig
  3. 37 0
      tests/test_git_config.py

+ 37 - 0
git_config.py

@@ -122,6 +122,43 @@ class GitConfig(object):
       return self.defaults.Has(name, include_defaults=True)
     return False
 
+  def GetInt(self, name):
+    """Returns an integer from the configuration file.
+
+    This follows the git config syntax.
+
+    Args:
+      name: The key to lookup.
+
+    Returns:
+      None if the value was not defined, or is not a boolean.
+      Otherwise, the number itself.
+    """
+    v = self.GetString(name)
+    if v is None:
+      return None
+    v = v.strip()
+
+    mult = 1
+    if v.endswith('k'):
+      v = v[:-1]
+      mult = 1024
+    elif v.endswith('m'):
+      v = v[:-1]
+      mult = 1024 * 1024
+    elif v.endswith('g'):
+      v = v[:-1]
+      mult = 1024 * 1024 * 1024
+
+    base = 10
+    if v.startswith('0x'):
+      base = 16
+
+    try:
+      return int(v, base=base) * mult
+    except ValueError:
+      return None
+
   def GetBoolean(self, name):
     """Returns a boolean from the configuration file.
        None : The value was not defined, or is not a boolean.

+ 10 - 0
tests/fixtures/test.gitconfig

@@ -1,3 +1,13 @@
 [section]
 	empty
 	nonempty = true
+	boolinvalid = oops
+	booltrue = true
+	boolfalse = false
+	intinvalid = oops
+	inthex = 0x10
+	inthexk = 0x10k
+	int = 10
+	intk = 10k
+	intm = 10m
+	intg = 10g

+ 37 - 0
tests/test_git_config.py

@@ -71,6 +71,43 @@ class GitConfigUnitTest(unittest.TestCase):
     val = config.GetString('empty')
     self.assertEqual(val, None)
 
+  def test_GetBoolean_undefined(self):
+    """Test GetBoolean on key that doesn't exist."""
+    self.assertIsNone(self.config.GetBoolean('section.missing'))
+
+  def test_GetBoolean_invalid(self):
+    """Test GetBoolean on invalid boolean value."""
+    self.assertIsNone(self.config.GetBoolean('section.boolinvalid'))
+
+  def test_GetBoolean_true(self):
+    """Test GetBoolean on valid true boolean."""
+    self.assertTrue(self.config.GetBoolean('section.booltrue'))
+
+  def test_GetBoolean_false(self):
+    """Test GetBoolean on valid false boolean."""
+    self.assertFalse(self.config.GetBoolean('section.boolfalse'))
+
+  def test_GetInt_undefined(self):
+    """Test GetInt on key that doesn't exist."""
+    self.assertIsNone(self.config.GetInt('section.missing'))
+
+  def test_GetInt_invalid(self):
+    """Test GetInt on invalid integer value."""
+    self.assertIsNone(self.config.GetBoolean('section.intinvalid'))
+
+  def test_GetInt_valid(self):
+    """Test GetInt on valid integers."""
+    TESTS = (
+        ('inthex', 16),
+        ('inthexk', 16384),
+        ('int', 10),
+        ('intk', 10240),
+        ('intm', 10485760),
+        ('intg', 10737418240),
+    )
+    for key, value in TESTS:
+      self.assertEqual(value, self.config.GetInt('section.%s' % (key,)))
+
 
 if __name__ == '__main__':
   unittest.main()