diff options
-rw-r--r-- | git_config.py | 21 | ||||
-rw-r--r-- | tests/fixtures/test.gitconfig | 3 | ||||
-rw-r--r-- | tests/test_git_config.py | 43 |
3 files changed, 59 insertions, 8 deletions
diff --git a/git_config.py b/git_config.py index a5b5b4d9..e658b059 100644 --- a/git_config.py +++ b/git_config.py | |||
@@ -259,21 +259,26 @@ class GitConfig(object): | |||
259 | os.remove(self._pickle) | 259 | os.remove(self._pickle) |
260 | 260 | ||
261 | def _ReadGit(self): | 261 | def _ReadGit(self): |
262 | d = self._do('--null', '--list') | 262 | """ |
263 | c = {} | 263 | Read configuration data from git. |
264 | while d: | 264 | |
265 | lf = d.index('\n') | 265 | This internal method populates the GitConfig cache. |
266 | nul = d.index('\0', lf + 1) | ||
267 | 266 | ||
268 | key = _key(d[0:lf]) | 267 | """ |
269 | val = d[lf + 1:nul] | 268 | d = self._do('--null', '--list').rstrip('\0') |
269 | c = {} | ||
270 | for line in d.split('\0'): | ||
271 | if '\n' in line: | ||
272 | key, val = line.split('\n', 1) | ||
273 | else: | ||
274 | key = line | ||
275 | val = None | ||
270 | 276 | ||
271 | if key in c: | 277 | if key in c: |
272 | c[key].append(val) | 278 | c[key].append(val) |
273 | else: | 279 | else: |
274 | c[key] = [val] | 280 | c[key] = [val] |
275 | 281 | ||
276 | d = d[nul + 1:] | ||
277 | return c | 282 | return c |
278 | 283 | ||
279 | def _do(self, *args): | 284 | def _do(self, *args): |
diff --git a/tests/fixtures/test.gitconfig b/tests/fixtures/test.gitconfig new file mode 100644 index 00000000..3c573c9e --- /dev/null +++ b/tests/fixtures/test.gitconfig | |||
@@ -0,0 +1,3 @@ | |||
1 | [section] | ||
2 | empty | ||
3 | nonempty = true | ||
diff --git a/tests/test_git_config.py b/tests/test_git_config.py new file mode 100644 index 00000000..d67a8bab --- /dev/null +++ b/tests/test_git_config.py | |||
@@ -0,0 +1,43 @@ | |||
1 | import os | ||
2 | import unittest | ||
3 | |||
4 | import git_config | ||
5 | |||
6 | def fixture(*paths): | ||
7 | """Return a path relative to test/fixtures. | ||
8 | """ | ||
9 | return os.path.join(os.path.dirname(__file__), 'fixtures', *paths) | ||
10 | |||
11 | class GitConfigUnitTest(unittest.TestCase): | ||
12 | """Tests the GitConfig class. | ||
13 | """ | ||
14 | def setUp(self): | ||
15 | """Create a GitConfig object using the test.gitconfig fixture. | ||
16 | """ | ||
17 | config_fixture = fixture('test.gitconfig') | ||
18 | self.config = git_config.GitConfig(config_fixture) | ||
19 | |||
20 | def test_GetString_with_empty_config_values(self): | ||
21 | """ | ||
22 | Test config entries with no value. | ||
23 | |||
24 | [section] | ||
25 | empty | ||
26 | |||
27 | """ | ||
28 | val = self.config.GetString('section.empty') | ||
29 | self.assertEqual(val, None) | ||
30 | |||
31 | def test_GetString_with_true_value(self): | ||
32 | """ | ||
33 | Test config entries with a string value. | ||
34 | |||
35 | [section] | ||
36 | nonempty = true | ||
37 | |||
38 | """ | ||
39 | val = self.config.GetString('section.nonempty') | ||
40 | self.assertEqual(val, 'true') | ||
41 | |||
42 | if __name__ == '__main__': | ||
43 | unittest.main() | ||