|
| 1 | +import subprocess |
| 2 | +import uuid |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | +import requests |
| 7 | +from sigstore import oidc |
| 8 | + |
| 9 | +import action |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="session") |
| 13 | +def id_token() -> oidc.IdentityToken: |
| 14 | + resp = requests.get( |
| 15 | + "https://raw.githubusercontent.com/sigstore-conformance/extremely-dangerous-public-oidc-beacon/refs/heads/current-token/oidc-token.txt", |
| 16 | + params={"cachebuster": uuid.uuid4().hex}, |
| 17 | + ) |
| 18 | + resp.raise_for_status() |
| 19 | + id_token = resp.text.strip() |
| 20 | + return oidc.IdentityToken(id_token) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def sampleproject(tmp_path: Path) -> Path: |
| 25 | + """ |
| 26 | + Create a sample Python project with a distribution file. |
| 27 | + """ |
| 28 | + |
| 29 | + project_dir = tmp_path / "sampleproject" |
| 30 | + project_dir.mkdir() |
| 31 | + |
| 32 | + pyproject = project_dir / "pyproject.toml" |
| 33 | + pyproject.write_text(""" |
| 34 | +name = "astral-sh-attest-action-test-sampleproject" |
| 35 | +version = "0.1.0" |
| 36 | +description = "Who's wants to know?" |
| 37 | +requires-python = ">=3.10" |
| 38 | +""") |
| 39 | + |
| 40 | + hello_py = project_dir / "hello.py" |
| 41 | + hello_py.write_text(""" |
| 42 | +def main(): |
| 43 | + print("Hello, world!") |
| 44 | +""") |
| 45 | + |
| 46 | + return project_dir |
| 47 | + |
| 48 | + |
| 49 | +def test_get_input(monkeypatch: pytest.MonkeyPatch) -> None: |
| 50 | + monkeypatch.setenv("ATTEST_ACTION_INPUT_FOO", "expected") |
| 51 | + |
| 52 | + assert action._get_input("foo") == "expected" |
| 53 | + |
| 54 | + |
| 55 | +def test_get_path_patterns(monkeypatch: pytest.MonkeyPatch) -> None: |
| 56 | + monkeypatch.setenv("ATTEST_ACTION_INPUT_PATHS", "dist/* another/** third/") |
| 57 | + |
| 58 | + patterns = action._get_path_patterns() |
| 59 | + assert patterns == {"dist/*", "another/**", "third/*"} |
| 60 | + |
| 61 | + # Deduplicates patterns / files. |
| 62 | + monkeypatch.setenv("ATTEST_ACTION_INPUT_PATHS", "dist/* dist/* another/") |
| 63 | + patterns = action._get_path_patterns() |
| 64 | + assert patterns == {"dist/*", "another/*"} |
| 65 | + |
| 66 | + monkeypatch.setenv("ATTEST_ACTION_INPUT_PATHS", "a a b b c") |
| 67 | + patterns = action._get_path_patterns() |
| 68 | + assert patterns == {"a", "b", "c"} |
| 69 | + |
| 70 | + |
| 71 | +def test_attest(sampleproject: Path, id_token: oidc.IdentityToken) -> None: |
| 72 | + subprocess.run(["uv", "build"], cwd=sampleproject, check=True) |
| 73 | + dist_dir = sampleproject / "dist" |
| 74 | + |
| 75 | + patterns = {str(dist_dir / "*")} |
| 76 | + |
| 77 | + dists = action._collect_dists(patterns) |
| 78 | + assert len(dists) == 2 # sdist and wheel |
| 79 | + |
| 80 | + action._attest( |
| 81 | + dists, |
| 82 | + id_token, |
| 83 | + overwrite=False, |
| 84 | + ) |
| 85 | + |
| 86 | + for dist_path, _ in dists: |
| 87 | + attestation_path = dist_path.with_name(f"{dist_path.name}.publish.attestation") |
| 88 | + assert attestation_path.exists() |
0 commit comments