Skip to content

Commit

Permalink
fix mypy; formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea committed Jan 26, 2024
1 parent 30b2987 commit ebba1c8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 32 deletions.
16 changes: 11 additions & 5 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

import nox

# 'update_lower_bounds' is excluded
# 'update_lower_bounds' is excluded
nox.options.sessions = [
"lint",
"blacken",
"lint_setup_py",
"mypy",
"unit",
"check_lower_bounds"
"check_lower_bounds",
]


Expand All @@ -50,7 +50,9 @@ def lint(session):
"""
session.install("flake8", BLACK_VERSION)
session.run(
"black", "--check", *BLACK_PATHS,
"black",
"--check",
*BLACK_PATHS,
)
session.run("flake8", *BLACK_PATHS)

Expand All @@ -63,7 +65,8 @@ def blacken(session):
"""
session.install(BLACK_VERSION)
session.run(
"black", *BLACK_PATHS,
"black",
*BLACK_PATHS,
)


Expand Down Expand Up @@ -93,7 +96,9 @@ def unit(session):
)

# Install two fake packages for the lower-bound-checker tests
session.install("-e", "tests/unit/resources/good_package", "tests/unit/resources/bad_package")
session.install(
"-e", "tests/unit/resources/good_package", "tests/unit/resources/bad_package"
)

session.install("pytest", "pytest-cov")
session.install("-e", ".", "-c", constraints_path)
Expand All @@ -113,6 +118,7 @@ def unit(session):
*session.posargs,
)


@nox.session(python="3.8")
def check_lower_bounds(session):
"""Check lower bounds in setup.py are reflected in constraints file"""
Expand Down
6 changes: 3 additions & 3 deletions owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
".kokoro/docs/",
".kokoro/publish-docs.sh",
"CONTRIBUTING.rst",
"renovate.json", # no bundle, ignore test resources
".github/workflows/docs.yml", # no docs to publish
"README.rst",
"renovate.json", # no bundle, ignore test resources
".github/workflows/docs.yml", # no docs to publish
"README.rst",
],
)

Expand Down
77 changes: 53 additions & 24 deletions tests/unit/test_lower_bound_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
from pathlib import Path
import re
import tempfile
import sys
from typing import List

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

try:
if sys.version_info >= (3, 8):
import importlib.metadata as importlib_metadata
except:
else:
# For Python 3.7 compatibility
import importlib_metadata

Expand All @@ -44,13 +45,15 @@ 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")
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.
Example:
Error: setup.py is missing explicit lower bounds for the following packages: ["requests", "grpcio"]
"""
Expand All @@ -61,12 +64,13 @@ def parse_error_msg(msg: str) -> List[str]:
if match:
reqs = match.groups(1)[0].split(",") # type: ignore
reqs = [r.strip().replace("'", "").replace('"', "") for r in reqs]

return reqs


def parse_diff_versions_error_msg(msg: str) -> List[str]:
"""Get package names from the error message listing different versions
Example:
'requests' lower bound is 1.2.0 in setup.py but constraints file has 1.3.0
'grpcio' lower bound is 1.0.0 in setup.py but constraints file has 1.10.0
Expand All @@ -76,6 +80,7 @@ def parse_diff_versions_error_msg(msg: str) -> List[str]:

return pkg_names


@contextmanager
def constraints_file(requirements: List[str]):
"""Write the list of requirements into a temporary file"""
Expand All @@ -96,16 +101,27 @@ def test_update_constraints():
constraints_path = Path(tmpdir) / "constraints.txt"

result = RUNNER.invoke(
lower_bound_checker.update, ["--package-name", GOOD_PACKAGE, "--constraints-file", str(constraints_path)]
lower_bound_checker.update,
[
"--package-name",
GOOD_PACKAGE,
"--constraints-file",
str(constraints_path),
],
)

assert result.exit_code == 0
assert constraints_path.exists()

output = constraints_path.read_text().split("\n")

assert output == ["click==7.0.0", "grpcio==1.0.0", "packaging==14.0", "requests==1.0.0", "wheel==0.41.0",]
output = constraints_path.read_text().split("\n")

assert output == [
"click==7.0.0",
"grpcio==1.0.0",
"packaging==14.0",
"requests==1.0.0",
"wheel==0.41.0",
]


