Skip to content

Commit

Permalink
tests: move and reorganize couple more tests from test_libraries.py
Browse files Browse the repository at this point in the history
Move `test_pkg_resource_res_string` to `test_pkg_resources.py`.
Inline the test program code and remove the old program script.

Move `test_pkgutil_get_data` and `test_pkgutil_get_data__main__` to
`test_pkgutil.py`. Inline the test program code and remove the old
program scripts.

Move `test_pil_img_conversion`, `test_pil_PyQt5`, and `test_pil_plugins`.
Inline the test program code for the image conversion test and remove
the old program script. Reorganize the test program so that it receives
the input and output file paths from the test via command-line arguments;
this way, we do not need to bundle the test image into frozen application,
and we can save the result into `tmp_path`.
  • Loading branch information
rokm committed Dec 26, 2024
1 parent c99fe9c commit 7d5a783
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 184 deletions.
26 changes: 0 additions & 26 deletions tests/functional/scripts/pkg_resource_res_string.py

This file was deleted.

24 changes: 0 additions & 24 deletions tests/functional/scripts/pkgutil_get_data.py

This file was deleted.

22 changes: 0 additions & 22 deletions tests/functional/scripts/pkgutil_get_data__main__.py

This file was deleted.

23 changes: 0 additions & 23 deletions tests/functional/scripts/pyi_lib_PIL_img_conversion.py

This file was deleted.

75 changes: 1 addition & 74 deletions tests/functional/test_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------

import pathlib

import pytest

from PyInstaller.compat import is_win, is_linux
from PyInstaller.utils.tests import importorskip, xfail, skipif, requires
from PyInstaller.utils.tests import importorskip, skipif, requires
from PyInstaller.utils.hooks import can_import_module

# :todo: find a way to get this from `conftest` or such directory with testing modules used in some tests.
_MODULES_DIR = pathlib.Path(__file__).parent / 'modules'
_DATA_DIR = pathlib.Path(__file__).parent / 'data'


@importorskip('gevent')
def test_gevent(pyi_builder):
Expand Down Expand Up @@ -117,25 +111,6 @@ def shutdown_timer_callback():
)


def test_pkg_resource_res_string(pyi_builder, monkeypatch):
# Include some data files for testing pkg_resources module.
add_data_arg = f"{_MODULES_DIR / 'pkg3' / 'sample-data.txt'}:pkg3"
pyi_builder.test_script('pkg_resource_res_string.py', pyi_args=['--add-data', add_data_arg])


def test_pkgutil_get_data(pyi_builder, monkeypatch):
# Include some data files for testing pkg_resources module.
add_data_arg = f"{_MODULES_DIR / 'pkg3' / 'sample-data.txt'}:pkg3"
pyi_builder.test_script('pkgutil_get_data.py', pyi_args=['--add-data', add_data_arg])


@xfail(reason='Our import mechanism returns the wrong loader-class for __main__.')
def test_pkgutil_get_data__main__(pyi_builder, monkeypatch):
# Include some data files for testing pkg_resources module.
add_data_arg = f"{_MODULES_DIR / 'pkg3' / 'sample-data.txt'}:pkg3"
pyi_builder.test_script('pkgutil_get_data__main__.py', pyi_args=['--add-data', add_data_arg])


@importorskip('sphinx')
def test_sphinx(pyi_builder, data_dir):
pyi_builder.test_script(
Expand Down Expand Up @@ -391,54 +366,6 @@ def test_zeep(pyi_builder):
)


@importorskip('PIL')
#@pytest.mark.xfail(reason="Fails with Pillow 3.0.0")
def test_pil_img_conversion(pyi_builder):
add_data_arg = f"{_DATA_DIR / 'PIL_images'}:."
pyi_builder.test_script(
'pyi_lib_PIL_img_conversion.py',
pyi_args=[
'--add-data', add_data_arg,
# Use console mode or else on Windows the VS() messageboxes will stall pytest.
'--console'
],
) # yapf: disable


