summaryrefslogtreecommitdiff
path: root/test/unittest_manager.py
blob: 7059f3445bb6a791aa4aba76c3e9f2f8b3e08cd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of astroid.
#
# astroid is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 2.1 of the License, or (at your
# option) any later version.
#
# astroid is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with astroid. If not, see <http://www.gnu.org/licenses/>.
from logilab.common.testlib import TestCase, unittest_main

import sys
from os.path import join, abspath, dirname
from astroid.manager import AstroidManager, _silent_no_wrap
from astroid.bases import  BUILTINS

DATA = join(dirname(abspath(__file__)), 'data')

class AstroidManagerTC(TestCase):
    def setUp(self):
        self.manager = AstroidManager()
        self.manager.astroid_cache.clear()

    def test_astroid_from_module(self):
        import unittest
        astroid = self.manager.astroid_from_module(unittest)
        self.assertEqual(astroid.pure_python, True)
        import time
        astroid = self.manager.astroid_from_module(time)
        self.assertEqual(astroid.pure_python, False)

    def test_astroid_from_class(self):
        astroid = self.manager.astroid_from_class(int)
        self.assertEqual(astroid.name, 'int')
        self.assertEqual(astroid.parent.frame().name, BUILTINS)

        astroid = self.manager.astroid_from_class(object)
        self.assertEqual(astroid.name, 'object')
        self.assertEqual(astroid.parent.frame().name, BUILTINS)
        self.assertIn('__setattr__', astroid)

    def _test_astroid_from_zip(self, archive):
        origpath = sys.path[:]
        sys.modules.pop('mypypa', None)
        archive_path = join(DATA, archive)
        sys.path.insert(0, archive_path)
        try:
            module = self.manager.astroid_from_module_name('mypypa')
            self.assertEqual(module.name, 'mypypa')
            self.assertTrue(module.file.endswith('%s/mypypa' % archive),
                            module.file)
        finally:
            # remove the module, else after importing egg, we don't get the zip
            if 'mypypa' in self.manager.astroid_cache:
                del self.manager.astroid_cache['mypypa']
                del self.manager._mod_file_cache[('mypypa', None)]
            if archive_path in sys.path_importer_cache:
                del sys.path_importer_cache[archive_path]
            sys.path = origpath

    def test_astroid_from_module_name_egg(self):
        self._test_astroid_from_zip('MyPyPa-0.1.0-py2.5.egg')

    def test_astroid_from_module_name_zip(self):
        self._test_astroid_from_zip('MyPyPa-0.1.0-py2.5.zip')

    def test_from_directory(self):
        obj = self.manager.project_from_files([DATA], _silent_no_wrap, 'data')
        self.assertEqual(obj.name, 'data')
        self.assertEqual(obj.path, join(DATA, '__init__.py'))

    def test_project_node(self):
        obj = self.manager.project_from_files([DATA], _silent_no_wrap, 'data')
        expected = set(['SSL1', '__init__', 'all', 'appl', 'format', 'module',
                        'module2', 'noendingnewline', 'nonregr', 'notall'])
        expected = ['data', 'data.SSL1', 'data.SSL1.Connection1',
                    'data.absimport', 'data.all',
                    'data.appl', 'data.appl.myConnection', 'data.email', 'data.format',
                    'data.module', 'data.module2', 'data.noendingnewline',
                    'data.nonregr', 'data.notall']
        self.assertListEqual(sorted(k for k in obj.keys()), expected)

    def test_do_not_expose_main(self):
      obj = self.manager.astroid_from_module_name('__main__')
      self.assertEqual(obj.name, '__main__')
      self.assertEqual(obj.items(), [])


class BorgAstroidManagerTC(TestCase):

    def test_borg(self):
        """test that the AstroidManager is really a borg, i.e. that two different
        instances has same cache"""
        first_manager = AstroidManager()
        built = first_manager.astroid_from_module_name(BUILTINS)

        second_manager = AstroidManager()
        second_built = first_manager.astroid_from_module_name(BUILTINS)
        self.assertIs(built, second_built)


if __name__ == '__main__':
    unittest_main()