Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unittest_modutils.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2014-2016 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2014 Google, Inc.
  4. # Copyright (c) 2015 Florian Bruhin <me@the-compiler.org>
  5. # Copyright (c) 2015 Radosław Ganczarek <radoslaw@ganczarek.in>
  6. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  7. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  8. """
  9. unit tests for module modutils (module manipulation utilities)
  10. """
  11. import email
  12. import os
  13. import sys
  14. import unittest
  15. from xml import etree
  16. import astroid
  17. from astroid.interpreter._import import spec
  18. from astroid import modutils
  19. from astroid.tests import resources
  20. def _get_file_from_object(obj):
  21. return modutils._path_from_filename(obj.__file__)
  22. class ModuleFileTest(unittest.TestCase):
  23. package = "mypypa"
  24. def tearDown(self):
  25. for k in list(sys.path_importer_cache):
  26. if 'MyPyPa' in k:
  27. del sys.path_importer_cache[k]
  28. def test_find_zipped_module(self):
  29. found_spec = spec.find_spec(
  30. [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.zip')])
  31. self.assertEqual(found_spec.type,
  32. spec.ModuleType.PY_ZIPMODULE)
  33. self.assertEqual(found_spec.location.split(os.sep)[-3:],
  34. ["data", "MyPyPa-0.1.0-py2.5.zip", self.package])
  35. def test_find_egg_module(self):
  36. found_spec = spec.find_spec(
  37. [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.egg')])
  38. self.assertEqual(found_spec.type,
  39. spec.ModuleType.PY_ZIPMODULE)
  40. self.assertEqual(found_spec.location.split(os.sep)[-3:],
  41. ["data", "MyPyPa-0.1.0-py2.5.egg", self.package])
  42. class LoadModuleFromNameTest(unittest.TestCase):
  43. """ load a python module from it's name """
  44. def test_knownValues_load_module_from_name_1(self):
  45. self.assertEqual(modutils.load_module_from_name('sys'), sys)
  46. def test_knownValues_load_module_from_name_2(self):
  47. self.assertEqual(modutils.load_module_from_name('os.path'), os.path)
  48. def test_raise_load_module_from_name_1(self):
  49. self.assertRaises(ImportError,
  50. modutils.load_module_from_name, 'os.path', use_sys=0)
  51. class GetModulePartTest(unittest.TestCase):
  52. """given a dotted name return the module part of the name"""
  53. def test_knownValues_get_module_part_1(self):
  54. self.assertEqual(modutils.get_module_part('astroid.modutils'),
  55. 'astroid.modutils')
  56. def test_knownValues_get_module_part_2(self):
  57. self.assertEqual(modutils.get_module_part('astroid.modutils.get_module_part'),
  58. 'astroid.modutils')
  59. def test_knownValues_get_module_part_3(self):
  60. """relative import from given file"""
  61. self.assertEqual(modutils.get_module_part('node_classes.AssName',
  62. modutils.__file__), 'node_classes')
  63. def test_knownValues_get_compiled_module_part(self):
  64. self.assertEqual(modutils.get_module_part('math.log10'), 'math')
  65. self.assertEqual(modutils.get_module_part('math.log10', __file__), 'math')
  66. def test_knownValues_get_builtin_module_part(self):
  67. self.assertEqual(modutils.get_module_part('sys.path'), 'sys')
  68. self.assertEqual(modutils.get_module_part('sys.path', '__file__'), 'sys')
  69. def test_get_module_part_exception(self):
  70. self.assertRaises(ImportError, modutils.get_module_part, 'unknown.module',
  71. modutils.__file__)
  72. class ModPathFromFileTest(unittest.TestCase):
  73. """ given an absolute file path return the python module's path as a list """
  74. def test_knownValues_modpath_from_file_1(self):
  75. from xml.etree import ElementTree
  76. self.assertEqual(modutils.modpath_from_file(ElementTree.__file__),
  77. ['xml', 'etree', 'ElementTree'])
  78. def test_knownValues_modpath_from_file_2(self):
  79. self.assertEqual(modutils.modpath_from_file('unittest_modutils.py',
  80. {os.getcwd(): 'arbitrary.pkg'}),
  81. ['arbitrary', 'pkg', 'unittest_modutils'])
  82. def test_raise_modpath_from_file_Exception(self):
  83. self.assertRaises(Exception, modutils.modpath_from_file, '/turlututu')
  84. class LoadModuleFromPathTest(resources.SysPathSetup, unittest.TestCase):
  85. def test_do_not_load_twice(self):
  86. modutils.load_module_from_modpath(['data', 'lmfp', 'foo'])
  87. modutils.load_module_from_modpath(['data', 'lmfp'])
  88. # pylint: disable=no-member; just-once is added by a test file dynamically.
  89. self.assertEqual(len(sys.just_once), 1)
  90. del sys.just_once
  91. class FileFromModPathTest(resources.SysPathSetup, unittest.TestCase):
  92. """given a mod path (i.e. splited module / package name), return the
  93. corresponding file, giving priority to source file over precompiled file
  94. if it exists"""
  95. def test_site_packages(self):
  96. filename = _get_file_from_object(modutils)
  97. result = modutils.file_from_modpath(['astroid', 'modutils'])
  98. self.assertEqual(os.path.realpath(result), os.path.realpath(filename))
  99. def test_std_lib(self):
  100. path = modutils.file_from_modpath(['os', 'path']).replace('.pyc', '.py')
  101. self.assertEqual(os.path.realpath(path),
  102. os.path.realpath(os.path.__file__.replace('.pyc', '.py')))
  103. def test_builtin(self):
  104. self.assertIsNone(modutils.file_from_modpath(['sys']))
  105. def test_unexisting(self):
  106. self.assertRaises(ImportError, modutils.file_from_modpath, ['turlututu'])
  107. def test_unicode_in_package_init(self):
  108. # file_from_modpath should not crash when reading an __init__
  109. # file with unicode characters.
  110. modutils.file_from_modpath(["data", "unicode_package", "core"])
  111. class GetSourceFileTest(unittest.TestCase):
  112. def test(self):
  113. filename = _get_file_from_object(os.path)
  114. self.assertEqual(modutils.get_source_file(os.path.__file__),
  115. os.path.normpath(filename))
  116. def test_raise(self):
  117. self.assertRaises(modutils.NoSourceFile, modutils.get_source_file, 'whatever')
  118. class StandardLibModuleTest(resources.SysPathSetup, unittest.TestCase):
  119. """
  120. return true if the module may be considered as a module from the standard
  121. library
  122. """
  123. def test_datetime(self):
  124. # This is an interesting example, since datetime, on pypy,
  125. # is under lib_pypy, rather than the usual Lib directory.
  126. self.assertTrue(modutils.is_standard_module('datetime'))
  127. def test_builtins(self):
  128. if sys.version_info < (3, 0):
  129. self.assertTrue(modutils.is_standard_module('__builtin__'))
  130. self.assertFalse(modutils.is_standard_module('builtins'))
  131. else:
  132. self.assertFalse(modutils.is_standard_module('__builtin__'))
  133. self.assertTrue(modutils.is_standard_module('builtins'))
  134. def test_builtin(self):
  135. self.assertTrue(modutils.is_standard_module('sys'))
  136. self.assertTrue(modutils.is_standard_module('marshal'))
  137. def test_nonstandard(self):
  138. self.assertFalse(modutils.is_standard_module('astroid'))
  139. def test_unknown(self):
  140. self.assertFalse(modutils.is_standard_module('unknown'))
  141. def test_4(self):
  142. self.assertTrue(modutils.is_standard_module('hashlib'))
  143. self.assertTrue(modutils.is_standard_module('pickle'))
  144. self.assertTrue(modutils.is_standard_module('email'))
  145. self.assertEqual(modutils.is_standard_module('io'),
  146. sys.version_info >= (2, 6))
  147. self.assertEqual(modutils.is_standard_module('StringIO'),
  148. sys.version_info < (3, 0))
  149. self.assertTrue(modutils.is_standard_module('unicodedata'))
  150. def test_custom_path(self):
  151. datadir = resources.find('')
  152. if datadir.startswith(modutils.EXT_LIB_DIR):
  153. self.skipTest('known breakage of is_standard_module on installed package')
  154. self.assertTrue(modutils.is_standard_module('data.module', (datadir,)))
  155. self.assertTrue(modutils.is_standard_module('data.module', (os.path.abspath(datadir),)))
  156. def test_failing_edge_cases(self):
  157. # using a subpackage/submodule path as std_path argument
  158. self.assertFalse(modutils.is_standard_module('xml.etree', etree.__path__))
  159. # using a module + object name as modname argument
  160. self.assertTrue(modutils.is_standard_module('sys.path'))
  161. # this is because only the first package/module is considered
  162. self.assertTrue(modutils.is_standard_module('sys.whatever'))
  163. self.assertFalse(modutils.is_standard_module('xml.whatever', etree.__path__))
  164. class IsRelativeTest(unittest.TestCase):
  165. def test_knownValues_is_relative_1(self):
  166. self.assertTrue(modutils.is_relative('utils', email.__path__[0]))
  167. def test_knownValues_is_relative_2(self):
  168. self.assertTrue(modutils.is_relative('ElementPath',
  169. etree.ElementTree.__file__))
  170. def test_knownValues_is_relative_3(self):
  171. self.assertFalse(modutils.is_relative('astroid', astroid.__path__[0]))
  172. class GetModuleFilesTest(unittest.TestCase):
  173. def test_get_module_files_1(self):
  174. package = resources.find('data/find_test')
  175. modules = set(modutils.get_module_files(package, []))
  176. expected = ['__init__.py', 'module.py', 'module2.py',
  177. 'noendingnewline.py', 'nonregr.py']
  178. self.assertEqual(modules,
  179. {os.path.join(package, x) for x in expected})
  180. def test_get_all_files(self):
  181. """test that list_all returns all Python files from given location
  182. """
  183. non_package = resources.find('data/notamodule')
  184. modules = modutils.get_module_files(non_package, [], list_all=True)
  185. self.assertEqual(
  186. modules,
  187. [os.path.join(non_package, 'file.py')],
  188. )
  189. def test_load_module_set_attribute(self):
  190. import xml.etree.ElementTree
  191. import xml
  192. del xml.etree.ElementTree
  193. del sys.modules['xml.etree.ElementTree']
  194. m = modutils.load_module_from_modpath(['xml', 'etree', 'ElementTree'])
  195. self.assertTrue(hasattr(xml, 'etree'))
  196. self.assertTrue(hasattr(xml.etree, 'ElementTree'))
  197. self.assertTrue(m is xml.etree.ElementTree)
  198. if __name__ == '__main__':
  199. unittest.main()