# Copyright (C) 2010 Leonard Thomas # # This file is part of Dodai. # # Dodai is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Dodai is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Dodai. If not, see . import sys import os import unittest path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) sys.path.append(path) from ConfigParser import ConfigParser from ConfigParser import MissingSectionHeaderError from dodai.config.files import ConfigFiles from dodai.tools.odict import OrderedDict from dodai.tools.himo import String2Himo from dodai.tools import Section from dodai.exception import InvalidConfigParser from dodai.exception import FileDoesNotExist from dodai.exception import FileIsDirectory class GoodParser(object): DATA = OrderedDict() DATA['sec_a'] = OrderedDict() DATA['sec_a']['opt_a'] = 'foo' DATA['sec_a']['opt_b'] = 'bar' DATA['sec_a']['opt_c'] = '1' DATA['sec_a']['opt_d'] = '22' DATA['sec_b'] = OrderedDict() DATA['sec_b']['opt_a'] = 'lt' DATA['sec_b']['opt_b'] = 'el' DATA['sec_b']['opt_c'] = 'mg' DATA['sec_b']['opt_d'] = 'ds' def sections(self): return self.DATA.keys() def options(self, section): return self.DATA[section].keys() def get(self, section, option): return self.DATA[section][option] def read(self, filename): return [filename] class BadParser(object): DATA = OrderedDict() DATA['sec_a'] = OrderedDict() DATA['sec_a']['opt_a'] = 'foo' DATA['sec_a']['opt_b'] = 'bar' DATA['sec_a']['opt_c'] = '1' DATA['sec_a']['opt_d'] = '22' DATA['sec_b'] = OrderedDict() DATA['sec_b']['opt_a'] = 'lt' DATA['sec_b']['opt_b'] = 'el' DATA['sec_b']['opt_c'] = 'mg' DATA['sec_b']['opt_d'] = 'ds' def sections(self): return self.DATA.keys() def get(self, section, option): return self.DATA[section][option] def read(self, filename): return [filename] class TestFiles(unittest.TestCase): def setUp(self): s2h = String2Himo() self.obj = ConfigFiles(OrderedDict, Section, s2h) def test_good_parser(self): self.obj.register_parser_object(GoodParser) def test_bad_parser(self): self.failUnlessRaises(InvalidConfigParser, self.obj.register_parser_object, BadParser) def test_add(self): filename = os.path.realpath(__file__) self.obj.add(filename) self.assertTrue(filename in self.obj.files) def test_add_bad_file(self): filename = os.path.dirname(os.path.realpath(__file__)) filename = os.path.join(filename, 'no_way_jose.234dasf2345') self.failUnlessRaises(FileDoesNotExist, self.obj.add, filename) def test_add_directory(self): filename = os.path.dirname(os.path.realpath(__file__)) self.failUnlessRaises(FileIsDirectory, self.obj.add, filename) def file_test_read(self): filename = os.path.realpath(__file__) self.obj.register_parser_object(GoodParser) self.obj.add(filename) sec = self.obj.load() def test_bad_read(self): self.obj.register_parser_object(ConfigParser) filename = os.path.realpath(__file__) self.obj.add(filename) self.failUnlessRaises(MissingSectionHeaderError, self.obj.load) def test_good_read_with_multiple_parsers(self): self.obj.register_parser_object(ConfigParser) self.obj.register_parser_object(GoodParser) filename = os.path.realpath(__file__) self.obj.add(filename) sec = self.obj.load()