File: test_toc_normalization.py

package info (click to toggle)
pyinstaller 6.13.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,520 kB
  • sloc: python: 41,347; ansic: 11,334; makefile: 176; sh: 136; xml: 19
file content (195 lines) | stat: -rw-r--r-- 7,495 bytes parent folder | download | duplicates (3)
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
#-----------------------------------------------------------------------------
# Copyright (c) 2023, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------

# Tests for explicit TOC list normalization that replaced the implicit normalization with class:``TOC``.
import copy
import pathlib

from PyInstaller import compat
from PyInstaller.building.datastruct import normalize_pyz_toc, normalize_toc

# Tests for regular TOC normalization.

_BASE_TOC = [
    ('libpython3.10.so', '/usr/lib64/libpython3.10.so', 'BINARY'),
    ('libsomething.so', '/usr/local/lib64/libsomething.so', 'BINARY'),
    ('README', '/home/user/tmp/README', 'DATA'),
    (str(pathlib.PurePath('data/data.csv')), '/home/user/tmp/data/data.csv', 'DATA'),
    ('dependency.bin', 'other_multipackage:dependency.bin', 'DEPENDENCY'),
    ('myextension.so', 'myextension.so', 'EXTENSION'),
]


def test_normalize_toc_no_duplicates():
    # No duplicates. We expect the output list to match the input list.
    toc = copy.copy(_BASE_TOC)
    expected_toc = _BASE_TOC

    normalized_toc = normalize_toc(toc)
    assert normalized_toc == expected_toc


def test_normalize_toc_duplicate_binary():
    # A duplicated BINARY entry. We expect the (second) duplicate to be removed.
    toc = copy.copy(_BASE_TOC)
    toc.insert(2, ('libsomething.so', '/opt/something/lib/libsomething.so', 'BINARY'))
    expected_toc = _BASE_TOC

    normalized_toc = normalize_toc(toc)
    assert normalized_toc == expected_toc


def test_normalize_toc_duplicate_binary_case_sensitive():
    # A BINARY entry that is a duplicate only on case-insensitive OSes.
    toc = copy.copy(_BASE_TOC)
    toc.insert(2, ('libSoMeThInG.so', '/opt/something/lib/libSoMeThInG.so', 'BINARY'))
    expected_toc = _BASE_TOC

    if compat.is_win:
        expected_toc = _BASE_TOC
    else:
        expected_toc = toc

    normalized_toc = normalize_toc(toc)
    assert normalized_toc == expected_toc


def test_normalize_toc_duplicate_data():
    # A duplicated DATA entry. We expect the (second) duplicate to be removed.
    toc = copy.copy(_BASE_TOC)
    toc.insert(3, ('README', '/home/user/tmp/README', 'DATA'))
    expected_toc = _BASE_TOC

    normalized_toc = normalize_toc(toc)
    assert normalized_toc == expected_toc


def test_normalize_toc_duplicate_data_case_sensitive():
    # A DATA entry that is a duplicate on case-insensitive OSes.
    toc = copy.copy(_BASE_TOC)
    toc.insert(-1, ('readme', '/home/user/tmp-other/readme', 'DATA'))
    expected_toc = _BASE_TOC

    if compat.is_win:
        expected_toc = _BASE_TOC
    else:
        expected_toc = toc

    normalized_toc = normalize_toc(toc)
    assert normalized_toc == expected_toc


def test_normalize_toc_conflicting_binary_and_data1():
    # An entry that's duplicated as both BINARY and DATA.
    # BINARY entry should be kept.
    toc = copy.copy(_BASE_TOC)
    toc.insert(2, ('libsomething.so', '/usr/local/lib64/libsomething.so', 'DATA'))  # Insert after BINARY entry
    expected_toc = _BASE_TOC

    normalized_toc = normalize_toc(toc)
    assert normalized_toc == expected_toc


def test_normalize_toc_conflicting_binary_and_data2():
    # An entry that's duplicated as both BINARY and DATA, in reverse order.
    # BINARY entry should be kept.
    toc = copy.copy(_BASE_TOC)
    toc.insert(1, ('libsomething.so', '/usr/local/lib64/libsomething.so', 'DATA'))  # Insert before BINARY entry
    expected_toc = _BASE_TOC

    normalized_toc = normalize_toc(toc)
    assert normalized_toc == expected_toc


