File: test_tools.py

package info (click to toggle)
python-auditwheel 6.6.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 980 kB
  • sloc: python: 6,165; ansic: 304; cpp: 66; sh: 28; makefile: 25; f90: 12
file content (217 lines) | stat: -rw-r--r-- 6,941 bytes parent folder | download
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from __future__ import annotations

import argparse
import lzma
import zipfile
import zlib
from pathlib import Path

import pytest

from auditwheel.tools import EnvironmentDefault, dir2zip, is_subdir, zip2dir


@pytest.mark.parametrize(
    ("environ", "passed", "expected"),
    [
        (None, None, "manylinux1"),
        (None, "manylinux2010", "manylinux2010"),
        ("manylinux2010", None, "manylinux2010"),
        ("manylinux2010", "linux", "linux"),
    ],
)
def test_plat_environment_action(
    monkeypatch: pytest.MonkeyPatch,
    environ: str | None,
    passed: str | None,
    expected: str,
) -> None:
    choices = ["linux", "manylinux1", "manylinux2010"]
    argv = []
    if passed:
        argv = ["--plat", passed]
    if environ:
        monkeypatch.setenv("AUDITWHEEL_PLAT", environ)
    p = argparse.ArgumentParser()
    p.add_argument(
        "--plat",
        action=EnvironmentDefault,
        env="AUDITWHEEL_PLAT",
        dest="PLAT",
        choices=choices,
        default="manylinux1",
    )
    args = p.parse_args(argv)
    assert expected == args.PLAT


_all_zip_level: list[int] = list(
    range(zlib.Z_NO_COMPRESSION, zlib.Z_BEST_COMPRESSION + 1),
)


@pytest.mark.parametrize(
    ("environ", "passed", "expected"),
    [
        (None, None, -1),
        (0, None, 0),
        (0, 1, 1),
        (6, 1, 1),
    ],
)
def test_zip_environment_action(
    monkeypatch: pytest.MonkeyPatch,
    environ: int | None,
    passed: int | None,
    expected: int,
) -> None:
    choices = _all_zip_level
    argv = []
    if passed is not None:
        argv = ["--zip-compression-level", str(passed)]
    if environ is not None:
        monkeypatch.setenv("AUDITWHEEL_ZIP_COMPRESSION_LEVEL", str(environ))
    p = argparse.ArgumentParser()
    p.add_argument(
        "-z",
        "--zip-compression-level",
        action=EnvironmentDefault,
        metavar="zip",
        env="AUDITWHEEL_ZIP_COMPRESSION_LEVEL",
        dest="zip",
        type=int,
        help="Compress level to be used to create zip file.",
        choices=choices,
        default=zlib.Z_DEFAULT_COMPRESSION,
    )
    args = p.parse_args(argv)
    assert expected == args.zip


def test_environment_action_invalid_plat_env(monkeypatch: pytest.MonkeyPatch) -> None:
    choices = ["linux", "manylinux1", "manylinux2010"]
    monkeypatch.setenv("AUDITWHEEL_PLAT", "foo")
    p = argparse.ArgumentParser()
    with pytest.raises(argparse.ArgumentError):
        p.add_argument(
            "--plat",
            action=EnvironmentDefault,
            env="AUDITWHEEL_PLAT",
            dest="PLAT",
            choices=choices,
            default="manylinux1",
        )


def test_environment_action_invalid_zip_env(monkeypatch: pytest.MonkeyPatch) -> None:
    choices = _all_zip_level
    monkeypatch.setenv("AUDITWHEEL_ZIP_COMPRESSION_LEVEL", "foo")
    p = argparse.ArgumentParser()
    with pytest.raises(argparse.ArgumentError):
        p.add_argument(
            "-z",
            "--zip-compression-level",
            action=EnvironmentDefault,
            metavar="zip",
            env="AUDITWHEEL_ZIP_COMPRESSION_LEVEL",
            dest="zip",
            type=int,
            help="Compress level to be used to create zip file.",
            choices=choices,
            default=zlib.Z_DEFAULT_COMPRESSION,
        )
    monkeypatch.setenv("AUDITWHEEL_ZIP_COMPRESSION_LEVEL", "10")
    with pytest.raises(argparse.ArgumentError):
        p.add_argument(
            "-z",
            "--zip-compression-level",
            action=EnvironmentDefault,
            metavar="zip",
            env="AUDITWHEEL_ZIP_COMPRESSION_LEVEL",
            dest="zip",
            type=int,
            help="Compress level to be used to create zip file.",
            choices=choices,
            default=zlib.Z_DEFAULT_COMPRESSION,
        )