def test_update_constraints_overwrites_existing_file():
Expand All @@ -119,15 +135,22 @@ def test_update_constraints_overwrites_existing_file():
]
with constraints_file(constraints) as c:
result = RUNNER.invoke(
lower_bound_checker.update, ["--package-name", GOOD_PACKAGE, "--constraints-file", c]
lower_bound_checker.update,
["--package-name", GOOD_PACKAGE, "--constraints-file", c],
)

assert result.exit_code == 0

output = c.read_text().split("\n")
assert output == ["click==7.0.0", "grpcio==1.0.0", "packaging==14.0", "requests==1.0.0", "wheel==0.41.0",
assert output == [
"click==7.0.0",
"grpcio==1.0.0",
"packaging==14.0",
"requests==1.0.0",
"wheel==0.41.0",
]


def test_update_constraints_with_setup_py_missing_lower_bounds():
skip_test_if_not_installed(BAD_PACKAGE)

Expand All @@ -139,7 +162,8 @@ def test_update_constraints_with_setup_py_missing_lower_bounds():
]
with constraints_file(constraints) as c:
result = RUNNER.invoke(
lower_bound_checker.update, ["--package-name", BAD_PACKAGE, "--constraints-file", c]
lower_bound_checker.update,
["--package-name", BAD_PACKAGE, "--constraints-file", c],
)

assert result.exit_code == 2
Expand All @@ -149,7 +173,6 @@ def test_update_constraints_with_setup_py_missing_lower_bounds():
assert set(invalid_pkg_list) == {"requests", "packaging", "wheel"}



def test_check():
skip_test_if_not_installed(GOOD_PACKAGE)

Expand All @@ -158,11 +181,12 @@ def test_check():
"packaging==14.0",
"wheel==0.41.0",
"click==7.0.0",
"grpcio==1.0.0"
"grpcio==1.0.0",
]
with constraints_file(constraints) as c:
result = RUNNER.invoke(
lower_bound_checker.check, ["--package-name", GOOD_PACKAGE, "--constraints-file", c]
lower_bound_checker.check,
["--package-name", GOOD_PACKAGE, "--constraints-file", c],
)

assert result.exit_code == 0
Expand All @@ -181,7 +205,8 @@ def test_update_constraints_with_extra_constraints():
]
with constraints_file(constraints) as c:
result = RUNNER.invoke(
lower_bound_checker.check, ["--package-name", GOOD_PACKAGE, "--constraints-file", c]
lower_bound_checker.check,
["--package-name", GOOD_PACKAGE, "--constraints-file", c],
)

assert result.exit_code == 0
Expand Down Expand Up @@ -215,7 +240,8 @@ def test_check_with_constraints_file_invalid_pins():
]
with constraints_file(constraints) as c:
result = RUNNER.invoke(
lower_bound_checker.check, ["--package-name", GOOD_PACKAGE, "--constraints-file", c]
lower_bound_checker.check,
["--package-name", GOOD_PACKAGE, "--constraints-file", c],
)

assert result.exit_code == 2
Expand All @@ -235,7 +261,8 @@ def test_check_with_constraints_file_missing_packages():
]
with constraints_file(constraints) as c:
result = RUNNER.invoke(
lower_bound_checker.check, ["--package-name", GOOD_PACKAGE, "--constraints-file", c]
lower_bound_checker.check,
["--package-name", GOOD_PACKAGE, "--constraints-file", c],
)

assert result.exit_code == 2
Expand All @@ -252,11 +279,12 @@ def test_check_with_constraints_file_different_versions():
"packaging==14.1", # setup.py has 14.0
"wheel==0.42.0", # setup.py has 0.41.0
"click==7.0.0",
"grpcio==1.0.0"
"grpcio==1.0.0",
]
with constraints_file(constraints) as c:
result = RUNNER.invoke(
lower_bound_checker.check, ["--package-name", GOOD_PACKAGE, "--constraints-file", c]
lower_bound_checker.check,
["--package-name", GOOD_PACKAGE, "--constraints-file", c],
)

assert result.exit_code == 2
Expand All @@ -276,7 +304,8 @@ def test_check_with_setup_py_missing_lower_bounds():
]
with constraints_file(constraints) as c:
result = RUNNER.invoke(
lower_bound_checker.check, ["--package-name", BAD_PACKAGE, "--constraints-file", c]
lower_bound_checker.check,
["--package-name", BAD_PACKAGE, "--constraints-file", c],
)

assert result.exit_code == 2
Expand Down

0 comments on commit ebba1c8

Please sign in to comment.