Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / astroid / tests / unittest_manager.py @ 745

History | View | Annotate | Download (8.57 KB)

1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
3
#
4
# This file is part of astroid.
5
#
6
# astroid is free software: you can redistribute it and/or modify it
7
# under the terms of the GNU Lesser General Public License as published by the
8
# Free Software Foundation, either version 2.1 of the License, or (at your
9
# option) any later version.
10
#
11
# astroid is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14
# for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with astroid. If not, see <http://www.gnu.org/licenses/>.
18
import os
19
import platform
20
import sys
21
import unittest
22

    
23
import six
24

    
25
from astroid import exceptions
26
from astroid import manager
27
from astroid.tests import resources
28

    
29

    
30
BUILTINS = six.moves.builtins.__name__
31

    
32

    
33
def _get_file_from_object(obj):
34
    if platform.python_implementation() == 'Jython':
35
        return obj.__file__.split("$py.class")[0] + ".py"
36
    if sys.version_info > (3, 0):
37
        return obj.__file__
38
    if not obj.__file__.endswith(".py"):
39
        return obj.__file__[:-1]
40
    return obj.__file__
41

    
42

    
43
class AstroidManagerTest(resources.SysPathSetup,
44
                         resources.AstroidCacheSetupMixin,
45
                         unittest.TestCase):
46

    
47
    def setUp(self):
48
        super(AstroidManagerTest, self).setUp()
49
        self.manager = manager.AstroidManager()
50
        self.manager.clear_cache(self._builtins) # take care of borg
51

    
52
    def test_ast_from_file(self):
53
        filepath = unittest.__file__
54
        astroid = self.manager.ast_from_file(filepath)
55
        self.assertEqual(astroid.name, 'unittest')
56
        self.assertIn('unittest', self.manager.astroid_cache)
57

    
58
    def test_ast_from_file_cache(self):
59
        filepath = unittest.__file__
60
        self.manager.ast_from_file(filepath)
61
        astroid = self.manager.ast_from_file('unhandledName', 'unittest')
62
        self.assertEqual(astroid.name, 'unittest')
63
        self.assertIn('unittest', self.manager.astroid_cache)
64

    
65
    def test_ast_from_file_astro_builder(self):
66
        filepath = unittest.__file__
67
        astroid = self.manager.ast_from_file(filepath, None, True, True)
68
        self.assertEqual(astroid.name, 'unittest')
69
        self.assertIn('unittest', self.manager.astroid_cache)
70

    
71
    def test_ast_from_file_name_astro_builder_exception(self):
72
        self.assertRaises(exceptions.AstroidBuildingException,
73
                          self.manager.ast_from_file, 'unhandledName')
74

    
75
    def test_do_not_expose_main(self):
76
        obj = self.manager.ast_from_module_name('__main__')
77
        self.assertEqual(obj.name, '__main__')
78
        self.assertEqual(obj.items(), [])
79

    
80
    def test_ast_from_module_name(self):
81
        astroid = self.manager.ast_from_module_name('unittest')
82
        self.assertEqual(astroid.name, 'unittest')
83
        self.assertIn('unittest', self.manager.astroid_cache)
84

    
85
    def test_ast_from_module_name_not_python_source(self):
86
        astroid = self.manager.ast_from_module_name('time')
87
        self.assertEqual(astroid.name, 'time')
88
        self.assertIn('time', self.manager.astroid_cache)
89
        self.assertEqual(astroid.pure_python, False)
90

    
91
    def test_ast_from_module_name_astro_builder_exception(self):
92
        self.assertRaises(exceptions.AstroidBuildingException,
93
                          self.manager.ast_from_module_name,
94
                          'unhandledModule')
95

    
96
    def _test_ast_from_zip(self, archive):
97
        origpath = sys.path[:]
98
        sys.modules.pop('mypypa', None)
99
        archive_path = resources.find(archive)
100
        sys.path.insert(0, archive_path)
101
        try:
102
            module = self.manager.ast_from_module_name('mypypa')
103
            self.assertEqual(module.name, 'mypypa')
104
            end = os.path.join(archive, 'mypypa')
105
            self.assertTrue(module.source_file.endswith(end),
106
                            "%s doesn't endswith %s" % (module.source_file, end))
107
        finally:
108
            # remove the module, else after importing egg, we don't get the zip
109
            if 'mypypa' in self.manager.astroid_cache:
110
                del self.manager.astroid_cache['mypypa']
111
                del self.manager._mod_file_cache[('mypypa', None)]
112
            if archive_path in sys.path_importer_cache:
113
                del sys.path_importer_cache[archive_path]
114
            sys.path = origpath
