1"""install_scripts tests 2""" 3 4import io 5import sys 6 7import pytest 8 9from setuptools.command.install_scripts import install_scripts 10from setuptools.dist import Distribution 11from . import contexts 12 13 14class TestInstallScripts: 15 settings = dict( 16 name='foo', 17 entry_points={'console_scripts': ['foo=foo:foo']}, 18 version='0.0', 19 ) 20 unix_exe = '/usr/dummy-test-path/local/bin/python' 21 unix_spaces_exe = '/usr/bin/env dummy-test-python' 22 win32_exe = 'C:\\Dummy Test Path\\Program Files\\Python 3.6\\python.exe' 23 24 def _run_install_scripts(self, install_dir, executable=None): 25 dist = Distribution(self.settings) 26 dist.script_name = 'setup.py' 27 cmd = install_scripts(dist) 28 cmd.install_dir = install_dir 29 if executable is not None: 30 bs = cmd.get_finalized_command('build_scripts') 31 bs.executable = executable 32 cmd.ensure_finalized() 33 with contexts.quiet(): 34 cmd.run() 35 36 @pytest.mark.skipif(sys.platform == 'win32', reason='non-Windows only') 37 def test_sys_executable_escaping_unix(self, tmpdir, monkeypatch): 38 """ 39 Ensure that shebang is not quoted on Unix when getting the Python exe 40 from sys.executable. 41 """ 42 expected = '#!%s\n' % self.unix_exe 43 monkeypatch.setattr('sys.executable', self.unix_exe) 44 with tmpdir.as_cwd(): 45 self._run_install_scripts(str(tmpdir)) 46 with io.open(str(tmpdir.join('foo')), 'r') as f: 47 actual = f.readline() 48 assert actual == expected 49 50 @pytest.mark.skipif(sys.platform != 'win32', reason='Windows only') 51 def test_sys_executable_escaping_win32(self, tmpdir, monkeypatch): 52 """ 53 Ensure that shebang is quoted on Windows when getting the Python exe 54 from sys.executable and it contains a space. 55 """ 56 expected = '#!"%s"\n' % self.win32_exe 57 monkeypatch.setattr('sys.executable', self.win32_exe) 58 with tmpdir.as_cwd(): 59 self._run_install_scripts(str(tmpdir)) 60 with io.open(str(tmpdir.join('foo-script.py')), 'r') as f: 61 actual = f.readline() 62 assert actual == expected 63 64 @pytest.mark.skipif(sys.platform == 'win32', reason='non-Windows only') 65 def test_executable_with_spaces_escaping_unix(self, tmpdir): 66 """ 67 Ensure that shebang on Unix is not quoted, even when 68 a value with spaces 69 is specified using --executable. 70 """ 71 expected = '#!%s\n' % self.unix_spaces_exe 72 with tmpdir.as_cwd(): 73 self._run_install_scripts(str(tmpdir), self.unix_spaces_exe) 74 with io.open(str(tmpdir.join('foo')), 'r') as f: 75 actual = f.readline() 76 assert actual == expected 77 78 @pytest.mark.skipif(sys.platform != 'win32', reason='Windows only') 79 def test_executable_arg_escaping_win32(self, tmpdir): 80 """ 81 Ensure that shebang on Windows is quoted when 82 getting a path with spaces 83 from --executable, that is itself properly quoted. 84 """ 85 expected = '#!"%s"\n' % self.win32_exe 86 with tmpdir.as_cwd(): 87 self._run_install_scripts(str(tmpdir), '"' + self.win32_exe + '"') 88 with io.open(str(tmpdir.join('foo-script.py')), 'r') as f: 89 actual = f.readline() 90 assert actual == expected 91