Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 1 | # Copyright 2024 The Pigweed Authors |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | # use this file except in compliance with the License. You may obtain a copy of |
| 5 | # the License at |
| 6 | # |
| 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations under |
| 13 | # the License. |
| 14 | """Full test of bazel module's update_commit_hash() method.""" |
| 15 | |
Rob Mohr | 758a472 | 2024-05-08 22:03:16 +0000 | [diff] [blame] | 16 | from __future__ import annotations |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 17 | |
| 18 | import dataclasses |
Rob Mohr | 758a472 | 2024-05-08 22:03:16 +0000 | [diff] [blame] | 19 | from typing import Generator, TYPE_CHECKING |
| 20 | |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 21 | from PB.recipe_modules.pigweed.bazel.tests.update_commit_hash import ( |
| 22 | UpdateCommitHashProperties, |
| 23 | ) |
| 24 | from PB.recipe_modules.pigweed.checkout.options import ( |
| 25 | Options as CheckoutOptions, |
| 26 | ) |
Rob Mohr | 758a472 | 2024-05-08 22:03:16 +0000 | [diff] [blame] | 27 | from recipe_engine import post_process |
| 28 | |
| 29 | if TYPE_CHECKING: # pragma: no cover |
| 30 | from recipe_engine import config_types, recipe_test_api |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 31 | |
| 32 | DEPS = [ |
| 33 | 'pigweed/bazel', |
| 34 | 'pigweed/checkout', |
Rob Mohr | 4b28bcb | 2024-06-05 21:37:13 +0000 | [diff] [blame] | 35 | 'recipe_engine/file', |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 36 | 'recipe_engine/path', |
| 37 | 'recipe_engine/properties', |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame^] | 38 | 'recipe_engine/step', |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 39 | ] |
| 40 | |
| 41 | PROPERTIES = UpdateCommitHashProperties |
| 42 | |
| 43 | |
| 44 | @dataclasses.dataclass |
| 45 | class _FakeCheckoutContext: |
| 46 | root: config_types.Path |
| 47 | |
| 48 | |
| 49 | def RunSteps(api, props): |
| 50 | options = CheckoutOptions(remote="https://pigweed.googlesource.com/super") |
| 51 | checkout = api.checkout(options) |
| 52 | |
| 53 | # For coverage. |
| 54 | path: config_types.Path | None = None |
| 55 | if 'bar' in props.project_remote: |
| 56 | path = api.path.start_dir / 'WORKSPACE' |
| 57 | |
| 58 | update_result = api.bazel.update_commit_hash( |
| 59 | checkout=checkout, |
| 60 | project_remote=props.project_remote, |
| 61 | new_revision='ffffffffffffffffffffffffffffffffffffffff', |
| 62 | path=path, |
| 63 | ) |
| 64 | |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame^] | 65 | if update_result: |
| 66 | step = api.step.empty('update result') |
| 67 | step.presentation.step_summary_text = repr(update_result) |
| 68 | |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 69 | |
| 70 | def GenTests(api) -> Generator[recipe_test_api.TestData, None, None]: |
| 71 | def properties(**kwargs): |
| 72 | return api.properties(UpdateCommitHashProperties(**kwargs)) |
| 73 | |
| 74 | def _url(x: str = 'pigweed/pigweed'): |
| 75 | assert ':' not in x |
| 76 | return f'https://pigweed.googlesource.com/{x}' |
| 77 | |
| 78 | yield api.test( |
| 79 | 'success', |
| 80 | properties(project_remote=_url('pigweed/pigweed')), |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame^] | 81 | api.post_process(post_process.MustRun, 'update result'), |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 82 | ) |
| 83 | |
| 84 | yield api.test( |
| 85 | 'remote-not-found', |
| 86 | properties(project_remote=_url('bar')), |
| 87 | api.post_process(post_process.MustRunRE, r'could not find remote.*'), |
| 88 | api.post_process(post_process.DropExpectation), |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame^] | 89 | api.post_process(post_process.DoesNotRun, 'update result'), |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 90 | ) |
| 91 | |
| 92 | yield api.test( |
| 93 | 'commit-not-found', |
| 94 | properties(project_remote=_url('other/repo')), |
| 95 | api.post_process(post_process.MustRunRE, r'could not find commit.*'), |
| 96 | api.post_process(post_process.DropExpectation), |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame^] | 97 | api.post_process(post_process.DoesNotRun, 'update result'), |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 98 | ) |
Rob Mohr | 4b28bcb | 2024-06-05 21:37:13 +0000 | [diff] [blame] | 99 | |
| 100 | yield api.test( |
| 101 | 'multiple', |
| 102 | properties(project_remote=_url('pigweed/pigweed')), |
| 103 | api.step_data( |
| 104 | 'read old WORKSPACE', |
| 105 | api.file.read_text(api.bazel.MULTIPLE_ROLL_WORKSPACE_FILE), |
| 106 | ), |
| 107 | ) |