confini

Parse and merge multiple ini files in python3
git clone git://git.defalsify.org/python-confini.git
Info | Log | Files | Refs | README | LICENSE

test_export.py (3643B)


      1 # standard imports
      2 import os
      3 import io
      4 import unittest
      5 import logging
      6 import configparser
      7 import tempfile
      8 import re
      9 
     10 # local imports
     11 from confini import Config
     12 from confini.export import ConfigExporter
     13 from confini.doc import ConfigDoc
     14 
     15 logging.basicConfig(level=logging.DEBUG)
     16 
     17 logg = logging.getLogger()
     18 
     19 
     20 class TestExport(unittest.TestCase):
     21 
     22     wd = os.path.dirname(__file__)
     23 
     24 
     25     def setUp(self):
     26         self.inidir = os.path.join(self.wd, 'files')
     27         c = Config(self.inidir)
     28         c.process()
     29         self.config = c
     30 
     31 
     32     def test_handle(self):
     33         w = io.StringIO()
     34         e = ConfigExporter(self.config, target=w)
     35         e.export()
     36 
     37         w.seek(0)
     38         a = configparser.ConfigParser()
     39         a.read_string(w.read())
     40 
     41         b = configparser.ConfigParser()
     42         b.read(os.path.join(self.inidir, 'foo.ini'))
     43         b.read(os.path.join(self.inidir, 'bar.ini'))
     44 
     45         for s in b.sections():
     46             for o in b.options(s):
     47                 self.assertEqual(b.get(s, o), a.get(s.lower(), o.lower()))
     48 
     49 
     50     def test_file(self):
     51         (fd, fn) = tempfile.mkstemp()
     52         e = ConfigExporter(self.config, target=fn)
     53         e.export()
     54 
     55         w = os.fdopen(fd)
     56         a = configparser.ConfigParser()
     57         a.read_string(w.read())
     58 
     59         b = configparser.ConfigParser()
     60         b.read(os.path.join(self.inidir, 'foo.ini'))
     61         b.read(os.path.join(self.inidir, 'bar.ini'))
     62 
     63         for s in b.sections():
     64             for o in b.options(s):
     65                 self.assertEqual(b.get(s, o), a.get(s.lower(), o.lower()))
     66 
     67 
     68     def test_dir_file(self):
     69         d = tempfile.mkdtemp()
     70         e = ConfigExporter(self.config, target=d)
     71         e.export()
     72 
     73         a = configparser.ConfigParser()
     74         a.read(os.path.join(d, 'config.ini'))
     75 
     76         b = configparser.ConfigParser()
     77         b.read(os.path.join(self.inidir, 'foo.ini'))
     78         b.read(os.path.join(self.inidir, 'bar.ini'))
     79 
     80         for s in b.sections():
     81             for o in b.options(s):
     82                 self.assertEqual(b.get(s, o), a.get(s.lower(), o.lower()))
     83 
     84 
     85     def test_dir_split(self):
     86         d = tempfile.mkdtemp()
     87         e = ConfigExporter(self.config, target=d, split=True)
     88         e.export()
     89 
     90         a = configparser.ConfigParser()
     91         a.read(os.path.join(d, 'foo.ini'))
     92         a.read(os.path.join(d, 'bar.ini'))
     93         a.read(os.path.join(d, 'xyzzy.ini'))
     94 
     95         b = configparser.ConfigParser()
     96         b.read(os.path.join(self.inidir, 'foo.ini'))
     97         b.read(os.path.join(self.inidir, 'bar.ini'))
     98 
     99         for s in b.sections():
    100             for o in b.options(s):
    101                 self.assertEqual(b.get(s, o), a.get(s.lower(), o.lower()))
    102 
    103 
    104 
    105     def test_doc(self):
    106         w = io.StringIO()
    107         e = ConfigExporter(self.config, target=w, doc=True)
    108         e.export()
    109     
    110         w.seek(0)
    111         s = w.read()
    112 
    113         print(s)
    114         re_c = re.compile('^# ', re.MULTILINE)
    115         m = re_c.finditer(s)
    116         next(m)
    117         next(m)
    118         next(m)
    119         with self.assertRaises(StopIteration):
    120             next(m)
    121 
    122         re_c = re.compile('^# .*foo$', re.MULTILINE)
    123         m = re_c.finditer(s)
    124         next(m)
    125         next(m)
    126         with self.assertRaises(StopIteration):
    127             next(m)
    128 
    129         a = configparser.ConfigParser()
    130         a.read_string(s)
    131 
    132         b = configparser.ConfigParser()
    133         b.read(os.path.join(self.inidir, 'foo.ini'))
    134         b.read(os.path.join(self.inidir, 'bar.ini'))
    135 
    136         for s in b.sections():
    137             for o in b.options(s):
    138                 self.assertEqual(b.get(s, o), a.get(s.lower(), o.lower()))
    139 
    140 
    141 if __name__ == '__main__':
    142     unittest.main()