diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-29 04:21:11 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-29 04:21:11 +0000 |
commit | cdb4a4e19b096cdbf1356e28287238122fc3599c (patch) | |
tree | c5ed3b2b40e4725bbaaae0710d1cbec21b23f3b0 /tests/test_scripts.py | |
parent | Initial commit. (diff) | |
download | python-installer-cdb4a4e19b096cdbf1356e28287238122fc3599c.tar.xz python-installer-cdb4a4e19b096cdbf1356e28287238122fc3599c.zip |
Adding upstream version 0.6.0+dfsg1.upstream/0.6.0+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/test_scripts.py')
-rw-r--r-- | tests/test_scripts.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/test_scripts.py b/tests/test_scripts.py new file mode 100644 index 0000000..2da6577 --- /dev/null +++ b/tests/test_scripts.py @@ -0,0 +1,70 @@ +import io +import os +import zipfile + +import pytest + +from installer import _scripts +from installer.scripts import InvalidScript, Script + + +def test_script_generate_simple(): + script = Script("foo", "foo.bar", "baz.qux", section="console") + name, data = script.generate("/path/to/my/python", kind="posix") + + assert name == "foo" + assert data.startswith(b"#!/path/to/my/python\n") + assert b"\nfrom foo.bar import baz\n" in data + assert b"baz.qux()" in data + + +def test_script_generate_space_in_executable(): + script = Script("foo", "foo.bar", "baz.qux", section="console") + name, data = script.generate("/path to my/python", kind="posix") + + assert name == "foo" + assert data.startswith(b"#!/bin/sh\n") + assert b" '/path to my/python'" in data + assert b"\nfrom foo.bar import baz\n" in data + assert b"baz.qux()" in data + + +def _read_launcher_data(section, kind): + prefix = {"console": "t", "gui": "w"}[section] + suffix = {"win-ia32": "32", "win-amd64": "64", "win-arm": "_arm"}[kind] + filename = os.path.join( + os.path.dirname(os.path.abspath(_scripts.__file__)), + f"{prefix}{suffix}.exe", + ) + with open(filename, "rb") as f: + return f.read() + + +@pytest.mark.parametrize("section", ["console", "gui"]) +@pytest.mark.parametrize("kind", ["win-ia32", "win-amd64", "win-arm"]) +def test_script_generate_launcher(section, kind): + launcher_data = _read_launcher_data(section, kind) + + script = Script("foo", "foo.bar", "baz.qux", section=section) + name, data = script.generate("#!C:\\path to my\\python.exe\n", kind=kind) + + prefix_len = len(launcher_data) + len(b"#!C:\\path to my\\python.exe\n") + stream = io.BytesIO(data[prefix_len:]) + with zipfile.ZipFile(stream) as zf: + code = zf.read("__main__.py") + + assert name == "foo.exe" + assert data.startswith(launcher_data) + assert b"#!C:\\path to my\\python.exe\n" in data + assert b"\nfrom foo.bar import baz\n" in code + assert b"baz.qux()" in code + + +@pytest.mark.parametrize( + "section, kind", + [("nonexist", "win-ia32"), ("console", "nonexist"), ("nonexist", "nonexist")], +) +def test_script_generate_launcher_error(section, kind): + script = Script("foo", "foo.bar", "baz.qux", section=section) + with pytest.raises(InvalidScript): + script.generate("#!C:\\path to my\\python.exe\n", kind=kind) |