/Lib/test/test_pkg.py

http://unladen-swallow.googlecode.com/ · Python · 287 lines · 229 code · 38 blank · 20 comment · 20 complexity · 8ac1ad3f2028aaae770e2265cd919f2b MD5 · raw file

  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. cleanout(self.root)
  44. # delete all modules concerning the tested hiearchy
  45. if self.pkgname:
  46. modules = [name for name in sys.modules
  47. if self.pkgname in name.split('.')]
  48. for name in modules:
  49. del sys.modules[name]
  50. def run_code(self, code):
  51. exec(textwrap.dedent(code), globals(), {"self": self})
  52. def mkhier(self, descr):
  53. root = tempfile.mkdtemp()
  54. sys.path.insert(0, root)
  55. if not os.path.isdir(root):
  56. os.mkdir(root)
  57. for name, contents in descr:
  58. comps = name.split()
  59. fullname = root
  60. for c in comps:
  61. fullname = os.path.join(fullname, c)
  62. if contents is None:
  63. os.mkdir(fullname)
  64. else:
  65. f = open(fullname, "w")
  66. f.write(contents)
  67. if contents and contents[-1] != '\n':
  68. f.write('\n')
  69. f.close()
  70. self.root = root
  71. # package name is the name of the first item
  72. self.pkgname = descr[0][0]
  73. def test_1(self):
  74. hier = [("t1", None), ("t1 __init__"+os.extsep+"py", "")]
  75. self.mkhier(hier)
  76. import t1
  77. def test_2(self):
  78. hier = [
  79. ("t2", None),
  80. ("t2 __init__"+os.extsep+"py", "'doc for t2'"),
  81. ("t2 sub", None),
  82. ("t2 sub __init__"+os.extsep+"py", ""),
  83. ("t2 sub subsub", None),
  84. ("t2 sub subsub __init__"+os.extsep+"py", "spam = 1"),
  85. ]
  86. self.mkhier(hier)
  87. import t2
  88. if sys.flags.optimize <= 1:
  89. self.assertEqual(t2.__doc__, "doc for t2")
  90. import t2.sub
  91. import t2.sub.subsub
  92. self.assertEqual(t2.__name__, "t2")
  93. self.assertEqual(t2.sub.__name__, "t2.sub")
  94. self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
  95. # This exec crap is needed because Py3k forbids 'import *' outside
  96. # of module-scope and __import__() is insufficient for what we need.
  97. s = """
  98. import t2
  99. from t2 import *
  100. self.assertEqual(dir(), ['self', 'sub', 't2'])
  101. """
  102. self.run_code(s)
  103. from t2 import sub
  104. from t2.sub import subsub
  105. from t2.sub.subsub import spam
  106. self.assertEqual(sub.__name__, "t2.sub")
  107. self.assertEqual(subsub.__name__, "t2.sub.subsub")
  108. self.assertEqual(sub.subsub.__name__, "t2.sub.subsub")
  109. for name in ['spam', 'sub', 'subsub', 't2']:
  110. self.failUnless(locals()["name"], "Failed to import %s" % name)
  111. import t2.sub
  112. import t2.sub.subsub
  113. self.assertEqual(t2.__name__, "t2")
  114. self.assertEqual(t2.sub.__name__, "t2.sub")
  115. self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
  116. s = """
  117. from t2 import *
  118. self.failUnless(dir(), ['self', 'sub'])
  119. """
  120. self.run_code(s)
  121. def test_3(self):
  122. hier = [
  123. ("t3", None),
  124. ("t3 __init__"+os.extsep+"py", ""),
  125. ("t3 sub", None),
  126. ("t3 sub __init__"+os.extsep+"py", ""),
  127. ("t3 sub subsub", None),
  128. ("t3 sub subsub __init__"+os.extsep+"py", "spam = 1"),
  129. ]
  130. self.mkhier(hier)
  131. import t3.sub.subsub
  132. self.assertEqual(t3.__name__, "t3")
  133. self.assertEqual(t3.sub.__name__, "t3.sub")
  134. self.assertEqual(t3.sub.subsub.__name__, "t3.sub.subsub")
  135. def test_4(self):
  136. hier = [
  137. ("t4.py", "raise RuntimeError('Shouldnt load t4.py')"),
  138. ("t4", None),
  139. ("t4 __init__"+os.extsep+"py", ""),
  140. ("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"),
  141. ("t4 sub", None),
  142. ("t4 sub __init__"+os.extsep+"py", ""),
  143. ("t4 sub subsub"+os.extsep+"py",
  144. "raise RuntimeError('Shouldnt load subsub.py')"),
  145. ("t4 sub subsub", None),
  146. ("t4 sub subsub __init__"+os.extsep+"py", "spam = 1"),
  147. ]
  148. self.mkhier(hier)
  149. s = """
  150. from t4.sub.subsub import *
  151. self.assertEqual(spam, 1)
  152. """
  153. self.run_code(s)
  154. def test_5(self):
  155. hier = [
  156. ("t5", None),
  157. ("t5 __init__"+os.extsep+"py", "import t5.foo"),
  158. ("t5 string"+os.extsep+"py", "spam = 1"),
  159. ("t5 foo"+os.extsep+"py",
  160. "from . import string; assert string.spam == 1"),
  161. ]
  162. self.mkhier(hier)
  163. import t5
  164. s = """
  165. from t5 import *
  166. self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
  167. """
  168. self.run_code(s)
  169. import t5
  170. self.assertEqual(fixdir(dir(t5)),
  171. ['__doc__', '__file__', '__name__',
  172. '__package__', '__path__', 'foo', 'string', 't5'])
  173. self.assertEqual(fixdir(dir(t5.foo)),
  174. ['__doc__', '__file__', '__name__', '__package__',
  175. 'string'])
  176. self.assertEqual(fixdir(dir(t5.string)),
  177. ['__doc__', '__file__', '__name__','__package__',
  178. 'spam'])
  179. def test_6(self):
  180. hier = [
  181. ("t6", None),
  182. ("t6 __init__"+os.extsep+"py",
  183. "__all__ = ['spam', 'ham', 'eggs']"),
  184. ("t6 spam"+os.extsep+"py", ""),
  185. ("t6 ham"+os.extsep+"py", ""),
  186. ("t6 eggs"+os.extsep+"py", ""),
  187. ]
  188. self.mkhier(hier)
  189. import t6
  190. self.assertEqual(fixdir(dir(t6)),
  191. ['__all__', '__doc__', '__file__',
  192. '__name__', '__package__', '__path__'])
  193. s = """
  194. import t6
  195. from t6 import *
  196. self.assertEqual(fixdir(dir(t6)),
  197. ['__all__', '__doc__', '__file__',
  198. '__name__', '__package__', '__path__',
  199. 'eggs', 'ham', 'spam'])
  200. self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
  201. """
  202. self.run_code(s)
  203. def test_7(self):
  204. hier = [
  205. ("t7", None),
  206. ("t7"+os.extsep+"py", ""),
  207. ("t7 __init__"+os.extsep+"py", ""),
  208. ("t7 sub"+os.extsep+"py",
  209. "raise RuntimeError('Shouldnt load sub.py')"),
  210. ("t7 sub", None),
  211. ("t7 sub __init__"+os.extsep+"py", ""),
  212. ("t7 sub "+os.extsep+"py",
  213. "raise RuntimeError('Shouldnt load subsub.py')"),
  214. ("t7 sub subsub", None),
  215. ("t7 sub subsub __init__"+os.extsep+"py",
  216. "spam = 1"),
  217. ]
  218. self.mkhier(hier)
  219. t7, sub, subsub = None, None, None
  220. import t7 as tas
  221. self.assertEqual(fixdir(dir(tas)),
  222. ['__doc__', '__file__', '__name__',
  223. '__package__', '__path__'])
  224. self.failIf(t7)
  225. from t7 import sub as subpar
  226. self.assertEqual(fixdir(dir(subpar)),
  227. ['__doc__', '__file__', '__name__',
  228. '__package__', '__path__'])
  229. self.failIf(t7)
  230. self.failIf(sub)
  231. from t7.sub import subsub as subsubsub
  232. self.assertEqual(fixdir(dir(subsubsub)),
  233. ['__doc__', '__file__', '__name__',
  234. '__package__', '__path__', 'spam'])
  235. self.failIf(t7)
  236. self.failIf(sub)
  237. self.failIf(subsub)
  238. from t7.sub.subsub import spam as ham
  239. self.assertEqual(ham, 1)
  240. self.failIf(t7)
  241. self.failIf(sub)
  242. self.failIf(subsub)
  243. def test_main():
  244. if sys.flags.optimize >= 2:
  245. print >>sys.stderr, "test_pkg -- skipping some tests due to -O flag."
  246. sys.stderr.flush()
  247. test_support.run_unittest(__name__)
  248. if __name__ == "__main__":
  249. test_main()