Skip to content

Commit

Permalink
tests: extend the multipackage tests with nested resources
Browse files Browse the repository at this point in the history
Extend the multipackage test to verify references to nested resources
(data files and extensions) within the shared package in the
multipackage mode. For nested extension, the psutil package is used
(as it contains one).

Refactor the test scripts to use a common test function from a
package to avoid duplicating test code.

The extended tests shows that multipackage currently does not
properly handle resources that are collected in subdirectories of
_MEIPASS instead of _MEIPASS itself.
  • Loading branch information
rokm committed Mar 8, 2021
1 parent bc03542 commit c0c8bd1
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 80 deletions.
@@ -0,0 +1,13 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2021, 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)
#-----------------------------------------------------------------------------

from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('multipackage_test_pkg')
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
@@ -0,0 +1,44 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2021, 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)
#-----------------------------------------------------------------------------

def _test_basic_imports():
# import a very simple and rarely used pure-python lib ...
import getopt # noqa: F401
# ... and a module importing a shared lib
import ssl # noqa: F401

print('Hello World!')


def _test_nested_data_file():
# try reading secret from a file in sub-directory
import os

secret_file = os.path.join(__path__[0], 'data', 'secret.txt')
print("Reading secret from %s..." % (secret_file))
with open(secret_file, 'r') as fp:
secret = fp.read().strip()
print("Secret: %s" % (secret))

assert secret == 'Secret1234'


def _test_nested_extensions():
# import psutil, which contains an extension in its package directory
import psutil # noqa: F401
print("Successfully imported psutil!")


def test_function():
# Main test function
_test_basic_imports()
_test_nested_data_file()
_test_nested_extensions()
@@ -0,0 +1 @@
Secret1234
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
Expand Up @@ -9,10 +9,5 @@
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


# import a very simple and rarely used pure-python lib ...
import getopt
# ... and a module importing a shared lib
import ssl

print('Hello World!')
import multipackage_test_pkg
multipackage_test_pkg.test_function()
4 changes: 3 additions & 1 deletion tests/functional/specs/test_multipackage1.spec
Expand Up @@ -19,8 +19,10 @@ __testname__ = 'test_multipackage1'
__testdep__ = 'multipackage1_B'

a = Analysis([os.path.join(SCRIPT_DIR, __testname__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])
b = Analysis([os.path.join(SCRIPT_DIR, __testdep__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])

MERGE((b, __testdep__, __testdep__), (a, __testname__, __testname__))
Expand All @@ -37,7 +39,7 @@ exe = EXE(pyz,
strip=False,
upx=False,
console=1 )

pyzB = PYZ(b.pure)
exeB = EXE(pyzB,
b.scripts,
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/specs/test_multipackage2.spec
Expand Up @@ -20,8 +20,10 @@ __testname__ = 'test_multipackage2'
__testdep__ = 'multipackage2_B'

a = Analysis([os.path.join(SCRIPT_DIR, __testname__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])
b = Analysis([os.path.join(SCRIPT_DIR, __testdep__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])

MERGE((b, __testdep__, os.path.join(__testdep__, __testdep__)),
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/specs/test_multipackage3.spec
Expand Up @@ -20,8 +20,10 @@ __testname__ = 'test_multipackage3'
__testdep__ = 'multipackage3_B'

a = Analysis([os.path.join(SCRIPT_DIR, __testname__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])
b = Analysis([os.path.join(SCRIPT_DIR, __testdep__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])

MERGE((b, __testdep__, os.path.join(__testdep__)),
Expand All @@ -46,7 +48,7 @@ coll = COLLECT( exe,
strip=False,
upx=True,
name=os.path.join('dist', __testname__ ))

pyzB = PYZ(b.pure)
exeB = EXE(pyzB,
b.scripts,
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/specs/test_multipackage4.spec
Expand Up @@ -20,8 +20,10 @@ __testname__ = 'test_multipackage4'
__testdep__ = 'multipackage4_B'

a = Analysis([os.path.join(SCRIPT_DIR, __testname__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])
b = Analysis([os.path.join(SCRIPT_DIR, __testdep__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])

MERGE((b, __testdep__, os.path.join(__testdep__, __testdep__)),
Expand Down
5 changes: 4 additions & 1 deletion tests/functional/specs/test_multipackage5.spec
Expand Up @@ -22,10 +22,13 @@ __testdep__ = 'multipackage5_B'
__testdep2__ = 'multipackage5_C'

a = Analysis([os.path.join(SCRIPT_DIR, __testname__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])
b = Analysis([os.path.join(SCRIPT_DIR, __testdep__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])
c = Analysis([os.path.join(SCRIPT_DIR, __testdep2__ + '.py')],
hookspath=[os.path.join(SPECPATH, SCRIPT_DIR, 'extra-hooks')],
pathex=['.'])


Expand Down Expand Up @@ -72,7 +75,7 @@ coll = COLLECT( exeB,
strip=False,
upx=True,
name=os.path.join('dist', __testdep__))

pyzC = PYZ(c.pure)
exeC = EXE(pyzC,
c.scripts,
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/test_multipackage.py
@@ -1,6 +1,9 @@
import pytest

from PyInstaller.utils.tests import importorskip


@importorskip('psutil') # Used as test for nested extension
@pytest.mark.parametrize(
"spec_file",
(
Expand Down

0 comments on commit c0c8bd1

Please sign in to comment.