|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Tests for tasks/scripts/check_maintainer_approval.py. |
| 5 | +
|
| 6 | +Run via `mise run test:maintainer-approval`, which provides pytest through |
| 7 | +`uv run --with pytest`. pytest puts this file's directory on sys.path, so the |
| 8 | +sibling script imports directly. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import json |
| 14 | + |
| 15 | +import check_maintainer_approval as gate |
| 16 | + |
| 17 | +TABLE = """# Maintainers |
| 18 | +
|
| 19 | +| Name | GitHub ID | Company/Organization | |
| 20 | +| --- | --- | --- | |
| 21 | +| Derek Carr | [@derekwaynecarr](https://github.com/derekwaynecarr) | Red Hat | |
| 22 | +| Evan Lezar | [@elezar](https://github.com/elezar) | NVIDIA | |
| 23 | +| Piotr Mlocek | [@pimlock](https://github.com/pimlock) | NVIDIA | |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +def review(login: str, state: str) -> dict: |
| 28 | + return {"user": {"login": login}, "state": state} |
| 29 | + |
| 30 | + |
| 31 | +def run(tmp_path, table: str, reviews: list[dict]) -> int: |
| 32 | + maintainers = tmp_path / "MAINTAINERS.md" |
| 33 | + maintainers.write_text(table, encoding="utf-8") |
| 34 | + reviews_file = tmp_path / "reviews.json" |
| 35 | + reviews_file.write_text(json.dumps(reviews), encoding="utf-8") |
| 36 | + return gate.main( |
| 37 | + ["--maintainers", str(maintainers), "--reviews", str(reviews_file)] |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def test_parse_maintainers_extracts_linked_logins() -> None: |
| 42 | + assert gate.parse_maintainers(TABLE) == {"derekwaynecarr", "elezar", "pimlock"} |
| 43 | + |
| 44 | + |
| 45 | +def test_parse_maintainers_ignores_unlinked_mentions() -> None: |
| 46 | + # A prose mention must not silently grant approval rights. |
| 47 | + prose = TABLE + "\nThanks to [@drive-by](mailto:nobody@example.com) too.\n" |
| 48 | + assert "drive-by" not in gate.parse_maintainers(prose) |
| 49 | + |
| 50 | + |
| 51 | +def test_comment_after_approval_does_not_revoke_it() -> None: |
| 52 | + reviews = [review("elezar", "APPROVED"), review("elezar", "COMMENTED")] |
| 53 | + assert gate.parse_reviews_to_approvers(reviews) == {"elezar"} |
| 54 | + |
| 55 | + |
| 56 | +def test_dismissed_review_revokes_approval() -> None: |
| 57 | + reviews = [review("elezar", "APPROVED"), review("elezar", "DISMISSED")] |
| 58 | + assert gate.parse_reviews_to_approvers(reviews) == set() |
| 59 | + |
| 60 | + |
| 61 | +def test_changes_requested_after_approval_revokes_it() -> None: |
| 62 | + reviews = [review("elezar", "APPROVED"), review("elezar", "CHANGES_REQUESTED")] |
| 63 | + assert gate.parse_reviews_to_approvers(reviews) == set() |
| 64 | + |
| 65 | + |
| 66 | +def test_out_of_order_reviews_still_respect_the_latest_position() -> None: |
| 67 | + # Ordering comes from the review id, not the order the caller happened |
| 68 | + # to assemble the pages in. |
| 69 | + reviews = [ |
| 70 | + {"id": 2, "user": {"login": "elezar"}, "state": "DISMISSED"}, |
| 71 | + {"id": 1, "user": {"login": "elezar"}, "state": "APPROVED"}, |
| 72 | + ] |
| 73 | + assert gate.parse_reviews_to_approvers(reviews) == set() |
| 74 | + |
| 75 | + |
| 76 | +def test_exits_zero_when_a_maintainer_approved(tmp_path) -> None: |
| 77 | + assert run(tmp_path, TABLE, [review("pimlock", "APPROVED")]) == 0 |
| 78 | + |
| 79 | + |
| 80 | +def test_matches_logins_case_insensitively(tmp_path) -> None: |
| 81 | + assert run(tmp_path, TABLE, [review("PiMlOcK", "APPROVED")]) == 0 |
| 82 | + |
| 83 | + |
| 84 | +def test_exits_nonzero_with_no_reviews(tmp_path) -> None: |
| 85 | + # The exit code is the check result, so this is the gate's actual contract. |
| 86 | + assert run(tmp_path, TABLE, []) != 0 |
| 87 | + |
| 88 | + |
| 89 | +def test_exits_nonzero_on_non_maintainer_approval(tmp_path) -> None: |
| 90 | + assert run(tmp_path, TABLE, [review("outsider", "APPROVED")]) != 0 |
| 91 | + |
| 92 | + |
| 93 | +def test_fails_closed_on_unparseable_list(tmp_path) -> None: |
| 94 | + assert run(tmp_path, "# Maintainers\n", [review("pimlock", "APPROVED")]) != 0 |
0 commit comments