| 1 | import io |
| 2 | from pathlib import Path |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from installer.destinations import SchemeDictionaryDestination, WheelDestination |
| 7 | from installer.records import RecordEntry |
| 8 | from installer.scripts import Script |
| 9 | from installer.utils import SCHEME_NAMES |
| 10 | |
| 11 | |
| 12 | class TestWheelDestination: |
| 13 | def test_takes_no_arguments(self): |
| 14 | WheelDestination() |
| 15 | |
| 16 | def test_raises_not_implemented_error(self): |
| 17 | destination = WheelDestination() |
| 18 | |
| 19 | with pytest.raises(NotImplementedError): |
| 20 | destination.write_script(name=None, module=None, attr=None, section=None) |
| 21 | |
| 22 | with pytest.raises(NotImplementedError): |
| 23 | destination.write_file( |
| 24 | scheme=None, path=None, stream=None, is_executable=False |
| 25 | ) |
| 26 | |
| 27 | with pytest.raises(NotImplementedError): |
| 28 | destination.finalize_installation( |
| 29 | scheme=None, |
| 30 | record_file_path=None, |
| 31 | records=None, |
| 32 | ) |
| 33 | |
| 34 | |
| 35 | class TestSchemeDictionaryDestination: |
| 36 | @pytest.fixture() |
| 37 | def destination(self, tmp_path): |
| 38 | scheme_dict = {} |
| 39 | for scheme in SCHEME_NAMES: |
| 40 | full_path = tmp_path / scheme |
| 41 | if not full_path.exists(): |
| 42 | full_path.mkdir() |
| 43 | scheme_dict[scheme] = str(full_path) |
| 44 | return SchemeDictionaryDestination(scheme_dict, "/my/python", "posix") |
| 45 | |
| 46 | @pytest.fixture() |
| 47 | def destination_overwrite_existing(self, tmp_path): |
| 48 | scheme_dict = {} |
| 49 | for scheme in SCHEME_NAMES: |
| 50 | full_path = tmp_path / scheme |
| 51 | if not full_path.exists(): |
| 52 | full_path.mkdir() |
| 53 | scheme_dict[scheme] = str(full_path) |
| 54 | return SchemeDictionaryDestination( |
| 55 | scheme_dict, "/my/python", "posix", overwrite_existing=True |
| 56 | ) |
| 57 | |
| 58 | @pytest.mark.parametrize( |
| 59 | ("scheme", "path", "data", "expected"), |
| 60 | [ |
| 61 | pytest.param( |
| 62 | "data", "my_data.bin", b"my data", b"my data", id="normal file" |
| 63 | ), |
| 64 | pytest.param( |
| 65 | "data", |
| 66 | "data_folder/my_data.bin", |
| 67 | b"my data", |
| 68 | b"my data", |
| 69 | id="normal file in subfolder", |
| 70 | ), |
| 71 | pytest.param( |
| 72 | "scripts", |
| 73 | "my_script.py", |
| 74 | b"#!python\nmy script", |
| 75 | b"#!/my/python\nmy script", |
| 76 | id="script file", |
| 77 | ), |
| 78 | pytest.param( |
| 79 | "scripts", |
| 80 | "script_folder/my_script.py", |
| 81 | b"#!python\nmy script", |
| 82 | b"#!/my/python\nmy script", |
| 83 | id="script file in subfolder", |
| 84 | ), |
| 85 | ], |
| 86 | ) |
| 87 | def test_write_file(self, destination, scheme, path, data, expected): |
| 88 | record = destination.write_file(scheme, path, io.BytesIO(data), False) |
| 89 | file_data = (Path(destination.scheme_dict[scheme]) / path).read_bytes() |
| 90 | assert file_data == expected |
| 91 | assert record.path == path |
| 92 | |
| 93 | def test_write_record_duplicate(self, destination): |
| 94 | destination.write_file("data", "my_data.bin", io.BytesIO(b"my data"), False) |
| 95 | with pytest.raises(FileExistsError): |
| 96 | destination.write_file("data", "my_data.bin", io.BytesIO(b"my data"), False) |
| 97 | |
| 98 | def test_write_record_duplicate_with_overwrite_existing( |
| 99 | self, destination_overwrite_existing |
| 100 | ): |
| 101 | destination_overwrite_existing.write_file( |
| 102 | "data", "my_data.bin", io.BytesIO(b"my data"), False |
| 103 | ) |
| 104 | destination_overwrite_existing.write_file( |
| 105 | "data", "my_data.bin", io.BytesIO(b"my data"), False |
| 106 | ) |
| 107 | |
| 108 | def test_write_script(self, destination): |
| 109 | script_args = ("my_entrypoint", "my_module", "my_function", "console") |
| 110 | record = destination.write_script(*script_args) |
| 111 | file_path = Path(destination.scheme_dict["scripts"]) / "my_entrypoint" |
| 112 | |
| 113 | assert file_path.is_file() |
| 114 | |
| 115 | file_data = file_path.read_bytes() |
| 116 | _, expected_data = Script(*script_args).generate("/my/python", "posix") |
| 117 | |
| 118 | assert file_data == expected_data |
| 119 | assert record.path == "my_entrypoint" |
| 120 | |
| 121 | def test_finalize_write_record(self, destination): |
| 122 | records = [ |
| 123 | ( |
| 124 | "data", |
| 125 | destination.write_file( |
| 126 | "data", |
| 127 | "my_data1.bin", |
| 128 | io.BytesIO(b"my data 1"), |
| 129 | is_executable=False, |
| 130 | ), |
| 131 | ), |
| 132 | ( |
| 133 | "data", |
| 134 | destination.write_file( |
| 135 | "data", |
| 136 | "my_data2.bin", |
| 137 | io.BytesIO(b"my data 2"), |
| 138 | is_executable=False, |
| 139 | ), |
| 140 | ), |
| 141 | ( |
| 142 | "data", |
| 143 | destination.write_file( |
| 144 | "data", |
| 145 | "my_data3,my_data4.bin", |
| 146 | io.BytesIO(b"my data 3"), |
| 147 | is_executable=False, |
| 148 | ), |
| 149 | ), |
| 150 | ( |
| 151 | "scripts", |
| 152 | destination.write_file( |
| 153 | "scripts", |
| 154 | "my_script", |
| 155 | io.BytesIO(b"my script"), |
| 156 | is_executable=True, |
| 157 | ), |
| 158 | ), |
| 159 | ( |
| 160 | "scripts", |
| 161 | destination.write_file( |
| 162 | "scripts", |
| 163 | "my_script2", |
| 164 | io.BytesIO(b"#!python\nmy script"), |
| 165 | is_executable=False, |
| 166 | ), |
| 167 | ), |
| 168 | ( |
| 169 | "scripts", |
| 170 | destination.write_script( |
| 171 | "my_entrypoint", "my_module", "my_function", "console" |
| 172 | ), |
| 173 | ), |
| 174 | ("purelib", RecordEntry("RECORD", None, None)), |
| 175 | ] |
| 176 | |
| 177 | destination.finalize_installation("purelib", "RECORD", records) |
| 178 | file_path = Path(destination.scheme_dict["purelib"]) / "RECORD" |
| 179 | |
| 180 | data = file_path.read_bytes() |
| 181 | expected_data = ( |
| 182 | b"RECORD,,\n" |
| 183 | b"data/my_data1.bin,sha256=NV0A-M4OPuqTsHjeD6Wth_-UqrpAAAdyplcustFZ8s4,9\n" |
| 184 | b"data/my_data2.bin,sha256=lP7V8oWLqgyXCbdASNiPdsUogzPUZhht_7F8T5bC3eQ,9\n" |
| 185 | b'data/my_data3,my_data4.bin",sha256=18krruu1gr01x-WM_9ChSASoHv0mfRAV6-B2bd9sxpo,9\n' |
| 186 | b"scripts/my_entrypoint,sha256=_p_9nwmeIeoMBfQ0akhr1KbKn3laDydg0J7cy0Fs6JI,216\n" |
| 187 | b"scripts/my_script,sha256=M60fWvUSMJkPtw2apUvjWWwOcnRPcVy_zO4-4lpH08o,9\n" |
| 188 | b"scripts/my_script2,sha256=k9_997kTbTYQm7EXFLclVZL1m2N98rU90QX46XeMvjY,22\n" |
| 189 | ) |
| 190 | assert all( |
| 191 | real.endswith(expected) |
| 192 | for real, expected in zip( |
| 193 | data.split(b"\n"), expected_data.split(b"\n"), strict=True |
| 194 | ) |
| 195 | ) |
| 196 | |
| 197 | def test_blocking_path_traversal(self, destination): |
| 198 | with pytest.raises(ValueError): |
| 199 | destination._path_with_destdir("purelib", "subdir/../../outside.txt") |