File: test_pkg_resources.py

package info (click to toggle)
pyinstaller 6.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,824 kB
  • sloc: python: 41,828; ansic: 12,123; makefile: 171; sh: 131; xml: 19
file content (101 lines) | stat: -rw-r--r-- 3,997 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
#-----------------------------------------------------------------------------
# Copyright (c) 2005-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)
#-----------------------------------------------------------------------------

import os
import pathlib

from PyInstaller.utils.tests import importorskip
from PyInstaller.compat import is_darwin, exec_python_rc

# Directory with testing modules used in some tests.
_MODULES_DIR = pathlib.Path(__file__).parent / 'modules'

# All tests in this file require pkg_resources
pytestmark = importorskip('pkg_resources')


def test_pkg_resources_importable(pyi_builder):
    """
    Check that a trivial example using pkg_resources does build.
    """
    pyi_builder.test_source("""
        import pkg_resources
        pkg_resources.working_set.require()
        """)


def test_pkg_resources_resource_string(pyi_builder):
    add_data_arg = f"{_MODULES_DIR / 'pkg3' / 'sample-data.txt'}:pkg3"
    pyi_builder.test_source(
        """
        import pkg_resources
        import pkg3

        expected_data = b'This is data text for testing the packaging module data.'

        # In a frozen app, the resources is available at: os.path.join(sys._MEIPASS, 'pkg3/sample-data.txt')
        data = pkg_resources.resource_string(pkg3.__name__, 'sample-data.txt')
        if not data:
            raise SystemExit('Error: Could not read data with pkg_resources.resource_string().')

        if data.strip() != expected_data:
            raise SystemExit('Error: Read data does not match expected data!')
        """,
        pyi_args=['--add-data', add_data_arg],
    )


# Tests for pkg_resources provider.
#
# These tests run a test script (scripts/pyi_pkg_resources_provider.py) in unfrozen and frozen form, in combination with
# a custom test package (modules/pyi_pkg_resources_provider/package).
#
# Running the unfrozen test script allows us to verify the behavior of DefaultProvider from pkg_resources and thereby
# also validate the test script itself. Running the frozen test validates the behavior of the PyiFrozenProvider.
#
# For details on the structure of the test and the contents of the test package, see the top comment in the test script
# itself.
def _exec_python_script(script_filename, pathex):
    # Prepare the environment - default to 'os.environ'...
    env = os.environ.copy()
    # ... and prepend PYTHONPATH with pathex
    if 'PYTHONPATH' in env:
        pathex = os.pathsep.join([str(pathex), env['PYTHONPATH']])
    env['PYTHONPATH'] = str(pathex)
    # Run the test script
    return exec_python_rc(str(script_filename), env=env)


def test_pkg_resources_provider_source(tmp_path, script_dir, monkeypatch):
    # Run the test script unfrozen - to validate it is working and to verify the behavior of
    # pkg_resources.DefaultProvider.
    pathex = _MODULES_DIR / 'pyi_pkg_resources_provider' / 'package'
    test_script = script_dir / 'pyi_pkg_resources_provider.py'
    ret = _exec_python_script(test_script, pathex=pathex)
    assert ret == 0, "Test script failed!"


def test_pkg_resources_provider_frozen(pyi_builder, tmp_path, script_dir, monkeypatch):
    # Run the test script as a frozen program
    pathex = _MODULES_DIR / 'pyi_pkg_resources_provider' / 'package'
    test_script = 'pyi_pkg_resources_provider.py'
    hooks_dir = _MODULES_DIR / 'pyi_pkg_resources_provider' / 'hooks'
    pyi_args = [
        '--paths', str(pathex),
        '--hidden-import', 'pyi_pkgres_testpkg',
        '--additional-hooks-dir', str(hooks_dir),
    ]  # yapf: disable
    if is_darwin:
        pyi_args += ['--windowed']  # Also build and test .app bundle executable
    pyi_builder.test_script(
        test_script,
        pyi_args=pyi_args,
    )