blob: 80ec58b62e820dc7d95fbe882b573641c1026c36 [file] [log] [blame]
Rob Mohrf9e555b2024-05-06 18:15:10 +00001# 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 Mohr758a4722024-05-08 22:03:16 +000016from __future__ import annotations
Rob Mohrf9e555b2024-05-06 18:15:10 +000017
18import dataclasses
Rob Mohr86531542024-08-13 19:33:21 +000019from typing import TYPE_CHECKING
Rob Mohr758a4722024-05-08 22:03:16 +000020
Rob Mohrf9e555b2024-05-06 18:15:10 +000021from PB.recipe_modules.pigweed.bazel.tests.update_commit_hash import (
22 UpdateCommitHashProperties,
23)
Rob Mohr0f2e60c2024-06-10 16:40:32 +000024from PB.recipe_modules.pigweed.checkout import options as checkout_pb2
Rob Mohr758a4722024-05-08 22:03:16 +000025from recipe_engine import post_process
26
27if TYPE_CHECKING: # pragma: no cover
Rob Mohr86531542024-08-13 19:33:21 +000028 from typing import Generator
Rob Mohr758a4722024-05-08 22:03:16 +000029 from recipe_engine import config_types, recipe_test_api
Rob Mohrf9e555b2024-05-06 18:15:10 +000030
31DEPS = [
32 'pigweed/bazel',
33 'pigweed/checkout',
Rob Mohr4b28bcb2024-06-05 21:37:13 +000034 'recipe_engine/file',
Rob Mohrf9e555b2024-05-06 18:15:10 +000035 'recipe_engine/path',
36 'recipe_engine/properties',
Rob Mohr62be9882024-06-05 21:46:50 +000037 'recipe_engine/step',
Rob Mohrf9e555b2024-05-06 18:15:10 +000038]
39
40PROPERTIES = UpdateCommitHashProperties
41
42
43@dataclasses.dataclass
44class _FakeCheckoutContext:
45 root: config_types.Path
46
47
48def RunSteps(api, props):
Rob Mohr0f2e60c2024-06-10 16:40:32 +000049 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 Mohrf9e555b2024-05-06 18:15:10 +000056 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 Mohr0f2e60c2024-06-10 16:40:32 +000068 replace_remote=True,
Rob Mohrf9e555b2024-05-06 18:15:10 +000069 )
70
Rob Mohr62be9882024-06-05 21:46:50 +000071 if update_result:
Rob Mohre067a5c2024-06-21 20:32:00 +000072 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 Mohr62be9882024-06-05 21:46:50 +000075
Rob Mohrf9e555b2024-05-06 18:15:10 +000076
77def 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 Mohr62be9882024-06-05 21:46:50 +000088 api.post_process(post_process.MustRun, 'update result'),
Rob Mohrf9e555b2024-05-06 18:15:10 +000089 )
90
91 yield api.test(
Rob Mohr0f2e60c2024-06-10 16:40:32 +000092 'equiv',
93 properties(project_remote=_url('pigweed/equiv')),
94 api.post_process(post_process.MustRun, 'update result'),
95 )
96
97 yield api.test(
Rob Mohrf9e555b2024-05-06 18:15:10 +000098 '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 Mohr62be9882024-06-05 21:46:50 +0000102 api.post_process(post_process.DoesNotRun, 'update result'),
Rob Mohrf9e555b2024-05-06 18:15:10 +0000103 )
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 Mohr62be9882024-06-05 21:46:50 +0000110 api.post_process(post_process.DoesNotRun, 'update result'),
Rob Mohrf9e555b2024-05-06 18:15:10 +0000111 )
Rob Mohr4b28bcb2024-06-05 21:37:13 +0000112
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 Mohre067a5c2024-06-21 20:32:00 +0000121
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 )