| # Copyright 2026 The Pigweed Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| # use this file except in compliance with the License. You may obtain a copy of |
| # the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations under |
| # the License. |
| """Test API for static_checks module.""" |
| |
| from __future__ import annotations |
| |
| from collections.abc import Mapping, Sequence |
| from typing import Any, NotRequired, TypedDict |
| |
| from recipe_engine import recipe_test_api |
| |
| |
| class InsertedDeleted(TypedDict): |
| lines_inserted: NotRequired[int] |
| lines_deleted: NotRequired[int] |
| |
| |
| class StaticChecksTestApi(recipe_test_api.RecipeTestApi): |
| """Test helpers for static_checks recipe module.""" |
| |
| InsertedDeleted = InsertedDeleted |
| |
| def change_details( |
| self, |
| msg: str, |
| description: str = '', |
| files: Sequence[str] | Mapping[str, InsertedDeleted] = (), |
| owner: str = 'nobody@example.com', |
| revert_of: int = 0, |
| votes: dict[str, str | None] | None = None, |
| work_in_progress: bool | None = None, |
| num_parents: int = 1, |
| ) -> recipe_test_api.TestData: |
| labels: dict[str, dict[str, bool]] = {} |
| if votes: |
| for k, v in votes.items(): |
| labels[k] = {} |
| if v is not None: # pragma: no cover |
| assert v in 'approved recommended disliked rejected'.split() |
| labels[k][v] = True |
| |
| if isinstance(files, (list, tuple)): |
| files = { |
| x: {'lines_inserted': 20, 'lines_deleted': 20} for x in files |
| } |
| |
| details: dict[str, Any] = { |
| 'current_revision': 'HASH', |
| 'revisions': { |
| 'HASH': { |
| '_number': 1, |
| 'commit': { |
| 'subject': msg.split('\n')[0], |
| 'message': msg, |
| 'parents': [ |
| {'commit': f'PARENT{i}'} for i in range(num_parents) |
| ], |
| }, |
| 'files': files, |
| 'description': description, |
| } |
| }, |
| 'owner': {'email': owner}, |
| 'revert_of': revert_of, |
| 'labels': labels, |
| '_number': 1234, |
| } |
| |
| if work_in_progress: |
| details['work_in_progress'] = work_in_progress # pragma: no cover |
| |
| return self.step_data('change details', self.m.json.output(details)) |