1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # This software is licensed as described in the file COPYING, which |
---|
8 | # you should have received as part of this distribution. The terms |
---|
9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
10 | |
---|
11 | import platform |
---|
12 | import os |
---|
13 | import tempfile |
---|
14 | import unittest |
---|
15 | |
---|
16 | from bitten.build.config import Configuration, ConfigFileNotFound |
---|
17 | |
---|
18 | |
---|
19 | class ConfigurationTestCase(unittest.TestCase): |
---|
20 | |
---|
21 | def test_sysinfo_defaults(self): |
---|
22 | config = Configuration() |
---|
23 | |
---|
24 | self.assertEqual(platform.machine(), config['machine']) |
---|
25 | self.assertEqual(platform.processor(), config['processor']) |
---|
26 | system, release, version = platform.system_alias(platform.system(), |
---|
27 | platform.release(), |
---|
28 | platform.version()) |
---|
29 | self.assertEqual(system, config['os']) |
---|
30 | self.assertEqual(os.name, config['family']) |
---|
31 | self.assertEqual(release, config['version']) |
---|
32 | |
---|
33 | def test_sysinfo_properties_override(self): |
---|
34 | config = Configuration(properties={ |
---|
35 | 'machine': 'MACHINE', |
---|
36 | 'processor': 'PROCESSOR', |
---|
37 | 'os': 'OS', |
---|
38 | 'family': 'FAMILY', |
---|
39 | 'version': 'VERSION' |
---|
40 | }) |
---|
41 | self.assertEqual('MACHINE', config['machine']) |
---|
42 | self.assertEqual('PROCESSOR', config['processor']) |
---|
43 | self.assertEqual('OS', config['os']) |
---|
44 | self.assertEqual('FAMILY', config['family']) |
---|
45 | self.assertEqual('VERSION', config['version']) |
---|
46 | |
---|
47 | def test_sysinfo_configfile_override(self): |
---|
48 | inifd, ininame = tempfile.mkstemp(prefix='bitten_test') |
---|
49 | try: |
---|
50 | os.write(inifd, """ |
---|
51 | [machine] |
---|
52 | name = MACHINE |
---|
53 | processor = PROCESSOR |
---|
54 | |
---|
55 | [os] |
---|
56 | name = OS |
---|
57 | family = FAMILY |
---|
58 | version = VERSION |
---|
59 | """) |
---|
60 | os.close(inifd) |
---|
61 | config = Configuration(ininame) |
---|
62 | |
---|
63 | self.assertEqual('MACHINE', config['machine']) |
---|
64 | self.assertEqual('PROCESSOR', config['processor']) |
---|
65 | self.assertEqual('OS', config['os']) |
---|
66 | self.assertEqual('FAMILY', config['family']) |
---|
67 | self.assertEqual('VERSION', config['version']) |
---|
68 | finally: |
---|
69 | os.remove(ininame) |
---|
70 | |
---|
71 | def test_sysinfo_configfile_partial_override(self): |
---|
72 | inifd, ininame = tempfile.mkstemp(prefix='bitten_test') |
---|
73 | try: |
---|
74 | os.write(inifd, """ |
---|
75 | [machine] |
---|
76 | name = MACHINE |
---|
77 | |
---|
78 | [os] |
---|
79 | name = OS |
---|
80 | """) |
---|
81 | os.close(inifd) |
---|
82 | config = Configuration(ininame) |
---|
83 | |
---|
84 | self.assertEqual('MACHINE', config['machine']) |
---|
85 | self.assertEqual('OS', config['os']) |
---|
86 | # Remaining options should be set to default value |
---|
87 | system, release, version = platform.system_alias(platform.system(), |
---|
88 | platform.release(), |
---|
89 | platform.version()) |
---|
90 | self.assertEqual(platform.processor(), config['processor']) |
---|
91 | self.assertEqual(os.name, config['family']) |
---|
92 | self.assertEqual(release, config['version']) |
---|
93 | finally: |
---|
94 | os.remove(ininame) |
---|
95 | |
---|
96 | def test_package_properties(self): |
---|
97 | config = Configuration(properties={ |
---|
98 | 'python.version': '2.3.5', |
---|
99 | 'python.path': '/usr/local/bin/python2.3', |
---|
100 | 'python.name': 'invalid option' |
---|
101 | }) |
---|
102 | self.assertEqual(True, 'python' in config.packages) |
---|
103 | self.assertEqual('/usr/local/bin/python2.3', config['python.path']) |
---|
104 | self.assertEqual('2.3.5', config['python.version']) |
---|
105 | self.failIf('name' in config.packages['python'], |
---|
106 | "Invalid option 'name' should not be included...") |
---|
107 | |
---|
108 | def test_package_configfile(self): |
---|
109 | inifd, ininame = tempfile.mkstemp(prefix='bitten_test') |
---|
110 | try: |
---|
111 | os.write(inifd, """ |
---|
112 | [python] |
---|
113 | path = /usr/local/bin/python2.3 |
---|
114 | version = 2.3.5 |
---|
115 | name = invalid option |
---|
116 | """) |
---|
117 | os.close(inifd) |
---|
118 | config = Configuration(ininame) |
---|
119 | |
---|
120 | self.assertEqual(True, 'python' in config.packages) |
---|
121 | self.assertEqual('/usr/local/bin/python2.3', config['python.path']) |
---|
122 | self.assertEqual('2.3.5', config['python.version']) |
---|
123 | self.failIf('name' in config.packages['python'], |
---|
124 | "Invalid option 'name' should not be included...") |
---|
125 | finally: |
---|
126 | os.remove(ininame) |
---|
127 | |
---|
128 | def test_package_configfile_non_existant(self): |
---|
129 | try: |
---|
130 | conf = Configuration(filename='doesnotexist.ini') |
---|
131 | self.fail('Expected ConfigFileNotFound') |
---|
132 | except ConfigFileNotFound, e: |
---|
133 | self.assertEquals(str(e), |
---|
134 | "Configuration file 'doesnotexist.ini' not found.") |
---|
135 | |
---|
136 | def test_get_dirpath_non_existant(self): |
---|
137 | tempdir = tempfile.mkdtemp() |
---|
138 | os.rmdir(tempdir) |
---|
139 | config = Configuration(properties={'somepkg.home': tempdir}) |
---|
140 | self.assertEqual(None, config.get_dirpath('somepkg.home')) |
---|
141 | |
---|
142 | def test_get_dirpath(self): |
---|
143 | tempdir = tempfile.mkdtemp() |
---|
144 | try: |
---|
145 | config = Configuration(properties={'somepkg.home': tempdir}) |
---|
146 | self.assertEqual(tempdir, config.get_dirpath('somepkg.home')) |
---|
147 | finally: |
---|
148 | os.rmdir(tempdir) |
---|
149 | |
---|
150 | def test_get_filepath_non_existant(self): |
---|
151 | testfile, testname = tempfile.mkstemp(prefix='bitten_test') |
---|
152 | os.close(testfile) |
---|
153 | os.remove(testname) |
---|
154 | config = Configuration(properties={'somepkg.path': testname}) |
---|
155 | self.assertEqual(None, config.get_filepath('somepkg.path')) |
---|
156 | |
---|
157 | def test_get_filepath(self): |
---|
158 | testfile = tempfile.NamedTemporaryFile(prefix='bitten_test') |
---|
159 | config = Configuration(properties={'somepkg.path': testfile.name}) |
---|
160 | self.assertEqual(testfile.name, config.get_filepath('somepkg.path')) |
---|
161 | |
---|
162 | def test_interpolate(self): |
---|
163 | config = Configuration(properties={ |
---|
164 | 'python.version': '2.3.5', |
---|
165 | 'python.path': '/usr/local/bin/python2.3' |
---|
166 | }) |
---|
167 | self.assertEqual('/usr/local/bin/python2.3', |
---|
168 | config.interpolate('${python.path}')) |
---|
169 | self.assertEqual('foo /usr/local/bin/python2.3 bar', |
---|
170 | config.interpolate('foo ${python.path} bar')) |
---|
171 | |
---|
172 | def test_interpolate_default(self): |
---|
173 | config = Configuration() |
---|
174 | self.assertEqual('python2.3', |
---|
175 | config.interpolate('${python.path:python2.3}')) |
---|
176 | self.assertEqual('foo python2.3 bar', |
---|
177 | config.interpolate('foo ${python.path:python2.3} bar')) |
---|
178 | |
---|
179 | def test_interpolate_missing(self): |
---|
180 | config = Configuration() |
---|
181 | self.assertEqual('${python.path}', |
---|
182 | config.interpolate('${python.path}')) |
---|
183 | self.assertEqual('foo ${python.path} bar', |
---|
184 | config.interpolate('foo ${python.path} bar')) |
---|
185 | |
---|
186 | def test_interpolate_environment(self): |
---|
187 | config = Configuration() |
---|
188 | os.environ['BITTEN_TEST'] = 'foo' |
---|
189 | try: |
---|
190 | # regular substitutions |
---|
191 | self.assertEquals(os.environ['BITTEN_TEST'], |
---|
192 | config.interpolate('$BITTEN_TEST')) |
---|
193 | self.assertEquals(os.environ['BITTEN_TEST'], |
---|
194 | config.interpolate('${BITTEN_TEST}')) |
---|
195 | if os.name == 'posix': |
---|
196 | # case-sensitive |
---|
197 | self.assertEquals('${bitten_test}', |
---|
198 | config.interpolate('${bitten_test}')) |
---|
199 | self.assertEquals('$bitten_test', |
---|
200 | config.interpolate('$bitten_test')) |
---|
201 | elif os.name == 'nt': |
---|
202 | # case-insensitive |
---|
203 | self.assertEquals(os.environ['bitten_test'], |
---|
204 | config.interpolate('$bitten_test')) |
---|
205 | self.assertEquals(os.environ['bitten_test'], |
---|
206 | config.interpolate('${bitten_test}')) |
---|
207 | finally: |
---|
208 | del os.environ['BITTEN_TEST'] |
---|
209 | |
---|
210 | def suite(): |
---|
211 | suite = unittest.TestSuite() |
---|
212 | suite.addTest(unittest.makeSuite(ConfigurationTestCase, 'test')) |
---|
213 | return suite |
---|
214 | |
---|
215 | if __name__ == '__main__': |
---|
216 | unittest.main(defaultTest='suite') |
---|