aboutsummaryrefslogtreecommitdiff
path: root/setuptools/tests/test_install_scripts.py
blob: 4338c792f126ab93454dda762df0033e298600a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""install_scripts tests
"""

import io
import sys

import pytest

from setuptools.command.install_scripts import install_scripts
from setuptools.dist import Distribution
from . import contexts


class TestInstallScripts:
    settings = dict(
        name='foo',
        entry_points={'console_scripts': ['foo=foo:foo']},
        version='0.0',
    )
    unix_exe = '/usr/dummy-test-path/local/bin/python'
    unix_spaces_exe = '/usr/bin/env dummy-test-python'
    win32_exe = 'C:\\Dummy Test Path\\Program Files\\Python 3.6\\python.exe'

    def _run_install_scripts(self, install_dir, executable=None):
        dist = Distribution(self.settings)
        dist.script_name = 'setup.py'
        cmd = install_scripts(dist)
        cmd.install_dir = install_dir
        if executable is not None:
            bs = cmd.get_finalized_command('build_scripts')
            bs.executable = executable
        cmd.ensure_finalized()
        with contexts.quiet():
            cmd.run()

    @pytest.mark.skipif(sys.platform == 'win32', reason='non-Windows only')
    def test_sys_executable_escaping_unix(self, tmpdir, monkeypatch):
        """
        Ensure that shebang is not quoted on Unix when getting the Python exe
        from sys.executable.
        """
        expected = '#!%s\n' % self.unix_exe
        monkeypatch.setattr('sys.executable', self.unix_exe)
        with tmpdir.as_cwd():
            self._run_install_scripts(str(tmpdir))
            with io.open(str(tmpdir.join('foo')), 'r') as f:
                actual = f.readline()
        assert actual == expected

    @pytest.mark.skipif(sys.platform != 'win32', reason='Windows only')
    def test_sys_executable_escaping_win32(self, tmpdir, monkeypatch):
        """
        Ensure that shebang is quoted on Windows when getting the Python exe
        from sys.executable and it contains a space.
        """
        expected = '#!"%s"\n' % self.win32_exe
        monkeypatch.setattr('sys.executable', self.win32_exe)
        with tmpdir.as_cwd():
            self._run_install_scripts(str(tmpdir))
            with io.open(str(tmpdir.join('foo-script.py')), 'r') as f:
                actual = f.readline()
        assert actual == expected

    @pytest.mark.skipif(sys.platform == 'win32', reason='non-Windows only')
    def test_executable_with_spaces_escaping_unix(self, tmpdir):
        """
        Ensure that shebang on Unix is not quoted, even when
        a value with spaces
        is specified using --executable.
        """
        expected = '#!%s\n' % self.unix_spaces_exe
        with tmpdir.as_cwd():
            self._run_install_scripts(str(tmpdir), self.unix_spaces_exe)
            with io.open(str(tmpdir.join('foo')), 'r') as f:
                actual = f.readline()
        assert actual == expected

    @pytest.mark.skipif(sys.platform != 'win32', reason='Windows only')
    def test_executable_arg_escaping_win32(self, tmpdir):
        """
        Ensure that shebang on Windows is quoted when
        getting a path with spaces
        from --executable, that is itself properly quoted.
        """
        expected = '#!"%s"\n' % self.win32_exe
        with tmpdir.as_cwd():
            self._run_install_scripts(str(tmpdir), '"' + self.win32_exe + '"')
            with io.open(str(tmpdir.join('foo-script.py')), 'r') as f:
                actual = f.readline()
        assert actual == expected