Wrote unittests for the config module and hooked them up
[git-stats.git] / src / git_stats / config_tests.py
blob2ce32d35f1aa545a73baad9b266a0b25c6c82651
1 #!/usr/bin/env python
3 import os
4 import unittest
6 from git_stats import config
8 def getTestClasses():
9 """
10 """
12 return [
13 TestRead,
14 TestGetDefaultConfigPaths,
15 TestReadConfigPaths,
18 class TestRead(unittest.TestCase):
19 """Tests the read function of the config module
20 """
22 def setUp(self):
23 self.lines = [
24 "[core]",
25 " foo = bar",
26 " camelCaseCoreSetting = eggs",
27 "[GitStats]",
28 " setting1 = value1",
29 " camelCaseSetting = camelCaseValue"
30 "",
33 self.empty = []
35 self.empty_config = [
36 "[GitStats]",
39 self.multiple_empty_configs = [
40 "[GitStats]",
41 "[GitStats]",
42 "[core]",
43 "foo = bar",
44 "",
45 "[GitStats]",
48 self.multiple_configs = [
49 "",
50 "[GitStats]",
51 " setting0 = value0",
52 " setting1 = value1",
53 " setting2 = value2",
54 "",
55 "[core]",
56 " core_setting = foo",
57 "[GitStats]",
58 " setting3 = value3",
59 "[color]",
60 " color_setting = bar",
61 "[GitStats]",
62 " setting4 = value4",
65 def testNoConvert(self):
66 """Test the readConfig method with no converting
67 """
69 result = config.read(self.lines, convert_camel_case=False)
71 self.failUnlessEqual(
72 "value1",
73 result["setting1"],
74 "Regular setting not properly parsed")
76 self.failUnlessEqual(
77 "camelCaseValue",
78 result["camelCaseSetting"],
79 "Camel case setting not properly parsed")
81 self.failIf(
82 "foo" in result,
83 "Non GitStats setting was parsed")
85 self.failIf(
86 "camelCaseCoreSetting" in result,
87 "Camel case non GitStats setting was parsed")
89 def testWithConvert(self):
90 """Test the readConfig method with converting enabled
91 """
93 result = config.read(self.lines, convert_camel_case=True)
95 self.failUnlessEqual(
96 "value1",
97 result["setting1"],
98 "Regular setting not properly parsed")
100 self.failUnlessEqual(
101 "camelCaseValue",
102 result["camel_case_setting"],
103 "Camel case setting not properly parsed")
105 self.failIf(
106 "foo" in result,
107 "Non GitStats setting was parsed")
109 self.failIf(
110 "camelCaseCoreSetting" in result,
111 "Camel case non GitStats setting was parsed")
113 def testEmpty(self):
114 """Test the readConfig method with no contents
117 result = config.read(self.empty)
119 self.failIf(
120 result,
121 "Empty config did not yield an empty result")
123 def testEmptyConfig(self):
124 """Test the readConfig method with an empty config
127 result = config.read(self.empty_config)
129 self.failIf(
130 result,
131 "Empty config did not yield an empty result")
133 def testMultipleEmptyConfigs(self):
134 """Test the readConfig method with multiple empty configs
137 result = config.read(self.multiple_empty_configs)
139 self.failIf(
140 result,
141 "Multiple empty configs did not yield an empty result")
143 def testMultipleConfigs(self):
144 """Test the readConfig method with multiple verses
147 result = config.read(self.multiple_configs)
149 self.failUnlessEqual(
151 len(result),
152 "Parsed result did not contain 5 values")
154 for i in range(5):
155 self.failUnlessEqual(
156 "value%d" % i,
157 result["setting%d" % i],
158 "Config value for setting%d did not match value%d." % (i,i))
160 class TestGetDefaultConfigPaths(unittest.TestCase):
161 """Tests the getDefaultConfigPaths method of the config module
164 def setUp(self):
165 self.result = config._getDefaultConfigPaths()
167 def testContainsConfig(self):
168 """Tests that the plain "config" file is included
171 self.failUnless(
172 "config" in self.result,
173 "The plain 'config' file is not in the default paths")
175 def testContainsGitConfig(self):
176 """Test that the .git/config file is included
179 self.failUnless(
180 any(s.endswith('.git/config') for s in self.result),
181 "The ./git/config file is not in the default paths")
183 def testContainsGitconfig(self):
184 """Test that the .gitconfig file is included
187 self.failUnless(
188 any(s.endswith('.gitconfig') for s in self.result),
189 "The .gitconfig file is not in the default paths")
191 class TestReadConfigPaths(unittest.TestCase):
192 """Test the readConfigPaths method of the config module
195 def setUp(self):
196 lines = [
197 "[GitStats]\n",
198 " option1 = value1\n",
199 " option2 = value2\n",
202 file = open("config1", "w")
203 file.writelines(lines)
205 lines = [
206 "[GitStats]\n",
207 " option2 = anothervalue2\n",
208 " option3 = anothervalue3\n",
211 file = open("config2", "w")
212 file.writelines(lines)
214 def tearDown(self):
215 os.remove("config1")
216 os.remove("config2")
218 def testReadMultipleValues(self):
219 """Tests if all values were read in
222 paths = ["config1", "config2"]
224 result = config._readConfigPaths(paths, False)
226 self.failUnlessEqual(
227 "value1",
228 result["option1"],
229 "Value of 'option1', present in only one file, not read in properly")
231 self.failIfEqual(
232 "value2",
233 result["option2"],
234 "Value of 'option2' not properly overriden by second file")
236 self.failUnlessEqual(
237 "anothervalue2",
238 result["option2"],
239 "Value of 'option2' not retreived properly")
241 self.failUnlessEqual(
242 "anothervalue3",
243 result["option3"],
244 "Value of 'option3', present in only one file, not read in properly")