Debian Codegraph @

tests/test_scripts.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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.parametrize("section", ["console", "gui"])
40 @pytest.mark.parametrize("kind", ["win-ia32", "win-amd64", "win-arm"])
41 def test_script_generate_launcher(section, kind):
42 launcher_data = _read_launcher_data(section, kind)
43
44 script = Script("foo", "foo.bar", "baz.qux", section=section)
45 name, data = script.generate("#!C:\\path to my\\python.exe\n", kind=kind)
46
47 prefix_len = len(launcher_data) + len(b"#!C:\\path to my\\python.exe\n")
48 stream = io.BytesIO(data[prefix_len:])
49 with zipfile.ZipFile(stream) as zf:
50 code = zf.read("__main__.py")
51
52 assert name == "foo.exe"
53 assert data.startswith(launcher_data)
54 if section == "gui":
55 assert b"#!C:\\path to my\\pythonw.exe\n" in data
56 else:
57 assert b"#!C:\\path to my\\python.exe\n" in data
58 assert b"\nfrom foo.bar import baz\n" in code
59 assert b"baz.qux()" in code
60
61
62 @pytest.mark.parametrize(
63 "section, kind",
64 [("nonexist", "win-ia32"), ("console", "nonexist"), ("nonexist", "nonexist")],
65 )
66 def test_script_generate_launcher_error(section, kind):
67 script = Script("foo", "foo.bar", "baz.qux", section=section)
68 with pytest.raises(InvalidScript):
69 script.generate("#!C:\\path to my\\python.exe\n", kind=kind)