|
1 | 1 | """Tests for distutils.spawn.""" |
2 | | -import unittest |
3 | 2 | import os |
| 3 | +import stat |
| 4 | +import sys |
4 | 5 | import time |
5 | | -from test.test_support import captured_stdout, run_unittest |
| 6 | +import unittest |
| 7 | +from test.support import captured_stdout, run_unittest |
| 8 | +from test import support as test_support |
6 | 9 |
|
7 | 10 | from distutils.spawn import _nt_quote_args |
8 | 11 | from distutils.spawn import spawn, find_executable |
@@ -53,6 +56,48 @@ def test_spawn(self): |
53 | 56 | os.chmod(exe, 0777) |
54 | 57 | spawn([exe]) # should work without any error |
55 | 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 | + |
56 | 101 | def test_suite(): |
57 | 102 | return unittest.makeSuite(SpawnTestCase) |
58 | 103 |
|
|
0 commit comments