| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TestEnvOps.py
Go to the documentation of this file.
1 '''
2 Created on Jul 12, 2011
3
4 @author: mplajner
5 '''
6 import unittest
7 import os
8 import sys
9 import shutil
10 from tempfile import mkdtemp
11
12 from EnvConfig import Variable
13 from EnvConfig import Control
14
15 # Keep only some Variable processors.
16 saved_processors = Variable.processors
17 Variable.processors = [Variable.EnvExpander,
18 Variable.PathNormalizer,
19 Variable.DuplicatesRemover]
20
21 def buildDir(files, rootdir=os.curdir):
22 '''
23 Create a directory structure from the content of files.
24
25 @param files: a dictionary or list of pairs mapping a filename to the content
26 if the content is a dictionary, recurse
27 @param rootdir: base directory
28 '''
29 if type(files) is dict:
30 files = files.items()
31
32 # ensure that the root exists (to allow empty directories)
33 if not os.path.exists(rootdir):
34 os.makedirs(rootdir)
35
36 # create all the entries
37 for filename, data in files:
38 filename = os.path.join(rootdir, filename)
39 if type(data) is dict:
40 buildDir(data, filename)
41 else:
42 d = os.path.dirname(filename)
43 if not os.path.exists(d):
44 os.makedirs(d)
45 f = open(filename, "w")
46 if data:
47 f.write(data)
48 f.close()
49
50 class TempDir(object):
51 '''
52 Class for easy creation, use and removal of temporary directory structures.
53 '''
54 def __init__(self, files=None):
55 self.tmpdir = mkdtemp()
56 if files is None:
57 files = {}
58 buildDir(files, self.tmpdir)
59
60 def __del__(self):
61 shutil.rmtree(self.tmpdir, ignore_errors=False)
62
63 def __call__(self, *args):
64 '''
65 Return the absolute path to a file in the temporary directory.
66 '''
67 return os.path.join(self.tmpdir, *args)
68
69 class Test(unittest.TestCase):
70
71
72 def setUp(self):
73 pass
74
75 def tearDown(self):
76 pass
77
78
79 def testValues(self):
80 '''Test of value appending, prepending, setting, unsetting, removing'''
81 control = Control.Environment()
82
83 self.assertFalse('MY_PATH' in control.vars())
84 control.append('MY_PATH', 'newValue')
85 self.assertTrue('MY_PATH' in control.vars())
86 var = control.var('MY_PATH')
87
88 control.append('MY_PATH', 'newValue:secondVal:valval')
89 self.assertTrue(var[len(var)-1] == 'valval')
90
91 self.assertTrue('newValue' in var)
92 control.remove('MY_PATH', 'newValue')
93 self.assertFalse('newValue' in var)
94
95 control.prepend('MY_PATH', 'newValue')
96 self.assertTrue('newValue' == var[0])
97
98 control.set('MY_PATH', 'hi:hello')
99 self.assertTrue(len(var) == 2)
100 self.assertTrue('hi' == var[0])
101
102 control.unset('MY_PATH')
103 self.assertTrue('MY_PATH' not in control)
104
105
106 def testWrite(self):
107 """XML file write and load test"""
108 control = Control.Environment(useAsWriter = True)
109 control.unset('MY_PATH')
110 control.set('MY_PATH', 'set:toDelete')
111 control.append('MY_PATH', 'appended:toDelete')
112 control.prepend('MY_PATH', 'prepended:toDelete')
113 control.remove('MY_PATH', 'toDelete')
114 control.finishXMLinput('testOutputFile.xml')
115
116 control = Control.Environment()
117 self.assertFalse('MY_PATH' in control.vars())
118 control.loadXML('testOutputFile.xml')
119
120 self.assertTrue('MY_PATH' in control.vars())
121 var = control.var('MY_PATH')
122 self.assertTrue(var[0] == 'prepended')
123 self.assertTrue(var[1] == 'set')
124 self.assertTrue(var[2] == 'appended')
125 self.assertFalse('toDelete' in var)
126
127 os.remove('testOutputFile.xml')
128
129
130 def testWriteWithList(self):
131 """XML file write and load test"""
132 control = Control.Environment(useAsWriter = True)
133 control.unset('MY_PATH')
134 control.set('MY_PATH', ['set','toDelete'])
135 control.append('MY_PATH', ['appended','toDelete'])
136 control.prepend('MY_PATH', ['prepended','toDelete'])
137 control.remove('MY_PATH', ['toDelete'])
138 control.finishXMLinput('testOutputFile.xml')
139
140 control = Control.Environment()
141 self.assertFalse('MY_PATH' in control.vars())
142 control.loadXML('testOutputFile.xml')
143
144 self.assertTrue('MY_PATH' in control.vars())
145 var = control.var('MY_PATH')
146 self.assertTrue(var[0] == 'prepended')
147 self.assertTrue(var[1] == 'set')
148 self.assertTrue(var[2] == 'appended')
149 self.assertFalse('toDelete' in var)
150
151 os.remove('testOutputFile.xml')
152
153
154 def testSaveToXML(self):
155 """XML file write and load test"""
156 control = Control.Environment()
157
158 control.unset('MY_PATH')
159 control.set('MY_PATH', 'set:toDelete')
160 control.append('MY_PATH', 'appended:toDelete')
161 control.prepend('MY_PATH', 'prepended:toDelete')
162 control.remove('MY_PATH', 'toDelete')
163 control.writeToXMLFile('testOutputFile.xml')
164
165 control = Control.Environment()
166 self.assertFalse('MY_PATH' in control.vars())
167 control.loadXML('testOutputFile.xml')
168
169 self.assertTrue('MY_PATH' in control.vars())
170 var = control.var('MY_PATH')
171 self.assertTrue(var[0] == 'prepended')
172 self.assertTrue(var[1] == 'set')
173 self.assertTrue(var[2] == 'appended')
174 self.assertFalse('toDelete' in var)
175
176 os.remove('testOutputFile.xml')
177
178 def testSaveToFile(self):
179 '''Test addition of variable to system'''
180 control = Control.Environment()
181
182 control.append('sysVar', 'newValue:lala')
183 control.writeToFile('setupFile.txt')
184
185 with open('setupFile.txt', "r") as f:
186 f.readline()
187 stri = f.readline()
188 f.close()
189
190 self.assertEqual(stri, 'export sysVar=newValue:lala\n')
191
192 os.remove('setupFile.txt')
193
194 def testSearch(self):
195 '''Testing searching in variables'''
196 control = Control.Environment()
197
198 control.append('MY_PATH', 'newValue:mess:something new:aaaabbcc')
199
200 def count(val, regExp = False):
201 return len(control.search('MY_PATH', val, regExp))
202
203 self.assertEqual(count('new'), 0)
204 self.assertEqual(count('newValue'), 1)
205
206 self.assertEqual(count('me', False), 0)
207 self.assertEqual(count('me', True), 2)
208
209 self.assertEqual(count('cc', False), 0)
210 self.assertEqual(count('cc', True), 1)
211
212 self.assertEqual(count('a{2}b{2}c{2}', True), 1)
213 self.assertEqual(count('a{2}b{2}', True), 1)
214 self.assertEqual(count('a{1}b{2}c{2}', True), 1)
215 self.assertEqual(count('a{1}b{1}c{2}', True), 0)
216 self.assertEqual(count('a{1,2}b{1,2}c{2}', True), 1)
217 self.assertEqual(count('a{2,3}', True), 1)
218 self.assertEqual(count('a{2,3}?', True), 1)
219
220 def testVariables(self):
221 '''Tests variables creation and redeclaration.'''
222 control = Control.Environment()
223
224 control.append('MY_PATH', 'newValue')
225 self.assertFalse(control.var('MY_PATH').local)
226 self.assertTrue(isinstance(control.var('MY_PATH'),Variable.List))
227
228 control.declare('loc', 'list', True)
229 self.assertTrue(control.var('loc').local)
230 self.assertTrue(isinstance(control.var('loc'),Variable.List))
231
232 control.declare('myVar2', 'scalar', False)
233 self.assertFalse(control.var('myVar2').local)
234 self.assertTrue(isinstance(control.var('myVar2'),Variable.Scalar))
235
236 control.declare('loc2', 'scalar', True)
237 self.assertTrue(control.var('loc2').local)
238 self.assertTrue(isinstance(control.var('loc2'),Variable.Scalar))
239
240 control.declare('MY_PATH', 'list', False)
241 self.failUnlessRaises(Variable.EnvError, control.declare, 'MY_PATH', 'list', True)
242 self.failUnlessRaises(Variable.EnvError, control.declare, 'MY_PATH', 'scalar', True)
243 self.failUnlessRaises(Variable.EnvError, control.declare, 'MY_PATH', 'scalar', True)
244
245 control.declare('loc', 'list', True)
246 self.failUnlessRaises(Variable.EnvError, control.declare,'loc', 'list', False)
247 self.failUnlessRaises(Variable.EnvError, control.declare,'loc', 'scalar', True)
248 self.failUnlessRaises(Variable.EnvError, control.declare,'loc', 'scalar', True)
249
250 control.declare('myVar2', 'scalar', False)
251 self.failUnlessRaises(Variable.EnvError, control.declare,'myVar2', 'list', False)
252 self.failUnlessRaises(Variable.EnvError, control.declare,'myVar2', 'list', True)
253 self.failUnlessRaises(Variable.EnvError, control.declare,'myVar2', 'scalar', True)
254
255 control.declare('loc2', 'scalar', True)
256 self.failUnlessRaises(Variable.EnvError, control.declare,'loc2', 'list', False)
257 self.failUnlessRaises(Variable.EnvError, control.declare,'loc2', 'list', True)
258 self.failUnlessRaises(Variable.EnvError, control.declare,'loc2', 'scalar', False)
259
260
261 def testDelete(self):
262 control = Control.Environment()
263
264 control.append('MY_PATH','myVal:anotherVal:lastVal')
265 control.remove('MY_PATH','anotherVal')
266
267 self.assertFalse('anotherVal' in control['MY_PATH'])
268 self.assertTrue('myVal' in control['MY_PATH'])
269 self.assertTrue('lastVal' in control['MY_PATH'])
270
271 control.set('MY_PATH','myVal:anotherVal:lastVal:else')
272 control.remove('MY_PATH', '^anotherVal$', False)
273 self.assertTrue('anotherVal' in control['MY_PATH'])
274 control.remove('MY_PATH', '^anotherVal$', True)
275 self.assertFalse('anotherVal' in control['MY_PATH'])
276 self.assertTrue('myVal' in control['MY_PATH'])
277 self.assertTrue('lastVal' in control['MY_PATH'])
278 self.assertTrue('lastVal' in control['MY_PATH'])
279 control.remove('MY_PATH', 'Val', True)
280 self.assertTrue('else' in control['MY_PATH'])
281 self.assertTrue(len(control['MY_PATH']) == 1)
282
283
284 control.declare('myLoc', 'scalar', False)
285 control.append('myLoc','myVal:anotherVal:lastVal')
286 control.remove('myLoc', 'Val:', True)
287 self.assertTrue(str(control['myLoc']) == 'myanotherlastVal')
288
289
290 def testSystemEnvironment(self):
291 control = Control.Environment()
292
293 os.environ['MY_PATH'] = '$myVal'
294 os.environ['myScal'] = '$myVal'
295
296 control.set('ABC','anyValue')
297 control.declare('MY_PATH', 'list', False)
298 control.append('MY_PATH','$ABC')
299 self.assertTrue(control['MY_PATH'].value(True) == '$myVal:anyValue')
300
301 control.declare('myScal', 'scalar', False)
302 control.append('myScal', '$ABC')
303 self.assertTrue(control['myScal'].value(True) == '$myValanyValue')
304
305
306 def testDependencies(self):
307 control = Control.Environment()
308
309 control.declare('myVar', 'list', False)
310
311 control.declare('loc', 'list', True)
312 control.append('loc','locVal')
313 control.append('loc','locVal2')
314
315 control.declare('scal', 'scalar', False)
316 control.append('scal','scalVal')
317 control.append('scal','scalVal2')
318
319 control.declare('scal2', 'scalar', True)
320 control.append('scal2','locScal')
321 control.append('scal2','locScal2')
322
323 control.set('myVar', 'newValue:$loc:endValue')
324 self.assertEqual(str(control['myVar']),'newValue:locVal:locVal2:endValue')
325
326 control.set('myVar', 'newValue:$scal:endValue')
327 self.assertEqual(str(control['myVar']),'newValue:scalValscalVal2:endValue')
328
329 control.set('myVar', 'new${scal}Value:endValue')
330 self.assertEqual(str(control['myVar']),'newscalValscalVal2Value:endValue')
331
332 control.set('myVar', 'bla:$myVar:Value')
333 self.assertEqual(str(control['myVar']),'bla:newscalValscalVal2Value:endValue:Value')
334
335 control.set('scal', 'new${scal2}Value')
336 self.assertEqual(str(control['scal']),'newlocScallocScal2Value')
337
338 control.set('scal', 'new${loc}Value')
339 self.assertEqual(str(control['scal']),'newlocVal:locVal2Value')
340
341 control.set('scal2', 'new${scal2}Value')
342 self.assertEqual(str(control['scal2']),'newlocScallocScal2Value')
343
344 def testInclude(self):
345 tmp = TempDir({'first.xml':
346 '''<?xml version="1.0" ?>
347 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
348 <env:set variable="main">first</env:set>
349 <env:append variable="test_path">data1</env:append>
350 <env:include>first_inc.xml</env:include>
351 </env:config>''',
352 'second.xml':
353 '''<?xml version="1.0" ?>
354 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
355 <env:set variable="main">second</env:set>
356 <env:include>second_inc.xml</env:include>
357 <env:append variable="test_path">data1</env:append>
358 </env:config>''',
359 'third.xml':
360 '''<?xml version="1.0" ?>
361 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
362 <env:set variable="main">third</env:set>
363 <env:append variable="test_path">data1</env:append>
364 <env:include>subdir/first_inc.xml</env:include>
365 </env:config>''',
366 'fourth.xml':
367 '''<?xml version="1.0" ?>
368 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
369 <env:set variable="main">fourth</env:set>
370 <env:include hints="subdir2">fourth_inc.xml</env:include>
371 </env:config>''',
372 'recursion.xml':
373 '''<?xml version="1.0" ?>
374 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
375 <env:set variable="main">recursion</env:set>
376 <env:include>recursion.xml</env:include>
377 </env:config>''',
378 'first_inc.xml':
379 '''<?xml version="1.0" ?>
380 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
381 <env:append variable="test_path">data2</env:append>
382 <env:append variable="derived">another_${main}</env:append>
383 </env:config>''',
384 'subdir': {'second_inc.xml':
385 '''<?xml version="1.0" ?>
386 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
387 <env:append variable="test_path">data0</env:append>
388 <env:set variable="map">this_is_second_inc</env:set>
389 </env:config>''',
390 'first_inc.xml':
391 '''<?xml version="1.0" ?>
392 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
393 <env:append variable="derived">second_${main}</env:append>
394 </env:config>''',
395 'fourth_inc.xml':
396 '''<?xml version="1.0" ?>
397 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
398 <env:append variable="included">from subdir</env:append>
399 </env:config>''',},
400 'subdir2': {'fourth_inc.xml':
401 '''<?xml version="1.0" ?>
402 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
403 <env:append variable="included">from subdir2</env:append>
404 </env:config>''',}})
405
406 # set the basic search path to the minimal default
407 if 'ENVXMLPATH' in os.environ:
408 del os.environ['ENVXMLPATH']
409 import EnvConfig
410 saved_path = list(EnvConfig.path)
411 EnvConfig.path[:] = ['.']
412
413 control = Control.Environment(searchPath=[])
414
415 #self.assertRaises(OSError, control.loadXML, tmp('first.xml'))
416 control.loadXML(tmp('first.xml'))
417 self.assertEqual(str(control['main']), 'first')
418 self.assertEqual(str(control['test_path']), 'data1:data2')
419 self.assertEqual(str(control['derived']), 'another_first')
420
421 control = Control.Environment(searchPath=[tmp()])
422 control.loadXML(tmp('first.xml'))
423 self.assertEqual(str(control['main']), 'first')
424 self.assertEqual(str(control['test_path']), 'data1:data2')
425 self.assertEqual(str(control['derived']), 'another_first')
426
427 control = Control.Environment(searchPath=[tmp()])
428 control.loadXML('first.xml')
429 self.assertEqual(str(control['main']), 'first')
430 self.assertEqual(str(control['test_path']), 'data1:data2')
431 self.assertEqual(str(control['derived']), 'another_first')
432
433 control = Control.Environment(searchPath=[tmp()])
434 self.assertRaises(OSError, control.loadXML, tmp('second.xml'))
435
436 control = Control.Environment(searchPath=[tmp(), tmp('subdir')])
437 control.loadXML(tmp('second.xml'))
438 self.assertEqual(str(control['main']), 'second')
439 self.assertEqual(str(control['test_path']), 'data0:data1')
440 self.assertEqual(str(control['map']), 'this_is_second_inc')
441
442 control = Control.Environment(searchPath=[tmp(), tmp('subdir')])
443 control.loadXML(tmp('first.xml'))
444 self.assertEqual(str(control['main']), 'first')
445 self.assertEqual(str(control['test_path']), 'data1:data2')
446 self.assertEqual(str(control['derived']), 'another_first')
447
448 control = Control.Environment(searchPath=[tmp('subdir'), tmp()])
449 control.loadXML(tmp('first.xml'))
450 self.assertEqual(str(control['main']), 'first')
451 self.assertEqual(str(control['test_path']), 'data1:data2')
452 self.assertEqual(str(control['derived']), 'another_first')
453
454 control = Control.Environment(searchPath=[tmp('subdir'), tmp()])
455 control.loadXML('first.xml')
456 self.assertEqual(str(control['main']), 'first')
457 self.assertEqual(str(control['test_path']), 'data1:data2')
458 self.assertEqual(str(control['derived']), 'another_first')
459
460 #os.environ['ENVXMLPATH'] = os.pathsep.join([tmp(), tmp('subdir')])
461 EnvConfig.path[:] = ['.', tmp(), tmp('subdir')]
462 control = Control.Environment(searchPath=[])
463 control.loadXML(tmp('second.xml'))
464 self.assertEqual(str(control['main']), 'second')
465 self.assertEqual(str(control['test_path']), 'data0:data1')
466 self.assertEqual(str(control['map']), 'this_is_second_inc')
467 #del os.environ['ENVXMLPATH']
468 EnvConfig.path[:] = ['.']
469
470 control = Control.Environment(searchPath=[])
471 control.loadXML(tmp('third.xml'))
472 self.assertEqual(str(control['main']), 'third')
473 self.assertEqual(str(control['test_path']), 'data1')
474 self.assertEqual(str(control['derived']), 'second_third')
475
476 control = Control.Environment(searchPath=[tmp('subdir')])
477 control.loadXML(tmp('fourth.xml'))
478 self.assertEqual(str(control['main']), 'fourth')
479 self.assertEqual(str(control['included']), 'from subdir')
480
481 control = Control.Environment(searchPath=[])
482 control.loadXML(tmp('fourth.xml'))
483 self.assertEqual(str(control['main']), 'fourth')
484 self.assertEqual(str(control['included']), 'from subdir2')
485
486 control = Control.Environment(searchPath=[])
487 #self.assertRaises(OSError, control.loadXML, tmp('first.xml'))
488 control.loadXML(tmp('recursion.xml'))
489
490 # restore search path
491 EnvConfig.path = saved_path
492
493
494 def testFileDir(self):
495 tmp = TempDir({'env.xml':
496 '''<?xml version="1.0" ?>
497 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
498 <env:set variable="mydirs">${.}</env:set>
499 <env:set variable="myparent">${.}/..</env:set>
500 </env:config>'''})
501
502 control = Control.Environment()
503 control.loadXML(tmp('env.xml'))
504 self.assertEqual(str(control['mydirs']), tmp())
505 self.assertEqual(str(control['myparent']), os.path.dirname(tmp()))
506
507 olddir = os.getcwd()
508 os.chdir(tmp())
509 try:
510 control = Control.Environment()
511 control.loadXML('env.xml')
512 self.assertEqual(str(control['mydirs']), tmp())
513 self.assertEqual(str(control['myparent']), os.path.dirname(tmp()))
514 finally:
515 os.chdir(olddir)
516
517 def testDefaults(self):
518 tmp = TempDir({'env.xml':
519 '''<?xml version="1.0" ?>
520 <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">
521 <env:default variable="var1">value1</env:default>
522 <env:declare variable="var2" local="true" />
523 <env:default variable="var2">test2</env:default>
524 </env:config>'''})
525
526 if 'var1' in os.environ:
527 del os.environ['var1']
528 control = Control.Environment()
529 control.loadXML(tmp('env.xml'))
530 self.assertEqual(str(control['var1']), "value1")
531 self.assertEqual(str(control['var2']), "test2")
532
533 os.environ['var1'] = "some_value"
534 control = Control.Environment()
535 control.loadXML(tmp('env.xml'))
536 self.assertEqual(str(control['var1']), "some_value")
537 self.assertEqual(str(control['var2']), "test2")
538
539
540 def testVariableManipulations(self):
541 l = Variable.List('PATH')
542
543 l.set("/usr/bin:/some//strange/../nice/./location")
544 assert l.value(asString=True) == "/usr/bin:/some/nice/location"
545
546 l.append("/another/path")
547 assert l.value(asString=True) == "/usr/bin:/some/nice/location:/another/path"
548
549 # duplicates removal
550 l.append("/usr/bin")
551 assert l.value(asString=True) == "/usr/bin:/some/nice/location:/another/path"
552 l.prepend("/another/path")
553 assert l.value(asString=True) == "/another/path:/usr/bin:/some/nice/location"
554
555 s = Variable.Scalar('VAR')
556
557 s.set("/usr/bin")
558 assert s.value(asString=True) == "/usr/bin"
559
560 s.set("/some//strange/../nice/./location")
561 assert s.value(asString=True) == "/some/nice/location"
562
563 # This is undefined
564 # l.set("http://cern.ch")
565
566 s.set("http://cern.ch")
567 assert s.value(asString=True) == "http://cern.ch"
568
569 if __name__ == "__main__":
570 #import sys;sys.argv = ['', 'Test.testName']
571 unittest.main()