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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
import io
import os.path
import pytest
from installer.destinations import SchemeDictionaryDestination, WheelDestination
from installer.records import RecordEntry
from installer.scripts import Script
from installer.utils import SCHEME_NAMES
class TestWheelDestination:
def test_takes_no_arguments(self):
WheelDestination()
def test_raises_not_implemented_error(self):
destination = WheelDestination()
with pytest.raises(NotImplementedError):
destination.write_script(name=None, module=None, attr=None, section=None)
with pytest.raises(NotImplementedError):
destination.write_file(
scheme=None, path=None, stream=None, is_executable=False
)
with pytest.raises(NotImplementedError):
destination.finalize_installation(
scheme=None,
record_file_path=None,
records=None,
)
class TestSchemeDictionaryDestination:
@pytest.fixture()
def destination(self, tmp_path):
scheme_dict = {}
for scheme in SCHEME_NAMES:
full_path = tmp_path / scheme
if not full_path.exists():
full_path.mkdir()
scheme_dict[scheme] = str(full_path)
return SchemeDictionaryDestination(scheme_dict, "/my/python", "posix")
@pytest.mark.parametrize(
("scheme", "path", "data", "expected"),
[
pytest.param(
"data", "my_data.bin", b"my data", b"my data", id="normal file"
),
pytest.param(
"data",
"data_folder/my_data.bin",
b"my data",
b"my data",
id="normal file in subfolder",
),
pytest.param(
"scripts",
"my_script.py",
b"#!python\nmy script",
b"#!/my/python\nmy script",
id="script file",
),
pytest.param(
"scripts",
"script_folder/my_script.py",
b"#!python\nmy script",
b"#!/my/python\nmy script",
id="script file in subfolder",
),
],
)
def test_write_file(self, destination, scheme, path, data, expected):
record = destination.write_file(scheme, path, io.BytesIO(data), False)
file_path = os.path.join(destination.scheme_dict[scheme], path)
with open(file_path, "rb") as f:
file_data = f.read()
assert file_data == expected
assert record.path == path
def test_write_record_duplicate(self, destination):
destination.write_file("data", "my_data.bin", io.BytesIO(b"my data"), False)
with pytest.raises(FileExistsError):
destination.write_file("data", "my_data.bin", io.BytesIO(b"my data"), False)
def test_write_script(self, destination):
script_args = ("my_entrypoint", "my_module", "my_function", "console")
record = destination.write_script(*script_args)
file_path = os.path.join(destination.scheme_dict["scripts"], "my_entrypoint")
assert os.path.isfile(file_path)
with open(file_path, "rb") as f:
file_data = f.read()
name, expected_data = Script(*script_args).generate("/my/python", "posix")
assert file_data == expected_data
assert record.path == "my_entrypoint"
def test_finalize_write_record(self, destination):
records = [
(
"data",
destination.write_file(
"data",
"my_data1.bin",
io.BytesIO(b"my data 1"),
is_executable=False,
),
),
(
"data",
destination.write_file(
"data",
"my_data2.bin",
io.BytesIO(b"my data 2"),
is_executable=False,
),
),
(
"data",
destination.write_file(
"data",
"my_data3,my_data4.bin",
io.BytesIO(b"my data 3"),
is_executable=False,
),
),
(
"scripts",
destination.write_file(
"scripts",
"my_script",
io.BytesIO(b"my script"),
is_executable=True,
),
),
(
"scripts",
destination.write_file(
"scripts",
"my_script2",
io.BytesIO(b"#!python\nmy script"),
is_executable=False,
),
),
(
"scripts",
destination.write_script(
"my_entrypoint", "my_module", "my_function", "console"
),
),
("purelib", RecordEntry("RECORD", None, None)),
]
destination.finalize_installation("purelib", "RECORD", records)
file_path = os.path.join(destination.scheme_dict["purelib"], "RECORD")
with open(file_path, "rb") as f:
data = f.read()
assert data == (
b"../data/my_data1.bin,sha256=NV0A-M4OPuqTsHjeD6Wth_-UqrpAAAdyplcustFZ8s4,9\n"
b"../data/my_data2.bin,sha256=lP7V8oWLqgyXCbdASNiPdsUogzPUZhht_7F8T5bC3eQ,9\n"
b'"../data/my_data3,my_data4.bin",sha256=18krruu1gr01x-WM_9ChSASoHv0mfRAV6-B2bd9sxpo,9\n'
b"../scripts/my_script,sha256=M60fWvUSMJkPtw2apUvjWWwOcnRPcVy_zO4-4lpH08o,9\n"
b"../scripts/my_script2,sha256=k9_997kTbTYQm7EXFLclVZL1m2N98rU90QX46XeMvjY,22\n"
b"../scripts/my_entrypoint,sha256=_p_9nwmeIeoMBfQ0akhr1KbKn3laDydg0J7cy0Fs6JI,216\n"
b"RECORD,,\n"
)
|