115

    
116
    def test_ast_from_module_name_egg(self):
117
        self._test_ast_from_zip(
118
            os.path.sep.join(['data', os.path.normcase('MyPyPa-0.1.0-py2.5.egg')])
119
        )
120

    
121
    def test_ast_from_module_name_zip(self):
122
        self._test_ast_from_zip(
123
            os.path.sep.join(['data', os.path.normcase('MyPyPa-0.1.0-py2.5.zip')])
124
        )
125

    
126
    def test_zip_import_data(self):
127
        """check if zip_import_data works"""
128
        filepath = resources.find('data/MyPyPa-0.1.0-py2.5.zip/mypypa')
129
        astroid = self.manager.zip_import_data(filepath)
130
        self.assertEqual(astroid.name, 'mypypa')
131

    
132
    def test_zip_import_data_without_zipimport(self):
133
        """check if zip_import_data return None without zipimport"""
134
        self.assertEqual(self.manager.zip_import_data('path'), None)
135

    
136
    def test_file_from_module(self):
137
        """check if the unittest filepath is equals to the result of the method"""
138
        self.assertEqual(
139
            _get_file_from_object(unittest),
140
            self.manager.file_from_module_name('unittest', None)[0])
141

    
142
    def test_file_from_module_name_astro_building_exception(self):
143
        """check if the method launch a exception with a wrong module name"""
144
        self.assertRaises(exceptions.AstroidBuildingException,
145
                          self.manager.file_from_module_name, 'unhandledModule', None)
146

    
147
    def test_ast_from_module(self):
148
        astroid = self.manager.ast_from_module(unittest)
149
        self.assertEqual(astroid.pure_python, True)
150
        import time
151
        astroid = self.manager.ast_from_module(time)
152
        self.assertEqual(astroid.pure_python, False)
153

    
154
    def test_ast_from_module_cache(self):
155
        """check if the module is in the cache manager"""
156
        astroid = self.manager.ast_from_module(unittest)
157
        self.assertEqual(astroid.name, 'unittest')
158
        self.assertIn('unittest', self.manager.astroid_cache)
159

    
160
    def test_ast_from_class(self):
161
        astroid = self.manager.ast_from_class(int)
162
        self.assertEqual(astroid.name, 'int')
163
        self.assertEqual(astroid.parent.frame().name, BUILTINS)
164

    
165
        astroid = self.manager.ast_from_class(object)
166
        self.assertEqual(astroid.name, 'object')
167
        self.assertEqual(astroid.parent.frame().name, BUILTINS)
168
        self.assertIn('__setattr__', astroid)
169

    
170
    def test_ast_from_class_with_module(self):
171
        """check if the method works with the module name"""
172
        astroid = self.manager.ast_from_class(int, int.__module__)
173
        self.assertEqual(astroid.name, 'int')
174
        self.assertEqual(astroid.parent.frame().name, BUILTINS)
175

    
176
        astroid = self.manager.ast_from_class(object, object.__module__)
177
        self.assertEqual(astroid.name, 'object')
178
        self.assertEqual(astroid.parent.frame().name, BUILTINS)
179
        self.assertIn('__setattr__', astroid)
180

    
181
    def test_ast_from_class_attr_error(self):
182
        """give a wrong class at the ast_from_class method"""
183
        self.assertRaises(exceptions.AstroidBuildingException,
184
                          self.manager.ast_from_class, None)
185

    
186
    def testFailedImportHooks(self):
187
        def hook(modname):
188
            if modname == 'foo.bar':
189
                return unittest
190
            else:
191
                raise exceptions.AstroidBuildingException()
192

    
193
        with self.assertRaises(exceptions.AstroidBuildingException):
194
            self.manager.ast_from_module_name('foo.bar')
195
        self.manager.register_failed_import_hook(hook)
196
        self.assertEqual(unittest, self.manager.ast_from_module_name('foo.bar'))
197
        with self.assertRaises(exceptions.AstroidBuildingException):
198
            self.manager.ast_from_module_name('foo.bar.baz')
199
        del self.manager._failed_import_hooks[0]
200

    
201

    
202
class BorgAstroidManagerTC(unittest.TestCase):
203

    
204
    def test_borg(self):
205
        """test that the AstroidManager is really a borg, i.e. that two different
206
        instances has same cache"""
207
        first_manager = manager.AstroidManager()
208
        built = first_manager.ast_from_module_name(BUILTINS)
209

    
210
        second_manager = manager.AstroidManager()
211
        second_built = second_manager.ast_from_module_name(BUILTINS)
212
        self.assertIs(built, second_built)
213

    
214

    
215
if __name__ == '__main__':
216
    unittest.main()