mirror of
https://github.com/openSUSE/osc.git
synced 2025-01-12 00:46:14 +01:00
Fix config parser to throw an exception on duplicate sections or options
This commit is contained in:
parent
d92f2677f4
commit
360a94c4a3
@ -263,7 +263,9 @@ class OscConfigParser(configparser.ConfigParser):
|
|||||||
mo = self.SECTCRE.match(line)
|
mo = self.SECTCRE.match(line)
|
||||||
if mo:
|
if mo:
|
||||||
sectname = mo.group('header')
|
sectname = mo.group('header')
|
||||||
if sectname in self._sections:
|
if self._strict and sectname in self._sections:
|
||||||
|
raise configparser.DuplicateSectionError(sectname, fpname, lineno)
|
||||||
|
elif sectname in self._sections:
|
||||||
cursect = self._sections[sectname]
|
cursect = self._sections[sectname]
|
||||||
elif sectname == configparser.DEFAULTSECT:
|
elif sectname == configparser.DEFAULTSECT:
|
||||||
cursect = self._defaults
|
cursect = self._defaults
|
||||||
@ -294,7 +296,9 @@ class OscConfigParser(configparser.ConfigParser):
|
|||||||
if optval == '""':
|
if optval == '""':
|
||||||
optval = ''
|
optval = ''
|
||||||
optname = self.optionxform(optname.rstrip())
|
optname = self.optionxform(optname.rstrip())
|
||||||
if cursect == configparser.DEFAULTSECT:
|
if self._strict and optname in self._sections[cursect]:
|
||||||
|
raise configparser.DuplicateOptionError(sectname, optname, fpname, lineno)
|
||||||
|
elif cursect == configparser.DEFAULTSECT:
|
||||||
self._defaults[optname] = optval
|
self._defaults[optname] = optval
|
||||||
else:
|
else:
|
||||||
self._sections[cursect]._add_option(optname, line=line)
|
self._sections[cursect]._add_option(optname, line=line)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import configparser
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from osc.OscConfigParser import OscConfigParser
|
from osc.OscConfigParser import OscConfigParser
|
||||||
@ -21,6 +22,28 @@ pass=
|
|||||||
# ValueError: invalid interpolation syntax in '%' at position 0
|
# ValueError: invalid interpolation syntax in '%' at position 0
|
||||||
self.parser.set("http://localhost", "pass", "%")
|
self.parser.set("http://localhost", "pass", "%")
|
||||||
|
|
||||||
|
def test_duplicate_section(self):
|
||||||
|
conf = """
|
||||||
|
[general]
|
||||||
|
|
||||||
|
[http://localhost]
|
||||||
|
|
||||||
|
[http://localhost]
|
||||||
|
"""
|
||||||
|
parser = OscConfigParser()
|
||||||
|
self.assertRaises(configparser.DuplicateSectionError, parser.read_string, conf)
|
||||||
|
|
||||||
|
def test_duplicate_option(self):
|
||||||
|
conf = """
|
||||||
|
[general]
|
||||||
|
|
||||||
|
[http://localhost]
|
||||||
|
user=
|
||||||
|
user=
|
||||||
|
"""
|
||||||
|
parser = OscConfigParser()
|
||||||
|
self.assertRaises(configparser.DuplicateOptionError, parser.read_string, conf)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user