1
2
3
4
5
6
7
8
9 """Test externals checking"""
10
11 import unittest
12
13 from mvpa import cfg
14 from mvpa.base import externals
15 from mvpa.support import copy
16
17 -class TestExternals(unittest.TestCase):
18
20 self.backup = []
21
22 self.cfgstr = str(cfg)
23
24 if cfg.has_section('externals'):
25 self.backup = copy.deepcopy(cfg.items('externals'))
26 cfg.remove_section('externals')
27
28
30 if len(self.backup):
31
32 if cfg.has_section('externals'):
33 cfg.remove_section('externals')
34 cfg.add_section('externals')
35 for o,v in self.backup:
36 cfg.set('externals', o,v)
37
38
39
40 self.failUnlessEqual(sorted(self.cfgstr.split('\n')),
41 sorted(str(cfg).split('\n')))
42
43
44 - def testExternals(self):
45 self.failUnlessRaises(ValueError, externals.exists, 'BoGuS')
46
47
49
50
51
52 class Checker(object):
53 """Helper class to increment count of actual checks"""
54 def __init__(self): self.checked = 0
55 def check(self): self.checked += 1
56
57 checker = Checker()
58
59 externals._KNOWN['checker'] = 'checker.check()'
60 externals.__dict__['checker'] = checker
61 externals.exists('checker')
62 self.failUnlessEqual(checker.checked, 1)
63 externals.exists('checker')
64 self.failUnlessEqual(checker.checked, 1)
65 externals.exists('checker', force=True)
66 self.failUnlessEqual(checker.checked, 2)
67 externals.exists('checker')
68 self.failUnlessEqual(checker.checked, 2)
69
70
71 externals.__dict__.pop('checker')
72 externals._KNOWN.pop('checker')
73
74
76
77 externals._KNOWN['checker2'] = 'raise ImportError'
78
79 self.failUnless(not externals.exists('checker2'),
80 msg="Should be False on 1st invocation")
81
82 self.failUnless(not externals.exists('checker2'),
83 msg="Should be False on 2nd invocation as well")
84
85 externals._KNOWN.pop('checker2')
86
87
88
91
92
93 if __name__ == '__main__':
94 import runner
95