/Lib/distutils/tests/test_build_scripts.py

http://unladen-swallow.googlecode.com/ · Python · 109 lines · 82 code · 21 blank · 6 comment · 8 complexity · 2e2941a6cbc2e5319cb2304f0f464002 MD5 · raw file

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