test_pkg.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # Test packages (dotted-name import)
  2. import sys
  3. import os
  4. import tempfile
  5. import textwrap
  6. import unittest
  7. from test import test_support
  8. # Helpers to create and destroy hierarchies.
  9. def cleanout(root):
  10. names = os.listdir(root)
  11. for name in names:
  12. fullname = os.path.join(root, name)
  13. if os.path.isdir(fullname) and not os.path.islink(fullname):
  14. cleanout(fullname)
  15. else:
  16. os.remove(fullname)
  17. os.rmdir(root)
  18. def fixdir(lst):
  19. if "__builtins__" in lst:
  20. lst.remove("__builtins__")
  21. return lst
  22. # XXX Things to test
  23. #
  24. # import package without __init__
  25. # import package with __init__
  26. # __init__ importing submodule
  27. # __init__ importing global module
  28. # __init__ defining variables
  29. # submodule importing other submodule
  30. # submodule importing global module
  31. # submodule import submodule via global name
  32. # from package import submodule
  33. # from package import subpackage
  34. # from package import variable (defined in __init__)
  35. # from package import * (defined in __init__)
  36. class Test(unittest.TestCase):
  37. def setUp(self):
  38. self.root = None
  39. self.pkgname = None
  40. self.syspath = list(sys.path)
  41. def tearDown(self):
  42. sys.path[:] = self.syspath
  43. if self.root: # Only clean if the test was actually run
  44. cleanout(self.root)
  45. # delete all modules concerning the tested hierarchy
  46. if self.pkgname:
  47. modules = [name for name in sys.modules
  48. if self.pkgname in name.split('.')]
  49. for name in modules:
  50. del sys.modules[name]
  51. def run_code(self, code):
  52. exec(textwrap.dedent(code), globals(), {"self": self})
  53. def mkhier(self, descr):
  54. root = tempfile.mkdtemp()
  55. sys.path.insert(0, root)
  56. if not os.path.isdir(root):
  57. os.mkdir(root)
  58. for name, contents in descr:
  59. comps = name.split()
  60. fullname = root
  61. for c in comps:
  62. fullname = os.path.join(fullname, c)
  63. if contents is None:
  64. os.mkdir(fullname)
  65. else:
  66. f = open(fullname, "w")
  67. f.write(contents)
  68. if contents and contents[-1] != '\n':
  69. f.write('\n')
  70. f.close()
  71. self.root = root
  72. # package name is the name of the first item
  73. self.pkgname = descr[0][0]
  74. def test_1(self):
  75. hier = [("t1", None), ("t1 __init__"+os.extsep+"py", "")]
  76. self.mkhier(hier)
  77. import t1
  78. def test_2(self):
  79. hier = [
  80. ("t2", None),
  81. ("t2 __init__"+os.extsep+"py", "'doc for t2'"),
  82. ("t2 sub", None),
  83. ("t2 sub __init__"+os.extsep+"py", ""),
  84. ("t2 sub subsub", None),
  85. ("t2 sub subsub __init__"+os.extsep+"py", "spam = 1"),
  86. ]
  87. self.mkhier(hier)
  88. import t2.sub
  89. import t2.sub.subsub
  90. self.assertEqual(t2.__name__, "t2")
  91. self.assertEqual(t2.sub.__name__, "t2.sub")
  92. self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
  93. # This exec crap is needed because Py3k forbids 'import *' outside
  94. # of module-scope and __import__() is insufficient for what we need.
  95. s = """
  96. import t2
  97. from t2 import *
  98. self.assertEqual(dir(), ['self', 'sub', 't2'])
  99. """
  100. self.run_code(s)
  101. from t2 import sub
  102. from t2.sub import subsub
  103. from t2.sub.subsub import spam
  104. self.assertEqual(sub.__name__, "t2.sub")
  105. self.assertEqual(subsub.__name__, "t2.sub.subsub")
  106. self.assertEqual(sub.subsub.__name__, "t2.sub.subsub")
  107. for name in ['spam', 'sub', 'subsub', 't2']:
  108. self.assertTrue(locals()["name"], "Failed to import %s" % name)
  109. import t2.sub
  110. import t2.sub.subsub
  111. self.assertEqual(t2.__name__, "t2")
  112. self.assertEqual(t2.sub.__name__, "t2.sub")
  113. self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
  114. s = """
  115. from t2 import *
  116. self.assertTrue(dir(), ['self', 'sub'])
  117. """
  118. self.run_code(s)
  119. def test_3(self):
  120. hier = [
  121. ("t3", None),
  122. ("t3 __init__"+os.extsep+"py", ""),
  123. ("t3 sub", None),
  124. ("t3 sub __init__"+os.extsep+"py", ""),
  125. ("t3 sub subsub", None),
  126. ("t3 sub subsub __init__"+os.extsep+"py", "spam = 1"),
  127. ]
  128. self.mkhier(hier)
  129. import t3.sub.subsub
  130. self.assertEqual(t3.__name__, "t3")
  131. self.assertEqual(t3.sub.__name__, "t3.sub")
  132. self.assertEqual(t3.sub.subsub.__name__, "t3.sub.subsub")
  133. def test_4(self):
  134. hier = [
  135. ("t4.py", "raise RuntimeError('Shouldnt load t4.py')"),
  136. ("t4", None),
  137. ("t4 __init__"+os.extsep+"py", ""),
  138. ("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"),
  139. ("t4 sub", None),
  140. ("t4 sub __init__"+os.extsep+"py", ""),
  141. ("t4 sub subsub"+os.extsep+"py",
  142. "raise RuntimeError('Shouldnt load subsub.py')"),
  143. ("t4 sub subsub", None),
  144. ("t4 sub subsub __init__"+os.extsep+"py", "spam = 1"),
  145. ]
  146. self.mkhier(hier)
  147. s = """
  148. from t4.sub.subsub import *
  149. self.assertEqual(spam, 1)
  150. """
  151. self.run_code(s)
  152. def test_5(self):
  153. hier = [
  154. ("t5", None),
  155. ("t5 __init__"+os.extsep+"py", "import t5.foo"),
  156. ("t5 string"+os.extsep+"py", "spam = 1"),
  157. ("t5 foo"+os.extsep+"py",
  158. "from . import string; assert string.spam == 1"),
  159. ]
  160. self.mkhier(hier)
  161. import t5
  162. s = """
  163. from t5 import *
  164. self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
  165. """
  166. self.run_code(s)
  167. import t5
  168. self.assertEqual(fixdir(dir(t5)),
  169. ['__doc__', '__file__', '__name__',
  170. '__package__', '__path__', 'foo', 'string', 't5'])
  171. self.assertEqual(fixdir(dir(t5.foo)),
  172. ['__doc__', '__file__', '__name__', '__package__',
  173. 'string'])
  174. self.assertEqual(fixdir(dir(t5.string)),
  175. ['__doc__', '__file__', '__name__','__package__',
  176. 'spam'])
  177. def test_6(self):
  178. hier = [
  179. ("t6", None),
  180. ("t6 __init__"+os.extsep+"py",
  181. "__all__ = ['spam', 'ham', 'eggs']"),
  182. ("t6 spam"+os.extsep+"py", ""),
  183. ("t6 ham"+os.extsep+"py", ""),
  184. ("t6 eggs"+os.extsep+"py", ""),
  185. ]
  186. self.mkhier(hier)
  187. import t6
  188. self.assertEqual(fixdir(dir(t6)),
  189. ['__all__', '__doc__', '__file__',
  190. '__name__', '__package__', '__path__'])
  191. s = """
  192. import t6
  193. from t6 import *
  194. self.assertEqual(fixdir(dir(t6)),
  195. ['__all__', '__doc__', '__file__',
  196. '__name__', '__package__', '__path__',
  197. 'eggs', 'ham', 'spam'])
  198. self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
  199. """
  200. self.run_code(s)
  201. def test_7(self):
  202. hier = [
  203. ("t7", None),
  204. ("t7"+os.extsep+"py", ""),
  205. ("t7 __init__"+os.extsep+"py", ""),
  206. ("t7 sub"+os.extsep+"py",
  207. "raise RuntimeError('Shouldnt load sub.py')"),
  208. ("t7 sub", None),
  209. ("t7 sub __init__"+os.extsep+"py", ""),
  210. ("t7 sub "+os.extsep+"py",
  211. "raise RuntimeError('Shouldnt load subsub.py')"),
  212. ("t7 sub subsub", None),
  213. ("t7 sub subsub __init__"+os.extsep+"py",
  214. "spam = 1"),
  215. ]
  216. self.mkhier(hier)
  217. t7, sub, subsub = None, None, None
  218. import t7 as tas
  219. self.assertEqual(fixdir(dir(tas)),
  220. ['__doc__', '__file__', '__name__',
  221. '__package__', '__path__'])
  222. self.assertFalse(t7)
  223. from t7 import sub as subpar
  224. self.assertEqual(fixdir(dir(subpar)),
  225. ['__doc__', '__file__', '__name__',
  226. '__package__', '__path__'])
  227. self.assertFalse(t7)
  228. self.assertFalse(sub)
  229. from t7.sub import subsub as subsubsub
  230. self.assertEqual(fixdir(dir(subsubsub)),
  231. ['__doc__', '__file__', '__name__',
  232. '__package__', '__path__', 'spam'])
  233. self.assertFalse(t7)
  234. self.assertFalse(sub)
  235. self.assertFalse(subsub)
  236. from t7.sub.subsub import spam as ham
  237. self.assertEqual(ham, 1)
  238. self.assertFalse(t7)
  239. self.assertFalse(sub)
  240. self.assertFalse(subsub)
  241. @unittest.skipIf(sys.flags.optimize >= 2,
  242. "Docstrings are omitted with -O2 and above")
  243. def test_8(self):
  244. hier = [
  245. ("t8", None),
  246. ("t8 __init__"+os.extsep+"py", "'doc for t8'"),
  247. ]
  248. self.mkhier(hier)
  249. import t8
  250. self.assertEqual(t8.__doc__, "doc for t8")
  251. def test_main():
  252. test_support.run_unittest(__name__)
  253. if __name__ == "__main__":
  254. test_main()