Skip to content

Commit 66260f8

Browse files
authored
Simplify and improve test_packages.py (#2219)
1 parent b821acd commit 66260f8

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

tests/docker-stacks-foundation/test_packages.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,21 @@ def package_helper(container: TrackedContainer) -> CondaPackageHelper:
9595

9696

9797
@pytest.fixture(scope="function")
98-
def packages(package_helper: CondaPackageHelper) -> dict[str, set[str]]:
98+
def requested_packages(package_helper: CondaPackageHelper) -> dict[str, set[str]]:
9999
"""Return the list of requested packages (i.e. packages explicitly installed excluding dependencies)"""
100100
return package_helper.requested_packages()
101101

102102

103-
def get_package_import_name(package: str) -> str:
104-
"""Perform a mapping between the python package name and the name used for the import"""
105-
return PACKAGE_MAPPING.get(package, package)
106-
107-
108-
def excluded_package_predicate(package: str) -> bool:
109-
"""Return whether a package is excluded from the list
110-
(i.e. a package that cannot be tested with standard imports)"""
111-
return package in EXCLUDED_PACKAGES
112-
103+
def is_r_package(package: str) -> bool:
104+
"""Check if a package is an R package"""
105+
return package.startswith("r-")
113106

114-
def python_package_predicate(package: str) -> bool:
115-
"""Predicate matching python packages"""
116-
return not excluded_package_predicate(package) and not r_package_predicate(package)
117107

118-
119-
def r_package_predicate(package: str) -> bool:
120-
"""Predicate matching R packages"""
121-
return not excluded_package_predicate(package) and package.startswith("r-")
108+
def get_package_import_name(package: str) -> str:
109+
"""Perform a mapping between the package name and the name used for the import"""
110+
if is_r_package(package):
111+
package = package[2:]
112+
return PACKAGE_MAPPING.get(package, package)
122113

123114

124115
def _check_import_package(
@@ -127,9 +118,7 @@ def _check_import_package(
127118
"""Generic function executing a command"""
128119
LOGGER.debug(f"Trying to import a package with [{command}] ...")
129120
exec_result = package_helper.running_container.exec_run(command)
130-
assert (
131-
exec_result.exit_code == 0
132-
), f"Import package failed, output: {exec_result.output}"
121+
assert exec_result.exit_code == 0, exec_result.output.decode("utf-8")
133122

134123

135124
def check_import_python_package(
@@ -154,25 +143,26 @@ def _check_import_packages(
154143
Note: using a list of packages instead of a fixture for the list of packages
155144
since pytest prevents the use of multiple yields
156145
"""
157-
failures = {}
146+
failed_imports = []
158147
LOGGER.info("Testing the import of packages ...")
159148
for package in packages_to_check:
160149
LOGGER.info(f"Trying to import {package}")
161150
try:
162151
check_function(package_helper, package)
163152
except AssertionError as err:
164-
failures[package] = err
165-
if len(failures) > 0:
166-
raise AssertionError(failures)
153+
failed_imports.append(package)
154+
LOGGER.error(f"Failed to import package: {package}, output:\n {err}")
155+
if failed_imports:
156+
pytest.fail(f"following packages are not import-able: {failed_imports}")
167157

168158

169159
@pytest.fixture(scope="function")
170-
def r_packages(packages: dict[str, set[str]]) -> Iterable[str]:
160+
def r_packages(requested_packages: dict[str, set[str]]) -> Iterable[str]:
171161
"""Return an iterable of R packages"""
172-
# package[2:] is to remove the leading "r-" appended on R packages
173-
return map(
174-
lambda package: get_package_import_name(package[2:]),
175-
filter(r_package_predicate, packages),
162+
return (
163+
get_package_import_name(pkg)
164+
for pkg in requested_packages
165+
if is_r_package(pkg) and pkg not in EXCLUDED_PACKAGES
176166
)
177167

178168

@@ -184,9 +174,13 @@ def test_r_packages(
184174

185175

186176
@pytest.fixture(scope="function")
187-
def python_packages(packages: dict[str, set[str]]) -> Iterable[str]:
177+
def python_packages(requested_packages: dict[str, set[str]]) -> Iterable[str]:
188178
"""Return an iterable of Python packages"""
189-
return map(get_package_import_name, filter(python_package_predicate, packages))
179+
return (
180+
get_package_import_name(pkg)
181+
for pkg in requested_packages
182+
if not is_r_package(pkg) and pkg not in EXCLUDED_PACKAGES
183+
)
190184

191185

192186
def test_python_packages(

0 commit comments

Comments
 (0)