summaryrefslogtreecommitdiff
path: root/tests/cli/test_pack.py
blob: 74fd9374d03a04a83d34fc33f8b414a4dcb115ed (plain)
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
from __future__ import annotations

import os
from pathlib import Path
from textwrap import dedent
from zipfile import ZipFile

import pytest
from pytest import TempPathFactory

from wheel._cli.pack import pack

THISDIR = os.path.dirname(__file__)
TESTWHEEL_NAME = "test-1.0-py2.py3-none-any.whl"
TESTWHEEL_PATH = os.path.join(THISDIR, "..", "testdata", TESTWHEEL_NAME)


@pytest.mark.filterwarnings("error:Duplicate name")
@pytest.mark.parametrize(
    "build_tag_arg, existing_build_tag, filename",
    [
        pytest.param(None, None, "test-1.0-py2.py3-none-any.whl", id="nobuildnum"),
        pytest.param("2b", None, "test-1.0-2b-py2.py3-none-any.whl", id="newbuildarg"),
        pytest.param(None, "3", "test-1.0-3-py2.py3-none-any.whl", id="oldbuildnum"),
        pytest.param("", "3", "test-1.0-py2.py3-none-any.whl", id="erasebuildnum"),
    ],
)
def test_pack(
    tmp_path_factory: TempPathFactory,
    tmp_path: Path,
    build_tag_arg: str | None,
    existing_build_tag: str | None,
    filename: str,
) -> None:
    unpack_dir = tmp_path_factory.mktemp("wheeldir")
    with ZipFile(TESTWHEEL_PATH) as zf:
        old_record = zf.read("test-1.0.dist-info/RECORD")
        old_record_lines = sorted(
            line.rstrip()
            for line in old_record.split(b"\n")
            if line and not line.startswith(b"test-1.0.dist-info/WHEEL,")
        )
        zf.extractall(str(unpack_dir))

    if existing_build_tag:
        # Add the build number to WHEEL
        wheel_file_path = unpack_dir / "test-1.0.dist-info" / "WHEEL"
        wheel_file_content = wheel_file_path.read_bytes()
        assert b"Build" not in wheel_file_content
        wheel_file_content += b"Build: 3\r\n"
        wheel_file_path.write_bytes(wheel_file_content)

    pack(unpack_dir, tmp_path, build_tag_arg)
    new_wheel_path = tmp_path / filename
    assert new_wheel_path.is_file()

    with ZipFile(str(new_wheel_path)) as zf:
        new_record = zf.read("test-1.0.dist-info/RECORD")
        new_record_lines = sorted(
            line.rstrip()
            for line in new_record.split(b"\n")
            if line and not line.startswith(b"test-1.0.dist-info/WHEEL,")
        )

        new_wheel_file_content = zf.read("test-1.0.dist-info/WHEEL")

    assert new_record_lines == old_record_lines

    expected_build_num = build_tag_arg or existing_build_tag
    expected_wheel_content = dedent(
        """\
        Wheel-Version: 1.0
        Generator: bdist_wheel (0.30.0)
        Root-Is-Purelib: false
        Tag: py2-none-any
        Tag: py3-none-any
    """.replace(
            "\n", "\r\n"
        )
    )
    if expected_build_num:
        expected_wheel_content += "Build: %s\r\n" % expected_build_num

    expected_wheel_content_bytes = expected_wheel_content.encode("ascii")
    assert new_wheel_file_content == expected_wheel_content_bytes