aboutsummaryrefslogtreecommitdiff
path: root/test/test_config/test_files.py
blob: 7d32a07151daff8dc151afe3c75ab1c639512e6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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 <http://www.gnu.org/licenses/>.

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()