test_spawn.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Tests for distutils.spawn."""
  2. import unittest
  3. import os
  4. import time
  5. from test.support import captured_stdout, run_unittest
  6. from distutils.spawn import _nt_quote_args
  7. from distutils.spawn import spawn, find_executable
  8. from distutils.errors import DistutilsExecError
  9. from distutils.tests import support
  10. class SpawnTestCase(support.TempdirManager,
  11. support.LoggingSilencer,
  12. unittest.TestCase):
  13. def test_nt_quote_args(self):
  14. for (args, wanted) in ((['with space', 'nospace'],
  15. ['"with space"', 'nospace']),
  16. (['nochange', 'nospace'],
  17. ['nochange', 'nospace'])):
  18. res = _nt_quote_args(args)
  19. self.assertEqual(res, wanted)
  20. @unittest.skipUnless(os.name in ('nt', 'posix'),
  21. 'Runs only under posix or nt')
  22. def test_spawn(self):
  23. tmpdir = self.mkdtemp()
  24. # creating something executable
  25. # through the shell that returns 1
  26. if os.name == 'posix':
  27. exe = os.path.join(tmpdir, 'foo.sh')
  28. self.write_file(exe, '#!/bin/sh\nexit 1')
  29. else:
  30. exe = os.path.join(tmpdir, 'foo.bat')
  31. self.write_file(exe, 'exit 1')
  32. os.chmod(exe, 0o777)
  33. self.assertRaises(DistutilsExecError, spawn, [exe])
  34. # now something that works
  35. if os.name == 'posix':
  36. exe = os.path.join(tmpdir, 'foo.sh')
  37. self.write_file(exe, '#!/bin/sh\nexit 0')
  38. else:
  39. exe = os.path.join(tmpdir, 'foo.bat')
  40. self.write_file(exe, 'exit 0')
  41. os.chmod(exe, 0o777)
  42. spawn([exe]) # should work without any error
  43. def test_suite():
  44. return unittest.makeSuite(SpawnTestCase)
  45. if __name__ == "__main__":
  46. run_unittest(test_suite())