def test_normalize_toc_multipackage_dependency():
    # An entry that's duplicated as both BINARY, DATA, EXTENSION, and DEPENDENCY.
    # DEPENDENCY should have the highest priority of the four.
    # The priority-based replacement during normalization might not preserve the order, so we need to sort the
    # resulting and expected TOC before comparing them. In this particular case, we insert duplicates at the
    # start of the list, so de-duplication effectively moves the DEPENDENCY entry to the first place in the
    # output list.
    toc = copy.copy(_BASE_TOC)
    toc.insert(0, ('dependency.bin', '/mnt/somewhere/dependency.bin', 'EXTENSION'))
    toc.insert(0, ('dependency.bin', '/mnt/somewhere/dependency.bin', 'BINARY'))
    toc.insert(0, ('dependency.bin', '/mnt/somewhere/dependency.bin', 'DATA'))
    expected_toc = _BASE_TOC

    normalized_toc = normalize_toc(toc)
    assert sorted(normalized_toc) == sorted(expected_toc)


def test_normalize_toc_with_parent_pardir_loops():
    # Check that de-duplication works even if destination paths contain local loop with parent directory (..) components
    # but can be normalized to the same path. Furthermore, we expect TOC normalization to sanitize the dest_name with
    # normalized version.
    toc = [
        (
            str(pathlib.PurePath('numpy/core/../../numpy.libs/libquadmath-2d0c479f.so.0.0.0')),
            '/path/to/venv/lib/python3.11/site-packages/numpy/core/../../numpy.libs/libquadmath-2d0c479f.so.0.0.0',
            'BINARY',
        ),
        (
            str(pathlib.PurePath('numpy/linalg/../../numpy.libs/libquadmath-2d0c479f.so.0.0.0')),
            '/path/to/venv/lib/python3.11/site-packages/numpy/linalg/../../numpy.libs/libquadmath-2d0c479f.so.0.0.0',
            'BINARY',
        ),
    ]
    expected_toc = [
        (
            str(pathlib.PurePath('numpy.libs/libquadmath-2d0c479f.so.0.0.0')),
            '/path/to/venv/lib/python3.11/site-packages/numpy/core/../../numpy.libs/libquadmath-2d0c479f.so.0.0.0',
            'BINARY',
        ),
    ]

    normalized_toc = normalize_toc(toc)
    assert sorted(normalized_toc) == sorted(expected_toc)


# Tests for PYZ TOC normalization.
_BASE_PYZ_TOC = [
    ('copy', '/usr/lib64/python3.11/copy.py', 'PYMODULE'),
    ('csv', '/usr/lib64/python3.11/csv.py', 'PYMODULE'),
    ('dataclasses', '/usr/lib64/python3.11/dataclasses.py', 'PYMODULE'),
    ('datetime', '/usr/lib64/python3.11/datetime.py', 'PYMODULE'),
    ('decimal', '/usr/lib64/python3.11/decimal.py', 'PYMODULE'),
    ('mymodule1', 'mymodule1.py', 'PYMODULE'),
    ('mymodule2', 'mymodule2.py', 'PYMODULE'),
]


def test_normalize_pyz_toc_no_duplicates():
    # No duplicates. We expect the output list to match the input list.
    toc = copy.copy(_BASE_PYZ_TOC)
    expected_toc = _BASE_PYZ_TOC

    normalized_toc = normalize_pyz_toc(toc)
    assert normalized_toc == expected_toc


def test_normalize_pyz_toc_duplicates():
    # Duplicated entry. We expect the (second) duplicate to be removed.
    toc = copy.copy(_BASE_PYZ_TOC)
    toc.insert(6, ('mymodule1', 'some-other-path/mymodule1.py', 'PYMODULE'))
    expected_toc = _BASE_PYZ_TOC

    normalized_toc = normalize_pyz_toc(toc)
    assert normalized_toc == expected_toc


def test_normalize_pyz_toc_case_sensitivity():
    # Duplicated entry with different case. In PYZ, the entries are case-sensitive, so the list is not modified.
    toc = copy.copy(_BASE_PYZ_TOC)
    toc.insert(6, ('MyMoDuLe1', 'some-other-path/MyMoDuLe1.py', 'PYMODULE'))
    expected_toc = toc

    normalized_toc = normalize_pyz_toc(toc)
    assert normalized_toc == expected_toc