def test_delete(self): sio = StringIO(self.s1) p = ini.INIConfig(sio) del p.section1.help self.assertEqual(list(p.section1), ["i'm", 'but']) self.assertEqual(str(p), """ [section1] I'm = desperate ; really! [section2] # comment and empty line before the first option just = what? just = kidding [section1] but = also me """) del p.section2 self.assertEqual(str(p), """ [section1] I'm = desperate ; really! [section1] but = also me """)
def test_write(self): c = ini.INIConfig() c._readfp(StringIO(self.s)) c.sec.opt = 'xyz' self.assertEqual(str(c), """\ [sec] opt = xyz""")
def test_option_continuation(self): ip = ini.INIConfig(StringIO(self.s2)) self.assertEqual(str(ip), self.s2) value = ip.section.option.split('\n') value.insert(3, 'mum') ip.section.option = '\n'.join(value) self.assertEqual(str(ip), self.s3)
def test_fuzz(self): random.seed(42) for i in range(100): # parse random file with errors disabled s = random_ini_file() c = ini.INIConfig(parse_exc=False) c._readfp(StringIO(s)) # check that file is preserved, except for # commenting out erroneous lines l1 = s.split('\n') l2 = str(c).split('\n') self.assertEqual(len(l1), len(l2)) good_lines = [] for i in range(len(l1)): try: self.assertEqual(l1[i], l2[i]) good_lines.append(l1[i]) except AssertionError: self.assertEqual('#' + l1[i], l2[i]) # parse the good subset of the file # using ConfigParser s = '\n'.join(good_lines) cc = compat.RawConfigParser() cc.readfp(StringIO(s)) cc_py = ConfigParser.RawConfigParser() cc_py.readfp(StringIO(s)) # compare the two configparsers self.assertEqualSorted(cc_py.sections(), cc.sections()) self.assertEqualSorted(cc_py.defaults().items(), cc.defaults().items()) for sec in cc_py.sections(): self.assertEqualSorted(cc_py.items(sec), cc.items(sec))
def test_ini(self): c = ini.INIConfig() c._readfp(StringIO(self.s)) self.do_ini_checks(c) for i in range(0, pickle.HIGHEST_PROTOCOL + 1): p = pickle.dumps(c, protocol=i) c2 = pickle.loads(p) self.do_ini_checks(c2)
def basic_tests(self, s, strable): f = StringIO(s) i = ini.INIConfig(f) self.assertEqual(unicode(i), s) self.assertEqual(type(i.foo.bar), unicode) if strable: self.assertEqual(str(i), str(s)) else: self.assertRaises(UnicodeEncodeError, lambda: str(i)) return i
def basic_tests(self, s, strable): f = six.StringIO(s) i = ini.INIConfig(f) self.assertEqual(six.text_type(i), s) self.assertEqual(type(i.foo.bar), six.text_type) if strable: self.assertEqual(str(i), str(s)) else: self.assertRaises(UnicodeEncodeError, lambda: six.text_type(i).encode('ascii')) return i
def test_lookup(self): sio = StringIO(self.s1) p = ini.INIConfig(sio) self.assertEqual(p.section1.help, 'yourself') self.assertEqual(p.section1.but, 'also me') self.assertEqual(getattr(p.section1, 'I\'m'), 'desperate') self.assertEqual(p.section2.just, 'kidding') self.assertEqual(p.section1.just.__class__, config.Undefined) self.assertEqual(p.section2.help.__class__, config.Undefined)
def test_queue(self): def getxy(q, w): cfg = q.get_nowait() w.put(cfg.x.y) cfg = ini.INIConfig() cfg.x.y = '42' q = Queue() w = Queue() q.put(cfg) p = Process(target=getxy, args=(q, w)) p.start() self.assertEqual(w.get(timeout=1), '42')
def test_newsection(self): sio = StringIO(self.s1) p = ini.INIConfig(sio) p.new1.created = 1 setattr(getattr(p, 'new2'), 'created', 1) p.new3['created'] = 1 p['new4'].created = 1 p['new5']['created'] = 1 self.assertEqual(p.new1.created, 1) self.assertEqual(p.new2.created, 1) self.assertEqual(p.new3.created, 1) self.assertEqual(p.new4.created, 1) self.assertEqual(p.new5.created, 1)
def test_fuzz(self): random.seed(42) try: num_iter = int(os.environ['INIPARSE_FUZZ_ITERATIONS']) except (KeyError, ValueError): num_iter = 100 for fuzz_iter in range(num_iter): try: # parse random file with errors disabled s = random_ini_file() c = ini.INIConfig(parse_exc=False) c._readfp(StringIO(s)) # check that file is preserved, except for # commenting out erroneous lines l1 = s.split('\n') l2 = str(c).split('\n') self.assertEqual(len(l1), len(l2)) good_lines = [] for i in range(len(l1)): try: self.assertEqual(l1[i], l2[i]) good_lines.append(l1[i]) except AssertionError: self.assertEqual('#'+l1[i], l2[i]) # parse the good subset of the file # using ConfigParser s = '\n'.join(good_lines) cc = compat.RawConfigParser() cc.readfp(StringIO(s)) cc_py = configparser.RawConfigParser() cc_py.readfp(StringIO(s)) # compare the two configparsers self.assertEqualConfig(cc_py, cc) # check that tidy does not change semantics tidy(cc) cc_tidy = configparser.RawConfigParser() cc_tidy.readfp(StringIO(str(cc.data))) self.assertEqualConfig(cc_py, cc_tidy) except AssertionError: fname = 'fuzz-test-iter-%d.ini' % fuzz_iter print('Fuzz test failed at iteration', fuzz_iter) print('Writing out failing INI file as', fname) f = open(fname, 'w') f.write(s) f.close() raise
def test_basic(self): sio = StringIO(self.s1) p = ini.INIConfig(sio) self.assertEqual(str(p), self.s1) self.assertEqual(p._data.find('section1').find('but').value, 'also me') self.assertEqual(p._data.find('section1').find('help').value, 'yourself') self.assertEqual(p._data.find('section2').find('just').value, 'kidding') itr = p._data.finditer('section1') v = itr.next() self.assertEqual(v.find('help').value, 'yourself') self.assertEqual(v.find('but').value, 'also me') v = itr.next() self.assertEqual(v.find('help').value, 'me') self.assertEqual(v.find('I\'m').value, 'desperate') self.assertRaises(StopIteration, itr.next) self.assertRaises(KeyError, p._data.find, 'section') self.assertRaises(KeyError, p._data.find('section2').find, 'ahem')
def test_ignore_includes(self): ini.change_comment_syntax() cfg = ini.INIConfig( StringIO( dedent(""" # This is a mercurial-style config % include foobar [ui] username = Firstname Lastname <[email protected]> """))) self.assertEqual(cfg.ui.username, 'Firstname Lastname <[email protected]>') self.assertEqual( str(cfg), dedent(""" # This is a mercurial-style config % include foobar [ui] username = Firstname Lastname <[email protected]> """))
def reload(self): """ This callback method is called, when i-notify or periodical directory polling detects any change of rhsm.conf file. Thus configuration file is reloaded and new values are used. """ parser = rhsm.config.get_config_parser() # We are going to read configuration file again, but we have to clean all data in parser object # this way, because iniparse module doesn't provide better method to do that. parser.data = ini.INIConfig(None, optionxformsource=parser) # We have to read parser again to get fresh data from the file files_read = parser.read() if len(files_read) > 0: log.debug("files read: %s" % str(files_read)) self.config = Config(parser) rhsm.logutil.init_logger(parser) log.debug("Configuration file: %s reloaded: %s" % (parser.config_file, str(self.config))) else: log.warning("Unable to read configuration file: %s" % parser.config_file)
def test_order(self): sio = StringIO(self.s1) p = ini.INIConfig(sio) self.assertEqual(list(p), ['section1','section2']) self.assertEqual(list(p.section1), ['help', "i'm", 'but']) self.assertEqual(list(p.section2), ['just'])
+ keys = list(self._data.keys()) keys.sort() for name in keys: value = self._data[name] @@ -258,7 +258,7 @@ def update_config(target, source): >>> n.ui.display_clock = True >>> n.ui.display_qlength = True >>> n.ui.width = 150 - >>> print n + >>> print(n) playlist.expand_playlist = True ui.display_clock = True ui.display_qlength = True @@ -267,7 +267,7 @@ def update_config(target, source): >>> from iniparse import ini >>> i = ini.INIConfig() >>> update_config(i, n) - >>> print i + >>> print(i) [playlist] expand_playlist = True <BLANKLINE> @@ -277,7 +277,7 @@ def update_config(target, source): width = 150 """ - for name in source: + for name in sorted(source): value = source[name] if isinstance(value, ConfigNamespace): if name in target:
def test_read(self): c = ini.INIConfig() c._readfp(StringIO(self.s)) self.assertEqual(str(c), '')
def test_read(self): c = ini.INIConfig() c._readfp(StringIO(self.s)) self.assertEqual(c.sec.opt, '1\n2\n\n3')
def test_option_continuation_single(self): ip = ini.INIConfig(StringIO(self.s5)) self.assertEqual(str(ip), self.s5) ip.section.option = '\n'.join(['', '', '', 'foo', '', '', '']) ip.section.another = 'baz' self.assertEqual(str(ip), self.s6)
def test_invalid(self): for (org, mod) in self.inv: ip = ini.INIConfig(StringIO(org), parse_exc=False) self.assertEqual(str(ip), mod)
def test_readline_iniconfig(self): for s in self.test_strings: fp = OnlyReadline(s) c = ini.INIConfig() c._readfp(fp) self.assertEqual(s, str(c))
$NetBSD$ --- tests/test_unicode.py.orig 2008-12-06 06:44:49.000000000 +0000 +++ tests/test_unicode.py @@ -1,5 +1,5 @@ import unittest -from StringIO import StringIO +import six from iniparse import compat, ini class test_unicode(unittest.TestCase): @@ -17,14 +17,14 @@ baz = Marc-Andr\202 """ def basic_tests(self, s, strable): - f = StringIO(s) + f = six.StringIO(s) i = ini.INIConfig(f) - self.assertEqual(unicode(i), s) - self.assertEqual(type(i.foo.bar), unicode) + self.assertEqual(six.text_type(i), s) + self.assertEqual(type(i.foo.bar), six.text_type) if strable: self.assertEqual(str(i), str(s)) else: - self.assertRaises(UnicodeEncodeError, lambda: str(i)) + self.assertRaises(UnicodeEncodeError, lambda: six.text_type(i).encode('ascii')) return i def test_ascii(self):