def _write_test_permissions_zip(path: Path) -> None:
    source_zip_xz = Path(__file__).parent / "test-permissions.zip.xz"
    with lzma.open(source_zip_xz) as f:
        path.write_bytes(f.read())


def _check_permissions(path: Path) -> None:
    for i in range(8):
        for j in range(8):
            for k in range(8):
                mode = (path / f"{i}{j}{k}.f").stat().st_mode
                assert ((mode >> 6) & 7) == (i | 6)  # always read/write
                assert ((mode >> 3) & 7) == j
                assert ((mode >> 0) & 7) == k
                mode = (path / f"{i}{j}{k}.d").stat().st_mode
                assert ((mode >> 6) & 7) == 7  # always read/write/execute
                assert ((mode >> 3) & 7) == 5  # always read/execute
                assert ((mode >> 0) & 7) == 5  # always read/execute


def test_zip2dir_permissions(tmp_path: Path) -> None:
    source_zip = tmp_path / "test-permissions.zip"
    _write_test_permissions_zip(source_zip)
    extract_path = tmp_path / "unzip"
    zip2dir(source_zip, extract_path)
    _check_permissions(extract_path)


def test_zip2dir_round_trip_permissions(tmp_path: Path) -> None:
    source_zip = tmp_path / "test-permissions.zip"
    _write_test_permissions_zip(source_zip)
    extract_path = tmp_path / "unzip2"
    zip2dir(source_zip, tmp_path / "unzip1")
    dir2zip(tmp_path / "unzip1", tmp_path / "tmp.zip", zlib.Z_DEFAULT_COMPRESSION, None)
    zip2dir(tmp_path / "tmp.zip", extract_path)
    _check_permissions(extract_path)


def test_dir2zip_deflate(tmp_path: Path) -> None:
    buffer = b"\0" * 1024 * 1024
    input_dir = tmp_path / "input_dir"
    input_dir.mkdir()
    input_file = input_dir / "zeros.bin"
    input_file.write_bytes(buffer)
    output_file = tmp_path / "ouput.zip"
    dir2zip(input_dir, output_file, zlib.Z_DEFAULT_COMPRESSION, None)
    assert output_file.stat().st_size < len(buffer) / 4


def test_dir2zip_folders(tmp_path: Path) -> None:
    input_dir = tmp_path / "input_dir"
    input_dir.mkdir()
    dist_info_folder = input_dir / "dummy-1.0.dist-info"
    dist_info_folder.mkdir()
    dist_info_folder.joinpath("METADATA").write_text("")
    empty_folder = input_dir / "dummy" / "empty"
    empty_folder.mkdir(parents=True)
    output_file = tmp_path / "output.zip"
    dir2zip(input_dir, output_file, zlib.Z_DEFAULT_COMPRESSION, None)
    expected_dirs = {"dummy/", "dummy/empty/", "dummy-1.0.dist-info/"}
    with zipfile.ZipFile(output_file, "r") as z:
        assert len(z.filelist) == 4
        for info in z.filelist:
            if info.is_dir():
                assert info.filename in expected_dirs
                expected_dirs.remove(info.filename)
            else:
                assert info.filename == "dummy-1.0.dist-info/METADATA"
    assert len(expected_dirs) == 0


def test_is_subdir(tmp_path: Path) -> None:
    root = tmp_path / "root"
    subdir = root / "subdir"
    subdir.mkdir(parents=True)
    assert is_subdir(subdir, root)
    assert is_subdir(root, root)
    assert not is_subdir(None, root)
    assert not is_subdir(tmp_path, root)