test_install_scripts.py 3.4 KB

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