summaryrefslogtreecommitdiff
path: root/git/test
diff options
context:
space:
mode:
authorA. Jesse Jiryu Davis <jesse@mongodb.com>2019-01-20 22:19:00 -0500
committerA. Jesse Jiryu Davis <jesse@mongodb.com>2019-01-20 22:22:24 -0500
commitf7bbe6b01c82d9bcb3333b07bae0c9755eecdbbf (patch)
tree9b958816ad4237d49a024d3ef3dce033984ee00b /git/test
parent7a6ca8c40433400f6bb3ece2ed30808316de5be3 (diff)
downloadgitpython-f7bbe6b01c82d9bcb3333b07bae0c9755eecdbbf.tar.gz
Support multiple git config values per option
Solves #717
Diffstat (limited to 'git/test')
-rw-r--r--git/test/fixtures/git_config_multiple7
-rw-r--r--git/test/test_config.py97
2 files changed, 104 insertions, 0 deletions
diff --git a/git/test/fixtures/git_config_multiple b/git/test/fixtures/git_config_multiple
new file mode 100644
index 00000000..03a97568
--- /dev/null
+++ b/git/test/fixtures/git_config_multiple
@@ -0,0 +1,7 @@
+[section0]
+ option0 = value0
+
+[section1]
+ option1 = value1a
+ option1 = value1b
+ other_option1 = other_value1
diff --git a/git/test/test_config.py b/git/test/test_config.py
index 4d6c8236..67e4810d 100644
--- a/git/test/test_config.py
+++ b/git/test/test_config.py
@@ -265,3 +265,100 @@ class TestBase(TestCase):
with self.assertRaises(cp.NoOptionError):
cr.get_value('color', 'ui')
+
+ def test_multiple_values(self):
+ file_obj = self._to_memcache(fixture_path('git_config_multiple'))
+ with GitConfigParser(file_obj, read_only=False) as cw:
+ self.assertEqual(cw.get('section0', 'option0'), 'value0')
+ self.assertEqual(cw.get_values('section0', 'option0'), ['value0'])
+ self.assertEqual(cw.items('section0'), [('option0', 'value0')])
+
+ # Where there are multiple values, "get" returns the last.
+ self.assertEqual(cw.get('section1', 'option1'), 'value1b')
+ self.assertEqual(cw.get_values('section1', 'option1'),
+ ['value1a', 'value1b'])
+ self.assertEqual(cw.items('section1'),
+ [('option1', 'value1b'),
+ ('other_option1', 'other_value1')])
+ self.assertEqual(cw.items_all('section1'),
+ [('option1', ['value1a', 'value1b']),
+ ('other_option1', ['other_value1'])])
+ with self.assertRaises(KeyError):
+ cw.get_values('section1', 'missing')
+
+ self.assertEqual(cw.get_values('section1', 'missing', 1), [1])
+ self.assertEqual(cw.get_values('section1', 'missing', 's'), ['s'])
+
+ def test_multiple_values_rename(self):
+ file_obj = self._to_memcache(fixture_path('git_config_multiple'))
+ with GitConfigParser(file_obj, read_only=False) as cw:
+ cw.rename_section('section1', 'section2')
+ cw.write()
+ file_obj.seek(0)
+ cr = GitConfigParser(file_obj, read_only=True)
+ self.assertEqual(cr.get_value('section2', 'option1'), 'value1b')
+ self.assertEqual(cr.get_values('section2', 'option1'),
+ ['value1a', 'value1b'])
+ self.assertEqual(cr.items('section2'),
+ [('option1', 'value1b'),
+ ('other_option1', 'other_value1')])
+ self.assertEqual(cr.items_all('section2'),
+ [('option1', ['value1a', 'value1b']),
+ ('other_option1', ['other_value1'])])
+
+ def test_multiple_to_single(self):
+ file_obj = self._to_memcache(fixture_path('git_config_multiple'))
+ with GitConfigParser(file_obj, read_only=False) as cw:
+ cw.set_value('section1', 'option1', 'value1c')
+
+ cw.write()
+ file_obj.seek(0)
+ cr = GitConfigParser(file_obj, read_only=True)
+ self.assertEqual(cr.get_value('section1', 'option1'), 'value1c')
+ self.assertEqual(cr.get_values('section1', 'option1'), ['value1c'])
+ self.assertEqual(cr.items('section1'),
+ [('option1', 'value1c'),
+ ('other_option1', 'other_value1')])
+ self.assertEqual(cr.items_all('section1'),
+ [('option1', ['value1c']),
+ ('other_option1', ['other_value1'])])
+
+ def test_single_to_multiple(self):
+ file_obj = self._to_memcache(fixture_path('git_config_multiple'))
+ with GitConfigParser(file_obj, read_only=False) as cw:
+ cw.add_value('section1', 'other_option1', 'other_value1a')
+
+ cw.write()
+ file_obj.seek(0)
+ cr = GitConfigParser(file_obj, read_only=True)
+ self.assertEqual(cr.get_value('section1', 'option1'), 'value1b')
+ self.assertEqual(cr.get_values('section1', 'option1'),
+ ['value1a', 'value1b'])
+ self.assertEqual(cr.get_value('section1', 'other_option1'),
+ 'other_value1a')
+ self.assertEqual(cr.get_values('section1', 'other_option1'),
+ ['other_value1', 'other_value1a'])
+ self.assertEqual(cr.items('section1'),
+ [('option1', 'value1b'),
+ ('other_option1', 'other_value1a')])
+ self.assertEqual(
+ cr.items_all('section1'),
+ [('option1', ['value1a', 'value1b']),
+ ('other_option1', ['other_value1', 'other_value1a'])])
+
+ def test_add_to_multiple(self):
+ file_obj = self._to_memcache(fixture_path('git_config_multiple'))
+ with GitConfigParser(file_obj, read_only=False) as cw:
+ cw.add_value('section1', 'option1', 'value1c')
+ cw.write()
+ file_obj.seek(0)
+ cr = GitConfigParser(file_obj, read_only=True)
+ self.assertEqual(cr.get_value('section1', 'option1'), 'value1c')
+ self.assertEqual(cr.get_values('section1', 'option1'),
+ ['value1a', 'value1b', 'value1c'])
+ self.assertEqual(cr.items('section1'),
+ [('option1', 'value1c'),
+ ('other_option1', 'other_value1')])
+ self.assertEqual(cr.items_all('section1'),
+ [('option1', ['value1a', 'value1b', 'value1c']),
+ ('other_option1', ['other_value1'])])