6 import simplejson
as json
12 JSON_TYPE
= 'simplejson'
14 import mozharness
.base
.config
as config
15 from copy
import deepcopy
17 MH_DIR
= os
.path
.dirname(os
.path
.dirname(__file__
))
20 class TestParseConfigFile(unittest
.TestCase
):
21 def _get_json_config(self
, filename
=os
.path
.join(MH_DIR
, "configs", "test", "test.json"),
24 contents
= json
.load(fh
)
26 if 'output' == 'dict':
31 def _get_python_config(self
, filename
=os
.path
.join(MH_DIR
, "configs", "test", "test.py"),
35 execfile(filename
, global_dict
, local_dict
)
36 return local_dict
['config']
38 def test_json_config(self
):
39 c
= config
.BaseConfig(initial_config_file
='test/test.json')
40 content_dict
= self
._get
_json
_config
()
41 for key
in content_dict
.keys():
42 self
.assertEqual(content_dict
[key
], c
._config
[key
])
44 def test_python_config(self
):
45 c
= config
.BaseConfig(initial_config_file
='test/test.py')
46 config_dict
= self
._get
_python
_config
()
47 for key
in config_dict
.keys():
48 self
.assertEqual(config_dict
[key
], c
._config
[key
])
50 def test_illegal_config(self
):
51 self
.assertRaises(IOError, config
.parse_config_file
, "this_file_does_not_exist.py", search_path
="yadda")
53 def test_illegal_suffix(self
):
54 self
.assertRaises(RuntimeError, config
.parse_config_file
, "test/test.illegal_suffix")
56 def test_malformed_json(self
):
57 if JSON_TYPE
== 'simplejson':
58 self
.assertRaises(json
.decoder
.JSONDecodeError
, config
.parse_config_file
, "test/test_malformed.json")
60 self
.assertRaises(ValueError, config
.parse_config_file
, "test/test_malformed.json")
62 def test_malformed_python(self
):
63 self
.assertRaises(SyntaxError, config
.parse_config_file
, "test/test_malformed.py")
65 def test_multiple_config_files_override_string(self
):
66 c
= config
.BaseConfig(initial_config_file
='test/test.py')
67 c
.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py'])
68 self
.assertEqual(c
._config
['override_string'], 'yay')
70 def test_multiple_config_files_override_list(self
):
71 c
= config
.BaseConfig(initial_config_file
='test/test.py')
72 c
.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py'])
73 self
.assertEqual(c
._config
['override_list'], ['yay', 'worked'])
75 def test_multiple_config_files_override_dict(self
):
76 c
= config
.BaseConfig(initial_config_file
='test/test.py')
77 c
.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py'])
78 self
.assertEqual(c
._config
['override_dict'], {'yay': 'worked'})
80 def test_multiple_config_files_keep_string(self
):
81 c
= config
.BaseConfig(initial_config_file
='test/test.py')
82 c
.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py'])
83 self
.assertEqual(c
._config
['keep_string'], "don't change me")
85 def test_optional_config_files_override_value(self
):
86 c
= config
.BaseConfig(initial_config_file
='test/test.py')
87 c
.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py',
88 '--opt-cfg', 'test/test_optional.py'])
89 self
.assertEqual(c
._config
['opt_override'], "new stuff")
91 def test_optional_config_files_missing_config(self
):
92 c
= config
.BaseConfig(initial_config_file
='test/test.py')
93 c
.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py',
94 '--opt-cfg', 'test/test_optional.py,does_not_exist.py'])
95 self
.assertEqual(c
._config
['opt_override'], "new stuff")
97 def test_optional_config_files_keep_string(self
):
98 c
= config
.BaseConfig(initial_config_file
='test/test.py')
99 c
.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py',
100 '--opt-cfg', 'test/test_optional.py'])
101 self
.assertEqual(c
._config
['keep_string'], "don't change me")
104 class TestReadOnlyDict(unittest
.TestCase
):
109 'e': ['f', 'g', {'turtles': ['turtle1']}],
111 'turtles': ['turtle1']
115 def get_unlocked_ROD(self
):
116 r
= config
.ReadOnlyDict(self
.control_dict
)
119 def get_locked_ROD(self
):
120 r
= config
.ReadOnlyDict(self
.control_dict
)
124 def test_create_ROD(self
):
125 r
= self
.get_unlocked_ROD()
126 self
.assertEqual(r
, self
.control_dict
,
127 msg
="can't transfer dict to ReadOnlyDict")
129 def test_pop_item(self
):
130 r
= self
.get_unlocked_ROD()
132 self
.assertEqual(len(r
), len(self
.control_dict
) - 1,
133 msg
="can't popitem() ReadOnlyDict when unlocked")
136 r
= self
.get_unlocked_ROD()
138 self
.assertEqual(len(r
), len(self
.control_dict
) - 1,
139 msg
="can't pop() ReadOnlyDict when unlocked")
142 r
= self
.get_unlocked_ROD()
144 self
.assertEqual(r
['e'], 'yarrr',
145 msg
="can't set var in ReadOnlyDict when unlocked")
148 r
= self
.get_unlocked_ROD()
150 self
.assertEqual(len(r
), len(self
.control_dict
) - 1,
151 msg
="can't del in ReadOnlyDict when unlocked")
153 def test_clear(self
):
154 r
= self
.get_unlocked_ROD()
156 self
.assertEqual(r
, {},
157 msg
="can't clear() ReadOnlyDict when unlocked")
159 def test_set_default(self
):
160 r
= self
.get_unlocked_ROD()
161 for key
in self
.control_dict
.keys():
162 r
.setdefault(key
, self
.control_dict
[key
])
163 self
.assertEqual(r
, self
.control_dict
,
164 msg
="can't setdefault() ReadOnlyDict when unlocked")
166 def test_locked_set(self
):
167 r
= self
.get_locked_ROD()
168 # TODO use |with self.assertRaises(AssertionError):| if/when we're
175 self
.assertEqual(0, 1, msg
="can set r['e'] when locked")
177 def test_locked_del(self
):
178 r
= self
.get_locked_ROD()
184 self
.assertEqual(0, 1, "can del r['e'] when locked")
186 def test_locked_popitem(self
):
187 r
= self
.get_locked_ROD()
188 self
.assertRaises(AssertionError, r
.popitem
)
190 def test_locked_update(self
):
191 r
= self
.get_locked_ROD()
192 self
.assertRaises(AssertionError, r
.update
, {})
194 def test_locked_set_default(self
):
195 r
= self
.get_locked_ROD()
196 self
.assertRaises(AssertionError, r
.setdefault
, {})
198 def test_locked_pop(self
):
199 r
= self
.get_locked_ROD()
200 self
.assertRaises(AssertionError, r
.pop
)
202 def test_locked_clear(self
):
203 r
= self
.get_locked_ROD()
204 self
.assertRaises(AssertionError, r
.clear
)
206 def test_locked_second_level_dict_pop(self
):
207 r
= self
.get_locked_ROD()
208 self
.assertRaises(AssertionError, r
['c'].update
, {})
210 def test_locked_second_level_list_pop(self
):
211 r
= self
.get_locked_ROD()
212 with self
.assertRaises(AttributeError):
215 def test_locked_third_level_mutate(self
):
216 r
= self
.get_locked_ROD()
217 with self
.assertRaises(AttributeError):
218 r
['d']['turtles'].append('turtle2')
220 def test_locked_object_in_tuple_mutate(self
):
221 r
= self
.get_locked_ROD()
222 with self
.assertRaises(AttributeError):
223 r
['e'][2]['turtles'].append('turtle2')
225 def test_locked_second_level_dict_pop2(self
):
226 r
= self
.get_locked_ROD()
227 self
.assertRaises(AssertionError, r
['c'].update
, {})
229 def test_locked_second_level_list_pop2(self
):
230 r
= self
.get_locked_ROD()
231 with self
.assertRaises(AttributeError):
234 def test_locked_third_level_mutate2(self
):
235 r
= self
.get_locked_ROD()
236 with self
.assertRaises(AttributeError):
237 r
['d']['turtles'].append('turtle2')
239 def test_locked_object_in_tuple_mutate2(self
):
240 r
= self
.get_locked_ROD()
241 with self
.assertRaises(AttributeError):
242 r
['e'][2]['turtles'].append('turtle2')
244 def test_locked_deepcopy_set(self
):
245 r
= self
.get_locked_ROD()
248 self
.assertEqual(c
['e'], 'hey', "can't set var in ROD after deepcopy")
251 class TestActions(unittest
.TestCase
):
252 all_actions
= ['a', 'b', 'c', 'd', 'e']
253 default_actions
= ['b', 'c', 'd']
255 def test_verify_actions(self
):
256 c
= config
.BaseConfig(initial_config_file
='test/test.json')
258 c
.verify_actions(['not_a_real_action'])
262 self
.assertEqual(0, 1, msg
="verify_actions() didn't die on invalid action")
263 c
= config
.BaseConfig(initial_config_file
='test/test.json')
264 returned_actions
= c
.verify_actions(c
.all_actions
)
265 self
.assertEqual(c
.all_actions
, returned_actions
,
266 msg
="returned actions from verify_actions() changed")
268 def test_default_actions(self
):
269 c
= config
.BaseConfig(default_actions
=self
.default_actions
,
270 all_actions
=self
.all_actions
,
271 initial_config_file
='test/test.json')
272 self
.assertEqual(self
.default_actions
, c
.get_actions(),
273 msg
="default_actions broken")
275 def test_no_action1(self
):
276 c
= config
.BaseConfig(default_actions
=self
.default_actions
,
277 all_actions
=self
.all_actions
,
278 initial_config_file
='test/test.json')
279 c
.parse_args(args
=['foo', '--no-action', 'a'])
280 self
.assertEqual(self
.default_actions
, c
.get_actions(),
281 msg
="--no-ACTION broken")
283 def test_no_action2(self
):
284 c
= config
.BaseConfig(default_actions
=self
.default_actions
,
285 all_actions
=self
.all_actions
,
286 initial_config_file
='test/test.json')
287 c
.parse_args(args
=['foo', '--no-c'])
288 self
.assertEqual(['b', 'd'], c
.get_actions(),
289 msg
="--no-ACTION broken")
291 def test_add_action(self
):
292 c
= config
.BaseConfig(default_actions
=self
.default_actions
,
293 all_actions
=self
.all_actions
,
294 initial_config_file
='test/test.json')
295 c
.parse_args(args
=['foo', '--add-action', 'e'])
296 self
.assertEqual(['b', 'c', 'd', 'e'], c
.get_actions(),
297 msg
="--add-action ACTION broken")
299 def test_only_action(self
):
300 c
= config
.BaseConfig(default_actions
=self
.default_actions
,
301 all_actions
=self
.all_actions
,
302 initial_config_file
='test/test.json')
303 c
.parse_args(args
=['foo', '--a', '--e'])
304 self
.assertEqual(['a', 'e'], c
.get_actions(),
305 msg
="--ACTION broken")
307 if __name__
== '__main__':