-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathtest_install_script.py
95 lines (80 loc) · 2.84 KB
/
test_install_script.py
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
91
92
93
94
95
import sys
import textwrap
import pytest
from tests.lib import PipTestEnvironment
# TODO:2024-10-05:snoopj:need a test for requires-python support, too.
# Implement in terms of sys.version_info ?
@pytest.mark.network
def test_script_file(script: PipTestEnvironment) -> None:
"""
Test installing from a script with inline metadata (PEP 723).
"""
other_lib_name, other_lib_version = "peppercorn", "0.6"
script_path = script.scratch_path.joinpath("script.py")
script_path.write_text(
textwrap.dedent(
f"""\
# /// script
# dependencies = [
# "INITools==0.2",
# "{other_lib_name}<={other_lib_version}",
# ]
# ///
print("Hello world from a dummy program")
"""
)
)
result = script.pip("install", "--script", script_path)
# NOTE:2024-10-05:snoopj:assertions same as in test_requirements_file
result.did_create(script.site_packages / "INITools-0.2.dist-info")
result.did_create(script.site_packages / "initools")
assert result.files_created[script.site_packages / other_lib_name].dir
fn = f"{other_lib_name}-{other_lib_version}.dist-info"
assert result.files_created[script.site_packages / fn].dir
def test_multiple_scripts(script: PipTestEnvironment) -> None:
"""
Test that --script can only be given once in an install command.
"""
result = script.pip(
"install",
"--script",
"does_not_exist.py",
"--script",
"also_does_not_exist.py",
allow_stderr_error=True,
expect_error=True,
)
assert "ERROR: --script can only be given once" in result.stderr, (
"multiple script did not fail as expected -- " + result.stderr
)
@pytest.mark.network
def test_script_file_python_version(script: PipTestEnvironment) -> None:
"""
Test installing from a script with an incompatible `requires-python`
"""
other_lib_name, other_lib_version = "peppercorn", "0.6"
script_path = script.scratch_path.joinpath("script.py")
target_python_ver = f"{sys.version_info.major}.{sys.version_info.minor + 1}"
script_path.write_text(
textwrap.dedent(
f"""\
# /// script
# requires-python = ">={target_python_ver}"
# dependencies = [
# "INITools==0.2",
# "{other_lib_name}<={other_lib_version}",
# ]
# ///
print("Hello world from a dummy program")
"""
)
)
result = script.pip(
"install", "--script", script_path, expect_stderr=True, expect_error=True
)
assert (
f"ERROR: Script '{script_path}' requires a different Python" in result.stderr
), (
"Script with incompatible requires-python did not fail as expected -- "
+ result.stderr
)