summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2020-02-19 17:55:22 -0500
committerMike Frysinger <vapier@google.com>2020-02-19 23:09:05 +0000
commit77b4397a7376fe52fe0725ed538891c89da161ed (patch)
tree2cfc3683cf4ae3ec9a48fd766865bfb1492eb5fc
parent0334b8c6738929ed5982a6572135714045a977fa (diff)
downloadgit-repo-77b4397a7376fe52fe0725ed538891c89da161ed.tar.gz
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>
-rw-r--r--git_config.py37
-rw-r--r--tests/fixtures/test.gitconfig10
-rw-r--r--tests/test_git_config.py37
3 files changed, 84 insertions, 0 deletions
diff --git a/git_config.py b/git_config.py
index cee33e92..6b1f7107 100644
--- a/git_config.py
+++ b/git_config.py
@@ -122,6 +122,43 @@ class GitConfig(object):
122 return self.defaults.Has(name, include_defaults=True) 122 return self.defaults.Has(name, include_defaults=True)
123 return False 123 return False
124 124
125 def GetInt(self, name):
126 """Returns an integer from the configuration file.
127
128 This follows the git config syntax.
129
130 Args:
131 name: The key to lookup.
132
133 Returns:
134 None if the value was not defined, or is not a boolean.
135 Otherwise, the number itself.
136 """
137 v = self.GetString(name)
138 if v is None:
139 return None
140 v = v.strip()
141
142 mult = 1
143 if v.endswith('k'):
144 v = v[:-1]
145 mult = 1024
146 elif v.endswith('m'):
147 v = v[:-1]
148 mult = 1024 * 1024
149 elif v.endswith('g'):
150 v = v[:-1]
151 mult = 1024 * 1024 * 1024
152
153 base = 10
154 if v.startswith('0x'):
155 base = 16
156
157 try:
158 return int(v, base=base) * mult
159 except ValueError:
160 return None
161
125 def GetBoolean(self, name): 162 def GetBoolean(self, name):
126 """Returns a boolean from the configuration file. 163 """Returns a boolean from the configuration file.
127 None : The value was not defined, or is not a boolean. 164 None : The value was not defined, or is not a boolean.
diff --git a/tests/fixtures/test.gitconfig b/tests/fixtures/test.gitconfig
index 3c573c9e..9b3f2574 100644
--- a/tests/fixtures/test.gitconfig
+++ b/tests/fixtures/test.gitconfig
@@ -1,3 +1,13 @@
1[section] 1[section]
2 empty 2 empty
3 nonempty = true 3 nonempty = true
4 boolinvalid = oops
5 booltrue = true
6 boolfalse = false
7 intinvalid = oops
8 inthex = 0x10
9 inthexk = 0x10k
10 int = 10
11 intk = 10k
12 intm = 10m
13 intg = 10g
diff --git a/tests/test_git_config.py b/tests/test_git_config.py
index 6aa6b381..4541b35c 100644
--- a/tests/test_git_config.py
+++ b/tests/test_git_config.py
@@ -71,6 +71,43 @@ class GitConfigUnitTest(unittest.TestCase):
71 val = config.GetString('empty') 71 val = config.GetString('empty')
72 self.assertEqual(val, None) 72 self.assertEqual(val, None)
73 73
74 def test_GetBoolean_undefined(self):
75 """Test GetBoolean on key that doesn't exist."""
76 self.assertIsNone(self.config.GetBoolean('section.missing'))
77
78 def test_GetBoolean_invalid(self):
79 """Test GetBoolean on invalid boolean value."""
80 self.assertIsNone(self.config.GetBoolean('section.boolinvalid'))
81
82 def test_GetBoolean_true(self):
83 """Test GetBoolean on valid true boolean."""
84 self.assertTrue(self.config.GetBoolean('section.booltrue'))
85
86 def test_GetBoolean_false(self):
87 """Test GetBoolean on valid false boolean."""
88 self.assertFalse(self.config.GetBoolean('section.boolfalse'))
89
90 def test_GetInt_undefined(self):
91 """Test GetInt on key that doesn't exist."""
92 self.assertIsNone(self.config.GetInt('section.missing'))
93
94 def test_GetInt_invalid(self):
95 """Test GetInt on invalid integer value."""
96 self.assertIsNone(self.config.GetBoolean('section.intinvalid'))
97
98 def test_GetInt_valid(self):
99 """Test GetInt on valid integers."""
100 TESTS = (
101 ('inthex', 16),
102 ('inthexk', 16384),
103 ('int', 10),
104 ('intk', 10240),
105 ('intm', 10485760),
106 ('intg', 10737418240),
107 )
108 for key, value in TESTS:
109 self.assertEqual(value, self.config.GetInt('section.%s' % (key,)))
110
74 111
75if __name__ == '__main__': 112if __name__ == '__main__':
76 unittest.main() 113 unittest.main()