/Lib/test/test_pkgutil.py

http://unladen-swallow.googlecode.com/ · Python · 130 lines · 86 code · 26 blank · 18 comment · 1 complexity · 79c9823fb2f0907eddf6a49a0e5c5342 MD5 · raw file

  1. from test.test_support import run_unittest
  2. import unittest
  3. import sys
  4. import imp
  5. import pkgutil
  6. import os
  7. import os.path
  8. import tempfile
  9. import shutil
  10. import zipfile
  11. class PkgutilTests(unittest.TestCase):
  12. def setUp(self):
  13. self.dirname = tempfile.mkdtemp()
  14. sys.path.insert(0, self.dirname)
  15. def tearDown(self):
  16. del sys.path[0]
  17. shutil.rmtree(self.dirname)
  18. def test_getdata_filesys(self):
  19. pkg = 'test_getdata_filesys'
  20. # Include a LF and a CRLF, to test that binary data is read back
  21. RESOURCE_DATA = 'Hello, world!\nSecond line\r\nThird line'
  22. # Make a package with some resources
  23. package_dir = os.path.join(self.dirname, pkg)
  24. os.mkdir(package_dir)
  25. # Empty init.py
  26. f = open(os.path.join(package_dir, '__init__.py'), "wb")
  27. f.close()
  28. # Resource files, res.txt, sub/res.txt
  29. f = open(os.path.join(package_dir, 'res.txt'), "wb")
  30. f.write(RESOURCE_DATA)
  31. f.close()
  32. os.mkdir(os.path.join(package_dir, 'sub'))
  33. f = open(os.path.join(package_dir, 'sub', 'res.txt'), "wb")
  34. f.write(RESOURCE_DATA)
  35. f.close()
  36. # Check we can read the resources
  37. res1 = pkgutil.get_data(pkg, 'res.txt')
  38. self.assertEqual(res1, RESOURCE_DATA)
  39. res2 = pkgutil.get_data(pkg, 'sub/res.txt')
  40. self.assertEqual(res2, RESOURCE_DATA)
  41. del sys.modules[pkg]
  42. def test_getdata_zipfile(self):
  43. zip = 'test_getdata_zipfile.zip'
  44. pkg = 'test_getdata_zipfile'
  45. # Include a LF and a CRLF, to test that binary data is read back
  46. RESOURCE_DATA = 'Hello, world!\nSecond line\r\nThird line'
  47. # Make a package with some resources
  48. zip_file = os.path.join(self.dirname, zip)
  49. z = zipfile.ZipFile(zip_file, 'w')
  50. # Empty init.py
  51. z.writestr(pkg + '/__init__.py', "")
  52. # Resource files, res.txt, sub/res.txt
  53. z.writestr(pkg + '/res.txt', RESOURCE_DATA)
  54. z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA)
  55. z.close()
  56. # Check we can read the resources
  57. sys.path.insert(0, zip_file)
  58. res1 = pkgutil.get_data(pkg, 'res.txt')
  59. self.assertEqual(res1, RESOURCE_DATA)
  60. res2 = pkgutil.get_data(pkg, 'sub/res.txt')
  61. self.assertEqual(res2, RESOURCE_DATA)
  62. del sys.path[0]
  63. del sys.modules[pkg]
  64. class PkgutilPEP302Tests(unittest.TestCase):
  65. class MyTestLoader(object):
  66. def load_module(self, fullname):
  67. # Create an empty module
  68. mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
  69. mod.__file__ = "<%s>" % self.__class__.__name__
  70. mod.__loader__ = self
  71. # Make it a package
  72. mod.__path__ = []
  73. # Count how many times the module is reloaded
  74. mod.__dict__['loads'] = mod.__dict__.get('loads',0) + 1
  75. return mod
  76. def get_data(self, path):
  77. return "Hello, world!"
  78. class MyTestImporter(object):
  79. def find_module(self, fullname, path=None):
  80. return PkgutilPEP302Tests.MyTestLoader()
  81. def setUp(self):
  82. sys.meta_path.insert(0, self.MyTestImporter())
  83. def tearDown(self):
  84. del sys.meta_path[0]
  85. def test_getdata_pep302(self):
  86. # Use a dummy importer/loader
  87. self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
  88. del sys.modules['foo']
  89. def test_alreadyloaded(self):
  90. # Ensure that get_data works without reloading - the "loads" module
  91. # variable in the example loader should count how many times a reload
  92. # occurs.
  93. import foo
  94. self.assertEqual(foo.loads, 1)
  95. self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
  96. self.assertEqual(foo.loads, 1)
  97. del sys.modules['foo']
  98. def test_main():
  99. run_unittest(PkgutilTests, PkgutilPEP302Tests)
  100. # this is necessary if test is run repeated (like when finding leaks)
  101. import zipimport
  102. zipimport._zip_directory_cache.clear()
  103. if __name__ == '__main__':
  104. test_main()