1"""Tests for distutils.spawn.""" 2import os 3import stat 4import sys 5import time 6import unittest 7from test.support import captured_stdout, run_unittest 8from test import support as test_support 9 10from distutils.spawn import _nt_quote_args 11from distutils.spawn import spawn, find_executable 12from distutils.errors import DistutilsExecError 13from distutils.tests import support 14 15class SpawnTestCase(support.TempdirManager, 16 support.LoggingSilencer, 17 unittest.TestCase): 18 19 def test_nt_quote_args(self): 20 21 for (args, wanted) in ((['with space', 'nospace'], 22 ['"with space"', 'nospace']), 23 (['nochange', 'nospace'], 24 ['nochange', 'nospace'])): 25 res = _nt_quote_args(args) 26 self.assertEqual(res, wanted) 27 28 29 @unittest.skipUnless(os.name in ('nt', 'posix'), 30 'Runs only under posix or nt') 31 def test_spawn(self): 32 tmpdir = self.mkdtemp() 33 34 # creating something executable 35 # through the shell that returns 1 36 if os.name == 'posix': 37 exe = os.path.join(tmpdir, 'foo.sh') 38 self.write_file(exe, '#!/bin/sh\nexit 1') 39 os.chmod(exe, 0777) 40 else: 41 exe = os.path.join(tmpdir, 'foo.bat') 42 self.write_file(exe, 'exit 1') 43 44 os.chmod(exe, 0777) 45 self.assertRaises(DistutilsExecError, spawn, [exe]) 46 47 # now something that works 48 if os.name == 'posix': 49 exe = os.path.join(tmpdir, 'foo.sh') 50 self.write_file(exe, '#!/bin/sh\nexit 0') 51 os.chmod(exe, 0777) 52 else: 53 exe = os.path.join(tmpdir, 'foo.bat') 54 self.write_file(exe, 'exit 0') 55 56 os.chmod(exe, 0777) 57 spawn([exe]) # should work without any error 58 59 def test_find_executable(self): 60 with test_support.temp_dir() as tmp_dir: 61 # use TESTFN to get a pseudo-unique filename 62 program_noeext = test_support.TESTFN 63 # Give the temporary program an ".exe" suffix for all. 64 # It's needed on Windows and not harmful on other platforms. 65 program = program_noeext + ".exe" 66 67 filename = os.path.join(tmp_dir, program) 68 with open(filename, "wb"): 69 pass 70 os.chmod(filename, stat.S_IXUSR) 71 72 # test path parameter 73 rv = find_executable(program, path=tmp_dir) 74 self.assertEqual(rv, filename) 75 76 if sys.platform == 'win32': 77 # test without ".exe" extension 78 rv = find_executable(program_noeext, path=tmp_dir) 79 self.assertEqual(rv, filename) 80 81 # test find in the current directory 82 with test_support.change_cwd(tmp_dir): 83 rv = find_executable(program) 84 self.assertEqual(rv, program) 85 86 # test non-existent program 87 dont_exist_program = "dontexist_" + program 88 rv = find_executable(dont_exist_program , path=tmp_dir) 89 self.assertIsNone(rv) 90 91 # test os.defpath: missing PATH environment variable 92 with test_support.EnvironmentVarGuard() as env: 93 from distutils import spawn 94 with test_support.swap_attr(spawn.os, 'defpath', tmp_dir): 95 env.pop('PATH') 96 97 rv = find_executable(program) 98 self.assertEqual(rv, filename) 99 100 101def test_suite(): 102 return unittest.makeSuite(SpawnTestCase) 103 104if __name__ == "__main__": 105 run_unittest(test_suite()) 106