|
| 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 subprocess |
| 20 | +import sys |
| 21 | +import zipfile |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +SCRIPT_PATH = Path(__file__).resolve().parents[2] / "docker" / "get_distribution_specs.py" |
| 27 | + |
| 28 | +CURRENT_PYTHON_MAJOR_MINOR = f"{sys.version_info.major}.{sys.version_info.minor}" |
| 29 | + |
| 30 | + |
| 31 | +def _make_wheel(directory: Path, name: str, version: str, requires_python: str | None = None) -> Path: |
| 32 | + """Create a minimal .whl (zip) with METADATA.""" |
| 33 | + safe_name = name.replace("-", "_") |
| 34 | + wheel_path = directory / f"{safe_name}-{version}-py3-none-any.whl" |
| 35 | + dist_info = f"{safe_name}-{version}.dist-info" |
| 36 | + metadata_lines = [ |
| 37 | + "Metadata-Version: 2.1", |
| 38 | + f"Name: {name}", |
| 39 | + f"Version: {version}", |
| 40 | + ] |
| 41 | + if requires_python is not None: |
| 42 | + metadata_lines.append(f"Requires-Python: {requires_python}") |
| 43 | + with zipfile.ZipFile(wheel_path, "w") as zf: |
| 44 | + zf.writestr(f"{dist_info}/METADATA", "\n".join(metadata_lines)) |
| 45 | + zf.writestr(f"{dist_info}/RECORD", "") |
| 46 | + return wheel_path |
| 47 | + |
| 48 | + |
| 49 | +def _run_script(*wheel_paths: Path, env: dict[str, str] | None = None) -> subprocess.CompletedProcess: |
| 50 | + return subprocess.run( |
| 51 | + [sys.executable, str(SCRIPT_PATH), *(str(p) for p in wheel_paths)], |
| 52 | + capture_output=True, |
| 53 | + text=True, |
| 54 | + check=False, |
| 55 | + env=env, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +class TestRequiresPythonFiltering: |
| 60 | + def test_wheel_without_requires_python_is_included(self, tmp_path): |
| 61 | + whl = _make_wheel(tmp_path, "some-package", "1.0.0") |
| 62 | + result = _run_script(whl) |
| 63 | + assert result.returncode == 0 |
| 64 | + assert f"some-package @ file://{whl}" in result.stdout |
| 65 | + |
| 66 | + def test_wheel_matching_current_python_is_included(self, tmp_path): |
| 67 | + whl = _make_wheel(tmp_path, "some-package", "1.0.0", requires_python=">=3.10") |
| 68 | + result = _run_script(whl) |
| 69 | + assert result.returncode == 0 |
| 70 | + assert f"some-package @ file://{whl}" in result.stdout |
| 71 | + |
| 72 | + def test_wheel_excluding_current_python_is_skipped(self, tmp_path): |
| 73 | + whl = _make_wheel( |
| 74 | + tmp_path, |
| 75 | + "excluded-package", |
| 76 | + "2.0.0", |
| 77 | + requires_python=f">=3.10,!={CURRENT_PYTHON_MAJOR_MINOR}.*", |
| 78 | + ) |
| 79 | + result = _run_script(whl) |
| 80 | + assert result.returncode == 0 |
| 81 | + assert result.stdout == "" |
| 82 | + assert "Skipping" in result.stderr |
| 83 | + assert "excluded-package" in result.stderr |
| 84 | + |
| 85 | + def test_corrupt_wheel_is_included_with_warning(self, tmp_path): |
| 86 | + bad_whl = tmp_path / "bad_package-1.0.0-py3-none-any.whl" |
| 87 | + bad_whl.write_bytes(b"not a zip") |
| 88 | + result = _run_script(bad_whl) |
| 89 | + assert result.returncode == 0 |
| 90 | + assert f"bad-package @ file://{bad_whl}" in result.stdout |
| 91 | + assert "Warning" in result.stderr |
| 92 | + |
| 93 | + |
| 94 | +class TestMixedInputs: |
| 95 | + def test_compatible_and_incompatible_together(self, tmp_path): |
| 96 | + good_whl = _make_wheel( |
| 97 | + tmp_path, "apache-airflow-providers-standard", "1.0.0", requires_python=">=3.10" |
| 98 | + ) |
| 99 | + bad_whl = _make_wheel( |
| 100 | + tmp_path, |
| 101 | + "apache-airflow-providers-google", |
| 102 | + "21.0.0", |
| 103 | + requires_python=f">=3.10,!={CURRENT_PYTHON_MAJOR_MINOR}.*", |
| 104 | + ) |
| 105 | + result = _run_script(good_whl, bad_whl) |
| 106 | + assert result.returncode == 0 |
| 107 | + assert "apache-airflow-providers-standard" in result.stdout |
| 108 | + assert "apache-airflow-providers-google" not in result.stdout |
| 109 | + assert "Skipping" in result.stderr |
| 110 | + |
| 111 | + def test_sdist_is_not_filtered(self, tmp_path): |
| 112 | + """Sdists cannot be inspected for Requires-Python without building, so they pass through.""" |
| 113 | + import tarfile |
| 114 | + |
| 115 | + sdist = tmp_path / "apache_airflow_providers_google-21.0.0.tar.gz" |
| 116 | + with tarfile.open(sdist, "w:gz"): |
| 117 | + pass |
| 118 | + result = _run_script(sdist) |
| 119 | + assert result.returncode == 0 |
| 120 | + assert "apache-airflow-providers-google" in result.stdout |
| 121 | + |
| 122 | + |
| 123 | +class TestExtrasEnvVar: |
| 124 | + def test_extras_appended_to_spec(self, tmp_path): |
| 125 | + import os |
| 126 | + |
| 127 | + whl = _make_wheel(tmp_path, "apache-airflow", "3.0.0", requires_python=">=3.10") |
| 128 | + env = {**os.environ, "EXTRAS": "[celery,google]"} |
| 129 | + result = _run_script(whl, env=env) |
| 130 | + assert result.returncode == 0 |
| 131 | + assert f"apache-airflow[celery,google] @ file://{whl}" in result.stdout |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.parametrize( |
| 135 | + ("requires_python", "should_include"), |
| 136 | + [ |
| 137 | + pytest.param(">=3.10", True, id="lower-bound-satisfied"), |
| 138 | + pytest.param(f"!={CURRENT_PYTHON_MAJOR_MINOR}.*", False, id="wildcard-minor-excluded"), |
| 139 | + pytest.param(f">=3.10,!={CURRENT_PYTHON_MAJOR_MINOR}.*", False, id="range-with-exclusion"), |
| 140 | + pytest.param("<3.10", False, id="upper-bound-below-current"), |
| 141 | + pytest.param(None, True, id="no-requires-python"), |
| 142 | + ], |
| 143 | +) |
| 144 | +def test_requires_python_specifiers(tmp_path, requires_python, should_include): |
| 145 | + whl = _make_wheel(tmp_path, "test-package", "1.0.0", requires_python=requires_python) |
| 146 | + result = _run_script(whl) |
| 147 | + assert result.returncode == 0 |
| 148 | + if should_include: |
| 149 | + assert f"test-package @ file://{whl}" in result.stdout |
| 150 | + else: |
| 151 | + assert result.stdout == "" |
| 152 | + assert "Skipping" in result.stderr |
0 commit comments