Example #1
0
    def test_write(self):
        c = ini.INIConfig()
        c._readfp(StringIO(self.s))
        c.sec.opt = 'xyz'
        self.assertEqual(str(c), """\
[sec]
opt = xyz""")
Example #2
0
 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)
Example #3
0
    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
""")
Example #4
0
 def test_ini(self):
     c = ini.INIConfig()
     c._readfp(StringIO(self.s))
     self.do_ini_checks(c)
     p = pickle.dumps(c)
     c = None
     c2 = pickle.loads(p)
     self.do_ini_checks(c2)
Example #5
0
    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)
Example #6
0
 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
Example #7
0
 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.assertEqualSorted(cc_py.sections(), cc.sections())
             self.assertEqualSorted(list(cc_py.defaults().items()),
                                    list(cc.defaults().items()))
             for sec in cc_py.sections():
                 self.assertEqualSorted(cc_py.options(sec), cc.options(sec))
                 for opt in cc_py.options(sec):
                     self.assertEqual(cc_py.get(sec, opt), cc.get(sec, opt))
         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
Example #8
0
    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 = next(itr)
        self.assertEqual(v.find('help').value, 'yourself')
        self.assertEqual(v.find('but').value, 'also me')
        v = next(itr)
        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')
Example #9
0
 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))
Example #10
0
 def test_read(self):
     c = ini.INIConfig()
     c._readfp(StringIO(self.s))
     self.assertEqual(str(c), '')
Example #11
0
 def test_read(self):
     c = ini.INIConfig()
     c._readfp(StringIO(self.s))
     self.assertEqual(c.sec.opt, '1\n2\n\n3')
Example #12
0
 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)
Example #13
0
 def test_invalid(self):
     for (org, mod) in self.inv:
         ip = ini.INIConfig(StringIO(org), parse_exc=False)
         self.assertEqual(str(ip), mod)
Example #14
0
 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'])