/Lib/distutils/tests/test_build_py.py

http://unladen-swallow.googlecode.com/ · Python · 97 lines · 68 code · 19 blank · 10 comment · 6 complexity · 47f1b37813b0f3e38c47677adf4b28b2 MD5 · raw file

  1. """Tests for distutils.command.build_py."""
  2. import os
  3. import sys
  4. import StringIO
  5. import unittest
  6. from distutils.command.build_py import build_py
  7. from distutils.core import Distribution
  8. from distutils.errors import DistutilsFileError
  9. from distutils.tests import support
  10. class BuildPyTestCase(support.TempdirManager,
  11. support.LoggingSilencer,
  12. unittest.TestCase):
  13. def test_package_data(self):
  14. sources = self.mkdtemp()
  15. f = open(os.path.join(sources, "__init__.py"), "w")
  16. f.write("# Pretend this is a package.")
  17. f.close()
  18. f = open(os.path.join(sources, "README.txt"), "w")
  19. f.write("Info about this package")
  20. f.close()
  21. destination = self.mkdtemp()
  22. dist = Distribution({"packages": ["pkg"],
  23. "package_dir": {"pkg": sources}})
  24. # script_name need not exist, it just need to be initialized
  25. dist.script_name = os.path.join(sources, "setup.py")
  26. dist.command_obj["build"] = support.DummyCommand(
  27. force=0,
  28. build_lib=destination)
  29. dist.packages = ["pkg"]
  30. dist.package_data = {"pkg": ["README.txt"]}
  31. dist.package_dir = {"pkg": sources}
  32. cmd = build_py(dist)
  33. cmd.compile = 1
  34. cmd.ensure_finalized()
  35. self.assertEqual(cmd.package_data, dist.package_data)
  36. cmd.run()
  37. # This makes sure the list of outputs includes byte-compiled
  38. # files for Python modules but not for package data files
  39. # (there shouldn't *be* byte-code files for those!).
  40. #
  41. self.assertEqual(len(cmd.get_outputs()), 3)
  42. pkgdest = os.path.join(destination, "pkg")
  43. files = os.listdir(pkgdest)
  44. self.assert_("__init__.py" in files)
  45. self.assert_("README.txt" in files)
  46. if sys.flags.optimize <= 1:
  47. self.assert_("__init__.pyc" in files)
  48. def test_empty_package_dir (self):
  49. # See SF 1668596/1720897.
  50. cwd = os.getcwd()
  51. # create the distribution files.
  52. sources = self.mkdtemp()
  53. open(os.path.join(sources, "__init__.py"), "w").close()
  54. testdir = os.path.join(sources, "doc")
  55. os.mkdir(testdir)
  56. open(os.path.join(testdir, "testfile"), "w").close()
  57. os.chdir(sources)
  58. sys.stdout = StringIO.StringIO()
  59. try:
  60. dist = Distribution({"packages": ["pkg"],
  61. "package_dir": {"pkg": ""},
  62. "package_data": {"pkg": ["doc/*"]}})
  63. # script_name need not exist, it just need to be initialized
  64. dist.script_name = os.path.join(sources, "setup.py")
  65. dist.script_args = ["build"]
  66. dist.parse_command_line()
  67. try:
  68. dist.run_commands()
  69. except DistutilsFileError:
  70. self.fail("failed package_data test when package_dir is ''")
  71. finally:
  72. # Restore state.
  73. os.chdir(cwd)
  74. sys.stdout = sys.__stdout__
  75. def test_suite():
  76. return unittest.makeSuite(BuildPyTestCase)
  77. if __name__ == "__main__":
  78. unittest.main(defaultTest="test_suite")