|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import textwrap |
| 20 | + |
| 21 | +import pytest |
| 22 | +from check_dependency_lower_bounds import check_pyproject_file, check_requirement, extract_requirements |
| 23 | + |
| 24 | +WORKSPACE_NAMES = frozenset({"apache-airflow-core", "apache-airflow-devel-common"}) |
| 25 | + |
| 26 | + |
| 27 | +class TestCheckRequirement: |
| 28 | + @pytest.mark.parametrize( |
| 29 | + "dependency", |
| 30 | + [ |
| 31 | + pytest.param("pyspark>=4.0.0", id="greater-or-equal"), |
| 32 | + pytest.param("pyspark>4.0.0", id="greater"), |
| 33 | + pytest.param("hatchling==1.31.0", id="pinned"), |
| 34 | + pytest.param("hatchling===1.31.0", id="arbitrary-equality"), |
| 35 | + pytest.param("hatchling~=1.31", id="compatible-release"), |
| 36 | + pytest.param("urllib3>=2.1.0,!=2.6.0", id="lower-bound-with-exclusion"), |
| 37 | + pytest.param("pydantic-ai-slim[mcp]>=2.0.0", id="extras"), |
| 38 | + pytest.param('fastavro>=1.10.0; python_version < "3.14"', id="marker"), |
| 39 | + ], |
| 40 | + ) |
| 41 | + def test_no_error_when_lower_bound_present(self, dependency): |
| 42 | + assert check_requirement("project.dependencies", dependency, WORKSPACE_NAMES) is None |
| 43 | + |
| 44 | + @pytest.mark.parametrize( |
| 45 | + "dependency", |
| 46 | + [ |
| 47 | + pytest.param("pyspark", id="bare"), |
| 48 | + pytest.param("pydantic-ai-slim[mcp]", id="extras"), |
| 49 | + pytest.param("pyspark<5.0.0", id="upper-bound-only"), |
| 50 | + pytest.param("pyspark!=4.1.0", id="exclusion-only"), |
| 51 | + pytest.param('krb5; python_version < "3.14"', id="marker-only"), |
| 52 | + ], |
| 53 | + ) |
| 54 | + def test_error_when_lower_bound_missing(self, dependency): |
| 55 | + error = check_requirement("project.dependencies", dependency, WORKSPACE_NAMES) |
| 56 | + assert error is not None |
| 57 | + assert "has no lower bound" in error |
| 58 | + assert "[project.dependencies]" in error |
| 59 | + |
| 60 | + @pytest.mark.parametrize( |
| 61 | + "dependency", |
| 62 | + [ |
| 63 | + pytest.param("apache-airflow-core", id="plain"), |
| 64 | + pytest.param("apache_airflow_core", id="non-canonical-name"), |
| 65 | + pytest.param("apache-airflow-devel-common[mypy]", id="extras"), |
| 66 | + ], |
| 67 | + ) |
| 68 | + def test_no_error_for_workspace_distribution(self, dependency): |
| 69 | + assert check_requirement("dependency-groups.dev", dependency, WORKSPACE_NAMES) is None |
| 70 | + |
| 71 | + def test_no_error_for_direct_url_requirement(self): |
| 72 | + dependency = ( |
| 73 | + "sphinx-airflow-theme@https://airflow.apache.org/sphinx-airflow-theme/" |
| 74 | + "sphinx_airflow_theme-0.3.13-py3-none-any.whl" |
| 75 | + ) |
| 76 | + assert check_requirement("project.optional-dependencies.docs", dependency, WORKSPACE_NAMES) is None |
| 77 | + |
| 78 | + def test_error_for_invalid_requirement(self): |
| 79 | + error = check_requirement("project.dependencies", "not a requirement!", WORKSPACE_NAMES) |
| 80 | + assert error is not None |
| 81 | + assert "is not a valid requirement" in error |
| 82 | + |
| 83 | + |
| 84 | +class TestExtractRequirements: |
| 85 | + def test_extracts_every_guarded_table(self): |
| 86 | + data = { |
| 87 | + "build-system": {"requires": ["hatchling"]}, |
| 88 | + "project": { |
| 89 | + "dependencies": ["pyspark"], |
| 90 | + "optional-dependencies": {"kerberos": ["krb5"]}, |
| 91 | + }, |
| 92 | + "dependency-groups": {"dev": ["pytest", {"include-group": "docs"}]}, |
| 93 | + } |
| 94 | + assert extract_requirements(data) == [ |
| 95 | + ("project.dependencies", "pyspark"), |
| 96 | + ('project.optional-dependencies."kerberos"', "krb5"), |
| 97 | + ('dependency-groups."dev"', "pytest"), |
| 98 | + ("build-system.requires", "hatchling"), |
| 99 | + ] |
| 100 | + |
| 101 | + def test_no_requirements_when_tables_absent(self): |
| 102 | + assert extract_requirements({"tool": {"uv": {"required-version": ">=0.9.0"}}}) == [] |
| 103 | + |
| 104 | + |
| 105 | +class TestCheckPyprojectFile: |
| 106 | + def _write(self, tmp_path, content): |
| 107 | + path = tmp_path / "pyproject.toml" |
| 108 | + path.write_text(textwrap.dedent(content)) |
| 109 | + return path |
| 110 | + |
| 111 | + def test_reports_every_unbounded_dependency(self, tmp_path): |
| 112 | + path = self._write( |
| 113 | + tmp_path, |
| 114 | + """ |
| 115 | + [project] |
| 116 | + name = "apache-airflow-providers-samba" |
| 117 | + dependencies = ["smbprotocol>=1.5.0"] |
| 118 | +
|
| 119 | + [project.optional-dependencies] |
| 120 | + "kerberos" = ["krb5", "apache-airflow-core"] |
| 121 | +
|
| 122 | + [dependency-groups] |
| 123 | + dev = ["pytest"] |
| 124 | + """, |
| 125 | + ) |
| 126 | + errors = check_pyproject_file(path, WORKSPACE_NAMES) |
| 127 | + assert len(errors) == 2 |
| 128 | + assert "krb5" in errors[0] |
| 129 | + assert "pytest" in errors[1] |
| 130 | + |
| 131 | + def test_no_errors_when_all_bounded(self, tmp_path): |
| 132 | + path = self._write( |
| 133 | + tmp_path, |
| 134 | + """ |
| 135 | + [project] |
| 136 | + name = "apache-airflow-providers-samba" |
| 137 | + dependencies = ["smbprotocol>=1.5.0"] |
| 138 | +
|
| 139 | + [dependency-groups] |
| 140 | + dev = ["pytest>=9.1.1"] |
| 141 | + """, |
| 142 | + ) |
| 143 | + assert check_pyproject_file(path, WORKSPACE_NAMES) == [] |
0 commit comments