test_build_scripts.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """Tests for distutils.command.build_scripts."""
  2. import os
  3. import unittest
  4. from distutils.command.build_scripts import build_scripts
  5. from distutils.core import Distribution
  6. from distutils import sysconfig
  7. from distutils.tests import support
  8. from test.support import run_unittest
  9. class BuildScriptsTestCase(support.TempdirManager,
  10. support.LoggingSilencer,
  11. unittest.TestCase):
  12. def test_default_settings(self):
  13. cmd = self.get_build_scripts_cmd("/foo/bar", [])
  14. self.assertFalse(cmd.force)
  15. self.assertIsNone(cmd.build_dir)
  16. cmd.finalize_options()
  17. self.assertTrue(cmd.force)
  18. self.assertEqual(cmd.build_dir, "/foo/bar")
  19. def test_build(self):
  20. source = self.mkdtemp()
  21. target = self.mkdtemp()
  22. expected = self.write_sample_scripts(source)
  23. cmd = self.get_build_scripts_cmd(target,
  24. [os.path.join(source, fn)
  25. for fn in expected])
  26. cmd.finalize_options()
  27. cmd.run()
  28. built = os.listdir(target)
  29. for name in expected:
  30. self.assertIn(name, built)
  31. def get_build_scripts_cmd(self, target, scripts):
  32. import sys
  33. dist = Distribution()
  34. dist.scripts = scripts
  35. dist.command_obj["build"] = support.DummyCommand(
  36. build_scripts=target,
  37. force=1,
  38. executable=sys.executable
  39. )
  40. return build_scripts(dist)
  41. def write_sample_scripts(self, dir):
  42. expected = []
  43. expected.append("script1.py")
  44. self.write_script(dir, "script1.py",
  45. ("#! /usr/bin/env python2.3\n"
  46. "# bogus script w/ Python sh-bang\n"
  47. "pass\n"))
  48. expected.append("script2.py")
  49. self.write_script(dir, "script2.py",
  50. ("#!/usr/bin/python\n"
  51. "# bogus script w/ Python sh-bang\n"
  52. "pass\n"))
  53. expected.append("shell.sh")
  54. self.write_script(dir, "shell.sh",
  55. ("#!/bin/sh\n"
  56. "# bogus shell script w/ sh-bang\n"
  57. "exit 0\n"))
  58. return expected
  59. def write_script(self, dir, name, text):
  60. f = open(os.path.join(dir, name), "w")
  61. try:
  62. f.write(text)
  63. finally:
  64. f.close()
  65. def test_version_int(self):
  66. source = self.mkdtemp()
  67. target = self.mkdtemp()
  68. expected = self.write_sample_scripts(source)
  69. cmd = self.get_build_scripts_cmd(target,
  70. [os.path.join(source, fn)
  71. for fn in expected])
  72. cmd.finalize_options()
  73. # http://bugs.python.org/issue4524
  74. #
  75. # On linux-g++-32 with command line `./configure --enable-ipv6
  76. # --with-suffix=3`, python is compiled okay but the build scripts
  77. # failed when writing the name of the executable
  78. old = sysconfig.get_config_vars().get('VERSION')
  79. sysconfig._config_vars['VERSION'] = 4
  80. try:
  81. cmd.run()
  82. finally:
  83. if old is not None:
  84. sysconfig._config_vars['VERSION'] = old
  85. built = os.listdir(target)
  86. for name in expected:
  87. self.assertIn(name, built)
  88. def test_suite():
  89. return unittest.makeSuite(BuildScriptsTestCase)
  90. if __name__ == "__main__":
  91. run_unittest(test_suite())