File: test_pusimp.py

package info (click to toggle)
dolfin 2019.2.0~legacy20240219.1c52e83-18
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 31,700 kB
  • sloc: xml: 104,040; cpp: 102,227; python: 24,356; sh: 460; makefile: 330; javascript: 226
file content (101 lines) | stat: -rw-r--r-- 4,333 bytes parent folder | download | duplicates (2)
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) 2024 by the pusimp authors
#
# This file is part of pusimp for FEniCS.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
"""Test DOLFIN patches."""

import typing

import pytest
import requests

from pusimp.utils import (
    assert_package_import_errors_with_local_packages,
    assert_package_import_errors_with_broken_non_optional_packages,
    assert_package_import_success_with_allowed_local_packages,
    assert_package_import_success_with_broken_optional_packages,
    assert_package_import_success_without_local_packages
)

try:
    response = requests.get("https://www.debian.org", timeout=5)
except requests.ConnectionError:
    has_internet = False
else:
    has_internet = True

UFL_LEGACY_WARNING = "legacy dolfin codes must now import ufl_legacy instead of ufl"


def pip_install_call(executable: str, dependency_pypi_name: str) -> str:
    """Command to be run to install a dependency with pip."""
    return (
        f"{executable} -m pip install --ignore-installed --break-system-packages {dependency_pypi_name}"
        # debian does not ship numpy 2.0 yet, and a pip install-ed numpy 2.0 would break binary compatibility
        + " 'numpy<2.0'"
    )


def pip_uninstall_call(executable: str, dependency_pypi_name: str, dependency_actual_path: str) -> str:
    """Command to be run to uninstall a dependency with pip."""
    return f"{executable} -m pip uninstall --break-system-packages {dependency_pypi_name}"


def test_dolfin_import_success_without_local_packages() -> None:
    """Test that dolfin imports correctly without any extra local packages."""
    assert_package_import_success_without_local_packages(
        "dolfin", "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py"
    )


@pytest.mark.skipif(not has_internet, reason="Requires downloading from pypi.org")
@pytest.mark.parametrize("dependencies_import_name,dependencies_pypi_name,dependencies_extra_error_message", [
    (["ufl_legacy"], ["fenics-ufl-legacy"], []),
    (["ufl"], ["fenics-ufl"], [UFL_LEGACY_WARNING]),
    (["FIAT", "ufl_legacy"], ["fenics-fiat", "fenics-ufl-legacy"], []),
    (["ufl", "ufl_legacy"], ["fenics-ufl", "fenics-ufl-legacy"], [UFL_LEGACY_WARNING])
])
def test_dolfin_import_errors_with_local_packages(
    dependencies_import_name: typing.List[str], dependencies_pypi_name: typing.List[str],
    dependencies_extra_error_message: typing.List[str]
) -> None:
    """Test that dolfin fails to import with extra local packages."""
    assert_package_import_errors_with_local_packages(
        "dolfin", dependencies_import_name, dependencies_pypi_name, dependencies_extra_error_message,
        pip_install_call, pip_uninstall_call
    )


@pytest.mark.skipif(not has_internet, reason="Requires downloading from pypi.org")
@pytest.mark.parametrize("dependencies_import_name,dependencies_pypi_name", [
    (["ufl_legacy"], ["fenics-ufl-legacy"]),
    (["ufl"], ["fenics-ufl"])
])
def test_dolfin_import_success_with_allowed_local_packages(
    dependencies_import_name: typing.List[str], dependencies_pypi_name: typing.List[str]
) -> None:
    """Test that dolfin imports correctly even with extra local packages when asked to allow user-site imports."""
    assert_package_import_success_with_allowed_local_packages(
        "dolfin", "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py",
        dependencies_import_name, dependencies_pypi_name, pip_install_call
    )


@pytest.mark.parametrize("dependencies_import_name", [
    ["ufl_legacy"],  # also breaks ffc/compiler.py (line 121)
    ["ffc"]
])
def test_dolfin_import_errors_with_broken_non_optional_packages(dependencies_import_name: typing.List[str]) -> None:
    """Test that dolfin fails to import when non-optional packages are broken."""
    assert_package_import_errors_with_broken_non_optional_packages("dolfin", dependencies_import_name, [False])


@pytest.mark.parametrize("dependencies_import_name", [
    ["ufl"]
])
def test_dolfin_import_success_with_broken_optional_packages(dependencies_import_name: typing.List[str]) -> None:
    """Test that dolfin fails to import imports correctly when optional packages are broken."""
    assert_package_import_success_with_broken_optional_packages(
        "dolfin", "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py", dependencies_import_name, [True]
    )