| 1 |
import io |
| 2 |
import zipfile |
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from installer import _scripts |
| 8 |
from installer.scripts import InvalidScript, Script |
| 9 |
|
| 10 |
|
| 11 |
def test_script_generate_simple(): |
| 12 |
script = Script("foo", "foo.bar", "baz.qux", section="console") |
| 13 |
name, data = script.generate("/path/to/my/python", kind="posix") |
| 14 |
|
| 15 |
assert name == "foo" |
| 16 |
assert data.startswith(b"#!/path/to/my/python\n") |
| 17 |
assert b"\nfrom foo.bar import baz\n" in data |
| 18 |
assert b"baz.qux()" in data |
| 19 |
|
| 20 |
|
| 21 |
def test_script_generate_space_in_executable(): |
| 22 |
script = Script("foo", "foo.bar", "baz.qux", section="console") |
| 23 |
name, data = script.generate("/path to my/python", kind="posix") |
| 24 |
|
| 25 |
assert name == "foo" |
| 26 |
assert data.startswith(b"#!/bin/sh\n") |
| 27 |
assert b" '/path to my/python'" in data |
| 28 |
assert b"\nfrom foo.bar import baz\n" in data |
| 29 |
assert b"baz.qux()" in data |
| 30 |
|
| 31 |
|
| 32 |
def _read_launcher_data(section, kind): |
| 33 |
prefix = {"console": "t", "gui": "w"}[section] |
| 34 |
suffix = {"win-ia32": "32", "win-amd64": "64", "win-arm": "_arm"}[kind] |
| 35 |
file = Path(_scripts.__file__).parent / f"{prefix}{suffix}.exe" |
| 36 |
return file.read_bytes() |
| 37 |
|
| 38 |
|
| •39 |
@pytest.mark.skip( |
| 40 |
"Skipped on Debian, we don't include the simple_launcher .exes") |
| 41 |
@pytest.mark.parametrize("section", ["console", "gui"]) |
| 42 |
@pytest.mark.parametrize("kind", ["win-ia32", "win-amd64", "win-arm"]) |
| 43 |
def test_script_generate_launcher(section, kind): |
| 44 |
launcher_data = _read_launcher_data(section, kind) |
| 45 |
|
| 46 |
script = Script("foo", "foo.bar", "baz.qux", section=section) |
| 47 |
name, data = script.generate("#!C:\\path to my\\python.exe\n", kind=kind) |
| 48 |
|
| 49 |
prefix_len = len(launcher_data) + len(b"#!C:\\path to my\\python.exe\n") |
| 50 |
stream = io.BytesIO(data[prefix_len:]) |
| 51 |
with zipfile.ZipFile(stream) as zf: |
| 52 |
code = zf.read("__main__.py") |
| 53 |
|
| 54 |
assert name == "foo.exe" |
| 55 |
assert data.startswith(launcher_data) |
| 56 |
if section == "gui": |
| 57 |
assert b"#!C:\\path to my\\pythonw.exe\n" in data |
| 58 |
else: |
| 59 |
assert b"#!C:\\path to my\\python.exe\n" in data |
| 60 |
assert b"\nfrom foo.bar import baz\n" in code |
| 61 |
assert b"baz.qux()" in code |
| 62 |
|
| 63 |
|
| •64 |
@pytest.mark.skip( |
| 65 |
"Skipped on Debian, we don't include the simple_launcher .exes") |
| 66 |
@pytest.mark.parametrize( |
| 67 |
"section, kind", |
| 68 |
[("nonexist", "win-ia32"), ("console", "nonexist"), ("nonexist", "nonexist")], |
| 69 |
) |
| 70 |
def test_script_generate_launcher_error(section, kind): |
| 71 |
script = Script("foo", "foo.bar", "baz.qux", section=section) |
| 72 |
with pytest.raises(InvalidScript): |
| 73 |
script.generate("#!C:\\path to my\\python.exe\n", kind=kind) |