|
1 | 1 | """Tests for distutils.spawn."""
|
2 |
| -import unittest |
3 |
| -import sys |
4 | 2 | import os
|
| 3 | +import stat |
| 4 | +import sys |
| 5 | +import unittest |
| 6 | +from unittest import mock |
5 | 7 | from test.support import run_unittest, unix_shell
|
| 8 | +from test import support as test_support |
6 | 9 |
|
| 10 | +from distutils.spawn import find_executable |
7 | 11 | from distutils.spawn import _nt_quote_args
|
8 | 12 | from distutils.spawn import spawn
|
9 | 13 | from distutils.errors import DistutilsExecError
|
@@ -51,6 +55,47 @@ def test_spawn(self):
|
51 | 55 | os.chmod(exe, 0o777)
|
52 | 56 | spawn([exe]) # should work without any error
|
53 | 57 |
|
| 58 | + def test_find_executable(self): |
| 59 | + with test_support.temp_dir() as tmp_dir: |
| 60 | + # use TESTFN to get a pseudo-unique filename |
| 61 | + program_noeext = test_support.TESTFN |
| 62 | + # Give the temporary program an ".exe" suffix for all. |
| 63 | + # It's needed on Windows and not harmful on other platforms. |
| 64 | + program = program_noeext + ".exe" |
| 65 | + |
| 66 | + filename = os.path.join(tmp_dir, program) |
| 67 | + with open(filename, "wb"): |
| 68 | + pass |
| 69 | + os.chmod(filename, stat.S_IXUSR) |
| 70 | + |
| 71 | + # test path parameter |
| 72 | + rv = find_executable(program, path=tmp_dir) |
| 73 | + self.assertEqual(rv, filename) |
| 74 | + |
| 75 | + if sys.platform == 'win32': |
| 76 | + # test without ".exe" extension |
| 77 | + rv = find_executable(program_noeext, path=tmp_dir) |
| 78 | + self.assertEqual(rv, filename) |
| 79 | + |
| 80 | + # test find in the current directory |
| 81 | + with test_support.change_cwd(tmp_dir): |
| 82 | + rv = find_executable(program) |
| 83 | + self.assertEqual(rv, program) |
| 84 | + |
| 85 | + # test non-existent program |
| 86 | + dont_exist_program = "dontexist_" + program |
| 87 | + rv = find_executable(dont_exist_program , path=tmp_dir) |
| 88 | + self.assertIsNone(rv) |
| 89 | + |
| 90 | + # test os.defpath: missing PATH environment variable |
| 91 | + with test_support.EnvironmentVarGuard() as env: |
| 92 | + with mock.patch('distutils.spawn.os.defpath', tmp_dir): |
| 93 | + env.pop('PATH') |
| 94 | + |
| 95 | + rv = find_executable(program) |
| 96 | + self.assertEqual(rv, filename) |
| 97 | + |
| 98 | + |
54 | 99 | def test_suite():
|
55 | 100 | return unittest.makeSuite(SpawnTestCase)
|
56 | 101 |
|
|
0 commit comments