Skip to content

Commit

Permalink
test: skip test_lower_bound_checker tests if the required packages ar…
Browse files Browse the repository at this point in the history
…e not installed
  • Loading branch information
parthea committed Jan 25, 2024
1 parent 24e6347 commit 805b1e8
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tests/unit/test_lower_bound_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
# limitations under the License.

from contextlib import contextmanager
import importlib.metadata
from pathlib import Path
import re
import tempfile
from typing import List

from click.testing import CliRunner
import pytest # type: ignore
import pytest

from test_utils.lower_bound_checker import lower_bound_checker

Expand All @@ -34,6 +35,14 @@
BAD_PACKAGE = "invalid-package"


def skip_test_if_not_installed(package_name: str):
"""Skips the current test if given package is not installed"""
try:
importlib.metadata.distribution(package_name)
except importlib.metadata.PackageNotFoundError:
pytest.skip(f"Skipping test which requires {package_name} in `tests/unit/resources/` to be installed")


def parse_error_msg(msg: str) -> List[str]:
"""Get package names from the error message.
Expand Down Expand Up @@ -76,6 +85,8 @@ def constraints_file(requirements: List[str]):


def test_update_constraints():
skip_test_if_not_installed(GOOD_PACKAGE)

with tempfile.TemporaryDirectory() as tmpdir:
constraints_path = Path(tmpdir) / "constraints.txt"

Expand All @@ -93,6 +104,8 @@ def test_update_constraints():


def test_update_constraints_overwrites_existing_file():
skip_test_if_not_installed(GOOD_PACKAGE)

constraints = [
"requests==1.0.0",
"packaging==13.0",
Expand All @@ -111,6 +124,8 @@ def test_update_constraints_overwrites_existing_file():
]

def test_update_constraints_with_setup_py_missing_lower_bounds():
skip_test_if_not_installed(BAD_PACKAGE)

constraints = [
"requests==1.0.0",
"packaging==14.0",
Expand All @@ -131,6 +146,8 @@ def test_update_constraints_with_setup_py_missing_lower_bounds():


def test_check():
skip_test_if_not_installed(GOOD_PACKAGE)

constraints = [
"requests==1.0.0",
"packaging==14.0",
Expand All @@ -147,6 +164,8 @@ def test_check():


def test_update_constraints_with_extra_constraints():
skip_test_if_not_installed(GOOD_PACKAGE)

constraints = [
"requests==1.0.0",
"packaging==14.0",
Expand All @@ -164,6 +183,8 @@ def test_update_constraints_with_extra_constraints():


def test_check_with_missing_constraints_file():
skip_test_if_not_installed(GOOD_PACKAGE)

result = RUNNER.invoke(
lower_bound_checker.check,
[
Expand All @@ -179,6 +200,8 @@ def test_check_with_missing_constraints_file():


def test_check_with_constraints_file_invalid_pins():
skip_test_if_not_installed(GOOD_PACKAGE)

constraints = [
"requests==1.0.0",
"packaging==14.0",
Expand All @@ -198,6 +221,8 @@ def test_check_with_constraints_file_invalid_pins():


def test_check_with_constraints_file_missing_packages():
skip_test_if_not_installed(GOOD_PACKAGE)

constraints = [
"requests==1.0.0",
"packaging==14.0",
Expand All @@ -215,6 +240,8 @@ def test_check_with_constraints_file_missing_packages():


def test_check_with_constraints_file_different_versions():
skip_test_if_not_installed(GOOD_PACKAGE)

constraints = [
"requests==1.2.0", # setup.py has 1.0.0
"packaging==14.1", # setup.py has 14.0
Expand All @@ -234,6 +261,8 @@ def test_check_with_constraints_file_different_versions():


def test_check_with_setup_py_missing_lower_bounds():
skip_test_if_not_installed(BAD_PACKAGE)

constraints = [
"requests==1.0.0",
"packaging==14.0",
Expand Down

0 comments on commit 805b1e8

Please sign in to comment.