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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
# Copyright (C) 2023-2024 by the pusimp authors
#
# This file is part of pusimp.
#
# SPDX-License-Identifier: MIT
"""Test utility functions defined in pusimp.utils."""
import os
import sys
import typing
import pytest
from pusimp.utils import (
assert_has_package, assert_not_has_package, assert_package_import_error,
assert_package_import_errors_with_broken_non_optional_packages, assert_package_import_errors_with_local_packages,
assert_package_import_success_with_allowed_local_packages,
assert_package_import_success_with_broken_optional_packages, assert_package_import_success_without_local_packages,
assert_package_location, VirtualEnv)
import pusimp_golden_source # isort: skip
def test_assert_has_package_success() -> None:
"""Test that assert_has_package succeeds with a package that is surely installed."""
assert_has_package(sys.executable, "pytest")
def test_assert_has_package_failure() -> None:
"""Test that assert_has_package fails with a package that is surely not installed."""
with pytest.raises(AssertionError) as excinfo:
assert_has_package(sys.executable, "not_existing_package")
assertion_error_text = str(excinfo.value)
assert assertion_error_text.startswith("Importing not_existing_package was not successful")
def test_assert_not_has_package_success() -> None:
"""Test that assert_not_has_package succeeds with a package that is surely not installed."""
assert_not_has_package(sys.executable, "not_existing_package")
def test_assert_not_has_package_failure() -> None:
"""Test that assert_not_has_package fails with a package that is surely installed."""
with pytest.raises(AssertionError) as excinfo:
assert_not_has_package(sys.executable, "pytest")
assertion_error_text = str(excinfo.value)
assert assertion_error_text.startswith("Importing pytest was unexpectedly successful")
def test_assert_package_location() -> None:
"""Test assert_package_location with a package that is surely installed."""
assert_package_location(sys.executable, "pytest", pytest.__file__)
def test_assert_package_import_error() -> None:
"""Test assert_package_import_error with a package that is surely not installed."""
assert_package_import_error(
sys.executable, "not_existing_package", ["No module named 'not_existing_package'"], [], False
)
def test_virtual_env() -> None:
"""Test that the creation of a virtual environment is successful."""
with VirtualEnv() as virtual_env:
assert virtual_env.path.exists()
def test_install_package_in_virtual_env_success() -> None:
"""Test that installing a package in a virtual environment is successful."""
with VirtualEnv() as virtual_env:
virtual_env.install_package("my-empty-package")
assert_package_location(
virtual_env.executable, "my_empty_package", str(virtual_env.dist_path / "my_empty_package" / "__init__.py")
)
assert_package_import_error(
sys.executable, "my_empty_package", ["No module named 'my_empty_package'"], [], False
)
def test_install_package_in_virtual_env_custom_call_success() -> None:
"""Test that installing a package in a virtual environment with a custom installation call is successful."""
with VirtualEnv() as virtual_env:
virtual_env.install_package(
"my-empty-package", lambda executable, package: f"{executable} -m pip install {package}")
assert_package_location(
virtual_env.executable, "my_empty_package", str(virtual_env.dist_path / "my_empty_package" / "__init__.py")
)
assert_package_import_error(
sys.executable, "my_empty_package", ["No module named 'my_empty_package'"], [], False
)
def test_install_package_in_virtual_env_failure() -> None:
"""Test that installing a package in a virtual environment is failing when the package does not exist on pypi."""
with VirtualEnv() as virtual_env:
with pytest.raises(RuntimeError) as excinfo:
virtual_env.install_package("not-existing-package")
runtime_error_text = str(excinfo.value)
assert runtime_error_text.startswith("Installing not-existing-package was not successful")
def test_install_package_in_virtual_env_custom_call_failure() -> None:
"""Test that installing a package in a virtual environment is failing when passing a wrong custom call."""
with VirtualEnv() as virtual_env:
with pytest.raises(RuntimeError) as excinfo:
virtual_env.install_package(
"not-really-used",
lambda executable, package: f"{executable} -m pip --this-option-does-not-exist {package}")
runtime_error_text = str(excinfo.value)
assert runtime_error_text.startswith("Installing not-really-used was not successful")
def test_break_package_in_virtual_env() -> None:
"""Test breaking an existing package by a mock installation in a virtual environment."""
with VirtualEnv() as virtual_env:
virtual_env.break_package("pytest")
assert_package_import_error(virtual_env.executable, "pytest", ["pytest was purposely broken."], [], False)
assert_package_location(sys.executable, "pytest", pytest.__file__)
def test_uninstall_package_in_virtual_env_success() -> None:
"""Test that uninstalling a package in a virtual environment is successful."""
with VirtualEnv() as virtual_env:
virtual_env.install_package("my-empty-package")
assert_package_location(
virtual_env.executable, "my_empty_package", str(virtual_env.dist_path / "my_empty_package" / "__init__.py")
)
virtual_env.uninstall_package("my-empty-package", "/not/really/used")
assert_package_import_error(
virtual_env.executable, "my_empty_package", ["No module named 'my_empty_package'"], [], False
)
assert_package_import_error(
sys.executable, "my_empty_package", ["No module named 'my_empty_package'"], [], False
)
def test_uninstall_package_in_virtual_env_custom_call_success() -> None:
"""Test that uninstalling a package in a virtual environment with a custom uninstallation call is successful."""
with VirtualEnv() as virtual_env:
virtual_env.install_package("my-empty-package")
assert_package_location(
virtual_env.executable, "my_empty_package", str(virtual_env.dist_path / "my_empty_package" / "__init__.py")
)
virtual_env.uninstall_package(
"my-empty-package", "/not/really/used",
lambda executable, package, _: f"{executable} -m pip uninstall -y {package}")
assert_package_import_error(
virtual_env.executable, "my_empty_package", ["No module named 'my_empty_package'"], [], False
)
assert_package_import_error(
sys.executable, "my_empty_package", ["No module named 'my_empty_package'"], [], False
)
def test_uninstall_package_in_virtual_env_failure() -> None:
"""Test that uninstalling a package in a virtual environment is failing when the package was never installed."""
with VirtualEnv() as virtual_env:
with pytest.raises(RuntimeError) as excinfo:
virtual_env.uninstall_package("not-installed-package", "/not/really/used")
runtime_error_text = str(excinfo.value)
assert runtime_error_text.startswith("Uninstalling not-installed-package was not successful")
def test_uninstall_package_in_virtual_env_custom_call_failure() -> None:
"""Test that uninstalling a package in a virtual environment is failing when passing a wrong custom call."""
with VirtualEnv() as virtual_env:
with pytest.raises(RuntimeError) as excinfo:
virtual_env.uninstall_package(
"not-really-used", "/not/really/used",
lambda executable, package, _: f"{executable} -m pip uninstall --this-option-does-not-exist {package}")
runtime_error_text = str(excinfo.value)
assert runtime_error_text.startswith("Uninstalling not-really-used was not successful")
def generate_test_data_pypi_names(import_names: typing.List[str]) -> typing.List[str]:
"""Replace underscore with dash in import names, and add installation from local directory."""
pypi_names = [import_name.replace("_", "-") for import_name in import_names]
tests_data_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data")
return [
f"'{pypi_name} @ file://{tests_data_dir}/{import_name}'"
for (import_name, pypi_name) in zip(import_names, pypi_names)
]
@pytest.mark.parametrize(
"package_name",
[
"pusimp_package_one",
"pusimp_package_two",
"pusimp_package_three",
# "pusimp_package_four", # raises ImportError due to mandatory missing dependency
"pusimp_package_five",
# "pusimp_package_six", # raises ImportError due to mandatory broken dependency
"pusimp_package_seven",
# "pusimp_package_eight", # its dependencies are supposed to always be local
# "pusimp_package_nine", # its dependencies are supposed to always be local
# "pusimp_package_ten", # its dependencies are supposed to either always local, missing or broken
]
)
def test_assert_package_import_success_without_local_packages_data(package_name: str) -> None:
"""Test assert_package_import_success_without_local_packages on mock packages that don't raise errors on import."""
assert_package_import_success_without_local_packages(
package_name, os.path.join(pusimp_golden_source.system_path, package_name, "__init__.py")
)
@pytest.mark.parametrize(
"package_name,dependencies_import_name,dependencies_extra_error_message",
[
("pusimp_package_one", ["pusimp_dependency_two"], ["pusimp_dependency_two is mandatory."]),
("pusimp_package_two", ["pusimp_dependency_two"], ["pusimp_dependency_two is mandatory."]),
("pusimp_package_two", ["pusimp_dependency_three"], ["pusimp_dependency_three is optional."]),
(
"pusimp_package_two", ["pusimp_dependency_two", "pusimp_dependency_three"],
["pusimp_dependency_two is mandatory.", "pusimp_dependency_three is optional."]
),
("pusimp_package_three", ["pusimp_dependency_two"], ["pusimp_dependency_two is mandatory."]),
("pusimp_package_three", ["pusimp_dependency_three"], ["pusimp_dependency_three is optional."]),
(
"pusimp_package_three", ["pusimp_dependency_two", "pusimp_dependency_three"],
["pusimp_dependency_two is mandatory.", "pusimp_dependency_three is optional."]
),
# ("pusimp_package_four", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_five", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_six", [], []), # pusimp_dependency_four is always broken
# ("pusimp_package_seven", [], []), # pusimp_dependency_four is always broken
("pusimp_package_eight", ["pusimp_dependency_five"], ["pusimp_dependency_five is mandatory."]),
("pusimp_package_eight", ["pusimp_dependency_six"], ["pusimp_dependency_six is optional."]),
(
"pusimp_package_eight", ["pusimp_dependency_five", "pusimp_dependency_six"],
["pusimp_dependency_five is mandatory.", "pusimp_dependency_six is optional."]
),
("pusimp_package_nine", ["pusimp_dependency_five"], ["pusimp_dependency_five is mandatory."]),
("pusimp_package_nine", ["pusimp_dependency_six"], ["pusimp_dependency_six is optional."]),
(
"pusimp_package_nine", ["pusimp_dependency_five", "pusimp_dependency_six"],
["pusimp_dependency_five is mandatory.", "pusimp_dependency_six is optional."]
)
# (
# "pusimp_package_ten", [], []
# ) # pusimp_dependency_missing is not installable, pusimp_dependency_four is always broken
]
)
def test_assert_package_import_errors_with_local_packages_data(
package_name: str, dependencies_import_name: typing.List[str], dependencies_extra_error_message: typing.List[str]
) -> None:
"""Test assert_package_import_errors_with_local_packages on mock packages."""
assert_package_import_errors_with_local_packages(
package_name, dependencies_import_name, generate_test_data_pypi_names(dependencies_import_name),
dependencies_extra_error_message,
lambda executable, dependency_pypi_name: (
f"{executable} -m pip install --ignore-installed {dependency_pypi_name}"),
lambda executable, dependency_pypi_name, _: f"{executable} -m pip uninstall {dependency_pypi_name}"
)
@pytest.mark.parametrize(
"package_name,dependencies_import_name",
[
("pusimp_package_one", ["pusimp_dependency_two"]),
("pusimp_package_two", ["pusimp_dependency_two"]),
("pusimp_package_two", ["pusimp_dependency_three"]),
("pusimp_package_two", ["pusimp_dependency_two", "pusimp_dependency_three"]),
("pusimp_package_three", ["pusimp_dependency_two"]),
("pusimp_package_three", ["pusimp_dependency_three"]),
("pusimp_package_three", ["pusimp_dependency_two", "pusimp_dependency_three"]),
# ("pusimp_package_four", []), # pusimp_dependency_missing is not installable
("pusimp_package_five", []),
# ("pusimp_package_six", []), # pusimp_dependency_four is always broken
("pusimp_package_seven", []),
("pusimp_package_eight", ["pusimp_dependency_five"]),
("pusimp_package_eight", ["pusimp_dependency_six"]),
("pusimp_package_eight", ["pusimp_dependency_five", "pusimp_dependency_six"]),
("pusimp_package_nine", ["pusimp_dependency_five"]),
("pusimp_package_nine", ["pusimp_dependency_six"]),
("pusimp_package_nine", ["pusimp_dependency_five", "pusimp_dependency_six"])
# (
# "pusimp_package_ten", [], []
# ), # pusimp_dependency_missing is not installable, pusimp_dependency_four is always broken
]
)
def test_assert_package_import_success_with_allowed_local_packages_data(
package_name: str, dependencies_import_name: typing.List[str]
) -> None:
"""Test assert_package_import_success_with_allowed_local_packages on mock packages."""
assert_package_import_success_with_allowed_local_packages(
package_name, os.path.join(pusimp_golden_source.system_path, package_name, "__init__.py"),
dependencies_import_name, generate_test_data_pypi_names(dependencies_import_name),
lambda executable, dependency_pypi_name: (
f"{executable} -m pip install --ignore-installed {dependency_pypi_name}")
)
@pytest.mark.parametrize(
"package_name,dependencies_import_name,dependencies_optional",
[
# ("pusimp_package_one", ["pusimp_dependency_two"], [False]), # in failure_position
# ("pusimp_package_two", ["pusimp_dependency_two"], [False]), # in failure_position
# ("pusimp_package_two", ["pusimp_dependency_three"], [True]), # in failure_only_optional
# (
# "pusimp_package_two", ["pusimp_dependency_two", "pusimp_dependency_three"], [False, True]
# ) # in failure_position
("pusimp_package_three", ["pusimp_dependency_two"], [False]),
# ("pusimp_package_three", ["pusimp_dependency_three"], [True]), # in failure_only_optional
("pusimp_package_three", ["pusimp_dependency_two", "pusimp_dependency_three"], [False, True]),
# ("pusimp_package_four", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_five", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_six", [], []), # pusimp_dependency_four is always broken
# ("pusimp_package_seven", [], []), # pusimp_dependency_four is always broken
# ("pusimp_package_eight", ["pusimp_dependency_five"], [False]), # in failure_position
# ("pusimp_package_eight", ["pusimp_dependency_six"], [True]), # in failure_only_optional
# (
# "pusimp_package_eight", ["pusimp_dependency_five", "pusimp_dependency_six"], [False, True]
# ) # in failure_position
("pusimp_package_nine", ["pusimp_dependency_five"], [False]),
# ("pusimp_package_nine", ["pusimp_dependency_six"], [True]), # in failure_only_optional
("pusimp_package_nine", ["pusimp_dependency_five", "pusimp_dependency_six"], [False, True])
# (
# "pusimp_package_ten", [], []
# ), # pusimp_dependency_missing is not installable, pusimp_dependency_four is always broken
]
)
def test_assert_package_import_errors_with_broken_non_optional_packages_data_success(
package_name: str, dependencies_import_name: typing.List[str], dependencies_optional: typing.List[bool]
) -> None:
"""Test success of assert_package_import_errors_with_broken_non_optional_packages on mock packages.
The successful cases are the cases in which dependencies_import_name lists actual mandatory dependencies,
and the import of the broken dependency happens after the call to pusimp.prevent_user_site_imports.
"""
assert_package_import_errors_with_broken_non_optional_packages(
package_name, dependencies_import_name, dependencies_optional
)
@pytest.mark.parametrize(
"package_name,dependencies_import_name,dependencies_optional",
[
("pusimp_package_one", ["pusimp_dependency_two"], [False]),
("pusimp_package_two", ["pusimp_dependency_two"], [False]),
# ("pusimp_package_two", ["pusimp_dependency_three"], [True]), # in failure_only_optional
("pusimp_package_two", ["pusimp_dependency_two", "pusimp_dependency_three"], [False, True]),
# ("pusimp_package_three", ["pusimp_dependency_two"], [False]), # in success
# ("pusimp_package_three", ["pusimp_dependency_three"], [True]), # in failure_only_optional
# ("pusimp_package_three", ["pusimp_dependency_two", "pusimp_dependency_three"], [False, True]), # in success
# ("pusimp_package_four", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_five", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_six", [], []), # pusimp_dependency_four is always broken
# ("pusimp_package_seven", [], []), # pusimp_dependency_four is always broken
("pusimp_package_eight", ["pusimp_dependency_five"], [False]),
# ("pusimp_package_eight", ["pusimp_dependency_six"], [True]), # in failure_only_optional
("pusimp_package_eight", ["pusimp_dependency_five", "pusimp_dependency_six"], [False, True])
# ("pusimp_package_nine", ["pusimp_dependency_five"], [False]), # in success
# ("pusimp_package_nine", ["pusimp_dependency_six"], [True]), # in failure_only_optional
# ("pusimp_package_nine", ["pusimp_dependency_five", "pusimp_dependency_six"], [False, True]) # in success
# (
# "pusimp_package_ten", [], []
# ), # pusimp_dependency_missing is not installable, pusimp_dependency_four is always broken
]
)
def test_assert_package_import_errors_with_broken_non_optional_packages_data_failure_position(
package_name: str, dependencies_import_name: typing.List[str], dependencies_optional: typing.List[bool]
) -> None:
"""Test failure of assert_package_import_errors_with_broken_non_optional_packages on mock packages.
These failing cases are the cases in which the import of the broken mandatory dependency listed
in dependencies_import_name happens before the call to pusimp.prevent_user_site_imports.
"""
with pytest.raises(AssertionError) as excinfo:
assert_package_import_errors_with_broken_non_optional_packages(
package_name, dependencies_import_name, dependencies_optional
)
assertion_error_text = str(excinfo.value)
assert assertion_error_text.startswith(
f"'* {dependencies_import_name[0]} is broken' was not found in the ImportError text, namely "
f"'Importing {package_name} was not successful"
)
assert f"{dependencies_import_name[0]} was purposely broken" in assertion_error_text
@pytest.mark.parametrize(
"package_name,dependencies_import_name,dependencies_optional",
[
# ("pusimp_package_one", ["pusimp_dependency_two"], [False]), # in failure_position
# ("pusimp_package_two", ["pusimp_dependency_two"], [False]), # in failure_position
("pusimp_package_two", ["pusimp_dependency_three"], [True]),
# (
# "pusimp_package_two", ["pusimp_dependency_two", "pusimp_dependency_three"], [False, True]
# ) # in failure_position
# ("pusimp_package_three", ["pusimp_dependency_two"], [False]), # in success
("pusimp_package_three", ["pusimp_dependency_three"], [True]),
# ("pusimp_package_three", ["pusimp_dependency_two", "pusimp_dependency_three"], [False, True]), # in success
# ("pusimp_package_four", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_five", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_six", [], []), # pusimp_dependency_four is always broken
# ("pusimp_package_seven", [], []), # pusimp_dependency_four is always broken
# ("pusimp_package_eight", ["pusimp_dependency_five"], [False]), # in failure_position
("pusimp_package_eight", ["pusimp_dependency_six"], [True]),
# (
# "pusimp_package_eight", ["pusimp_dependency_five", "pusimp_dependency_six"], [False, True]
# ), # in failure_position
# ("pusimp_package_nine", ["pusimp_dependency_five"], [False]), # in success
("pusimp_package_nine", ["pusimp_dependency_six"], [True]),
# ("pusimp_package_nine", ["pusimp_dependency_five", "pusimp_dependency_six"], [False, True]) # in success
# (
# "pusimp_package_ten", [], []
# ), # pusimp_dependency_missing is not installable, pusimp_dependency_four is always broken
]
)
def test_assert_package_import_errors_with_broken_non_optional_packages_data_failure_only_optional(
package_name: str, dependencies_import_name: typing.List[str], dependencies_optional: typing.List[bool]
) -> None:
"""Test failure of assert_package_import_errors_with_broken_non_optional_packages on mock packages.
These failing cases are the cases in which only optional dependencies have been listed in dependencies_import_name.
"""
with pytest.raises(AssertionError) as excinfo:
assert_package_import_errors_with_broken_non_optional_packages(
package_name, dependencies_import_name, dependencies_optional
)
assertion_error_text = str(excinfo.value)
assert f"Importing {package_name} was unexpectedly successful" in assertion_error_text
@pytest.mark.parametrize(
"package_name,dependencies_import_name,dependencies_optional",
[
("pusimp_package_one", ["pusimp_dependency_two"], [False]),
("pusimp_package_two", ["pusimp_dependency_two"], [False]),
("pusimp_package_two", ["pusimp_dependency_three"], [True]),
("pusimp_package_two", ["pusimp_dependency_two", "pusimp_dependency_three"], [False, True]),
("pusimp_package_three", ["pusimp_dependency_two"], [False]),
("pusimp_package_three", ["pusimp_dependency_three"], [True]),
("pusimp_package_three", ["pusimp_dependency_two", "pusimp_dependency_three"], [False, True]),
# ("pusimp_package_four", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_five", [], []), # pusimp_dependency_missing is not installable
# ("pusimp_package_six", [], []), # pusimp_dependency_four is always broken
# ("pusimp_package_seven", [], []), # pusimp_dependency_four is always broken
("pusimp_package_eight", ["pusimp_dependency_five"], [False]),
("pusimp_package_eight", ["pusimp_dependency_six"], [True]),
("pusimp_package_eight", ["pusimp_dependency_five", "pusimp_dependency_six"], [False, True]),
("pusimp_package_nine", ["pusimp_dependency_five"], [False]),
("pusimp_package_nine", ["pusimp_dependency_six"], [True]),
("pusimp_package_nine", ["pusimp_dependency_five", "pusimp_dependency_six"], [False, True])
# (
# "pusimp_package_ten", [], []
# ), # pusimp_dependency_missing is not installable, pusimp_dependency_four is always broken
]
)
def test_assert_package_import_success_with_broken_optional_packages_data(
package_name: str, dependencies_import_name: typing.List[str], dependencies_optional: typing.List[bool]
) -> None:
"""Test success of assert_package_import_success_with_broken_optional_packages on mock packages.
The successful cases are the cases in which dependencies_import_name lists actual optional dependencies.
"""
assert_package_import_success_with_broken_optional_packages(
package_name, os.path.join(pusimp_golden_source.system_path, package_name, "__init__.py"),
dependencies_import_name, dependencies_optional
)
|