| •1 |
# Copyright (C) 2024 by the pusimp authors |
| 2 |
# |
| 3 |
# This file is part of pusimp for FEniCS. |
| 4 |
# |
| 5 |
# SPDX-License-Identifier: LGPL-3.0-or-later |
| 6 |
"""Test DOLFIN patches.""" |
| 7 |
|
| 8 |
import typing |
| 9 |
|
| 10 |
import pytest |
| 11 |
import requests |
| 12 |
|
| 13 |
from pusimp.utils import ( |
| 14 |
assert_package_import_errors_with_local_packages, |
| 15 |
assert_package_import_errors_with_broken_non_optional_packages, |
| 16 |
assert_package_import_success_with_allowed_local_packages, |
| 17 |
assert_package_import_success_with_broken_optional_packages, |
| 18 |
assert_package_import_success_without_local_packages |
| 19 |
) |
| 20 |
|
| 21 |
try: |
| 22 |
response = requests.get("https://www.debian.org", timeout=5) |
| 23 |
except requests.ConnectionError: |
| 24 |
has_internet = False |
| 25 |
else: |
| 26 |
has_internet = True |
| 27 |
|
| 28 |
UFL_LEGACY_WARNING = "legacy dolfin codes must now import ufl_legacy instead of ufl" |
| 29 |
|
| 30 |
|
| 31 |
def pip_install_call(executable: str, dependency_pypi_name: str) -> str: |
| 32 |
"""Command to be run to install a dependency with pip.""" |
| 33 |
return ( |
| 34 |
f"{executable} -m pip install --ignore-installed --break-system-packages {dependency_pypi_name}" |
| 35 |
# debian does not ship numpy 2.0 yet, and a pip install-ed numpy 2.0 would break binary compatibility |
| 36 |
+ " 'numpy<2.0'" |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def pip_uninstall_call(executable: str, dependency_pypi_name: str, dependency_actual_path: str) -> str: |
| 41 |
"""Command to be run to uninstall a dependency with pip.""" |
| 42 |
return f"{executable} -m pip uninstall --break-system-packages {dependency_pypi_name}" |
| 43 |
|
| 44 |
|
| 45 |
def test_dolfin_import_success_without_local_packages() -> None: |
| 46 |
"""Test that dolfin imports correctly without any extra local packages.""" |
| 47 |
assert_package_import_success_without_local_packages( |
| 48 |
"dolfin", "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py" |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.skipif(not has_internet, reason="Requires downloading from pypi.org") |
| 53 |
@pytest.mark.parametrize("dependencies_import_name,dependencies_pypi_name,dependencies_extra_error_message", [ |
| 54 |
(["ufl_legacy"], ["fenics-ufl-legacy"], []), |
| 55 |
(["ufl"], ["fenics-ufl"], [UFL_LEGACY_WARNING]), |
| 56 |
(["FIAT", "ufl_legacy"], ["fenics-fiat", "fenics-ufl-legacy"], []), |
| 57 |
(["ufl", "ufl_legacy"], ["fenics-ufl", "fenics-ufl-legacy"], [UFL_LEGACY_WARNING]) |
| 58 |
]) |
| 59 |
def test_dolfin_import_errors_with_local_packages( |
| 60 |
dependencies_import_name: typing.List[str], dependencies_pypi_name: typing.List[str], |
| 61 |
dependencies_extra_error_message: typing.List[str] |
| 62 |
) -> None: |
| 63 |
"""Test that dolfin fails to import with extra local packages.""" |
| 64 |
assert_package_import_errors_with_local_packages( |
| 65 |
"dolfin", dependencies_import_name, dependencies_pypi_name, dependencies_extra_error_message, |
| 66 |
pip_install_call, pip_uninstall_call |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.skipif(not has_internet, reason="Requires downloading from pypi.org") |
| 71 |
@pytest.mark.parametrize("dependencies_import_name,dependencies_pypi_name", [ |
| 72 |
(["ufl_legacy"], ["fenics-ufl-legacy"]), |
| 73 |
(["ufl"], ["fenics-ufl"]) |
| 74 |
]) |
| 75 |
def test_dolfin_import_success_with_allowed_local_packages( |
| 76 |
dependencies_import_name: typing.List[str], dependencies_pypi_name: typing.List[str] |
| 77 |
) -> None: |
| 78 |
"""Test that dolfin imports correctly even with extra local packages when asked to allow user-site imports.""" |
| 79 |
assert_package_import_success_with_allowed_local_packages( |
| 80 |
"dolfin", "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py", |
| 81 |
dependencies_import_name, dependencies_pypi_name, pip_install_call |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.parametrize("dependencies_import_name", [ |
| 86 |
["ufl_legacy"], # also breaks ffc/compiler.py (line 121) |
| 87 |
["ffc"] |
| 88 |
]) |
| 89 |
def test_dolfin_import_errors_with_broken_non_optional_packages(dependencies_import_name: typing.List[str]) -> None: |
| 90 |
"""Test that dolfin fails to import when non-optional packages are broken.""" |
| 91 |
assert_package_import_errors_with_broken_non_optional_packages("dolfin", dependencies_import_name, [False]) |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.parametrize("dependencies_import_name", [ |
| 95 |
["ufl"] |
| 96 |
]) |
| 97 |
def test_dolfin_import_success_with_broken_optional_packages(dependencies_import_name: typing.List[str]) -> None: |
| 98 |
"""Test that dolfin fails to import imports correctly when optional packages are broken.""" |
| 99 |
assert_package_import_success_with_broken_optional_packages( |
| 100 |
"dolfin", "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py", dependencies_import_name, [True] |
| 101 |
) |