Aluode/PerceptionLabPortable
0
1"""Tests for distutils.spawn."""2 3import os4import stat5import sys6import unittest.mock as mock7from distutils.errors import DistutilsExecError8from distutils.spawn import find_executable, spawn9from distutils.tests import support10 11import path12import pytest13from test.support import unix_shell14 15from .compat import py39 as os_helper16 17 18class TestSpawn(support.TempdirManager):19 @pytest.mark.skipif("os.name not in ('nt', 'posix')")20 def test_spawn(self):21 tmpdir = self.mkdtemp()22 23 # creating something executable24 # through the shell that returns 125 if sys.platform != 'win32':26 exe = os.path.join(tmpdir, 'foo.sh')27 self.write_file(exe, f'#!{unix_shell}\nexit 1')28 else:29 exe = os.path.join(tmpdir, 'foo.bat')30 self.write_file(exe, 'exit 1')31 32 os.chmod(exe, 0o777)33 with pytest.raises(DistutilsExecError):34 spawn([exe])35 36 # now something that works37 if sys.platform != 'win32':38 exe = os.path.join(tmpdir, 'foo.sh')39 self.write_file(exe, f'#!{unix_shell}\nexit 0')40 else:41 exe = os.path.join(tmpdir, 'foo.bat')42 self.write_file(exe, 'exit 0')43 44 os.chmod(exe, 0o777)45 spawn([exe]) # should work without any error46 47 def test_find_executable(self, tmp_path):48 program_path = self._make_executable(tmp_path, '.exe')49 program = program_path.name50 program_noeext = program_path.with_suffix('').name51 filename = str(program_path)52 tmp_dir = path.Path(tmp_path)53 54 # test path parameter55 rv = find_executable(program, path=tmp_dir)56 assert rv == filename57 58 if sys.platform == 'win32':59 # test without ".exe" extension60 rv = find_executable(program_noeext, path=tmp_dir)61 assert rv == filename62 63 # test find in the current directory64 with tmp_dir:65 rv = find_executable(program)66 assert rv == program67 68 # test non-existent program69 dont_exist_program = "dontexist_" + program70 rv = find_executable(dont_exist_program, path=tmp_dir)71 assert rv is None72 73 # PATH='': no match, except in the current directory74 with os_helper.EnvironmentVarGuard() as env:75 env['PATH'] = ''76 with (77 mock.patch(78 'distutils.spawn.os.confstr', return_value=tmp_dir, create=True79 ),80 mock.patch('distutils.spawn.os.defpath', tmp_dir),81 ):82 rv = find_executable(program)83 assert rv is None84 85 # look in current directory86 with tmp_dir:87 rv = find_executable(program)88 assert rv == program89 90 # PATH=':': explicitly looks in the current directory91 with os_helper.EnvironmentVarGuard() as env:92 env['PATH'] = os.pathsep93 with (94 mock.patch('distutils.spawn.os.confstr', return_value='', create=True),95 mock.patch('distutils.spawn.os.defpath', ''),96 ):97 rv = find_executable(program)98 assert rv is None99 100 # look in current directory101 with tmp_dir:102 rv = find_executable(program)103 assert rv == program104 105 # missing PATH: test os.confstr("CS_PATH") and os.defpath106 with os_helper.EnvironmentVarGuard() as env:107 env.pop('PATH', None)108 109 # without confstr110 with (111 mock.patch(112 'distutils.spawn.os.confstr', side_effect=ValueError, create=True113 ),114 mock.patch('distutils.spawn.os.defpath', tmp_dir),115 ):116 rv = find_executable(program)117 assert rv == filename118 119 # with confstr120 with (121 mock.patch(122 'distutils.spawn.os.confstr', return_value=tmp_dir, create=True123 ),124 mock.patch('distutils.spawn.os.defpath', ''),125 ):126 rv = find_executable(program)127 assert rv == filename128 129 @staticmethod130 def _make_executable(tmp_path, ext):131 # Give the temporary program a suffix regardless of platform.132 # It's needed on Windows and not harmful on others.133 program = tmp_path.joinpath('program').with_suffix(ext)134 program.write_text("", encoding='utf-8')135 program.chmod(stat.S_IXUSR)136 return program137 138 def test_spawn_missing_exe(self):139 with pytest.raises(DistutilsExecError) as ctx:140 spawn(['does-not-exist'])141 assert "command 'does-not-exist' failed" in str(ctx.value)142 