@requires("pillow >= 1.1.6")
@importorskip('PyQt5')
def test_pil_PyQt5(pyi_builder):
# hook-PIL is excluding PyQt5, but is must still be included since it is imported elsewhere.
# Also see issue #1584.
pyi_builder.test_source("""
import PyQt5
import PIL
import PIL.ImageQt
""")


@importorskip('PIL')
def test_pil_plugins(pyi_builder):
pyi_builder.test_source(
"""
# Verify packaging of PIL.Image.
from PIL.Image import frombytes
print(frombytes)
# PIL import hook should bundle all available PIL plugins. Verify that plugins are collected.
from PIL import Image
Image.init()
MIN_PLUG_COUNT = 7 # Without all plugins the count is usually 6.
plugins = list(Image.SAVE.keys())
plugins.sort()
if len(plugins) < MIN_PLUG_COUNT:
raise SystemExit('No PIL image plugins were collected!')
else:
print('PIL supported image formats: %s' % plugins)
"""
)


@importorskip('pandas')
def test_pandas_extension(pyi_builder):
# Tests that the C extension ``pandas._libs.lib`` is properly bundled. Issue #1580.
Expand Down
65 changes: 62 additions & 3 deletions tests/functional/test_pil.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,76 @@
which retains the same Python package `PIL`.
"""

import pathlib

import pytest

from PyInstaller.utils.tests import importorskip
from PyInstaller.utils.hooks import can_import_module

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


# Functional test that tries to convert a .tiff image to a .png
def test_pil_image_conversion(pyi_builder, tmp_path):
pyi_builder.test_source(
"""
import sys
import os
import PIL.Image
if len(sys.argv) != 3:
print(f"use: {sys.argv[0]} <input-filename> <output-filename>")
raise SystemExit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
image = PIL.Image.open(input_file)
image.save(output_file)
""",
app_args=[
str(pathlib.Path(__file__).parent / 'data' / 'PIL_images' / 'tinysample.tiff'),
str(tmp_path / 'converted_tinysample.png'),
],
)


def test_pil_pyqt5(pyi_builder):
# hook-PIL is excluding PyQt5, but is must still be included since it is imported elsewhere.
# Also see issue #1584.
pyi_builder.test_source("""
import PyQt5
import PIL
import PIL.ImageQt
""")


def test_pil_plugins(pyi_builder):
pyi_builder.test_source(
"""
# Verify packaging of PIL.Image.
from PIL.Image import frombytes
print(frombytes)
# PIL import hook should bundle all available PIL plugins. Verify that plugins are collected.
from PIL import Image
Image.init()
MIN_PLUG_COUNT = 7 # Without all plugins the count is usually 6.
plugins = list(Image.SAVE.keys())
plugins.sort()
if len(plugins) < MIN_PLUG_COUNT:
raise SystemExit('No PIL image plugins were collected!')
else:
print('PIL supported image formats: %s' % plugins)
"""
)


# The tkinter module may be available for import, but not actually importable due to missing shared libraries.
# Therefore, we need to use `can_import_module`-based skip decorator instead of `@importorskip`.
@importorskip('PIL')
@pytest.mark.skipif(not can_import_module("tkinter"), reason="tkinter cannot be imported.")
def test_pil_no_tkinter(pyi_builder):
"""
Expand Down Expand Up @@ -56,7 +117,6 @@ def test_pil_no_tkinter(pyi_builder):

# The tkinter module may be available for import, but not actually importable due to missing shared libraries.
# Therefore, we need to use `can_import_module`-based skip decorator instead of `@importorskip`.
@importorskip('PIL')
@pytest.mark.skipif(not can_import_module("tkinter"), reason="tkinter cannot be imported.")
def test_pil_tkinter(pyi_builder):
"""
Expand All @@ -83,7 +143,6 @@ def test_pil_tkinter(pyi_builder):
)


@importorskip('PIL')
@importorskip('matplotlib')
def test_pil_no_matplotlib(pyi_builder):
"""
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/test_pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ def test_pkg_resources_importable(pyi_builder):
""")


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
Expand Down
Loading

0 comments on commit 7d5a783

Please sign in to comment.