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 | 8653154 | 2024-08-13 19:33:21 +0000 | [diff] [blame] | 19 | from typing import TYPE_CHECKING |
Rob Mohr | 758a472 | 2024-05-08 22:03:16 +0000 | [diff] [blame] | 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 | ) |
Rob Mohr | 0f2e60c | 2024-06-10 16:40:32 +0000 | [diff] [blame] | 24 | from PB.recipe_modules.pigweed.checkout import options as checkout_pb2 |
Rob Mohr | 758a472 | 2024-05-08 22:03:16 +0000 | [diff] [blame] | 25 | from recipe_engine import post_process |
| 26 | |
| 27 | if TYPE_CHECKING: # pragma: no cover |
Rob Mohr | 8653154 | 2024-08-13 19:33:21 +0000 | [diff] [blame] | 28 | from typing import Generator |
Rob Mohr | 758a472 | 2024-05-08 22:03:16 +0000 | [diff] [blame] | 29 | from recipe_engine import config_types, recipe_test_api |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 30 | |
| 31 | DEPS = [ |
| 32 | 'pigweed/bazel', |
| 33 | 'pigweed/checkout', |
Rob Mohr | 4b28bcb | 2024-06-05 21:37:13 +0000 | [diff] [blame] | 34 | 'recipe_engine/file', |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 35 | 'recipe_engine/path', |
| 36 | 'recipe_engine/properties', |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame] | 37 | 'recipe_engine/step', |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 38 | ] |
| 39 | |
| 40 | PROPERTIES = UpdateCommitHashProperties |
| 41 | |
| 42 | |
| 43 | @dataclasses.dataclass |
| 44 | class _FakeCheckoutContext: |
| 45 | root: config_types.Path |
| 46 | |
| 47 | |
| 48 | def RunSteps(api, props): |
Rob Mohr | 0f2e60c | 2024-06-10 16:40:32 +0000 | [diff] [blame] | 49 | options = checkout_pb2.Options( |
| 50 | remote="https://pigweed.googlesource.com/super" |
| 51 | ) |
| 52 | equiv = checkout_pb2.EquivalentRemotes() |
| 53 | equiv.remotes.append("https://pigweed.googlesource.com/pigweed/pigweed") |
| 54 | equiv.remotes.append("https://pigweed.googlesource.com/pigweed/equiv") |
| 55 | options.equivalent_remotes.append(equiv) |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 56 | checkout = api.checkout(options) |
| 57 | |
| 58 | # For coverage. |
| 59 | path: config_types.Path | None = None |
| 60 | if 'bar' in props.project_remote: |
| 61 | path = api.path.start_dir / 'WORKSPACE' |
| 62 | |
| 63 | update_result = api.bazel.update_commit_hash( |
| 64 | checkout=checkout, |
| 65 | project_remote=props.project_remote, |
| 66 | new_revision='ffffffffffffffffffffffffffffffffffffffff', |
| 67 | path=path, |
Rob Mohr | 0f2e60c | 2024-06-10 16:40:32 +0000 | [diff] [blame] | 68 | replace_remote=True, |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 69 | ) |
| 70 | |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame] | 71 | if update_result: |
Rob Mohr | e067a5c | 2024-06-21 20:32:00 +0000 | [diff] [blame] | 72 | with api.step.nest('update result'): |
| 73 | api.step.empty(f'old revision {update_result.old_revision}') |
| 74 | api.step.empty(f'project name {update_result.project_name}') |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame] | 75 | |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 76 | |
| 77 | def GenTests(api) -> Generator[recipe_test_api.TestData, None, None]: |
| 78 | def properties(**kwargs): |
| 79 | return api.properties(UpdateCommitHashProperties(**kwargs)) |
| 80 | |
| 81 | def _url(x: str = 'pigweed/pigweed'): |
| 82 | assert ':' not in x |
| 83 | return f'https://pigweed.googlesource.com/{x}' |
| 84 | |
| 85 | yield api.test( |
| 86 | 'success', |
| 87 | properties(project_remote=_url('pigweed/pigweed')), |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame] | 88 | api.post_process(post_process.MustRun, 'update result'), |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 89 | ) |
| 90 | |
| 91 | yield api.test( |
Rob Mohr | 0f2e60c | 2024-06-10 16:40:32 +0000 | [diff] [blame] | 92 | 'equiv', |
| 93 | properties(project_remote=_url('pigweed/equiv')), |
| 94 | api.post_process(post_process.MustRun, 'update result'), |
| 95 | ) |
| 96 | |
| 97 | yield api.test( |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 98 | 'remote-not-found', |
| 99 | properties(project_remote=_url('bar')), |
| 100 | api.post_process(post_process.MustRunRE, r'could not find remote.*'), |
| 101 | api.post_process(post_process.DropExpectation), |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame] | 102 | api.post_process(post_process.DoesNotRun, 'update result'), |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 103 | ) |
| 104 | |
| 105 | yield api.test( |
| 106 | 'commit-not-found', |
| 107 | properties(project_remote=_url('other/repo')), |
| 108 | api.post_process(post_process.MustRunRE, r'could not find commit.*'), |
| 109 | api.post_process(post_process.DropExpectation), |
Rob Mohr | 62be988 | 2024-06-05 21:46:50 +0000 | [diff] [blame] | 110 | api.post_process(post_process.DoesNotRun, 'update result'), |
Rob Mohr | f9e555b | 2024-05-06 18:15:10 +0000 | [diff] [blame] | 111 | ) |
Rob Mohr | 4b28bcb | 2024-06-05 21:37:13 +0000 | [diff] [blame] | 112 | |
| 113 | yield api.test( |
| 114 | 'multiple', |
| 115 | properties(project_remote=_url('pigweed/pigweed')), |
| 116 | api.step_data( |
| 117 | 'read old WORKSPACE', |
| 118 | api.file.read_text(api.bazel.MULTIPLE_ROLL_WORKSPACE_FILE), |
| 119 | ), |
| 120 | ) |
Rob Mohr | e067a5c | 2024-06-21 20:32:00 +0000 | [diff] [blame] | 121 | |
| 122 | yield api.test( |
| 123 | 'multiple-2', |
| 124 | properties(project_remote=_url('pigweed/pigweed')), |
| 125 | api.step_data( |
| 126 | 'read old WORKSPACE', |
| 127 | api.file.read_text(api.bazel.MULTIPLE_ROLL_WORKSPACE_FILE_2), |
| 128 | ), |
| 129 | api.post_process( |
| 130 | post_process.MustRun, |
| 131 | 'update result.project name pigweed, pw_toolchain', |
| 132 | ), |
| 133 | ) |