| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import argparse |
| 4 |
import lzma |
| 5 |
import zipfile |
| 6 |
import zlib |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from auditwheel.tools import EnvironmentDefault, dir2zip, is_subdir, zip2dir |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.parametrize( |
| 15 |
("environ", "passed", "expected"), |
| 16 |
[ |
| 17 |
(None, None, "manylinux1"), |
| 18 |
(None, "manylinux2010", "manylinux2010"), |
| 19 |
("manylinux2010", None, "manylinux2010"), |
| 20 |
("manylinux2010", "linux", "linux"), |
| 21 |
], |
| 22 |
) |
| 23 |
def test_plat_environment_action( |
| 24 |
monkeypatch: pytest.MonkeyPatch, |
| 25 |
environ: str | None, |
| 26 |
passed: str | None, |
| 27 |
expected: str, |
| 28 |
) -> None: |
| 29 |
choices = ["linux", "manylinux1", "manylinux2010"] |
| 30 |
argv = [] |
| 31 |
if passed: |
| 32 |
argv = ["--plat", passed] |
| 33 |
if environ: |
| 34 |
monkeypatch.setenv("AUDITWHEEL_PLAT", environ) |
| 35 |
p = argparse.ArgumentParser() |
| 36 |
p.add_argument( |
| 37 |
"--plat", |
| 38 |
action=EnvironmentDefault, |
| 39 |
env="AUDITWHEEL_PLAT", |
| 40 |
dest="PLAT", |
| 41 |
choices=choices, |
| 42 |
default="manylinux1", |
| 43 |
) |
| 44 |
args = p.parse_args(argv) |
| 45 |
assert expected == args.PLAT |
| 46 |
|
| 47 |
|
| 48 |
_all_zip_level: list[int] = list( |
| 49 |
range(zlib.Z_NO_COMPRESSION, zlib.Z_BEST_COMPRESSION + 1), |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize( |
| 54 |
("environ", "passed", "expected"), |
| 55 |
[ |
| 56 |
(None, None, -1), |
| 57 |
(0, None, 0), |
| 58 |
(0, 1, 1), |
| 59 |
(6, 1, 1), |
| 60 |
], |
| 61 |
) |
| 62 |
def test_zip_environment_action( |
| 63 |
monkeypatch: pytest.MonkeyPatch, |
| 64 |
environ: int | None, |
| 65 |
passed: int | None, |
| 66 |
expected: int, |
| 67 |
) -> None: |
| 68 |
choices = _all_zip_level |
| 69 |
argv = [] |
| 70 |
if passed is not None: |
| 71 |
argv = ["--zip-compression-level", str(passed)] |
| 72 |
if environ is not None: |
| 73 |
monkeypatch.setenv("AUDITWHEEL_ZIP_COMPRESSION_LEVEL", str(environ)) |
| 74 |
p = argparse.ArgumentParser() |
| 75 |
p.add_argument( |
| 76 |
"-z", |
| 77 |
"--zip-compression-level", |
| 78 |
action=EnvironmentDefault, |
| 79 |
metavar="zip", |
| 80 |
env="AUDITWHEEL_ZIP_COMPRESSION_LEVEL", |
| 81 |
dest="zip", |
| 82 |
type=int, |
| 83 |
help="Compress level to be used to create zip file.", |
| 84 |
choices=choices, |
| 85 |
default=zlib.Z_DEFAULT_COMPRESSION, |
| 86 |
) |
| 87 |
args = p.parse_args(argv) |
| 88 |
assert expected == args.zip |
| 89 |
|
| 90 |
|
| 91 |
def test_environment_action_invalid_plat_env(monkeypatch: pytest.MonkeyPatch) -> None: |
| 92 |
choices = ["linux", "manylinux1", "manylinux2010"] |
| 93 |
monkeypatch.setenv("AUDITWHEEL_PLAT", "foo") |
| 94 |
p = argparse.ArgumentParser() |
| 95 |
with pytest.raises(argparse.ArgumentError): |
| 96 |
p.add_argument( |
| 97 |
"--plat", |
| 98 |
action=EnvironmentDefault, |
| 99 |
env="AUDITWHEEL_PLAT", |
| 100 |
dest="PLAT", |
| 101 |
choices=choices, |
| 102 |
default="manylinux1", |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
def test_environment_action_invalid_zip_env(monkeypatch: pytest.MonkeyPatch) -> None: |
| 107 |
choices = _all_zip_level |
| 108 |
monkeypatch.setenv("AUDITWHEEL_ZIP_COMPRESSION_LEVEL", "foo") |
| 109 |
p = argparse.ArgumentParser() |
| 110 |
with pytest.raises(argparse.ArgumentError): |
| 111 |
p.add_argument( |
| 112 |
"-z", |
| 113 |
"--zip-compression-level", |
| 114 |
action=EnvironmentDefault, |
| 115 |
metavar="zip", |
| 116 |
env="AUDITWHEEL_ZIP_COMPRESSION_LEVEL", |
| 117 |
dest="zip", |
| 118 |
type=int, |
| 119 |
help="Compress level to be used to create zip file.", |
| 120 |
choices=choices, |
| 121 |
default=zlib.Z_DEFAULT_COMPRESSION, |
| 122 |
) |
| 123 |
monkeypatch.setenv("AUDITWHEEL_ZIP_COMPRESSION_LEVEL", "10") |
| 124 |
with pytest.raises(argparse.ArgumentError): |
| 125 |
p.add_argument( |
| 126 |
"-z", |
| 127 |
"--zip-compression-level", |
| 128 |
action=EnvironmentDefault, |
| 129 |
metavar="zip", |
| 130 |
env="AUDITWHEEL_ZIP_COMPRESSION_LEVEL", |
| 131 |
dest="zip", |
| 132 |
type=int, |
| 133 |
help="Compress level to be used to create zip file.", |
| 134 |
choices=choices, |
| 135 |
default=zlib.Z_DEFAULT_COMPRESSION, |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
def _write_test_permissions_zip(path: Path) -> None: |
| 140 |
source_zip_xz = Path(__file__).parent / "test-permissions.zip.xz" |
| 141 |
with lzma.open(source_zip_xz) as f: |
| 142 |
path.write_bytes(f.read()) |
| 143 |
|
| 144 |
|
| 145 |
def _check_permissions(path: Path) -> None: |
| 146 |
for i in range(8): |
| 147 |
for j in range(8): |
| 148 |
for k in range(8): |
| 149 |
mode = (path / f"{i}{j}{k}.f").stat().st_mode |
| 150 |
assert ((mode >> 6) & 7) == (i | 6) # always read/write |
| 151 |
assert ((mode >> 3) & 7) == j |
| 152 |
assert ((mode >> 0) & 7) == k |
| 153 |
mode = (path / f"{i}{j}{k}.d").stat().st_mode |
| 154 |
assert ((mode >> 6) & 7) == 7 # always read/write/execute |
| 155 |
assert ((mode >> 3) & 7) == 5 # always read/execute |
| 156 |
assert ((mode >> 0) & 7) == 5 # always read/execute |
| 157 |
|
| 158 |
|
| 159 |
def test_zip2dir_permissions(tmp_path: Path) -> None: |
| 160 |
source_zip = tmp_path / "test-permissions.zip" |
| 161 |
_write_test_permissions_zip(source_zip) |
| 162 |
extract_path = tmp_path / "unzip" |
| 163 |
zip2dir(source_zip, extract_path) |
| 164 |
_check_permissions(extract_path) |
| 165 |
|
| 166 |
|
| 167 |
def test_zip2dir_round_trip_permissions(tmp_path: Path) -> None: |
| 168 |
source_zip = tmp_path / "test-permissions.zip" |
| 169 |
_write_test_permissions_zip(source_zip) |
| 170 |
extract_path = tmp_path / "unzip2" |
| 171 |
zip2dir(source_zip, tmp_path / "unzip1") |
| 172 |
dir2zip(tmp_path / "unzip1", tmp_path / "tmp.zip", zlib.Z_DEFAULT_COMPRESSION, None) |
| 173 |
zip2dir(tmp_path / "tmp.zip", extract_path) |
| 174 |
_check_permissions(extract_path) |
| 175 |
|
| 176 |
|
| 177 |
def test_dir2zip_deflate(tmp_path: Path) -> None: |
| 178 |
buffer = b"\0" * 1024 * 1024 |
| 179 |
input_dir = tmp_path / "input_dir" |
| 180 |
input_dir.mkdir() |
| 181 |
input_file = input_dir / "zeros.bin" |
| 182 |
input_file.write_bytes(buffer) |
| 183 |
output_file = tmp_path / "ouput.zip" |
| 184 |
dir2zip(input_dir, output_file, zlib.Z_DEFAULT_COMPRESSION, None) |
| 185 |
assert output_file.stat().st_size < len(buffer) / 4 |
| 186 |
|
| 187 |
|
| 188 |
def test_dir2zip_folders(tmp_path: Path) -> None: |
| 189 |
input_dir = tmp_path / "input_dir" |
| 190 |
input_dir.mkdir() |
| 191 |
dist_info_folder = input_dir / "dummy-1.0.dist-info" |
| 192 |
dist_info_folder.mkdir() |
| 193 |
dist_info_folder.joinpath("METADATA").write_text("") |
| 194 |
empty_folder = input_dir / "dummy" / "empty" |
| 195 |
empty_folder.mkdir(parents=True) |
| 196 |
output_file = tmp_path / "output.zip" |
| 197 |
dir2zip(input_dir, output_file, zlib.Z_DEFAULT_COMPRESSION, None) |
| 198 |
expected_dirs = {"dummy/", "dummy/empty/", "dummy-1.0.dist-info/"} |
| 199 |
with zipfile.ZipFile(output_file, "r") as z: |
| 200 |
assert len(z.filelist) == 4 |
| 201 |
for info in z.filelist: |
| 202 |
if info.is_dir(): |
| 203 |
assert info.filename in expected_dirs |
| 204 |
expected_dirs.remove(info.filename) |
| 205 |
else: |
| 206 |
assert info.filename == "dummy-1.0.dist-info/METADATA" |
| 207 |
assert len(expected_dirs) == 0 |
| 208 |
|
| 209 |
|
| 210 |
def test_is_subdir(tmp_path: Path) -> None: |
| 211 |
root = tmp_path / "root" |
| 212 |
subdir = root / "subdir" |
| 213 |
subdir.mkdir(parents=True) |
| 214 |
assert is_subdir(subdir, root) |
| 215 |
assert is_subdir(root, root) |
| 216 |
assert not is_subdir(None, root) |
| 217 |
assert not is_subdir(tmp_path, root) |